Skip to content

Commit

Permalink
updated storage usage - 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbolog committed Oct 17, 2023
1 parent 054eee4 commit 7f4a59b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 78 deletions.
50 changes: 50 additions & 0 deletions scripts/resetStaking.js
Original file line number Diff line number Diff line change
@@ -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));
24 changes: 1 addition & 23 deletions src/staking_modules/coding_division_sft_staking_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C::Api>) {
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<<C>::Api>) -> bool {
Expand Down
32 changes: 0 additions & 32 deletions src/staking_modules/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C::Api, NonceQtyPair<C::Api>> {
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<C::Api>,
Expand Down Expand Up @@ -97,32 +91,6 @@ where

fn start_unbonding(&mut self, payload: StartUnbondingPayload<<C>::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);
Expand Down
24 changes: 1 addition & 23 deletions src/staking_modules/snakes_sft_staking_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,8 @@ where
snakes_score
}

//TODO: use storage
fn add_to_storage(&mut self, nonce: u64, amount: BigUint<C::Api>) {
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<<C>::Api>) -> bool {
Expand Down

0 comments on commit 7f4a59b

Please sign in to comment.