Skip to content

Commit

Permalink
updated storage usage - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbolog committed Oct 17, 2023
1 parent bc401f0 commit 054eee4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/staking_modules/coding_division_sft_staking_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ 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
Expand Down
65 changes: 46 additions & 19 deletions src/staking_modules/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ where
impl_token_id: &TokenIdentifier<C::Api>,
user_address: &ManagedAddress<C::Api>,
) -> ManagedVec<C::Api, NonceQtyPair<C::Api>> {
// sc_ref.staked_nfts(&user_address, &impl_token_id);
ManagedVec::new()
sc_ref.get_staked_nfts(user_address, impl_token_id)
}
}

Expand Down Expand Up @@ -89,38 +88,66 @@ where
}

fn add_to_storage(&mut self, nonce: u64, quantity: BigUint<C::Api>) {
self.staked_assets.push(NonceQtyPair { nonce, quantity });
self.sc_ref
.staked_nfts(&self.user_address, &self.impl_token_id)
.entry(nonce)
.and_modify(|old_qty| *old_qty += &quantity)
.or_insert(quantity);
}

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;
// 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);
for item in payload.items.iter() {
let matching_staked_nft = staked_assets_storage.remove(&item.nonce);
if matching_staked_nft.is_none() {
// unbonding should fail
return false;
}

if &unstake_nonce_quantity > &staked_nft.quantity {
let matching_staked_nft = matching_staked_nft.unwrap();
if &matching_staked_nft < &item.quantity {
// unbonding should fail
return false;
}

if &staked_nft.quantity == &unstake_nonce_quantity {
total_unstaked_quantity += &staked_nft.quantity;
total_unstaked_quantity += &item.quantity;
if &matching_staked_nft == &item.quantity {
// don't add this back to storage
continue;
}

total_unstaked_quantity += &unstake_nonce_quantity;
remaining_staked_nfts.push(NonceQtyPair {
nonce: staked_nft.nonce,
quantity: &staked_nft.quantity - &unstake_nonce_quantity,
});
staked_assets_storage.insert(item.nonce, &matching_staked_nft - &item.quantity);
}

self.staked_assets = remaining_staked_nfts;

&total_unstaked_quantity > &0
}
}
Expand Down
1 change: 1 addition & 0 deletions src/staking_modules/snakes_sft_staking_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ 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
Expand Down

0 comments on commit 054eee4

Please sign in to comment.