Skip to content

Commit

Permalink
fixes and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ruseinov committed Sep 26, 2023
1 parent 3d7a4ad commit 562b284
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 36 deletions.
24 changes: 13 additions & 11 deletions examples/non-fungible-token/nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<u32>) -> 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,
Expand All @@ -234,10 +232,14 @@ impl NonFungibleTokenPayout for Contract {
balance: U128,
max_len_payout: Option<u32>,
) -> 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,
)
}
}

Expand Down
1 change: 1 addition & 0 deletions examples/non-fungible-token/tests/workspaces/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod test_approval;
mod test_core;
mod test_enumeration;
mod test_payout;
mod utils;
16 changes: 4 additions & 12 deletions examples/non-fungible-token/tests/workspaces/test_approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Token>()?;
let token =
nft_contract.call("nft_token").args_json((TOKEN_ID,)).view().await?.json::<Token>()?;
let mut expected_approvals: HashMap<AccountId, u64> = HashMap::new();
expected_approvals.insert(AccountId::try_from(alice.id().to_string())?, 1);
assert_eq!(token.approved_account_ids.unwrap(), expected_approvals);
Expand Down Expand Up @@ -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::<Token>()?;
let token =
nft_contract.call("nft_token").args_json((TOKEN_ID,)).view().await?.json::<Token>()?;
assert_eq!(token.owner_id.to_string(), alice.id().to_string());

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions examples/non-fungible-token/tests/workspaces/test_payout.rs
Original file line number Diff line number Diff line change
@@ -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::<u32>::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::<Token>()?;
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::<u64>::None,
Some("simple transfer".to_string()),
U128::from(1),
Option::<u32>::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::<Token>()?;
assert_eq!(token.owner_id.to_string(), alice.id().to_string());

Ok(())
}
16 changes: 9 additions & 7 deletions near-contract-standards/src/non_fungible_token/payout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ pub struct Royalties {
/// balance: U128,
/// max_len_payout: Option<u32>,
/// ) -> 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(
Expand All @@ -79,10 +77,14 @@ pub struct Royalties {
/// balance: U128,
/// max_len_payout: Option<u32>,
/// ) -> 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,
/// )
/// }
/// }
/// ```
Expand Down

0 comments on commit 562b284

Please sign in to comment.