Skip to content

Commit

Permalink
Fix bug where locked_amount and get_available_amount() show incorrect…
Browse files Browse the repository at this point in the history
… results (#68)

* make sure locked_amount is always up to date

* address review comments

Co-authored-by: Constantin Dogaru <constantin.dogaru@mbntech.ro>
  • Loading branch information
ctindogaru and constantindogaru authored Dec 6, 2021
1 parent a84bbbd commit 2da4bc5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions sputnikdao2/src/bounties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl Contract {
});
self.bounty_claimers
.insert(&env::predecessor_account_id(), &claims);
self.locked_amount += env::attached_deposit();
}

/// Removes given claims from this bounty and user's claims.
Expand Down Expand Up @@ -195,6 +196,7 @@ impl Contract {
PromiseOrValue::Value(())
} else {
// Within forgiveness period. Return bond.
self.locked_amount -= policy.bounty_bond.0;
Promise::new(env::predecessor_account_id())
.transfer(policy.bounty_bond.0)
.into()
Expand Down
7 changes: 2 additions & 5 deletions sputnikdao2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct Contract {
/// Voting and permissions policy.
pub policy: LazyOption<VersionedPolicy>,

/// Amount of $NEAR locked for storage / bonds.
/// Amount of $NEAR locked for bonds.
pub locked_amount: Balance,

/// Vote staking contract id. That contract must have this account as owner.
Expand Down Expand Up @@ -92,8 +92,7 @@ impl Contract {
bounty_claimers: LookupMap::new(StorageKeys::BountyClaimers),
bounty_claims_count: LookupMap::new(StorageKeys::BountyClaimCounts),
blobs: LookupMap::new(StorageKeys::Blobs),
// TODO: only accounts for contract but not for this state object. Can just add fixed size of it.
locked_amount: env::storage_byte_cost() * (env::storage_usage() as u128),
locked_amount: 0,
}
}

Expand Down Expand Up @@ -125,7 +124,6 @@ impl Contract {
env::storage_remove(&hash);
let blob_len = env::register_len(u64::MAX - 1).unwrap();
let storage_cost = ((blob_len + 32) as u128) * env::storage_byte_cost();
self.locked_amount -= storage_cost;
Promise::new(account_id).transfer(storage_cost)
}
}
Expand Down Expand Up @@ -190,7 +188,6 @@ pub extern "C" fn store_blob() {
"ERR_NOT_ENOUGH_DEPOSIT:{}",
storage_cost
);
contract.locked_amount += storage_cost;
// Store value of register 0 into key = register 1.
sys::storage_write(u64::MAX as _, 1 as _, u64::MAX as _, 0 as _, 2);
// Load register 1 into blob_hash and save into LookupMap.
Expand Down
6 changes: 4 additions & 2 deletions sputnikdao2/src/proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ impl Contract {
}
}

fn internal_return_bond(&self, policy: &Policy, proposal: &Proposal) -> Promise {
fn internal_return_bond(&mut self, policy: &Policy, proposal: &Proposal) -> Promise {
self.locked_amount -= policy.proposal_bond.0;
Promise::new(proposal.proposer.clone()).transfer(policy.proposal_bond.0)
}

Expand Down Expand Up @@ -391,7 +392,7 @@ impl Contract {
) -> PromiseOrValue<()> {
if return_bond {
// Return bond to the proposer.
Promise::new(proposal.proposer.clone()).transfer(policy.proposal_bond.0);
self.internal_return_bond(policy, proposal);
}
match &proposal.kind {
ProposalKind::BountyDone {
Expand Down Expand Up @@ -463,6 +464,7 @@ impl Contract {
self.proposals
.insert(&id, &VersionedProposal::Default(proposal.into()));
self.last_proposal_id += 1;
self.locked_amount += env::attached_deposit();
id
}

Expand Down
8 changes: 7 additions & 1 deletion sputnikdao2/src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ impl Contract {
env::storage_has_key(&CryptoHash::from(hash))
}

/// Returns locked amount of NEAR that is used for storage.
pub fn get_locked_storage_amount(&self) -> U128 {
let locked_storage_amount = env::storage_byte_cost() * (env::storage_usage() as u128);
U128(locked_storage_amount)
}

/// Returns available amount of NEAR that can be spent (outside of amount for storage and bonds).
pub fn get_available_amount(&self) -> U128 {
U128(env::account_balance() - self.locked_amount)
U128(env::account_balance() - self.get_locked_storage_amount().0 - self.locked_amount)
}

/// Returns total delegated stake.
Expand Down

0 comments on commit 2da4bc5

Please sign in to comment.