1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//!
//! [`AccountMeta`](https://solana-labs.github.io/solana-web3.js/types/AccountMeta.html) and `ProgramAccount` (alias to [`AccountInfo`](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)) class bindings.
//!
use crate::imports::*;
use crate::publickey::PublicKey;
use js_sys::BigInt;
use solana_program::instruction::AccountMeta as SolanaAccountMeta;
use solana_sdk::account::Account;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(extends = Object)]
    #[derive(Debug, Clone, PartialEq, Eq)]
    /// AccountMeta is bindings for [`AccountMeta`](solana_program::instruction::AccountMeta)
    pub type AccountMeta;

    #[wasm_bindgen(extends = Object)]
    #[derive(Debug)]
    /// Information describing an account. This type is an alias for interfacing with the [`AccountInfo`](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html) class.
    /// In `solana-web3-sys` JavaScript AccountInfo class is renamed to `ProgramAccount` to prevent name clashes with native Solana SDK `AccountInfo` class.
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub type ProgramAccount;

    #[wasm_bindgen(constructor, js_class=Object)]
    /// Information describing an account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn new() -> ProgramAccount;

    #[wasm_bindgen(getter, method)]
    /// Getter: Data assigned to the account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn data(this: &ProgramAccount) -> Vec<u8>;

    #[wasm_bindgen(setter, method, js_name = "data")]
    /// Setter: Data assigned to the account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn set_data(this: &ProgramAccount, data: Vec<u8>);

    #[wasm_bindgen(getter, method)]
    /// Getter: `true` if this account's data contains a loaded program
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn executable(this: &ProgramAccount) -> bool;

    #[wasm_bindgen(setter, method, js_name = "executable")]
    /// Setter: `true` if this account's data contains a loaded program
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn set_executable(this: &ProgramAccount, executable: bool);

    #[wasm_bindgen(getter, method)]
    /// Getter: Number of lamports assigned to the account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn lamports(this: &ProgramAccount) -> BigInt;

    #[wasm_bindgen(setter, method, js_name = "lamports")]
    /// Setter: Number of lamports assigned to the account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn set_lamports(this: &ProgramAccount, lamports: BigInt);

    #[wasm_bindgen(getter, method)]
    /// Getter: [`PublicKey`] (Identifier) of the program that owns the account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn owner(this: &ProgramAccount) -> PublicKey;

    #[wasm_bindgen(setter, method, js_name = "owner")]
    /// Setter: [`PublicKey`] (Identifier) of the program that owns the account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn set_owner(this: &ProgramAccount, owner: PublicKey);

    #[wasm_bindgen(getter, method, js_name = "rentEpoch")]
    /// Getter: Rent epoch info for account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn rent_epoch(this: &ProgramAccount) -> BigInt;

    #[wasm_bindgen(setter, method, js_name = "rentEpoch")]
    /// Setter: Rent epoch info for account
    ///
    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/types/AccountInfo.html)
    ///
    pub fn set_rent_epoch(this: &ProgramAccount, rent_epoch: BigInt);

    #[wasm_bindgen(getter, method)]
    pub fn space(this: &ProgramAccount) -> BigInt;

    #[wasm_bindgen(setter, method, js_name = "space")]
    pub fn set_space(this: &ProgramAccount, space: BigInt);
}

impl OptionsTrait for AccountMeta {}

impl AccountMeta {
    /// Set writable
    pub fn is_writable(self, is_writable: bool) -> Self {
        self.set("isWritable", JsValue::from(is_writable))
    }

    /// Set signer
    pub fn is_signer(self, is_signer: bool) -> Self {
        self.set("isSigner", JsValue::from(is_signer))
    }

    /// Set pubkey
    pub fn pubkey(self, pubkey: &Pubkey) -> Result<Self> {
        Ok(self.set("pubkey", pubkey_to_jsvalue(pubkey)?))
    }
}
impl TryFrom<&SolanaAccountMeta> for AccountMeta {
    type Error = crate::error::Error;
    fn try_from(account: &SolanaAccountMeta) -> Result<Self> {
        AccountMeta::new()
            .is_signer(account.is_signer)
            .is_writable(account.is_writable)
            .pubkey(&account.pubkey)
    }
}

impl TryFrom<ProgramAccount> for Account {
    type Error = crate::error::Error;

    fn try_from(account: ProgramAccount) -> Result<Self> {
        if !account.is_object() {
            return Err(JsValue::from("Invalid ProgramAccount").into());
        }
        Ok(Self {
            lamports: account.lamports().as_f64().unwrap() as u64,
            data: account.data(),
            owner: account.owner().try_into()?,
            rent_epoch: account.rent_epoch().as_f64().unwrap() as u64,
            executable: account.executable(),
        })
    }
}

impl TryFrom<Account> for ProgramAccount {
    type Error = crate::error::Error;

    fn try_from(account: Account) -> Result<Self> {
        let acc = ProgramAccount::new();
        acc.set_lamports(BigInt::from(account.lamports));
        acc.set_data(account.data);
        acc.set_owner(account.owner.as_ref().try_into()?);
        acc.set_rent_epoch(BigInt::from(account.rent_epoch));
        acc.set_executable(account.executable);

        Ok(acc)
    }
}