Skip to content

Commit

Permalink
Merge pull request #17 from near-examples/mattlockyer-patch-1
Browse files Browse the repository at this point in the history
remove any to_vector/as_vector stuff to save gas
  • Loading branch information
BenKurrek authored Feb 12, 2022
2 parents 783c064 + 50f1b00 commit d709e03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions nft-contract/src/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ impl Contract {

//Query for nft tokens on the contract regardless of the owner using pagination
pub fn nft_tokens(&self, from_index: Option<U128>, limit: Option<u64>) -> Vec<JsonToken> {
//get a vector of the keys in the token_metadata_by_id collection.
let keys = self.token_metadata_by_id.keys_as_vector();

//where to start pagination - if we have a from_index, we'll use that - otherwise start from 0 index
let start = u128::from(from_index.unwrap_or(U128(0)));

//iterate through the keys vector
keys.iter()
//iterate through each token using an iterator
self.token_metadata_by_id.keys()
//skip to the index we specified in the start variable
.skip(start as usize)
//take the first "limit" elements in the vector. If we didn't specify a limit, use 0
.take(limit.unwrap_or(0) as usize)
//take the first "limit" elements in the vector. If we didn't specify a limit, use 50
.take(limit.unwrap_or(50) as usize)
//we'll map the token IDs which are strings into Json Tokens
.map(|token_id| self.nft_token(token_id.clone()).unwrap())
//since we turned the keys into an iterator, we need to turn it back into a vector to return
Expand Down Expand Up @@ -61,21 +58,19 @@ impl Contract {
//if there is no set of tokens, we'll simply return an empty vector.
return vec![];
};
//we'll convert the UnorderedSet into a vector of strings
let keys = tokens.as_vector();

//where to start pagination - if we have a from_index, we'll use that - otherwise start from 0 index
let start = u128::from(from_index.unwrap_or(U128(0)));

//iterate through the keys vector
keys.iter()
tokens.iter()
//skip to the index we specified in the start variable
.skip(start as usize)
//take the first "limit" elements in the vector. If we didn't specify a limit, use 0
.take(limit.unwrap_or(0) as usize)
//take the first "limit" elements in the vector. If we didn't specify a limit, use 50
.take(limit.unwrap_or(50) as usize)
//we'll map the token IDs which are strings into Json Tokens
.map(|token_id| self.nft_token(token_id.clone()).unwrap())
//since we turned the keys into an iterator, we need to turn it back into a vector to return
.collect()
}
}
}
Binary file modified out/main.wasm
Binary file not shown.

0 comments on commit d709e03

Please sign in to comment.