From 7f4a59b02c2074f87f7e504db0bd29b851b721b4 Mon Sep 17 00:00:00 2001 From: alexbolog Date: Tue, 17 Oct 2023 11:53:42 +0300 Subject: [PATCH] updated storage usage - 2 --- scripts/resetStaking.js | 50 +++++++++++++++++++ .../coding_division_sft_staking_module.rs | 24 +-------- src/staking_modules/default.rs | 32 ------------ .../snakes_sft_staking_module.rs | 24 +-------- 4 files changed, 52 insertions(+), 78 deletions(-) create mode 100644 scripts/resetStaking.js diff --git a/scripts/resetStaking.js b/scripts/resetStaking.js new file mode 100644 index 0000000..15acb3f --- /dev/null +++ b/scripts/resetStaking.js @@ -0,0 +1,50 @@ +const { getSmartContract, signAndSendTx } = require("./common"); + +const run = async () => { + const contract = await getSmartContract(); + const payload = [ + // [ + // "erd1tztluf08g90max7jkr4jtac9w5qv7qacgkhh57q9nz2erq9y2p3sd5njkg", + // "DEMIOU-704b5c", + // 1, + // 3, + // ], + // [ + // "erd1j43ssgjr8x9m0s7zz0xhn7qdgssdetm86jc6a2dzsegs74fmyl5ssv44c4", + // "DHCD-bc9963", + // 1, + // 1, + // ], + // [ + // "erd1j43ssgjr8x9m0s7zz0xhn7qdgssdetm86jc6a2dzsegs74fmyl5ssv44c4", + // "DHCD-bc9963", + // 2, + // 1, + // ], + [ + "erd1j43ssgjr8x9m0s7zz0xhn7qdgssdetm86jc6a2dzsegs74fmyl5ssv44c4", + "VESTAXDAO-e6c48c", + 2, + 2, + ], + ]; + + for (let i = 0; i < payload.length; i++) { + const [address, token, nonce, amount] = payload[i]; + await reset(contract, address, token, nonce, amount); + } +}; + +const reset = async (contract, address, token, nonce, amount) => { + const interaction = contract.methods.reset([ + address, + token, + nonce, + amount, + ]); + interaction.withGasLimit(15_000_000); + + await signAndSendTx(interaction); +}; + +run().then(() => process.exit(0)); diff --git a/src/staking_modules/coding_division_sft_staking_module.rs b/src/staking_modules/coding_division_sft_staking_module.rs index fef0433..f094993 100644 --- a/src/staking_modules/coding_division_sft_staking_module.rs +++ b/src/staking_modules/coding_division_sft_staking_module.rs @@ -97,30 +97,8 @@ where self.apply_full_set_bonus(default_base_score) } - // todo: use storage fn add_to_storage(&mut self, nonce: u64, amount: BigUint) { - let existing_item_index = self - .default_impl - .staked_assets - .iter() - .position(|p| p.nonce == nonce); - let item_to_insert; - if existing_item_index.is_none() { - item_to_insert = NonceQtyPair { - nonce, - quantity: amount, - }; - } else { - let index_to_remove = existing_item_index.unwrap(); - let existing_item = self.default_impl.staked_assets.get(index_to_remove); - self.default_impl.staked_assets.remove(index_to_remove); - item_to_insert = NonceQtyPair { - nonce, - quantity: existing_item.quantity + amount, - }; - } - - self.default_impl.staked_assets.push(item_to_insert); + self.default_impl.add_to_storage(nonce, amount); } fn start_unbonding(&mut self, payload: StartUnbondingPayload<::Api>) -> bool { diff --git a/src/staking_modules/default.rs b/src/staking_modules/default.rs index 16cc9af..daa2340 100644 --- a/src/staking_modules/default.rs +++ b/src/staking_modules/default.rs @@ -39,12 +39,6 @@ where } } - //TODO: add a struct field and store this data there instead of reading from storage everytime - //TODO: add a default DROP fn that stores the changes in this struct field to storage - pub fn get_staked_nfts_data(&self) -> ManagedVec> { - Self::get_staked_assets(&self.sc_ref, &self.impl_token_id, &self.user_address) - } - fn get_staked_assets( sc_ref: &'a C, impl_token_id: &TokenIdentifier, @@ -97,32 +91,6 @@ where fn start_unbonding(&mut self, payload: StartUnbondingPayload<::Api>) -> bool { let mut total_unstaked_quantity = BigUint::zero(); - - // let mut remaining_staked_nfts = ManagedVec::new(); - // for staked_nft in self.staked_assets.iter() { - // let unstake_nonce_quantity = payload.get_nonce_quantity(staked_nft.nonce); - // if &unstake_nonce_quantity == &BigUint::zero() { - // remaining_staked_nfts.push(staked_nft); - // continue; - // } - - // if &unstake_nonce_quantity > &staked_nft.quantity { - // return false; - // } - - // if &staked_nft.quantity == &unstake_nonce_quantity { - // total_unstaked_quantity += &staked_nft.quantity; - // continue; - // } - - // total_unstaked_quantity += &unstake_nonce_quantity; - // remaining_staked_nfts.push(NonceQtyPair { - // nonce: staked_nft.nonce, - // quantity: &staked_nft.quantity - &unstake_nonce_quantity, - // }); - // } - - // self.staked_assets = remaining_staked_nfts; let mut staked_assets_storage = self .sc_ref .staked_nfts(&self.user_address, &self.impl_token_id); diff --git a/src/staking_modules/snakes_sft_staking_module.rs b/src/staking_modules/snakes_sft_staking_module.rs index a180d25..5d7dce1 100644 --- a/src/staking_modules/snakes_sft_staking_module.rs +++ b/src/staking_modules/snakes_sft_staking_module.rs @@ -113,30 +113,8 @@ where snakes_score } - //TODO: use storage fn add_to_storage(&mut self, nonce: u64, amount: BigUint) { - let existing_item_index = self - .default_impl - .staked_assets - .iter() - .position(|p| p.nonce == nonce); - let item_to_insert; - if existing_item_index.is_none() { - item_to_insert = NonceQtyPair { - nonce, - quantity: amount, - }; - } else { - let index_to_remove = existing_item_index.unwrap(); - let existing_item = self.default_impl.staked_assets.get(index_to_remove); - self.default_impl.staked_assets.remove(index_to_remove); - item_to_insert = NonceQtyPair { - nonce, - quantity: existing_item.quantity + amount, - }; - } - - self.default_impl.staked_assets.push(item_to_insert); + self.default_impl.add_to_storage(nonce, amount); } fn start_unbonding(&mut self, payload: StartUnbondingPayload<::Api>) -> bool {