Skip to content

Commit 356a14c

Browse files
committed
chore: format code and tests
1 parent 645d253 commit 356a14c

File tree

3 files changed

+73
-44
lines changed

3 files changed

+73
-44
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
2-
name = "contract"
2+
name = "token-factory"
33
description = "Factory Contract Example"
4-
version = "0.1.0"
4+
version = "1.0.0"
55
edition = "2021"
66
# TODO: Fill out the repository field to help NEAR ecosystem tools to discover your project.
77
# NEP-0330 is automatically implemented for all contracts built with https://github.com/near/cargo-near.

src/deploy.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn is_valid_token_id(token_id: &TokenId) -> bool {
2828
impl Contract {
2929
pub fn get_required(&self, args: &TokenArgs) -> NearToken {
3030
env::storage_byte_cost().saturating_mul(
31-
(FT_CONTRACT.len() + EXTRA_BYTES + borsh::to_vec(args).unwrap().len() * 2)
31+
(FT_CONTRACT.len() + EXTRA_BYTES + borsh::to_vec(args).unwrap().len())
3232
.try_into()
3333
.unwrap(),
3434
)
@@ -57,12 +57,9 @@ impl Contract {
5757
format!("Attach at least {required} yⓃ")
5858
);
5959

60-
let token_account_id: AccountId = format!("{}.{}", token_id, env::current_account_id())
61-
.parse()
62-
.unwrap();
6360
let init_args = near_sdk::serde_json::to_vec(&args).unwrap();
6461

65-
Promise::new(token_account_id)
62+
Promise::new(token_account_id.parse().unwrap())
6663
.create_account()
6764
.transfer(attached)
6865
.deploy_contract(FT_CONTRACT.to_vec())

tests/sandbox.rs

+69-37
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1515
let sandbox = near_workspaces::sandbox().await?;
1616
let contract_wasm = near_workspaces::compile_project("./").await?;
1717
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
2028
.create_subaccount("alice")
21-
.initial_balance(NearToken::from_near(30))
29+
.initial_balance(NearToken::from_near(5))
2230
.transact()
2331
.await?
2432
.into_result()?;
25-
let bob_account = token_owner_account
33+
34+
let bob_account = root
2635
.create_subaccount("bob")
27-
.initial_balance(NearToken::from_near(30))
36+
.initial_balance(NearToken::from_near(5))
2837
.transact()
2938
.await?
3039
.into_result()?;
@@ -47,26 +56,27 @@ async fn create_token(
4756
bob_account: &Account,
4857
) -> Result<(), Box<dyn std::error::Error>> {
4958
// Initial setup
50-
let symbol = "TEST";
59+
let symbol = "SOMETHING";
5160
let total_supply = U128(100);
5261
let token_id = symbol.to_ascii_lowercase();
5362
let metadata = FungibleTokenMetadata {
5463
spec: "ft-1.0.0".to_string(),
55-
name: "Test Token".to_string(),
64+
name: "The Something Token".to_string(),
5665
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()),
5968
reference: None,
6069
reference_hash: None,
6170
};
71+
6272
let token_args = TokenArgs {
6373
owner_id: token_owner_account.id().clone(),
6474
total_supply,
6575
metadata,
6676
};
6777

6878
// Getting required deposit based on provided arguments
69-
let required_deposit: serde_json::Value = contract
79+
let required_deposit: U128 = contract
7080
.view("get_required")
7181
.args_json(json!({"args": token_args}))
7282
.await?
@@ -77,9 +87,7 @@ async fn create_token(
7787
.call("create_token")
7888
.args_json(json!({"args": token_args}))
7989
.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))
8391
.transact()
8492
.await?;
8593
assert!(res_0.is_failure());
@@ -89,14 +97,25 @@ async fn create_token(
8997
.call("create_token")
9098
.args_json(json!({"args": token_args}))
9199
.max_gas()
92-
.deposit(NearToken::from_yoctonear(
93-
required_deposit.as_str().unwrap().parse::<u128>()?,
94-
))
100+
.deposit(NearToken::from_yoctonear(required_deposit.0))
95101
.transact()
96102
.await?;
97103

104+
println!("{:?}", required_deposit);
98105
assert!(res_1.is_success());
99106

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+
100119
// Checking created token account and metadata
101120
let token_account_id: AccountId = format!("{}.{}", token_id, contract.id()).parse().unwrap();
102121
let token_metadata: FungibleTokenMetadata = token_owner_account
@@ -108,75 +127,71 @@ async fn create_token(
108127
assert_eq!(token_metadata.symbol, symbol);
109128

110129
// Checking token supply
111-
let token_total_supply: serde_json::Value = token_owner_account
130+
let token_total_supply: U128 = token_owner_account
112131
.view(&token_account_id, "ft_total_supply")
113132
.args_json(json!({}))
114133
.await?
115134
.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);
120136

121137
// 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
123139
.view(&token_account_id, "ft_balance_of")
124140
.args_json(json!({"account_id": token_owner_account.id()}))
125141
.await?
126142
.json()?;
127143

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);
132145

133-
// Checking transfering tokens from owner to other account
146+
// Checking transferring tokens from owner to other account
134147
let _ = alice_account
135148
.call(&token_account_id, "storage_deposit")
136149
.args_json(json!({"account_id": alice_account.id()}))
137150
.max_gas()
138151
.deposit(NearToken::from_millinear(250))
139152
.transact()
140153
.await?;
141-
let alice_balance_before: serde_json::Value = alice_account
154+
155+
let alice_balance_before: U128 = alice_account
142156
.view(&token_account_id, "ft_balance_of")
143157
.args_json(json!({"account_id": alice_account.id()}))
144158
.await?
145159
.json()?;
146-
assert_eq!(alice_balance_before, "0");
160+
assert_eq!(alice_balance_before.0, 0);
147161

148162
let _ = token_owner_account
149163
.call(&token_account_id, "ft_transfer")
150164
.args_json(json!({
151165
"receiver_id": alice_account.id(),
152-
"amount": "1",
166+
"amount": "2",
153167
}))
154168
.max_gas()
155169
.deposit(NearToken::from_yoctonear(1))
156170
.transact()
157171
.await?;
158172

159-
let alice_balance_after: serde_json::Value = alice_account
173+
let alice_balance_after: U128 = alice_account
160174
.view(&token_account_id, "ft_balance_of")
161175
.args_json(json!({"account_id": alice_account.id()}))
162176
.await?
163177
.json()?;
164-
assert_eq!(alice_balance_after, "1");
178+
assert_eq!(alice_balance_after.0, 2);
165179

166-
// Checking transfering token from alice to bob
180+
// Checking transferring token from alice to bob
167181
let _ = bob_account
168182
.call(&token_account_id, "storage_deposit")
169183
.args_json(json!({"account_id": bob_account.id()}))
170184
.max_gas()
171185
.deposit(NearToken::from_millinear(250))
172186
.transact()
173187
.await?;
174-
let bob_balance_before: serde_json::Value = bob_account
188+
let bob_balance_before: U128 = bob_account
175189
.view(&token_account_id, "ft_balance_of")
176190
.args_json(json!({"account_id": bob_account.id()}))
177191
.await?
178192
.json()?;
179-
assert_eq!(bob_balance_before, "0");
193+
assert_eq!(bob_balance_before.0, 0);
194+
180195
let _ = alice_account
181196
.call(&token_account_id, "ft_transfer")
182197
.args_json(json!({
@@ -187,11 +202,28 @@ async fn create_token(
187202
.deposit(NearToken::from_yoctonear(1))
188203
.transact()
189204
.await?;
190-
let bob_balance_after: serde_json::Value = bob_account
205+
let bob_balance_after: U128 = bob_account
191206
.view(&token_account_id, "ft_balance_of")
192207
.args_json(json!({"account_id": bob_account.id()}))
193208
.await?
194209
.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+
196228
Ok(())
197229
}

0 commit comments

Comments
 (0)