Skip to content

Commit

Permalink
initial impl of new 4.0.0 sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
BenKurrek committed Jun 20, 2022
1 parent 48f9601 commit 2f706e2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 75 deletions.
4 changes: 2 additions & 2 deletions nft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "3.1.0"
near-contract-standards = "3.1.1"
near-sdk = "4.0.0"
near-contract-standards = "4.0.0"
19 changes: 8 additions & 11 deletions nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ use near_contract_standards::non_fungible_token::{Token, TokenId};
use near_contract_standards::non_fungible_token::NonFungibleToken;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::LazyOption;
use near_sdk::json_types::ValidAccountId;
use near_sdk::{
env, near_bindgen, AccountId, BorshStorageKey, PanicOnDefault, Promise, PromiseOrValue,
};

near_sdk::setup_alloc!();

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {
Expand All @@ -52,7 +49,7 @@ impl Contract {
/// Initializes the contract owned by `owner_id` with
/// default metadata (for example purposes only).
#[init]
pub fn new_default_meta(owner_id: ValidAccountId) -> Self {
pub fn new_default_meta(owner_id: AccountId) -> Self {
Self::new(
owner_id,
NFTContractMetadata {
Expand All @@ -68,7 +65,7 @@ impl Contract {
}

#[init]
pub fn new(owner_id: ValidAccountId, metadata: NFTContractMetadata) -> Self {
pub fn new(owner_id: AccountId, metadata: NFTContractMetadata) -> Self {
assert!(!env::state_exists(), "Already initialized");
metadata.assert_valid();
Self {
Expand All @@ -95,7 +92,7 @@ impl Contract {
pub fn nft_mint(
&mut self,
token_id: TokenId,
receiver_id: ValidAccountId,
receiver_id: AccountId,
token_metadata: TokenMetadata,
) -> Token {
self.tokens.mint(token_id, receiver_id, Some(token_metadata))
Expand All @@ -117,13 +114,13 @@ impl NonFungibleTokenMetadataProvider for Contract {
mod tests {
use near_sdk::test_utils::{accounts, VMContextBuilder};
use near_sdk::testing_env;
use near_sdk::MockedBlockchain;
use std::collections::HashMap;

use super::*;

const MINT_STORAGE_COST: u128 = 5870000000000000000000;

fn get_context(predecessor_account_id: ValidAccountId) -> VMContextBuilder {
fn get_context(predecessor_account_id: AccountId) -> VMContextBuilder {
let mut builder = VMContextBuilder::new();
builder
.current_account_id(accounts(0))
Expand Down Expand Up @@ -181,7 +178,7 @@ mod tests {
let token_id = "0".to_string();
let token = contract.nft_mint(token_id.clone(), accounts(0), sample_token_metadata());
assert_eq!(token.token_id, token_id);
assert_eq!(token.owner_id, accounts(0).to_string());
assert_eq!(token.owner_id.to_string(), accounts(0).to_string());
assert_eq!(token.metadata.unwrap(), sample_token_metadata());
assert_eq!(token.approved_account_ids.unwrap(), HashMap::new());
}
Expand Down Expand Up @@ -215,7 +212,7 @@ mod tests {
.build());
if let Some(token) = contract.nft_token(token_id.clone()) {
assert_eq!(token.token_id, token_id);
assert_eq!(token.owner_id, accounts(1).to_string());
assert_eq!(token.owner_id.to_string(), accounts(1).to_string());
assert_eq!(token.metadata.unwrap(), sample_token_metadata());
assert_eq!(token.approved_account_ids.unwrap(), HashMap::new());
} else {
Expand Down Expand Up @@ -329,4 +326,4 @@ mod tests {
.build());
assert!(!contract.nft_is_approved(token_id.clone(), accounts(1), Some(1)));
}
}
}
Empty file modified scripts/build.sh
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions test-approval-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "3.1.0"
near-contract-standards = "3.1.1"
near-sdk = "4.0.0"
near-contract-standards = "4.0.0"
27 changes: 6 additions & 21 deletions test-approval-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,11 @@ A stub contract that implements nft_on_approve for simulation testing nft_approv
use near_contract_standards::non_fungible_token::approval::NonFungibleTokenApprovalReceiver;
use near_contract_standards::non_fungible_token::TokenId;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::ValidAccountId;
use near_sdk::{
env, ext_contract, log, near_bindgen, setup_alloc, AccountId, Balance, Gas, PanicOnDefault,
env, ext_contract, log, near_bindgen, AccountId, PanicOnDefault,
PromiseOrValue,
};

setup_alloc!();

const BASE_GAS: Gas = 5_000_000_000_000;
const PROMISE_CALL: Gas = 5_000_000_000_000;
const GAS_FOR_NFT_ON_APPROVE: Gas = BASE_GAS + PROMISE_CALL;

const NO_DEPOSIT: Balance = 0;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct ApprovalReceiver {
Expand All @@ -30,15 +21,10 @@ pub trait ValueReturnTrait {
fn ok_go(&self, msg: String) -> PromiseOrValue<String>;
}

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

#[near_bindgen]
impl ApprovalReceiver {
#[init]
pub fn new(non_fungible_token_account_id: ValidAccountId) -> Self {
pub fn new(non_fungible_token_account_id: AccountId) -> Self {
Self { non_fungible_token_account_id: non_fungible_token_account_id.into() }
}
}
Expand Down Expand Up @@ -74,10 +60,9 @@ impl NonFungibleTokenApprovalReceiver for ApprovalReceiver {
match msg.as_str() {
"return-now" => PromiseOrValue::Value("cool".to_string()),
_ => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::ok_go(msg, &account_id, NO_DEPOSIT, prepaid_gas - GAS_FOR_NFT_ON_APPROVE)
.into()
// Call ok_go with no attached deposit and all unspent GAS (weight of 1)
Self::ext(env::current_account_id())
.ok_go(msg).into()
}
}
}
Expand All @@ -89,4 +74,4 @@ impl ValueReturnTrait for ApprovalReceiver {
log!("in ok_go, msg={}", msg);
PromiseOrValue::Value(msg)
}
}
}
4 changes: 2 additions & 2 deletions test-token-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = "3.1.0"
near-contract-standards = "3.1.1"
near-sdk = "4.0.0"
near-contract-standards = "4.0.0"
47 changes: 10 additions & 37 deletions test-token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ 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::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::ValidAccountId;
use near_sdk::{
env, ext_contract, log, near_bindgen, setup_alloc, AccountId, Balance, Gas, PanicOnDefault,
env, ext_contract, log, near_bindgen, AccountId, PanicOnDefault,
PromiseOrValue,
};

setup_alloc!();

const BASE_GAS: Gas = 5_000_000_000_000;
const PROMISE_CALL: Gas = 5_000_000_000_000;
const GAS_FOR_NFT_ON_TRANSFER: Gas = BASE_GAS + PROMISE_CALL;

const NO_DEPOSIT: Balance = 0;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct TokenReceiver {
Expand All @@ -30,15 +20,10 @@ pub trait ValueReturnTrait {
fn ok_go(&self, return_it: bool) -> PromiseOrValue<bool>;
}

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

#[near_bindgen]
impl TokenReceiver {
#[init]
pub fn new(non_fungible_token_account_id: ValidAccountId) -> Self {
pub fn new(non_fungible_token_account_id: AccountId) -> Self {
Self { non_fungible_token_account_id: non_fungible_token_account_id.into() }
}
}
Expand Down Expand Up @@ -75,29 +60,17 @@ impl NonFungibleTokenReceiver for TokenReceiver {
match msg.as_str() {
"return-it-now" => PromiseOrValue::Value(true),
"return-it-later" => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::ok_go(
true,
&account_id,
NO_DEPOSIT,
prepaid_gas - GAS_FOR_NFT_ON_TRANSFER,
)
.into()
// Call ok_go with no attached deposit and all unspent GAS (weight of 1)
Self::ext(env::current_account_id())
.ok_go(true).into()
}
"keep-it-now" => PromiseOrValue::Value(false),
"keep-it-later" => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::ok_go(
false,
&account_id,
NO_DEPOSIT,
prepaid_gas - GAS_FOR_NFT_ON_TRANSFER,
)
.into()
// Call ok_go with no attached deposit and all unspent GAS (weight of 1)
Self::ext(env::current_account_id())
.ok_go(false).into()
}
_ => env::panic(b"unsupported msg"),
_ => env::panic_str("unsupported msg"),
}
}
}
Expand All @@ -108,4 +81,4 @@ impl ValueReturnTrait for TokenReceiver {
log!("in ok_go, return_it={}", return_it);
PromiseOrValue::Value(return_it)
}
}
}

0 comments on commit 2f706e2

Please sign in to comment.