Skip to content

Commit

Permalink
refactor: logging with price for nft_buy
Browse files Browse the repository at this point in the history
  • Loading branch information
emarai committed Sep 3, 2021
1 parent b2262b4 commit 703b7a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
45 changes: 31 additions & 14 deletions paras-nft-contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,29 @@ impl Contract {
"Paras: attached deposit is less than price : {}",
price
);
let token_id: TokenId = self._nft_mint_series(token_series_id, receiver_id);
let token_id: TokenId = self._nft_mint_series(token_series_id, receiver_id.clone());

let for_treasury = price as u128 * TREASURY_FEE / 10_000u128;
let price_deducted = price - for_treasury;
Promise::new(token_series.creator_id).transfer(price_deducted);
Promise::new(self.treasury_id.clone()).transfer(for_treasury);

refund_deposit(env::storage_usage() - initial_storage_usage, price);

env::log(
json!({
"type": "nft_transfer",
"params": {
"token_id": token_id,
"sender_id": "",
"receiver_id": receiver_id,
"price": price.to_string(),
}
})
.to_string()
.as_bytes(),
);

token_id
}

Expand All @@ -251,9 +266,23 @@ impl Contract {

let token_series = self.token_series_by_id.get(&token_series_id).expect("Paras: Token series not exist");
assert_eq!(env::predecessor_account_id(), token_series.creator_id, "Paras: not creator");
let token_id: TokenId = self._nft_mint_series(token_series_id, receiver_id);
let token_id: TokenId = self._nft_mint_series(token_series_id, receiver_id.clone());

refund_deposit(env::storage_usage() - initial_storage_usage, 0);

env::log(
json!({
"type": "nft_transfer",
"params": {
"token_id": token_id,
"sender_id": "",
"receiver_id": receiver_id,
}
})
.to_string()
.as_bytes(),
);

token_id
}

Expand Down Expand Up @@ -318,18 +347,6 @@ impl Contract {
tokens_per_owner.insert(&owner_id, &token_ids);
}

env::log(
json!({
"type": "nft_transfer",
"params": {
"token_id": token_id,
"sender_id": "",
"receiver_id": owner_id,
}
})
.to_string()
.as_bytes(),
);

token_id
}
Expand Down
5 changes: 3 additions & 2 deletions tests/simulation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn simulate_mint() {
fn simulate_approve() {
let (root, nft, _) = init();

let trst = root.create_user("trst".repeat(16), to_yocto("100"));
root.call(
nft.account_id(),
"nft_create_series",
Expand All @@ -179,7 +180,7 @@ fn simulate_approve() {
nft.account_id(),
"nft_buy",
&json!({
"token_series_id": u128::MAX.to_string(),
"token_series_id": "1",
"receiver_id": root.account_id(),
}).to_string().into_bytes(),
DEFAULT_GAS,
Expand All @@ -194,7 +195,7 @@ fn simulate_approve() {
"nft_approve",
&json!({
"token_id": format!("1:1"),
"account_id": "test".repeat(16),
"account_id": trst.account_id(),
"msg": "{\"price\":\"3000000000000000000000000\",\"ft_token_id\":\"near\"}",
}).to_string().into_bytes(),
DEFAULT_GAS,
Expand Down

0 comments on commit 703b7a8

Please sign in to comment.