Skip to content

Commit e641671

Browse files
committed
fix: tests
1 parent 6715e77 commit e641671

File tree

10 files changed

+565
-269
lines changed

10 files changed

+565
-269
lines changed

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ impl Contract {
9393
/// in this call. `self.tokens.mint` will also require it to be Some, since
9494
/// `StorageKey::TokenMetadata` was provided at initialization.
9595
///
96-
/// `self.tokens.mint` will enforce `predecessor_account_id` to equal the `owner_id` given in
97-
/// initialization call to `new`.
9896
#[payable]
9997
pub fn nft_mint(
10098
&mut self,

tests/common/mod.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use near_contract_standards::non_fungible_token::metadata::TokenMetadata;
2+
use near_contract_standards::non_fungible_token::TokenId;
3+
4+
use near_sdk::serde_json::json;
5+
use near_sdk::AccountId;
6+
use near_workspaces::types::NearToken;
7+
use near_workspaces::{Account, Contract};
8+
9+
pub async fn mint_nft(
10+
minter: &Account,
11+
contract_id: &AccountId,
12+
token_id: TokenId,
13+
token_owner_id: &AccountId,
14+
) -> anyhow::Result<()> {
15+
let token_metadata = TokenMetadata {
16+
title: Some(format!("Title for {token_id}")),
17+
description: Some(format!("Description for {token_id}")),
18+
media: None,
19+
media_hash: None,
20+
copies: Some(1u64),
21+
issued_at: None,
22+
expires_at: None,
23+
starts_at: None,
24+
updated_at: None,
25+
extra: None,
26+
reference: None,
27+
reference_hash: None,
28+
};
29+
let res = minter
30+
.call(contract_id, "nft_mint")
31+
.args_json(json!({"token_id": token_id, "token_owner_id": token_owner_id, "token_metadata": token_metadata}))
32+
.max_gas()
33+
.deposit(NearToken::from_millinear(7))
34+
.transact()
35+
.await?;
36+
assert!(res.is_success());
37+
38+
Ok(())
39+
}
40+
41+
pub async fn init_nft_contract(contract: &Contract) -> anyhow::Result<()> {
42+
let res = contract
43+
.call("new_default_meta")
44+
.args_json((contract.id(),))
45+
.max_gas()
46+
.transact()
47+
.await?;
48+
assert!(res.is_success());
49+
50+
Ok(())
51+
}

tests/contracts/approval-receiver/src/lib.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,20 @@ A stub contract that implements nft_on_approve for e2e testing nft_approve.
33
*/
44
use near_contract_standards::non_fungible_token::approval::NonFungibleTokenApprovalReceiver;
55
use near_contract_standards::non_fungible_token::TokenId;
6-
use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue};
6+
use near_sdk::{env, log, near, require, AccountId, Gas, PromiseOrValue};
77

88
/// It is estimated that we need to attach 5 TGas for the code execution and 5 TGas for cross-contract call
99
const GAS_FOR_NFT_ON_APPROVE: Gas = Gas::from_tgas(10);
1010

1111
#[near(contract_state)]
12-
#[derive(PanicOnDefault)]
13-
pub struct ApprovalReceiver {
14-
non_fungible_token_account_id: AccountId,
15-
}
12+
#[derive(Default)]
13+
pub struct ApprovalReceiver {}
1614

1715
// Have to repeat the same trait for our own implementation.
1816
pub trait ValueReturnTrait {
1917
fn ok_go(&self, msg: String) -> PromiseOrValue<String>;
2018
}
2119

22-
#[near]
23-
impl ApprovalReceiver {
24-
#[init]
25-
pub fn new(non_fungible_token_account_id: AccountId) -> Self {
26-
Self { non_fungible_token_account_id: non_fungible_token_account_id.into() }
27-
}
28-
}
29-
3020
#[near]
3121
impl NonFungibleTokenApprovalReceiver for ApprovalReceiver {
3222
/// Could do anything useful to the approval-receiving contract, such as store the given
@@ -43,10 +33,6 @@ impl NonFungibleTokenApprovalReceiver for ApprovalReceiver {
4333
msg: String,
4434
) -> PromiseOrValue<String> {
4535
// Verifying that we were called by non-fungible token contract that we expect.
46-
require!(
47-
env::predecessor_account_id() == self.non_fungible_token_account_id,
48-
"Only supports the one non-fungible token contract"
49-
);
5036
log!(
5137
"in nft_on_approve; sender_id={}, previous_owner_id={}, token_id={}, msg={}",
5238
&token_id,

tests/contracts/token-receiver/src/lib.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,20 @@ A stub contract that implements nft_on_transfer for simulation testing nft_trans
33
*/
44
use near_contract_standards::non_fungible_token::core::NonFungibleTokenReceiver;
55
use near_contract_standards::non_fungible_token::TokenId;
6-
use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue};
6+
use near_sdk::{env, log, near, require, AccountId, Gas, PromiseOrValue};
77

88
/// It is estimated that we need to attach 5 TGas for the code execution and 5 TGas for cross-contract call
99
const GAS_FOR_NFT_ON_TRANSFER: Gas = Gas::from_tgas(10);
1010

1111
#[near(contract_state)]
12-
#[derive(PanicOnDefault)]
13-
pub struct TokenReceiver {
14-
non_fungible_token_account_id: AccountId,
15-
}
12+
#[derive(Default)]
13+
pub struct TokenReceiver {}
1614

1715
// Have to repeat the same trait for our own implementation.
1816
pub trait ValueReturnTrait {
1917
fn ok_go(&self, return_it: bool) -> PromiseOrValue<bool>;
2018
}
2119

22-
#[near]
23-
impl TokenReceiver {
24-
#[init]
25-
pub fn new(non_fungible_token_account_id: AccountId) -> Self {
26-
Self { non_fungible_token_account_id }
27-
}
28-
}
29-
3020
#[near]
3121
impl NonFungibleTokenReceiver for TokenReceiver {
3222
/// Returns true if token should be returned to `sender_id`
@@ -44,10 +34,6 @@ impl NonFungibleTokenReceiver for TokenReceiver {
4434
msg: String,
4535
) -> PromiseOrValue<bool> {
4636
// Verifying that we were called by non-fungible token contract that we expect.
47-
require!(
48-
env::predecessor_account_id() == self.non_fungible_token_account_id,
49-
"Only supports the one non-fungible token contract"
50-
);
5137
log!(
5238
"in nft_on_transfer; sender_id={}, previous_owner_id={}, token_id={}, msg={}",
5339
&sender_id,

tests/init.rs

-122
This file was deleted.

tests/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod init;
1+
mod common;
22
mod tests;

0 commit comments

Comments
 (0)