solana_web3_sys/
instruction.rs1use crate::account::*;
5use crate::imports::*;
6use solana_program::instruction::Instruction;
7
8#[wasm_bindgen]
9extern "C" {
10    #[wasm_bindgen(extends = Object)]
11    #[derive(Debug, Clone, PartialEq, Eq)]
12    pub type TransactionInstructionConfig;
17}
18
19impl OptionsTrait for TransactionInstructionConfig {}
20
21impl TransactionInstructionConfig {
22    pub fn keys(self, keys: Vec<AccountMeta>) -> Self {
24        let list = Array::new();
25        for key in keys {
26            list.push(&key.into());
27        }
28        self.set("keys", JsValue::from(list))
29    }
30
31    pub fn program_id(self, program_id: &Pubkey) -> Result<Self> {
33        Ok(self.set("programId", pubkey_to_jsvalue(program_id)?))
34    }
35
36    pub fn data(self, data: &[u8]) -> Self {
38        self.set("data", Uint8Array::from(data).into())
39    }
40}
41
42#[wasm_bindgen]
43extern "C" {
44    #[wasm_bindgen(js_namespace=solanaWeb3, js_name = TransactionInstruction)]
45    #[derive(Debug, Clone)]
46    pub type TransactionInstruction;
51
52    #[wasm_bindgen(constructor, js_namespace=["solanaWeb3"])]
53    pub fn new(options: &TransactionInstructionConfig) -> TransactionInstruction;
58}
59
60impl TryFrom<&Instruction> for TransactionInstruction {
61    type Error = crate::error::Error;
62    fn try_from(instruction: &Instruction) -> Result<Self> {
63        let mut accounts_list = vec![];
64
65        for account in &instruction.accounts {
66            accounts_list.push(account.try_into()?);
67        }
68
69        let cfg = TransactionInstructionConfig::new()
70            .data(&instruction.data)
71            .keys(accounts_list)
72            .program_id(&instruction.program_id)?;
73
74        Ok(TransactionInstruction::new(&cfg))
75    }
76}