-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathft.rs
92 lines (80 loc) · 2.49 KB
/
ft.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
use near_api::*;
use serde_json::json;
#[tokio::main]
async fn main() {
let network = near_workspaces::sandbox().await.unwrap();
let token = network.dev_create_account().await.unwrap();
let account = network.dev_create_account().await.unwrap();
let network = NetworkConfig::from(network);
let token_signer = Signer::new(Signer::from_workspace(&token)).unwrap();
// Deploying token contract
Contract::deploy(
token.id().clone(),
include_bytes!("../resources/fungible_token.wasm").to_vec(),
)
.with_init_call(
"new_default_meta",
json!({
"owner_id": token.id().to_string(),
"total_supply": "1000000000000000000000000000"
}),
)
.unwrap()
.with_signer(token_signer.clone())
.send_to(&network)
.await
.unwrap();
// Verifying that user has 1000 tokens
let tokens = Tokens::account(token.id().clone())
.ft_balance(token.id().clone())
.unwrap()
.fetch_from(&network)
.await
.unwrap();
println!("Owner has {}", tokens);
// Transfer 100 tokens to the account
// We handle internally the storage deposit for the receiver account
Tokens::account(token.id().clone())
.send_to(account.id().clone())
.ft(
token.id().clone(),
// Send 1.5 tokens
FTBalance::with_decimals(24).with_whole_amount(100),
)
.unwrap()
.with_signer(token_signer.clone())
.send_to(&network)
.await
.unwrap()
.assert_success();
let tokens = Tokens::account(account.id().clone())
.ft_balance(token.id().clone())
.unwrap()
.fetch_from(&network)
.await
.unwrap();
println!("Account has {}", tokens);
let tokens = Tokens::account(token.id().clone())
.ft_balance(token.id().clone())
.unwrap()
.fetch_from(&network)
.await
.unwrap();
println!("Owner has {}", tokens);
// We validate decimals at the network level so this should fail with a validation error
let token = Tokens::account(token.id().clone())
.send_to(account.id().clone())
.ft(
token.id().clone(),
FTBalance::with_decimals(8).with_whole_amount(100),
)
.unwrap()
.with_signer(token_signer)
.send_to(&network)
.await;
assert!(token.is_err());
println!(
"Expected decimal validation error: {}",
token.err().unwrap()
);
}