|
1 |
| -use near_workspaces::types::{AccountId, KeyType, NearToken, SecretKey}; |
| 1 | +use near_contract_standards::fungible_token::metadata::FungibleTokenMetadata; |
| 2 | +use near_sdk::{json_types::U128, near}; |
| 3 | +use near_workspaces::{types::NearToken, Account, AccountId, Contract}; |
2 | 4 | use serde_json::json;
|
3 | 5 |
|
| 6 | +#[near(serializers = [json, borsh])] |
| 7 | +struct TokenArgs { |
| 8 | + owner_id: AccountId, |
| 9 | + total_supply: U128, |
| 10 | + metadata: FungibleTokenMetadata, |
| 11 | +} |
| 12 | + |
4 | 13 | #[tokio::test]
|
5 | 14 | async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
6 | 15 | let sandbox = near_workspaces::sandbox().await?;
|
7 | 16 | let contract_wasm = near_workspaces::compile_project("./").await?;
|
8 | 17 | let contract = sandbox.dev_deploy(&contract_wasm).await?;
|
| 18 | + let token_owner_account = sandbox.root_account().unwrap(); |
| 19 | + let alice_account = token_owner_account |
| 20 | + .create_subaccount("alice") |
| 21 | + .initial_balance(NearToken::from_near(30)) |
| 22 | + .transact() |
| 23 | + .await? |
| 24 | + .into_result()?; |
| 25 | + let bob_account = token_owner_account |
| 26 | + .create_subaccount("bob") |
| 27 | + .initial_balance(NearToken::from_near(30)) |
| 28 | + .transact() |
| 29 | + .await? |
| 30 | + .into_result()?; |
| 31 | + |
| 32 | + create_token( |
| 33 | + &contract, |
| 34 | + &token_owner_account, |
| 35 | + &alice_account, |
| 36 | + &bob_account, |
| 37 | + ) |
| 38 | + .await?; |
9 | 39 |
|
10 |
| - let alice = sandbox |
11 |
| - .create_tla( |
12 |
| - "alice.test.near".parse().unwrap(), |
13 |
| - SecretKey::from_random(KeyType::ED25519), |
14 |
| - ) |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +async fn create_token( |
| 44 | + contract: &Contract, |
| 45 | + token_owner_account: &Account, |
| 46 | + alice_account: &Account, |
| 47 | + bob_account: &Account, |
| 48 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 49 | + // Initial setup |
| 50 | + let symbol = "TEST"; |
| 51 | + let total_supply = U128(100); |
| 52 | + let token_id = symbol.to_ascii_lowercase(); |
| 53 | + let metadata = FungibleTokenMetadata { |
| 54 | + spec: "ft-1.0.0".to_string(), |
| 55 | + name: "Test Token".to_string(), |
| 56 | + symbol: symbol.to_string(), |
| 57 | + decimals: 1, |
| 58 | + icon: None, |
| 59 | + reference: None, |
| 60 | + reference_hash: None, |
| 61 | + }; |
| 62 | + let token_args = TokenArgs { |
| 63 | + owner_id: token_owner_account.id().clone(), |
| 64 | + total_supply, |
| 65 | + metadata, |
| 66 | + }; |
| 67 | + |
| 68 | + // Getting required deposit based on provided arguments |
| 69 | + let required_deposit: serde_json::Value = contract |
| 70 | + .view("get_required") |
| 71 | + .args_json(json!({"args": token_args})) |
15 | 72 | .await?
|
16 |
| - .unwrap(); |
| 73 | + .json()?; |
17 | 74 |
|
18 |
| - let bob = sandbox.dev_create_account().await?; |
| 75 | + // Creating token with less than required deposit (should fail) |
| 76 | + let res_0 = contract |
| 77 | + .call("create_token") |
| 78 | + .args_json(json!({"args": token_args})) |
| 79 | + .max_gas() |
| 80 | + .deposit(NearToken::from_yoctonear( |
| 81 | + required_deposit.as_str().unwrap().parse::<u128>()? - 1, |
| 82 | + )) |
| 83 | + .transact() |
| 84 | + .await?; |
| 85 | + assert!(res_0.is_failure()); |
19 | 86 |
|
20 |
| - let res = contract |
21 |
| - .call("create_factory_subaccount_and_deploy") |
22 |
| - .args_json(json!({"name": "donation_for_alice", "beneficiary": alice.id()})) |
| 87 | + // Creating token with the required deposit |
| 88 | + let res_1 = contract |
| 89 | + .call("create_token") |
| 90 | + .args_json(json!({"args": token_args})) |
23 | 91 | .max_gas()
|
24 |
| - .deposit(NearToken::from_near(5)) |
| 92 | + .deposit(NearToken::from_yoctonear( |
| 93 | + required_deposit.as_str().unwrap().parse::<u128>()?, |
| 94 | + )) |
25 | 95 | .transact()
|
26 | 96 | .await?;
|
27 | 97 |
|
28 |
| - assert!(res.is_success()); |
| 98 | + assert!(res_1.is_success()); |
| 99 | + |
| 100 | + // Checking created token account and metadata |
| 101 | + let token_account_id: AccountId = format!("{}.{}", token_id, contract.id()).parse().unwrap(); |
| 102 | + let token_metadata: FungibleTokenMetadata = token_owner_account |
| 103 | + .view(&token_account_id, "ft_metadata") |
| 104 | + .args_json(json!({})) |
| 105 | + .await? |
| 106 | + .json()?; |
29 | 107 |
|
30 |
| - let sub_accountid: AccountId = format!("donation_for_alice.{}", contract.id()) |
31 |
| - .parse() |
32 |
| - .unwrap(); |
| 108 | + assert_eq!(token_metadata.symbol, symbol); |
33 | 109 |
|
34 |
| - let res = bob |
35 |
| - .view(&sub_accountid, "get_beneficiary") |
36 |
| - .args_json({}) |
37 |
| - .await?; |
| 110 | + // Checking token supply |
| 111 | + let token_total_supply: serde_json::Value = token_owner_account |
| 112 | + .view(&token_account_id, "ft_total_supply") |
| 113 | + .args_json(json!({})) |
| 114 | + .await? |
| 115 | + .json()?; |
| 116 | + assert_eq!( |
| 117 | + token_total_supply.as_str().unwrap().parse::<u128>()?, |
| 118 | + u128::from(total_supply) |
| 119 | + ); |
| 120 | + |
| 121 | + // Checking total supply belongs to the owner account |
| 122 | + let token_owner_balance: serde_json::Value = token_owner_account |
| 123 | + .view(&token_account_id, "ft_balance_of") |
| 124 | + .args_json(json!({"account_id": token_owner_account.id()})) |
| 125 | + .await? |
| 126 | + .json()?; |
38 | 127 |
|
39 |
| - assert_eq!(res.json::<AccountId>()?, alice.id().clone()); |
| 128 | + assert_eq!( |
| 129 | + token_owner_balance.as_str().unwrap().parse::<u128>()?, |
| 130 | + u128::from(total_supply) |
| 131 | + ); |
40 | 132 |
|
41 |
| - let res = bob |
42 |
| - .call(&sub_accountid, "donate") |
43 |
| - .args_json({}) |
| 133 | + // Checking transfering tokens from owner to other account |
| 134 | + let _ = alice_account |
| 135 | + .call(&token_account_id, "storage_deposit") |
| 136 | + .args_json(json!({"account_id": alice_account.id()})) |
44 | 137 | .max_gas()
|
45 |
| - .deposit(NearToken::from_near(5)) |
| 138 | + .deposit(NearToken::from_millinear(250)) |
46 | 139 | .transact()
|
47 | 140 | .await?;
|
| 141 | + let alice_balance_before: serde_json::Value = alice_account |
| 142 | + .view(&token_account_id, "ft_balance_of") |
| 143 | + .args_json(json!({"account_id": alice_account.id()})) |
| 144 | + .await? |
| 145 | + .json()?; |
| 146 | + assert_eq!(alice_balance_before, "0"); |
48 | 147 |
|
49 |
| - assert!(res.is_success()); |
| 148 | + let _ = token_owner_account |
| 149 | + .call(&token_account_id, "ft_transfer") |
| 150 | + .args_json(json!({ |
| 151 | + "receiver_id": alice_account.id(), |
| 152 | + "amount": "1", |
| 153 | + })) |
| 154 | + .max_gas() |
| 155 | + .deposit(NearToken::from_yoctonear(1)) |
| 156 | + .transact() |
| 157 | + .await?; |
| 158 | + |
| 159 | + let alice_balance_after: serde_json::Value = alice_account |
| 160 | + .view(&token_account_id, "ft_balance_of") |
| 161 | + .args_json(json!({"account_id": alice_account.id()})) |
| 162 | + .await? |
| 163 | + .json()?; |
| 164 | + assert_eq!(alice_balance_after, "1"); |
50 | 165 |
|
| 166 | + // Checking transfering token from alice to bob |
| 167 | + let _ = bob_account |
| 168 | + .call(&token_account_id, "storage_deposit") |
| 169 | + .args_json(json!({"account_id": bob_account.id()})) |
| 170 | + .max_gas() |
| 171 | + .deposit(NearToken::from_millinear(250)) |
| 172 | + .transact() |
| 173 | + .await?; |
| 174 | + let bob_balance_before: serde_json::Value = bob_account |
| 175 | + .view(&token_account_id, "ft_balance_of") |
| 176 | + .args_json(json!({"account_id": bob_account.id()})) |
| 177 | + .await? |
| 178 | + .json()?; |
| 179 | + assert_eq!(bob_balance_before, "0"); |
| 180 | + let _ = alice_account |
| 181 | + .call(&token_account_id, "ft_transfer") |
| 182 | + .args_json(json!({ |
| 183 | + "receiver_id": bob_account.id(), |
| 184 | + "amount": "1", |
| 185 | + })) |
| 186 | + .max_gas() |
| 187 | + .deposit(NearToken::from_yoctonear(1)) |
| 188 | + .transact() |
| 189 | + .await?; |
| 190 | + let bob_balance_after: serde_json::Value = bob_account |
| 191 | + .view(&token_account_id, "ft_balance_of") |
| 192 | + .args_json(json!({"account_id": bob_account.id()})) |
| 193 | + .await? |
| 194 | + .json()?; |
| 195 | + assert_eq!(bob_balance_after, "1"); |
51 | 196 | Ok(())
|
52 | 197 | }
|
0 commit comments