Skip to content

Commit

Permalink
Merge pull request #111 from Alien-Worlds/DEMZNE-1257_Reset-staketime…
Browse files Browse the repository at this point in the history
…-automatically-when-stakes-and-unstakes-are-removed

Demzne 1257 reset staketime automatically when stakes and unstakes are removed
  • Loading branch information
angelol authored Mar 15, 2023
2 parents e8ffaf9 + f06bb4f commit d63b60a
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1,155 deletions.
14 changes: 13 additions & 1 deletion contract-shared-headers/eosdactokens_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,14 @@ namespace eosdac {

asset liquid = get_balance(owner, code, sym.code());

auto canDeleteStakeTime = true;

auto existing_stake = stakes.find(owner.value);
if (existing_stake != stakes.end()) {
liquid -= existing_stake->stake;
canDeleteStakeTime = false;
}

auto unstakes_itr = unstakes_idx.find(owner.value);
while (unstakes_itr != unstakes_idx.end() && unstakes_itr->account == owner) {
if (unstakes_itr->released()) {
Expand All @@ -188,12 +192,20 @@ namespace eosdac {
unstakes_itr = unstakes_idx.erase(unstakes_itr);
} else {
print("NOT yet released");

canDeleteStakeTime = false;
// otherwise it still negatively impacts the liquid balance
liquid -= unstakes_itr->stake;
unstakes_itr++;
}
}
if (canDeleteStakeTime) {
staketimes_table staketimes(code, dac.dac_id.value);

auto existing_time = staketimes.find(owner.value);
if (existing_time != staketimes.end()) {
staketimes.erase(existing_time);
}
}

return liquid;
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/eosdactokens/eosdactokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ namespace eosdac {
}

// send notification for unstake at current delay and then stake with new delay
name custodian_contract = dac.account_for_type(dacdir::CUSTODIAN);
name vote_contract = dac.account_for_type(dacdir::VOTE_WEIGHT);
name notify_contract = (vote_contract) ? vote_contract : custodian_contract;
asset current_stake = eosdac::get_staked(account, get_self(), token_symbol);
const auto custodian_contract = dac.account_for_type_maybe(dacdir::CUSTODIAN);
const auto vote_contract = dac.account_for_type_maybe(dacdir::VOTE_WEIGHT);
const auto notify_contract = (vote_contract) ? *vote_contract : *custodian_contract;
asset current_stake = eosdac::get_staked(account, get_self(), token_symbol);

account_stake_delta stake_deltas_sub = {account, -current_stake, unstake_time_before};
account_stake_delta stake_deltas_add = {account, current_stake, unstake_time};
Expand Down
116 changes: 114 additions & 2 deletions contracts/eosdactokens/eosdactokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ describe('EOSDacTokens', () => {
let staker1: l.Account;
let staker2: l.Account;
before(async () => {
staker1 = await l.AccountManager.createAccount();
staker2 = await l.AccountManager.createAccount();
staker1 = await l.AccountManager.createAccount('abcstaker1');
staker2 = await l.AccountManager.createAccount('abcstaker2');
await shared.dac_token_contract.issue(
issuer.name,
'30000.0000 ABC',
Expand Down Expand Up @@ -548,6 +548,118 @@ describe('EOSDacTokens', () => {
});
});
});
context('resetstakes', async () => {
let user1: Account;
let user2: Account;
before(async () => {
user1 = await l.AccountManager.createAccount('abcuser1');
user2 = await l.AccountManager.createAccount('abcuser2');
await shared.dac_token_contract.issue(
issuer.name,
'200.0000 ABC',
'initial issued tokens',
{ from: issuer }
);
await shared.dac_token_contract.transfer(
issuer.name,
user1.name,
'100.0000 ABC',
'please take these tokens for staking',
validAuths
);
await shared.dac_token_contract.transfer(
issuer.name,
user2.name,
'100.0000 ABC',
'please take these tokens for staking',
validAuths
);
});
context('staketime', async () => {
it('should populate staketimesTable', async () => {
await shared.dac_token_contract.staketime(user1, 14, '4,ABC', {
from: user1,
});
await shared.dac_token_contract.staketime(user2, 14, '4,ABC', {
from: user2,
});
await l.assertRowsEqual(
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
[
{
account: user1.name,
delay: 14,
},
{
account: user2.name,
delay: 14,
},
]
);
});
it('staking and unstaking', async () => {
await shared.dac_token_contract.stake(user1.name, '10.0000 ABC', {
from: user1,
});
await shared.dac_token_contract.unstake(user1.name, '4.0000 ABC', {
from: user1,
});
// await l.sleep(10_000);
await shared.dac_token_contract.unstake(user1.name, '6.0000 ABC', {
from: user1,
});
await l.assertRowsEqual(
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
[
{
account: user1.name,
delay: 14,
},
{
account: user2.name,
delay: 14,
},
]
);
});
it('unstake 1 should not reset staketime', async () => {
await l.sleep(4_000);
await shared.dac_token_contract.claimunstkes(user1.name, '4,ABC', {
from: user1,
});

await l.assertRowsEqual(
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
[
{
account: user1.name,
delay: 14,
},
{
account: user2.name,
delay: 14,
},
]
);
});
it('unstake 2nd should reset staketime', async () => {
await l.sleep(10_000);

await shared.dac_token_contract.claimunstkes(user1.name, '4,ABC', {
from: user1,
});
await l.assertRowsEqual(
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
[
{
account: user2.name,
delay: 14,
},
]
);
});
});
});

context('transfer', async () => {
let sender: l.Account;
Expand Down
Loading

0 comments on commit d63b60a

Please sign in to comment.