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

remove any to_vector/as_vector stuff to save gas #17

Merged
merged 2 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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.