-
Notifications
You must be signed in to change notification settings - Fork 4
/
fungible_proxy.rs
297 lines (255 loc) · 10.2 KB
/
fungible_proxy.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use crate::utils::*;
use fungible_proxy::FungibleProxyContract;
use fungible_proxy::PaymentArgs;
use mocks::fungible_token_mock::FungibleTokenContractContract;
use near_sdk::json_types::U128;
use near_sdk::serde_json::json;
use near_sdk_sim::init_simulator;
use near_sdk_sim::runtime::GenesisConfig;
use near_sdk_sim::ContractAccount;
use near_sdk_sim::UserAccount;
use near_sdk_sim::{call, deploy, lazy_static_include, to_yocto};
use std::convert::TryInto;
use std::ops::Sub;
use std::str;
near_sdk::setup_alloc!();
const PROXY_ID: &str = "fungible_proxy";
lazy_static_include::lazy_static_include_bytes! {
PROXY_BYTES => "target/wasm32-unknown-unknown/release/fungible_proxy.wasm"
}
lazy_static_include::lazy_static_include_bytes! {
MOCKED_BYTES => "target/wasm32-unknown-unknown/debug/mocks.wasm"
}
const DEFAULT_BALANCE: &str = "400000";
// Initialize test environment with 3 accounts (alice, bob, builder), a fungible conversion mock, and a fungible token mock.
fn init_fungible() -> (
UserAccount,
UserAccount,
UserAccount,
ContractAccount<FungibleProxyContract>,
ContractAccount<FungibleTokenContractContract>,
) {
let genesis = GenesisConfig::default();
let root = init_simulator(Some(genesis));
let ft_contract = deploy!(
contract: FungibleTokenContractContract,
contract_id: "mockedft".to_string(),
bytes: &MOCKED_BYTES,
signer_account: root,
deposit: to_yocto("8")
);
let account = root.create_user("alice".to_string(), to_yocto(DEFAULT_BALANCE));
let empty_account_1 = root.create_user("bob".parse().unwrap(), to_yocto(DEFAULT_BALANCE));
let empty_account_2 = root.create_user("builder".parse().unwrap(), to_yocto(DEFAULT_BALANCE));
let proxy = deploy!(
contract: FungibleProxyContract,
contract_id: PROXY_ID,
bytes: &PROXY_BYTES,
signer_account: root,
deposit: to_yocto("5")
);
(
account,
empty_account_1,
empty_account_2,
proxy,
ft_contract,
)
}
// Helper function for setting up fungible token transfer tests
fn fungible_transfer_setup(
alice: &UserAccount,
bob: &UserAccount,
builder: &UserAccount,
ft_contract: &ContractAccount<FungibleTokenContractContract>,
send_amt: U128,
) -> (u128, u128, u128) {
// Register alice, bob, builder, and the contract with the fungible token
call!(alice, ft_contract.register_account(alice.account_id()));
call!(bob, ft_contract.register_account(bob.account_id()));
call!(builder, ft_contract.register_account(builder.account_id()));
call!(builder, ft_contract.register_account(PROXY_ID.into()));
// Set initial balances
call!(
alice,
ft_contract.set_balance(alice.account_id(), 1000000000.into()) // 1000 USDC.e
);
call!(
bob,
ft_contract.set_balance(bob.account_id(), 1000000.into()) // 1 USDC.e
);
call!(
builder,
ft_contract.set_balance(builder.account_id(), 0.into()) // 0 USDC.e
);
call!(
builder,
ft_contract.set_balance(PROXY_ID.into(), 0.into()) // 0 USDC.e
);
let alice_balance_before = call!(alice, ft_contract.ft_balance_of(alice.account_id()))
.unwrap_json::<U128>()
.0;
let bob_balance_before = call!(bob, ft_contract.ft_balance_of(bob.account_id()))
.unwrap_json::<U128>()
.0;
let builder_balance_before = call!(builder, ft_contract.ft_balance_of(builder.account_id()))
.unwrap_json::<U128>()
.0;
// In real usage, the user calls `ft_transfer_call` on the token contract, which calls `ft_on_transfer` on our contract
// The token contract will transfer the specificed tokens from the caller to our contract before calling our contract
call!(alice, ft_contract.set_balance(PROXY_ID.into(), send_amt));
call!(
alice,
ft_contract.set_balance(
alice.account_id(),
U128::from(alice_balance_before - send_amt.0)
)
);
(
alice_balance_before,
bob_balance_before,
builder_balance_before,
)
}
#[test]
fn test_transfer() {
let (alice, bob, builder, proxy, ft_contract) = init_fungible();
let send_amt = U128::from(500000000); // 500 USDC.e
let (alice_balance_before, bob_balance_before, builder_balance_before) =
fungible_transfer_setup(&alice, &bob, &builder, &ft_contract, send_amt);
let args = PaymentArgs {
fee_address: builder.account_id().try_into().unwrap(),
fee_amount: 2000000.into(), // 2 USDC.e
payment_reference: "abc7c8bb1234fd11".into(),
to: bob.account_id().try_into().unwrap(),
};
let result = call!(
ft_contract.user_account,
proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into())
);
result.assert_success_one_log(
&json!({
"amount": "498000000", // 500 USDC.e - 2 USDC.e fee
"token_address": "mockedft",
"fee_address": "builder",
"fee_amount": "2000000",
"payment_reference": "abc7c8bb1234fd11",
"to": "bob",
})
.to_string(),
);
// The mocked fungible token does not handle change
let change = result.unwrap_json::<String>().parse::<u128>().unwrap();
assert!(change == 0);
assert_spent(alice, alice_balance_before, send_amt.into(), &ft_contract);
assert_received(bob, bob_balance_before, 498000000, &ft_contract);
assert_received(builder, builder_balance_before, 2000000, &ft_contract);
}
#[test]
fn transfer_less_than_fee_amount() {
let (alice, bob, builder, proxy, ft_contract) = init_fungible();
let send_amt = U128::from(500000000); // 500 USDC.e
let (_, _, _) = fungible_transfer_setup(&alice, &bob, &builder, &ft_contract, send_amt);
let args = PaymentArgs {
fee_address: "builder".try_into().unwrap(),
fee_amount: 500100000.into(), // 500.10 USDC.e
payment_reference: "abc7c8bb1234fd12".into(),
to: bob.account_id().try_into().unwrap(),
};
let result = call!(
ft_contract.user_account,
proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into())
);
result.assert_one_promise_error("amount smaller than fee_amount");
}
#[test]
fn test_transfer_receiver_send_failed() {
let (alice, bob, builder, proxy, ft_contract) = init_fungible();
let send_amt = U128::from(500000000); // 500 USDC.e
let (alice_balance_before, _, _) =
fungible_transfer_setup(&alice, &bob, &builder, &ft_contract, send_amt);
// Previous line registers all accounts, so we unregister bob here
// Receiver is not registered with the token contract, so sending to it will fail
call!(bob, ft_contract.unregister_account(bob.account_id()));
let args = PaymentArgs {
fee_address: builder.account_id().try_into().unwrap(),
fee_amount: 2000000.into(), // 2 USDC.e
payment_reference: "abc7c8bb1234fd12".into(),
to: bob.account_id().try_into().unwrap(),
};
let result = call!(
ft_contract.user_account,
proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into())
);
result.assert_success_one_log("Transfer failed to bob or builder. Returning attached amount of 500000000 of token mockedft to alice");
// The mocked fungible token does not handle change
let change = result.unwrap_json::<String>().parse::<u128>().unwrap();
let result = call!(alice, ft_contract.ft_balance_of(alice.account_id()));
let alice_balance_after = result.unwrap_json::<U128>().0 + change;
// Ensure no balance changes / all funds returned to sender
assert!(alice_balance_after == alice_balance_before);
}
#[test]
fn test_transfer_fee_receiver_send_failed() {
let (alice, bob, builder, proxy, ft_contract) = init_fungible();
let send_amt = U128::from(500000000); // 500 USDC.e
let (alice_balance_before, _, _) =
fungible_transfer_setup(&alice, &bob, &builder, &ft_contract, send_amt);
// Previous line registers all accounts, so we unregister builder here
// Fee_receiver is not registered with the token contract, so sending to it will fail
call!(
builder,
ft_contract.unregister_account(builder.account_id())
);
let args = PaymentArgs {
fee_address: "builder".try_into().unwrap(),
fee_amount: 200.into(),
payment_reference: "abc7c8bb1234fd12".into(),
to: bob.account_id().try_into().unwrap(),
};
let result = call!(
ft_contract.user_account,
proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into())
);
result.assert_success_one_log("Transfer failed to bob or builder. Returning attached amount of 500000000 of token mockedft to alice");
// The mocked fungible token does not handle change
let change = result.unwrap_json::<String>().parse::<u128>().unwrap();
assert_eq!(change, 500000000);
assert_unchanged_balance(
alice,
alice_balance_before.sub(change),
&ft_contract,
"Alice",
);
}
#[test]
fn test_transfer_zero_usd() {
let (alice, bob, builder, proxy, ft_contract) = init_fungible();
let send_amt = U128::from(0); // 0 USDC.e
let (alice_balance_before, bob_balance_before, builder_balance_before) =
fungible_transfer_setup(&alice, &bob, &builder, &ft_contract, send_amt);
let args = PaymentArgs {
fee_address: "builder".try_into().unwrap(),
fee_amount: 0.into(),
payment_reference: "abc7c8bb1234fd12".into(),
to: bob.account_id().try_into().unwrap(),
};
let result = call!(
ft_contract.user_account,
proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into())
);
result.assert_success_one_log(
&json!({
"amount": "0",
"token_address": "mockedft",
"fee_address": "builder",
"fee_amount": "0",
"payment_reference": "abc7c8bb1234fd12",
"to": "bob",
})
.to_string(),
);
assert_unchanged_balance(alice, alice_balance_before, &ft_contract, "Alice");
assert_unchanged_balance(bob, bob_balance_before, &ft_contract, "Bob");
assert_unchanged_balance(builder, builder_balance_before, &ft_contract, "Builder");
}