From 562b284e526f9fb179c2f2ecebbe47b24e5ca298 Mon Sep 17 00:00:00 2001 From: Roman Useinov Date: Tue, 26 Sep 2023 20:40:34 +0200 Subject: [PATCH] fixes and tests --- examples/non-fungible-token/nft/src/lib.rs | 24 ++++---- .../tests/workspaces/main.rs | 1 + .../tests/workspaces/test_approval.rs | 16 ++--- .../tests/workspaces/test_enumeration.rs | 8 +-- .../tests/workspaces/test_payout.rs | 59 +++++++++++++++++++ .../src/non_fungible_token/payout/mod.rs | 16 ++--- 6 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 examples/non-fungible-token/tests/workspaces/test_payout.rs diff --git a/examples/non-fungible-token/nft/src/lib.rs b/examples/non-fungible-token/nft/src/lib.rs index bc319f674..14de726b4 100644 --- a/examples/non-fungible-token/nft/src/lib.rs +++ b/examples/non-fungible-token/nft/src/lib.rs @@ -30,8 +30,7 @@ use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; use near_sdk::collections::LazyOption; use near_sdk::json_types::U128; use near_sdk::{ - assert_one_yocto, env, near_bindgen, require, AccountId, BorshStorageKey, PanicOnDefault, - Promise, PromiseOrValue, + env, near_bindgen, require, AccountId, BorshStorageKey, PanicOnDefault, Promise, PromiseOrValue, }; use std::collections::HashMap; @@ -216,15 +215,14 @@ impl NonFungibleTokenEnumeration for Contract { } } +#[near_bindgen] impl NonFungibleTokenPayout for Contract { + #[allow(unused_variables)] fn nft_payout(&self, token_id: String, balance: U128, max_len_payout: Option) -> Payout { - let owner_id = self.tokens.owner_by_id.get(&token_id).expect("No such token_id"); - self.tokens - .royalties - .as_ref() - .map_or(Payout::default(), |r| r.create_payout(balance.0, &owner_id)) + self.tokens.nft_payout(token_id, balance, max_len_payout) } + #[payable] fn nft_transfer_payout( &mut self, receiver_id: AccountId, @@ -234,10 +232,14 @@ impl NonFungibleTokenPayout for Contract { balance: U128, max_len_payout: Option, ) -> Payout { - assert_one_yocto(); - let payout = self.nft_payout(token_id.clone(), balance, max_len_payout); - self.nft_transfer(receiver_id, token_id, approval_id, memo); - payout + self.tokens.nft_transfer_payout( + receiver_id, + token_id, + approval_id, + memo, + balance, + max_len_payout, + ) } } diff --git a/examples/non-fungible-token/tests/workspaces/main.rs b/examples/non-fungible-token/tests/workspaces/main.rs index b0d04236c..9a09d5bc0 100644 --- a/examples/non-fungible-token/tests/workspaces/main.rs +++ b/examples/non-fungible-token/tests/workspaces/main.rs @@ -1,4 +1,5 @@ mod test_approval; mod test_core; mod test_enumeration; +mod test_payout; mod utils; diff --git a/examples/non-fungible-token/tests/workspaces/test_approval.rs b/examples/non-fungible-token/tests/workspaces/test_approval.rs index eb26802c8..e8121057a 100644 --- a/examples/non-fungible-token/tests/workspaces/test_approval.rs +++ b/examples/non-fungible-token/tests/workspaces/test_approval.rs @@ -50,12 +50,8 @@ async fn simulate_simple_approve() -> anyhow::Result<()> { assert!(!alice_approval_id_is_2); // alternatively, one could check the data returned by nft_token - let token = nft_contract - .call("nft_token") - .args_json((TOKEN_ID,)) - .view() - .await? - .json::()?; + let token = + nft_contract.call("nft_token").args_json((TOKEN_ID,)).view().await?.json::()?; let mut expected_approvals: HashMap = HashMap::new(); expected_approvals.insert(AccountId::try_from(alice.id().to_string())?, 1); assert_eq!(token.approved_account_ids.unwrap(), expected_approvals); @@ -157,12 +153,8 @@ async fn simulate_approved_account_transfers_token() -> anyhow::Result<()> { assert!(res.is_success()); // token now owned by alice - let token = nft_contract - .call("nft_token") - .args_json((TOKEN_ID,)) - .view() - .await? - .json::()?; + let token = + nft_contract.call("nft_token").args_json((TOKEN_ID,)).view().await?.json::()?; assert_eq!(token.owner_id.to_string(), alice.id().to_string()); Ok(()) diff --git a/examples/non-fungible-token/tests/workspaces/test_enumeration.rs b/examples/non-fungible-token/tests/workspaces/test_enumeration.rs index 330ab4b16..f4ca13261 100644 --- a/examples/non-fungible-token/tests/workspaces/test_enumeration.rs +++ b/examples/non-fungible-token/tests/workspaces/test_enumeration.rs @@ -97,12 +97,8 @@ async fn simulate_enum_nft_supply_for_owner() -> anyhow::Result<()> { let (nft_contract, alice, _, _) = init(&worker).await?; // Get number from account with no NFTs - let owner_num_tokens: U128 = nft_contract - .call("nft_supply_for_owner") - .args_json((alice.id(),)) - .view() - .await? - .json()?; + let owner_num_tokens: U128 = + nft_contract.call("nft_supply_for_owner").args_json((alice.id(),)).view().await?.json()?; assert_eq!(owner_num_tokens, U128::from(0)); let owner_num_tokens: U128 = nft_contract diff --git a/examples/non-fungible-token/tests/workspaces/test_payout.rs b/examples/non-fungible-token/tests/workspaces/test_payout.rs new file mode 100644 index 000000000..d38928890 --- /dev/null +++ b/examples/non-fungible-token/tests/workspaces/test_payout.rs @@ -0,0 +1,59 @@ +use crate::utils::{init, TOKEN_ID}; +use near_contract_standards::non_fungible_token::Token; +use near_sdk::json_types::U128; +use near_sdk::ONE_YOCTO; + +#[tokio::test] +async fn simulate_payout() -> anyhow::Result<()> { + let worker = workspaces::sandbox().await?; + let (nft_contract, alice, _, _) = init(&worker).await?; + + let res = nft_contract + .call("nft_payout") + .args_json((TOKEN_ID, U128::from(1), Option::::None)) + .max_gas() + .transact() + .await?; + assert!(res.is_success()); + + // A single NFT transfer event should have been logged: + assert_eq!(res.logs().len(), 0); + + Ok(()) +} + +#[tokio::test] +async fn nft_transfer_payout() -> anyhow::Result<()> { + let worker = workspaces::sandbox().await?; + let (nft_contract, alice, _, _) = init(&worker).await?; + + let token = + nft_contract.call("nft_token").args_json((TOKEN_ID,)).view().await?.json::()?; + assert_eq!(token.owner_id.to_string(), nft_contract.id().to_string()); + + let res = nft_contract + .call("nft_transfer_payout") + .args_json(( + alice.id(), + TOKEN_ID, + Option::::None, + Some("simple transfer".to_string()), + U128::from(1), + Option::::None, + )) + .max_gas() + .deposit(ONE_YOCTO) + .transact() + .await?; + + assert!(res.is_success()); + + // A single NFT transfer event should have been logged: + assert_eq!(res.logs().len(), 1); + + let token = + nft_contract.call("nft_token").args_json((TOKEN_ID,)).view().await?.json::()?; + assert_eq!(token.owner_id.to_string(), alice.id().to_string()); + + Ok(()) +} diff --git a/near-contract-standards/src/non_fungible_token/payout/mod.rs b/near-contract-standards/src/non_fungible_token/payout/mod.rs index 10ef32e22..7e0592dfb 100644 --- a/near-contract-standards/src/non_fungible_token/payout/mod.rs +++ b/near-contract-standards/src/non_fungible_token/payout/mod.rs @@ -65,9 +65,7 @@ pub struct Royalties { /// balance: U128, /// max_len_payout: Option, /// ) -> Payout { -/// let owner_id = self.tokens.owner_by_id.get(&token_id).expect("No such token_id"); -/// self.tokens.royalties.as_ref() -/// .map_or(Payout::default(), |r| r.create_payout(balance.0, &owner_id)) +/// self.tokens.nft_payout(token_id, balance, max_len_payout) /// } /// #[payable] /// fn nft_transfer_payout( @@ -79,10 +77,14 @@ pub struct Royalties { /// balance: U128, /// max_len_payout: Option, /// ) -> Payout { -/// assert_one_yocto(); -/// let payout = self.nft_payout(token_id.clone(), balance, max_len_payout); -/// self.nft_transfer(receiver_id, token_id, approval_id, memo); -/// payout +/// self.tokens.nft_transfer_payout( +/// receiver_id, +/// token_id, +/// approval_id, +/// memo, +/// balance, +/// max_len_payout, +/// ) /// } /// } /// ```