-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration-tests.rs
75 lines (63 loc) · 2.35 KB
/
integration-tests.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
// https://github.com/near/workspaces-rs/blob/8f12f3dc3b0251ac3f44ddf6ab6fc63003579139/workspaces/tests/create_account.rs
#![recursion_limit = "256"]
use anyhow::Error;
use donation_matcher_contract::{
generic::{near_string_to_yocto, yocto_to_near_string},
GAS_FOR_ACCOUNT_CALLBACK,
};
use near_sdk::serde_json::json;
use test_log::test;
use workspaces::{network::Sandbox, prelude::*, Account, Worker};
async fn create_subaccount(
worker: &Worker<Sandbox>,
parent_account: &Account,
name: &str,
initial_balance: &str,
) -> Result<Account, Error> {
let subaccount = parent_account
.create_subaccount(&worker, name)
.initial_balance(near_string_to_yocto(initial_balance.to_string()))
.transact()
.await?
.into_result()?;
Ok(subaccount)
}
#[test(tokio::test)]
async fn test_offer_matching_funds_and_get_commitments_and_rescind_matching_funds_and_donate(
) -> anyhow::Result<()> {
let worker = workspaces::sandbox().await?;
let contract = worker
.dev_deploy(&include_bytes!("../target/res/donation_matcher_contract.wasm").to_vec())
.await?;
let parent_account = worker.dev_create_account().await?;
contract
.call(&worker, "new")
//.args_json(json!({"recipient": &recipient.id()}))?
.gas(GAS_FOR_ACCOUNT_CALLBACK.0)
.transact()
.await?;
let recipient = create_subaccount(&worker, &parent_account, "recipient", "1 Ⓝ").await?;
assert_eq!(
yocto_to_near_string(&recipient.view_account(&worker).await?.balance),
"1 Ⓝ".to_string()
);
let matcher1 = create_subaccount(&worker, &parent_account, "matcher1", "1 Ⓝ").await?;
// TODO: How to set matcher1 as the caller.
let _matcher1_offer_result = contract
.call(&worker, "offer_matching_funds")
.args_json(json!({"recipient": &recipient.id()}))?
.gas(GAS_FOR_ACCOUNT_CALLBACK.0)
.deposit(near_string_to_yocto("0.3".to_string()))
.transact()
.await?;
assert_eq!(
yocto_to_near_string(&recipient.view_account(&worker).await?.balance),
"1 Ⓝ".to_string()
); // The recipient hasn't received any donation yet.
assert_eq!(
yocto_to_near_string(&matcher1.view_account(&worker).await?.balance),
"0.7 Ⓝ".to_string()
);
// TODO: Write the rest of the test.
Ok(())
}