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
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)]
pub type AccountMeta;
#[wasm_bindgen(extends = Object)]
#[derive(Debug)]
pub type ProgramAccount;
#[wasm_bindgen(constructor, js_class=Object)]
pub fn new() -> ProgramAccount;
#[wasm_bindgen(getter, method)]
pub fn data(this: &ProgramAccount) -> Vec<u8>;
#[wasm_bindgen(setter, method, js_name = "data")]
pub fn set_data(this: &ProgramAccount, data: Vec<u8>);
#[wasm_bindgen(getter, method)]
pub fn executable(this: &ProgramAccount) -> bool;
#[wasm_bindgen(setter, method, js_name = "executable")]
pub fn set_executable(this: &ProgramAccount, executable: bool);
#[wasm_bindgen(getter, method)]
pub fn lamports(this: &ProgramAccount) -> BigInt;
#[wasm_bindgen(setter, method, js_name = "lamports")]
pub fn set_lamports(this: &ProgramAccount, lamports: BigInt);
#[wasm_bindgen(getter, method)]
pub fn owner(this: &ProgramAccount) -> PublicKey;
#[wasm_bindgen(setter, method, js_name = "owner")]
pub fn set_owner(this: &ProgramAccount, owner: PublicKey);
#[wasm_bindgen(getter, method, js_name = "rentEpoch")]
pub fn rent_epoch(this: &ProgramAccount) -> BigInt;
#[wasm_bindgen(setter, method, js_name = "rentEpoch")]
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 {
pub fn is_writable(self, is_writable: bool) -> Self {
self.set("isWritable", JsValue::from(is_writable))
}
pub fn is_signer(self, is_signer: bool) -> Self {
self.set("isSigner", JsValue::from(is_signer))
}
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)
}
}