Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nft): switch nft traits to not require moving underlying struct #475

Merged
merged 6 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 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
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