From 3353fe9b71aa58b13f867d782af60f54bbe71b63 Mon Sep 17 00:00:00 2001 From: Matt Lockyer Date: Fri, 11 Feb 2022 15:41:13 -0800 Subject: [PATCH 1/2] remove any to_vector/as_vector stuff to save gas --- nft-contract/src/enumeration.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/nft-contract/src/enumeration.rs b/nft-contract/src/enumeration.rs index 1dc1083..68c39ad 100644 --- a/nft-contract/src/enumeration.rs +++ b/nft-contract/src/enumeration.rs @@ -10,14 +10,12 @@ impl Contract { //Query for nft tokens on the contract regardless of the owner using pagination pub fn nft_tokens(&self, from_index: Option, limit: Option) -> Vec { - //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() + 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 @@ -61,14 +59,12 @@ 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 @@ -78,4 +74,4 @@ impl Contract { //since we turned the keys into an iterator, we need to turn it back into a vector to return .collect() } -} \ No newline at end of file +} From 50f1b004fec019e1e430f99730f9ce2511d841a0 Mon Sep 17 00:00:00 2001 From: BenKurrek Date: Sat, 12 Feb 2022 15:27:06 -0500 Subject: [PATCH 2/2] Added more comments and changed default limit from 0 to 50 --- nft-contract/src/enumeration.rs | 11 +++++------ out/main.wasm | Bin 301803 -> 301803 bytes 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/nft-contract/src/enumeration.rs b/nft-contract/src/enumeration.rs index 68c39ad..5d1e0de 100644 --- a/nft-contract/src/enumeration.rs +++ b/nft-contract/src/enumeration.rs @@ -10,16 +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, limit: Option) -> Vec { - //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 + //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 @@ -67,8 +66,8 @@ impl Contract { 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 diff --git a/out/main.wasm b/out/main.wasm index 86f569d374964c4645911b883d4d46ccaf9345ab..622cb0d238560f5e9787c1e5eda7797580f3e84f 100755 GIT binary patch delta 57 zcmaF8Rp|9rp@tU5Elh5;j7HntYniMW8I9VT>zKAT*D=TJ78GY-V6X$?DN+IqLO|MM K`v*&w-w6OKnh_fS delta 57 zcmaF8Rp|9rp@tU5Elh5;j11e|YniMW85!D}>zKAT*D=TJ7L;OOV6X$?DN+IqLO|Mg K`v*&w-w6O5#t^>%