Skip to content

Commit

Permalink
Merge pull request #475 from near/austin/nft_token_int
Browse files Browse the repository at this point in the history
fix(nft): switch nft traits to not require moving underlying struct
  • Loading branch information
mikedotexe authored Jul 22, 2021
2 parents 2743258 + 973fc4f commit e379227
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
* This type will have `ValidAccountId`'s JSON (de)serialization and the borsh serialization will be equivalent to what it was previously
* Initializes default for `BLOCKCHAIN_INTERFACE` to avoid requiring to initialize testing environment for tests that don't require custom blockchain interface configuration
* This default only affects outside of `wasm32` environments and is optional/backwards compatible
* Updates internal NFT traits to not move the underlying type for methods
* This should not be a breaking change if using the `impl` macros, only if implementing manually
* Makes `BLOCKCHAIN_INTERFACE` a concrete type and no longer exports it.
* If for testing you need this mocked blockchain, `near_sdk::mock::with_mocked_blockchain` can be used
* `near_sdk::env::take_blockchain_interface` is removed, as this interface is no longer optional
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,12 @@ impl NonFungibleTokenCore for NonFungibleToken {
.into()
}

fn nft_token(self, token_id: TokenId) -> Option<Token> {
fn nft_token(&self, token_id: TokenId) -> Option<Token> {
let owner_id = self.owner_by_id.get(&token_id)?;
let metadata = self.token_metadata_by_id.and_then(|by_id| by_id.get(&token_id));
let metadata = self.token_metadata_by_id.as_ref().and_then(|by_id| by_id.get(&token_id));
let approved_account_ids = self
.approvals_by_id
.as_ref()
.and_then(|by_id| by_id.get(&token_id).or_else(|| Some(HashMap::new())));
Some(Token { token_id, owner_id, metadata, approved_account_ids })
}
Expand Down
2 changes: 1 addition & 1 deletion near-contract-standards/src/non_fungible_token/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub trait NonFungibleTokenCore {
) -> PromiseOrValue<bool>;

/// Returns the token with the given `token_id` or `null` if no such token.
fn nft_token(self, token_id: TokenId) -> Option<Token>;
fn nft_token(&self, token_id: TokenId) -> Option<Token>;

/// Mint a new token. Not part of official standard, but needed in most situations.
/// Consuming contract expected to wrap this with an `nft_mint` function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl NonFungibleToken {
}

impl NonFungibleTokenEnumeration for NonFungibleToken {
fn nft_total_supply(self) -> U128 {
fn nft_total_supply(&self) -> U128 {
// An unfortunate cast from the max of TreeMap to the spec
(self.owner_by_id.len() as u128).into()
}
Expand All @@ -43,8 +43,8 @@ impl NonFungibleTokenEnumeration for NonFungibleToken {
.collect()
}

fn nft_supply_for_owner(self, account_id: AccountId) -> U128 {
let tokens_per_owner = self.tokens_per_owner.expect(
fn nft_supply_for_owner(&self, account_id: AccountId) -> U128 {
let tokens_per_owner = self.tokens_per_owner.as_ref().expect(
"Could not find tokens_per_owner when calling a method on the enumeration standard.",
);
tokens_per_owner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use near_sdk::AccountId;
pub trait NonFungibleTokenEnumeration {
/// Returns the total supply of non-fungible tokens as a string representing an
/// unsigned 128-bit integer to avoid JSON number limit of 2^53.
fn nft_total_supply(self) -> U128;
fn nft_total_supply(&self) -> U128;

/// Get a list of all tokens
///
Expand All @@ -32,7 +32,7 @@ pub trait NonFungibleTokenEnumeration {
/// Returns the number of non-fungible tokens owned by given `account_id` as
/// a string representing the value as an unsigned 128-bit integer to avoid JSON
/// number limit of 2^53.
fn nft_supply_for_owner(self, account_id: AccountId) -> U128;
fn nft_supply_for_owner(&self, account_id: AccountId) -> U128;

/// Get list of all tokens owned by a given account
///
Expand Down
6 changes: 3 additions & 3 deletions near-contract-standards/src/non_fungible_token/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ macro_rules! impl_non_fungible_token_core {
self.$token.nft_transfer_call(receiver_id, token_id, approval_id, memo, msg)
}

fn nft_token(self, token_id: TokenId) -> Option<Token> {
fn nft_token(&self, token_id: TokenId) -> Option<Token> {
self.$token.nft_token(token_id)
}

Expand Down Expand Up @@ -118,15 +118,15 @@ macro_rules! impl_non_fungible_token_enumeration {

#[near_bindgen]
impl NonFungibleTokenEnumeration for $contract {
fn nft_total_supply(self) -> U128 {
fn nft_total_supply(&self) -> U128 {
self.$token.nft_total_supply()
}

fn nft_tokens(&self, from_index: Option<U128>, limit: Option<u64>) -> Vec<Token> {
self.$token.nft_tokens(from_index, limit)
}

fn nft_supply_for_owner(self, account_id: AccountId) -> U128 {
fn nft_supply_for_owner(&self, account_id: AccountId) -> U128 {
self.$token.nft_supply_for_owner(account_id)
}

Expand Down

0 comments on commit e379227

Please sign in to comment.