Skip to content

Commit d63b60a

Browse files
authored
Merge pull request #111 from Alien-Worlds/DEMZNE-1257_Reset-staketime-automatically-when-stakes-and-unstakes-are-removed
Demzne 1257 reset staketime automatically when stakes and unstakes are removed
2 parents e8ffaf9 + f06bb4f commit d63b60a

5 files changed

Lines changed: 131 additions & 1155 deletions

File tree

contract-shared-headers/eosdactokens_shared.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,14 @@ namespace eosdac {
176176

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

179+
auto canDeleteStakeTime = true;
180+
179181
auto existing_stake = stakes.find(owner.value);
180182
if (existing_stake != stakes.end()) {
181183
liquid -= existing_stake->stake;
184+
canDeleteStakeTime = false;
182185
}
186+
183187
auto unstakes_itr = unstakes_idx.find(owner.value);
184188
while (unstakes_itr != unstakes_idx.end() && unstakes_itr->account == owner) {
185189
if (unstakes_itr->released()) {
@@ -188,12 +192,20 @@ namespace eosdac {
188192
unstakes_itr = unstakes_idx.erase(unstakes_itr);
189193
} else {
190194
print("NOT yet released");
191-
195+
canDeleteStakeTime = false;
192196
// otherwise it still negatively impacts the liquid balance
193197
liquid -= unstakes_itr->stake;
194198
unstakes_itr++;
195199
}
196200
}
201+
if (canDeleteStakeTime) {
202+
staketimes_table staketimes(code, dac.dac_id.value);
203+
204+
auto existing_time = staketimes.find(owner.value);
205+
if (existing_time != staketimes.end()) {
206+
staketimes.erase(existing_time);
207+
}
208+
}
197209

198210
return liquid;
199211
}

contracts/eosdactokens/eosdactokens.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ namespace eosdac {
403403
}
404404

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

411411
account_stake_delta stake_deltas_sub = {account, -current_stake, unstake_time_before};
412412
account_stake_delta stake_deltas_add = {account, current_stake, unstake_time};

contracts/eosdactokens/eosdactokens.test.ts

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ describe('EOSDacTokens', () => {
224224
let staker1: l.Account;
225225
let staker2: l.Account;
226226
before(async () => {
227-
staker1 = await l.AccountManager.createAccount();
228-
staker2 = await l.AccountManager.createAccount();
227+
staker1 = await l.AccountManager.createAccount('abcstaker1');
228+
staker2 = await l.AccountManager.createAccount('abcstaker2');
229229
await shared.dac_token_contract.issue(
230230
issuer.name,
231231
'30000.0000 ABC',
@@ -548,6 +548,118 @@ describe('EOSDacTokens', () => {
548548
});
549549
});
550550
});
551+
context('resetstakes', async () => {
552+
let user1: Account;
553+
let user2: Account;
554+
before(async () => {
555+
user1 = await l.AccountManager.createAccount('abcuser1');
556+
user2 = await l.AccountManager.createAccount('abcuser2');
557+
await shared.dac_token_contract.issue(
558+
issuer.name,
559+
'200.0000 ABC',
560+
'initial issued tokens',
561+
{ from: issuer }
562+
);
563+
await shared.dac_token_contract.transfer(
564+
issuer.name,
565+
user1.name,
566+
'100.0000 ABC',
567+
'please take these tokens for staking',
568+
validAuths
569+
);
570+
await shared.dac_token_contract.transfer(
571+
issuer.name,
572+
user2.name,
573+
'100.0000 ABC',
574+
'please take these tokens for staking',
575+
validAuths
576+
);
577+
});
578+
context('staketime', async () => {
579+
it('should populate staketimesTable', async () => {
580+
await shared.dac_token_contract.staketime(user1, 14, '4,ABC', {
581+
from: user1,
582+
});
583+
await shared.dac_token_contract.staketime(user2, 14, '4,ABC', {
584+
from: user2,
585+
});
586+
await l.assertRowsEqual(
587+
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
588+
[
589+
{
590+
account: user1.name,
591+
delay: 14,
592+
},
593+
{
594+
account: user2.name,
595+
delay: 14,
596+
},
597+
]
598+
);
599+
});
600+
it('staking and unstaking', async () => {
601+
await shared.dac_token_contract.stake(user1.name, '10.0000 ABC', {
602+
from: user1,
603+
});
604+
await shared.dac_token_contract.unstake(user1.name, '4.0000 ABC', {
605+
from: user1,
606+
});
607+
// await l.sleep(10_000);
608+
await shared.dac_token_contract.unstake(user1.name, '6.0000 ABC', {
609+
from: user1,
610+
});
611+
await l.assertRowsEqual(
612+
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
613+
[
614+
{
615+
account: user1.name,
616+
delay: 14,
617+
},
618+
{
619+
account: user2.name,
620+
delay: 14,
621+
},
622+
]
623+
);
624+
});
625+
it('unstake 1 should not reset staketime', async () => {
626+
await l.sleep(4_000);
627+
await shared.dac_token_contract.claimunstkes(user1.name, '4,ABC', {
628+
from: user1,
629+
});
630+
631+
await l.assertRowsEqual(
632+
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
633+
[
634+
{
635+
account: user1.name,
636+
delay: 14,
637+
},
638+
{
639+
account: user2.name,
640+
delay: 14,
641+
},
642+
]
643+
);
644+
});
645+
it('unstake 2nd should reset staketime', async () => {
646+
await l.sleep(10_000);
647+
648+
await shared.dac_token_contract.claimunstkes(user1.name, '4,ABC', {
649+
from: user1,
650+
});
651+
await l.assertRowsEqual(
652+
shared.dac_token_contract.staketimeTable({ scope: 'abcdac' }),
653+
[
654+
{
655+
account: user2.name,
656+
delay: 14,
657+
},
658+
]
659+
);
660+
});
661+
});
662+
});
551663

552664
context('transfer', async () => {
553665
let sender: l.Account;

0 commit comments

Comments
 (0)