Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Nov 27, 2024
1 parent 6715e77 commit e641671
Show file tree
Hide file tree
Showing 10 changed files with 565 additions and 269 deletions.
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ impl Contract {
/// in this call. `self.tokens.mint` will also require it to be Some, since
/// `StorageKey::TokenMetadata` was provided at initialization.
///
/// `self.tokens.mint` will enforce `predecessor_account_id` to equal the `owner_id` given in
/// initialization call to `new`.
#[payable]
pub fn nft_mint(
&mut self,
Expand Down
51 changes: 51 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use near_contract_standards::non_fungible_token::metadata::TokenMetadata;
use near_contract_standards::non_fungible_token::TokenId;

use near_sdk::serde_json::json;
use near_sdk::AccountId;
use near_workspaces::types::NearToken;
use near_workspaces::{Account, Contract};

pub async fn mint_nft(
minter: &Account,
contract_id: &AccountId,
token_id: TokenId,
token_owner_id: &AccountId,
) -> anyhow::Result<()> {
let token_metadata = TokenMetadata {
title: Some(format!("Title for {token_id}")),
description: Some(format!("Description for {token_id}")),
media: None,
media_hash: None,
copies: Some(1u64),
issued_at: None,
expires_at: None,
starts_at: None,
updated_at: None,
extra: None,
reference: None,
reference_hash: None,
};
let res = minter
.call(contract_id, "nft_mint")
.args_json(json!({"token_id": token_id, "token_owner_id": token_owner_id, "token_metadata": token_metadata}))
.max_gas()
.deposit(NearToken::from_millinear(7))
.transact()
.await?;
assert!(res.is_success());

Ok(())
}

pub async fn init_nft_contract(contract: &Contract) -> anyhow::Result<()> {
let res = contract
.call("new_default_meta")
.args_json((contract.id(),))
.max_gas()
.transact()
.await?;
assert!(res.is_success());

Ok(())
}
20 changes: 3 additions & 17 deletions tests/contracts/approval-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,20 @@ A stub contract that implements nft_on_approve for e2e testing nft_approve.
*/
use near_contract_standards::non_fungible_token::approval::NonFungibleTokenApprovalReceiver;
use near_contract_standards::non_fungible_token::TokenId;
use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue};
use near_sdk::{env, log, near, require, AccountId, Gas, PromiseOrValue};

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

#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct ApprovalReceiver {
non_fungible_token_account_id: AccountId,
}
#[derive(Default)]
pub struct ApprovalReceiver {}

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

#[near]
impl ApprovalReceiver {
#[init]
pub fn new(non_fungible_token_account_id: AccountId) -> Self {
Self { non_fungible_token_account_id: non_fungible_token_account_id.into() }
}
}

#[near]
impl NonFungibleTokenApprovalReceiver for ApprovalReceiver {
/// Could do anything useful to the approval-receiving contract, such as store the given
Expand All @@ -43,10 +33,6 @@ impl NonFungibleTokenApprovalReceiver for ApprovalReceiver {
msg: String,
) -> PromiseOrValue<String> {
// Verifying that we were called by non-fungible token contract that we expect.
require!(
env::predecessor_account_id() == self.non_fungible_token_account_id,
"Only supports the one non-fungible token contract"
);
log!(
"in nft_on_approve; sender_id={}, previous_owner_id={}, token_id={}, msg={}",
&token_id,
Expand Down
20 changes: 3 additions & 17 deletions tests/contracts/token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,20 @@ A stub contract that implements nft_on_transfer for simulation testing nft_trans
*/
use near_contract_standards::non_fungible_token::core::NonFungibleTokenReceiver;
use near_contract_standards::non_fungible_token::TokenId;
use near_sdk::{env, log, near, require, AccountId, Gas, PanicOnDefault, PromiseOrValue};
use near_sdk::{env, log, near, require, AccountId, Gas, PromiseOrValue};

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

#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct TokenReceiver {
non_fungible_token_account_id: AccountId,
}
#[derive(Default)]
pub struct TokenReceiver {}

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

#[near]
impl TokenReceiver {
#[init]
pub fn new(non_fungible_token_account_id: AccountId) -> Self {
Self { non_fungible_token_account_id }
}
}

#[near]
impl NonFungibleTokenReceiver for TokenReceiver {
/// Returns true if token should be returned to `sender_id`
Expand All @@ -44,10 +34,6 @@ impl NonFungibleTokenReceiver for TokenReceiver {
msg: String,
) -> PromiseOrValue<bool> {
// Verifying that we were called by non-fungible token contract that we expect.
require!(
env::predecessor_account_id() == self.non_fungible_token_account_id,
"Only supports the one non-fungible token contract"
);
log!(
"in nft_on_transfer; sender_id={}, previous_owner_id={}, token_id={}, msg={}",
&sender_id,
Expand Down
122 changes: 0 additions & 122 deletions tests/init.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod init;
mod common;
mod tests;
Loading

0 comments on commit e641671

Please sign in to comment.