This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
testutils.rs
212 lines (178 loc) · 8.1 KB
/
testutils.rs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#![cfg(feature = "testutils")]
use crate::contract::TokenClient;
use ed25519_dalek::Keypair;
use soroban_auth::{Ed25519Signature, Identifier, Signature, SignaturePayload, SignaturePayloadV0};
use soroban_sdk::testutils::ed25519::Sign;
use soroban_sdk::{symbol, BigInt, Bytes, BytesN, Env, IntoVal};
pub fn register_test_contract(e: &Env, contract_id: &[u8; 32]) {
let contract_id = BytesN::from_array(e, contract_id);
e.register_contract(&contract_id, crate::contract::Token {});
}
pub fn to_ed25519(e: &Env, kp: &Keypair) -> Identifier {
Identifier::Ed25519(kp.public.to_bytes().into_val(e))
}
pub struct Token {
env: Env,
contract_id: BytesN<32>,
}
impl Token {
pub fn new(env: &Env, contract_id: &[u8; 32]) -> Self {
Self {
env: env.clone(),
contract_id: BytesN::from_array(env, contract_id),
}
}
pub fn initialize(&self, admin: &Identifier, decimals: u32, name: &str, symbol: &str) {
let name: Bytes = name.into_val(&self.env);
let symbol: Bytes = symbol.into_val(&self.env);
TokenClient::new(&self.env, &self.contract_id)
.initialize(&admin, &decimals, &name, &symbol);
}
pub fn nonce(&self, id: &Identifier) -> BigInt {
TokenClient::new(&self.env, &self.contract_id).nonce(&id)
}
pub fn allowance(&self, from: &Identifier, spender: &Identifier) -> BigInt {
TokenClient::new(&self.env, &self.contract_id).allowance(&from, &spender)
}
pub fn approve(&self, from: &Keypair, spender: &Identifier, amount: &BigInt) {
let from_id = to_ed25519(&self.env, from);
let nonce = self.nonce(&from_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("approve"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (from_id, &nonce, spender, amount).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: from.public.to_bytes().into_val(&self.env),
signature: from.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).approve(&auth, &nonce, &spender, &amount)
}
pub fn balance(&self, id: &Identifier) -> BigInt {
TokenClient::new(&self.env, &self.contract_id).balance(&id)
}
pub fn is_frozen(&self, id: &Identifier) -> bool {
TokenClient::new(&self.env, &self.contract_id).is_frozen(&id)
}
pub fn xfer(&self, from: &Keypair, to: &Identifier, amount: &BigInt) {
let from_id = to_ed25519(&self.env, from);
let nonce = self.nonce(&from_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("xfer"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (from_id, &nonce, to, amount).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: BytesN::from_array(&self.env, &from.public.to_bytes()),
signature: from.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).xfer(&auth, &nonce, &to, &amount)
}
pub fn xfer_from(
&self,
spender: &Keypair,
from: &Identifier,
to: &Identifier,
amount: &BigInt,
) {
let spender_id = to_ed25519(&self.env, spender);
let nonce = self.nonce(&spender_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("xfer_from"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (spender_id, &nonce, from, to, amount).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: spender.public.to_bytes().into_val(&self.env),
signature: spender.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).xfer_from(&auth, &nonce, &from, &to, &amount)
}
pub fn burn(&self, admin: &Keypair, from: &Identifier, amount: &BigInt) {
let admin_id = to_ed25519(&self.env, admin);
let nonce = self.nonce(&admin_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("burn"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (admin_id, &nonce, from, amount).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: admin.public.to_bytes().into_val(&self.env),
signature: admin.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).burn(&auth, &nonce, &from, &amount)
}
pub fn freeze(&self, admin: &Keypair, id: &Identifier) {
let admin_id = to_ed25519(&self.env, admin);
let nonce = self.nonce(&admin_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("freeze"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (admin_id, &nonce, id).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: admin.public.to_bytes().into_val(&self.env),
signature: admin.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).freeze(&auth, &nonce, &id)
}
pub fn mint(&self, admin: &Keypair, to: &Identifier, amount: &BigInt) {
let admin_id = to_ed25519(&self.env, admin);
let nonce = self.nonce(&admin_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("mint"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (admin_id, &nonce, to, amount).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: admin.public.to_bytes().into_val(&self.env),
signature: admin.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).mint(&auth, &nonce, &to, &amount)
}
pub fn set_admin(&self, admin: &Keypair, new_admin: &Identifier) {
let admin_id = to_ed25519(&self.env, admin);
let nonce = self.nonce(&admin_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("set_admin"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (admin_id, &nonce, new_admin).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: admin.public.to_bytes().into_val(&self.env),
signature: admin.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).set_admin(&auth, &nonce, &new_admin)
}
pub fn unfreeze(&self, admin: &Keypair, id: &Identifier) {
let admin_id = to_ed25519(&self.env, admin);
let nonce = self.nonce(&admin_id);
let msg = SignaturePayload::V0(SignaturePayloadV0 {
function: symbol!("unfreeze"),
contract: self.contract_id.clone(),
network: self.env.ledger().network_passphrase(),
args: (admin_id, &nonce, id).into_val(&self.env),
});
let auth = Signature::Ed25519(Ed25519Signature {
public_key: admin.public.to_bytes().into_val(&self.env),
signature: admin.sign(msg).unwrap().into_val(&self.env),
});
TokenClient::new(&self.env, &self.contract_id).unfreeze(&auth, &nonce, &id)
}
pub fn decimals(&self) -> u32 {
TokenClient::new(&self.env, &self.contract_id).decimals()
}
pub fn name(&self) -> Bytes {
TokenClient::new(&self.env, &self.contract_id).name()
}
pub fn symbol(&self) -> Bytes {
TokenClient::new(&self.env, &self.contract_id).symbol()
}
}