@@ -15,16 +15,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
15
15
let sandbox = near_workspaces:: sandbox ( ) . await ?;
16
16
let contract_wasm = near_workspaces:: compile_project ( "./" ) . await ?;
17
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
18
+ let root = sandbox. root_account ( ) . unwrap ( ) ;
19
+
20
+ let token_owner_account = root
21
+ . create_subaccount ( "the-token-owner-account-1234567890123456789" )
22
+ . initial_balance ( NearToken :: from_near ( 5 ) )
23
+ . transact ( )
24
+ . await ?
25
+ . into_result ( ) ?;
26
+
27
+ let alice_account = root
20
28
. create_subaccount ( "alice" )
21
- . initial_balance ( NearToken :: from_near ( 30 ) )
29
+ . initial_balance ( NearToken :: from_near ( 5 ) )
22
30
. transact ( )
23
31
. await ?
24
32
. into_result ( ) ?;
25
- let bob_account = token_owner_account
33
+
34
+ let bob_account = root
26
35
. create_subaccount ( "bob" )
27
- . initial_balance ( NearToken :: from_near ( 30 ) )
36
+ . initial_balance ( NearToken :: from_near ( 5 ) )
28
37
. transact ( )
29
38
. await ?
30
39
. into_result ( ) ?;
@@ -47,26 +56,27 @@ async fn create_token(
47
56
bob_account : & Account ,
48
57
) -> Result < ( ) , Box < dyn std:: error:: Error > > {
49
58
// Initial setup
50
- let symbol = "TEST " ;
59
+ let symbol = "SOMETHING " ;
51
60
let total_supply = U128 ( 100 ) ;
52
61
let token_id = symbol. to_ascii_lowercase ( ) ;
53
62
let metadata = FungibleTokenMetadata {
54
63
spec : "ft-1.0.0" . to_string ( ) ,
55
- name : "Test Token" . to_string ( ) ,
64
+ name : "The Something Token" . to_string ( ) ,
56
65
symbol : symbol. to_string ( ) ,
57
- decimals : 1 ,
58
- icon : None ,
66
+ decimals : 6 ,
67
+ icon : Some ( "data:image/svg+xml,%3Csvg width='111' height='90' viewBox='0 0 111 90' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath fill-rule='evenodd' clip-rule='evenodd' d='M24.4825 0.862305H88.0496C89.5663 0.862305 90.9675 1.64827 91.7239 2.92338L110.244 34.1419C111.204 35.7609 110.919 37.8043 109.549 39.1171L58.5729 87.9703C56.9216 89.5528 54.2652 89.5528 52.6139 87.9703L1.70699 39.1831C0.305262 37.8398 0.0427812 35.7367 1.07354 34.1077L20.8696 2.82322C21.6406 1.60483 23.0087 0.862305 24.4825 0.862305ZM79.8419 14.8003V23.5597H61.7343V29.6329C74.4518 30.2819 83.9934 32.9475 84.0642 36.1425L84.0638 42.803C83.993 45.998 74.4518 48.6635 61.7343 49.3125V64.2168H49.7105V49.3125C36.9929 48.6635 27.4513 45.998 27.3805 42.803L27.381 36.1425C27.4517 32.9475 36.9929 30.2819 49.7105 29.6329V23.5597H31.6028V14.8003H79.8419ZM55.7224 44.7367C69.2943 44.7367 80.6382 42.4827 83.4143 39.4727C81.0601 36.9202 72.5448 34.9114 61.7343 34.3597V40.7183C59.7966 40.8172 57.7852 40.8693 55.7224 40.8693C53.6595 40.8693 51.6481 40.8172 49.7105 40.7183V34.3597C38.8999 34.9114 30.3846 36.9202 28.0304 39.4727C30.8066 42.4827 42.1504 44.7367 55.7224 44.7367Z' fill='%23009393'/%3E%3C/svg%3E" . to_string ( ) ) ,
59
68
reference : None ,
60
69
reference_hash : None ,
61
70
} ;
71
+
62
72
let token_args = TokenArgs {
63
73
owner_id : token_owner_account. id ( ) . clone ( ) ,
64
74
total_supply,
65
75
metadata,
66
76
} ;
67
77
68
78
// Getting required deposit based on provided arguments
69
- let required_deposit: serde_json :: Value = contract
79
+ let required_deposit: U128 = contract
70
80
. view ( "get_required" )
71
81
. args_json ( json ! ( { "args" : token_args} ) )
72
82
. await ?
@@ -77,9 +87,7 @@ async fn create_token(
77
87
. call ( "create_token" )
78
88
. args_json ( json ! ( { "args" : token_args} ) )
79
89
. max_gas ( )
80
- . deposit ( NearToken :: from_yoctonear (
81
- required_deposit. as_str ( ) . unwrap ( ) . parse :: < u128 > ( ) ? - 1 ,
82
- ) )
90
+ . deposit ( NearToken :: from_yoctonear ( required_deposit. 0 - 1 ) )
83
91
. transact ( )
84
92
. await ?;
85
93
assert ! ( res_0. is_failure( ) ) ;
@@ -89,14 +97,25 @@ async fn create_token(
89
97
. call ( "create_token" )
90
98
. args_json ( json ! ( { "args" : token_args} ) )
91
99
. max_gas ( )
92
- . deposit ( NearToken :: from_yoctonear (
93
- required_deposit. as_str ( ) . unwrap ( ) . parse :: < u128 > ( ) ?,
94
- ) )
100
+ . deposit ( NearToken :: from_yoctonear ( required_deposit. 0 ) )
95
101
. transact ( )
96
102
. await ?;
97
103
104
+ println ! ( "{:?}" , required_deposit) ;
98
105
assert ! ( res_1. is_success( ) ) ;
99
106
107
+ // Creating token with the required deposit
108
+ let res_3 = contract
109
+ . call ( "create_token" )
110
+ . args_json ( json ! ( { "args" : token_args} ) )
111
+ . max_gas ( )
112
+ . deposit ( NearToken :: from_yoctonear ( required_deposit. 0 ) )
113
+ . transact ( )
114
+ . await ?;
115
+
116
+ println ! ( "{:?}" , required_deposit) ;
117
+ assert ! ( res_3. is_failure( ) ) ;
118
+
100
119
// Checking created token account and metadata
101
120
let token_account_id: AccountId = format ! ( "{}.{}" , token_id, contract. id( ) ) . parse ( ) . unwrap ( ) ;
102
121
let token_metadata: FungibleTokenMetadata = token_owner_account
@@ -108,75 +127,71 @@ async fn create_token(
108
127
assert_eq ! ( token_metadata. symbol, symbol) ;
109
128
110
129
// Checking token supply
111
- let token_total_supply: serde_json :: Value = token_owner_account
130
+ let token_total_supply: U128 = token_owner_account
112
131
. view ( & token_account_id, "ft_total_supply" )
113
132
. args_json ( json ! ( { } ) )
114
133
. await ?
115
134
. json ( ) ?;
116
- assert_eq ! (
117
- token_total_supply. as_str( ) . unwrap( ) . parse:: <u128 >( ) ?,
118
- u128 :: from( total_supply)
119
- ) ;
135
+ assert_eq ! ( token_total_supply. 0 , total_supply. 0 ) ;
120
136
121
137
// Checking total supply belongs to the owner account
122
- let token_owner_balance: serde_json :: Value = token_owner_account
138
+ let token_owner_balance: U128 = token_owner_account
123
139
. view ( & token_account_id, "ft_balance_of" )
124
140
. args_json ( json ! ( { "account_id" : token_owner_account. id( ) } ) )
125
141
. await ?
126
142
. json ( ) ?;
127
143
128
- assert_eq ! (
129
- token_owner_balance. as_str( ) . unwrap( ) . parse:: <u128 >( ) ?,
130
- u128 :: from( total_supply)
131
- ) ;
144
+ assert_eq ! ( token_owner_balance. 0 , total_supply. 0 ) ;
132
145
133
- // Checking transfering tokens from owner to other account
146
+ // Checking transferring tokens from owner to other account
134
147
let _ = alice_account
135
148
. call ( & token_account_id, "storage_deposit" )
136
149
. args_json ( json ! ( { "account_id" : alice_account. id( ) } ) )
137
150
. max_gas ( )
138
151
. deposit ( NearToken :: from_millinear ( 250 ) )
139
152
. transact ( )
140
153
. await ?;
141
- let alice_balance_before: serde_json:: Value = alice_account
154
+
155
+ let alice_balance_before: U128 = alice_account
142
156
. view ( & token_account_id, "ft_balance_of" )
143
157
. args_json ( json ! ( { "account_id" : alice_account. id( ) } ) )
144
158
. await ?
145
159
. json ( ) ?;
146
- assert_eq ! ( alice_balance_before, "0" ) ;
160
+ assert_eq ! ( alice_balance_before. 0 , 0 ) ;
147
161
148
162
let _ = token_owner_account
149
163
. call ( & token_account_id, "ft_transfer" )
150
164
. args_json ( json ! ( {
151
165
"receiver_id" : alice_account. id( ) ,
152
- "amount" : "1 " ,
166
+ "amount" : "2 " ,
153
167
} ) )
154
168
. max_gas ( )
155
169
. deposit ( NearToken :: from_yoctonear ( 1 ) )
156
170
. transact ( )
157
171
. await ?;
158
172
159
- let alice_balance_after: serde_json :: Value = alice_account
173
+ let alice_balance_after: U128 = alice_account
160
174
. view ( & token_account_id, "ft_balance_of" )
161
175
. args_json ( json ! ( { "account_id" : alice_account. id( ) } ) )
162
176
. await ?
163
177
. json ( ) ?;
164
- assert_eq ! ( alice_balance_after, "1" ) ;
178
+ assert_eq ! ( alice_balance_after. 0 , 2 ) ;
165
179
166
- // Checking transfering token from alice to bob
180
+ // Checking transferring token from alice to bob
167
181
let _ = bob_account
168
182
. call ( & token_account_id, "storage_deposit" )
169
183
. args_json ( json ! ( { "account_id" : bob_account. id( ) } ) )
170
184
. max_gas ( )
171
185
. deposit ( NearToken :: from_millinear ( 250 ) )
172
186
. transact ( )
173
187
. await ?;
174
- let bob_balance_before: serde_json :: Value = bob_account
188
+ let bob_balance_before: U128 = bob_account
175
189
. view ( & token_account_id, "ft_balance_of" )
176
190
. args_json ( json ! ( { "account_id" : bob_account. id( ) } ) )
177
191
. await ?
178
192
. json ( ) ?;
179
- assert_eq ! ( bob_balance_before, "0" ) ;
193
+ assert_eq ! ( bob_balance_before. 0 , 0 ) ;
194
+
180
195
let _ = alice_account
181
196
. call ( & token_account_id, "ft_transfer" )
182
197
. args_json ( json ! ( {
@@ -187,11 +202,28 @@ async fn create_token(
187
202
. deposit ( NearToken :: from_yoctonear ( 1 ) )
188
203
. transact ( )
189
204
. await ?;
190
- let bob_balance_after: serde_json :: Value = bob_account
205
+ let bob_balance_after: U128 = bob_account
191
206
. view ( & token_account_id, "ft_balance_of" )
192
207
. args_json ( json ! ( { "account_id" : bob_account. id( ) } ) )
193
208
. await ?
194
209
. json ( ) ?;
195
- assert_eq ! ( bob_balance_after, "1" ) ;
210
+ assert_eq ! ( bob_balance_after. 0 , 1 ) ;
211
+
212
+ let alice_balance_after: U128 = alice_account
213
+ . view ( & token_account_id, "ft_balance_of" )
214
+ . args_json ( json ! ( { "account_id" : alice_account. id( ) } ) )
215
+ . await ?
216
+ . json ( ) ?;
217
+ assert_eq ! ( alice_balance_after. 0 , 1 ) ;
218
+
219
+ // Checking total supply belongs to the owner account
220
+ let token_owner_balance: U128 = token_owner_account
221
+ . view ( & token_account_id, "ft_balance_of" )
222
+ . args_json ( json ! ( { "account_id" : token_owner_account. id( ) } ) )
223
+ . await ?
224
+ . json ( ) ?;
225
+
226
+ assert_eq ! ( token_owner_balance. 0 , total_supply. 0 - 2 ) ;
227
+
196
228
Ok ( ( ) )
197
229
}
0 commit comments