Skip to content

Commit 2d1454e

Browse files
authored
Merge pull request #86 from uprendis/feature/fix-tests-2.0.1-rc3
fix tests 2.0.1 rc3
2 parents 9023a20 + c146bf8 commit 2d1454e

File tree

4 files changed

+121
-84
lines changed

4 files changed

+121
-84
lines changed

README.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,43 +35,51 @@ Using network 'test'.
3535
3636
Compiling your contracts...
3737
===========================
38-
> Compiling ./contracts/SafeMath.sol
39-
> Compiling ./contracts/Staker.sol
38+
> Compiling ./contracts/ownership/Ownable.sol
39+
> Compiling ./contracts/sfc/Migrations.sol
40+
> Compiling ./contracts/sfc/SafeMath.sol
41+
> Compiling ./contracts/sfc/Staker.sol
42+
> Compiling ./contracts/sfc/StakerConstants.sol
43+
> Compiling ./contracts/test/LegacyStaker.sol
44+
> Compiling ./contracts/test/UnitTestStakers.sol
45+
> Compiling ./contracts/upgradeability/Address.sol
46+
> Compiling ./contracts/upgradeability/BaseUpgradeabilityProxy.sol
47+
> Compiling ./contracts/upgradeability/Proxy.sol
48+
> Compiling ./contracts/upgradeability/UpgradeabilityProxy.sol
49+
> Compiling ./contracts/version/Version.sol
4050
4151
4252
4353
Contract: SFC
44-
Delegation migration tests
45-
gas used for sfc deploying: 4915131
46-
✓ should auto migrate legacy deposition to new model (1699ms)
47-
✓ should manually migrate legacy deposition to new model (272ms)
48-
✓ should not call calcDelegationRewards while delegation is in the legacy model (404ms)
54+
Locking stake tests
55+
✓ should start "locked stake" feature (736ms)
56+
✓ should calc raw ValidatorEpochReward correctly after locked up started (1076ms)
57+
✓ should lock stake (1208ms)
58+
✓ should lock stake with right duration (513ms)
59+
✓ should not call prepareToWithdrawStake, until locked time is pass (455ms)
60+
✓ should not call prepareToWithdrawStakePartial, until locked time is pass (477ms)
61+
✓ should lock delegation (1427ms)
62+
✓ should lock delegation with right duration (758ms)
63+
✓ should subtract penalty if prepareToWithdrawDelegation will call earlier than locked time is pass (842ms)
64+
✓ should subtract penalty if prepareToWithdrawDelegationPartial will call earlier than locked time is pass (886ms)
4965
5066
Contract: SFC
5167
Methods tests
52-
✓ checking Staker parameters (127ms)
53-
✓ checking createStake function (521ms)
54-
✓ checking increaseStake function (316ms)
55-
✓ checking createDelegation function (473ms)
56-
✓ checking createDelegation function to several stakers (581ms)
57-
✓ checking calcRawValidatorEpochReward function (385ms)
58-
✓ checking epoch snapshot logic (148ms)
59-
✓ checking calcValidatorEpochReward function (484ms)
60-
✓ checking calcDelegationEpochReward function (462ms)
61-
✓ checking claimDelegationRewards function (703ms)
68+
✓ checking Staker parameters (165ms)
69+
✓ checking createStake function (525ms)
70+
✓ checking increaseStake function (249ms)
71+
✓ checking createDelegation function (436ms)
72+
✓ checking calcRawValidatorEpochReward function (363ms)
73+
✓ checking epoch snapshot logic (163ms)
74+
✓ checking calcValidatorEpochReward function (453ms)
75+
✓ checking calcDelegationEpochReward function (418ms)
76+
✓ checking claimDelegationRewards function (647ms)
6277
✓ checking bonded ratio (107ms)
63-
✓ checking claimValidatorRewards function (419ms)
64-
✓ checking prepareToWithdrawStake function (272ms)
65-
✓ checking withdrawStake function (903ms)
66-
✓ checking prepareToWithdrawDelegation function (364ms)
67-
✓ checking withdrawDelegation function (1498ms)
68-
Locked stake tests
69-
✓ should start "locked stake" feature (352ms)
70-
✓ should calc raw ValidatorEpochReward correctly after locked up started (980ms)
71-
✓ should lock stake (2200ms)
72-
✓ should lock stake with right duration (505ms)
73-
✓ should not call prepareToWithdrawStake, until locked time is pass (441ms)
74-
✓ should not call prepareToWithdrawStakePartial, until locked time is pass (447ms)
78+
✓ checking claimValidatorRewards function (388ms)
79+
✓ checking prepareToWithdrawStake function (198ms)
80+
✓ checking withdrawStake function (922ms)
81+
✓ checking prepareToWithdrawDelegation function (330ms)
82+
✓ checking withdrawDelegation function (1497ms)
7583
7684
7785
25 passing (18s)

contracts/test/UnitTestStakers.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ contract UnitTestStakers is Stakers {
141141
return rewards.unlockedReward + rewards.lockupBaseReward + rewards.lockupExtraReward;
142142
}
143143

144-
function calcDelegationEpochReward(address delegator, uint256 stakerID, uint256 epoch, uint256 delegationAmount, uint256 commission) external view returns (uint256) {
144+
function calcDelegationEpochReward(address delegator, uint256 stakerID, uint256 epoch, uint256, uint256 commission) external view returns (uint256) {
145145
_RewardsSet memory rewards = _calcDelegationEpochReward(delegator, stakerID, epoch, commission);
146146
return rewards.unlockedReward + rewards.lockupBaseReward + rewards.lockupExtraReward;
147147
}
@@ -156,7 +156,7 @@ contract UnitTestStakers is Stakers {
156156
stakers[stakerID].paidUntilEpoch = currentSealedEpoch;
157157
}
158158

159-
function discardDelegationRewards(uint256 stakerID) public {
159+
function discardDelegationRewards(uint256) public {
160160
if (delegations[msg.sender].amount != 0) {
161161
delegations[msg.sender].paidUntilEpoch = currentSealedEpoch;
162162
}/* else if (delegations_v2[msg.sender][stakerID].amount != 0) {

test/StakeLocking.js

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const {
88
const {expect} = require('chai');
99

1010
const UnitTestStakers = artifacts.require('UnitTestStakers');
11-
const getDeposition = async (depositor, to) => this.stakers.delegations_v2.call(depositor, to);
11+
//const getDeposition = async (depositor, to) => this.stakers.delegations_v2.call(depositor, to);
12+
const getDeposition = async (depositor, to) => this.stakers.delegations.call(depositor);
1213
const getStaker = async (stakerID) => this.stakers.stakers.call(stakerID);
1314

1415
contract('SFC', async ([firstStaker, secondStaker, thirdStaker, firstDepositor, secondDepositor, thirdDepositor]) => {
@@ -453,18 +454,29 @@ contract('SFC', async ([firstStaker, secondStaker, thirdStaker, firstDepositor,
453454
await this.stakers.lockUpDelegation(duration, firstStakerID, { from: firstDepositor });
454455
await this.stakers.lockUpDelegation(duration, firstStakerID, { from: secondDepositor });
455456
await this.stakers.makeEpochSnapshots(10000, false); // epoch #3
456-
await this.stakers.discardDelegationRewards(firstStakerID, {from: firstDepositor});
457-
await this.stakers.discardDelegationRewards(firstStakerID, {from: secondDepositor});
458-
/*time.increase(86400 * 14 - 2);
457+
458+
await this.stakers.claimDelegationRewards(2, {from: firstDepositor});
459+
const penaltyNonLocked = await this.stakers.calcDelegationPenalty(firstDepositor, firstStakerID, ether('1.0'));
460+
expect(penaltyNonLocked).to.be.bignumber.equal(ether('0.0')); // penalty must be zero for non-lockup epochs
461+
462+
const reward = await this.stakers.calcDelegationRewards(firstDepositor, 0, 1);
463+
expect(reward[0]).to.be.bignumber.equal(ether('0.000000070833333333'));
464+
await this.stakers.claimDelegationRewards(100, {from: firstDepositor});
465+
await this.stakers.claimDelegationRewards(100, {from: secondDepositor});
459466
const penalty = await this.stakers.calcDelegationPenalty(firstDepositor, firstStakerID, ether('1.0'));
460-
expect(penalty).to.be.bignumber.equal(ether('0.000000068958333333'));
461-
await this.stakers.prepareToWithdrawDelegation(firstStakerID, { from: firstDepositor });
467+
expect(penalty).to.be.bignumber.equal(ether('0.000000060208333333')); // (50% of base reward + 100% of extra reward) * 1.0 FTM / 1.0 FTM
468+
469+
time.increase(86400 * 14 - 2); // not unlocked yet
470+
471+
await this.stakers.prepareToWithdrawDelegation({ from: firstDepositor });
462472
const firstDeposition = await getDeposition(firstDepositor, firstStakerID);
463473
expect(firstDeposition.amount).to.be.bignumber.equal(ether('1.0').sub(penalty));
464-
time.increase(3);
465-
await this.stakers.prepareToWithdrawDelegation(firstStakerID, { from: secondDepositor });
474+
475+
time.increase(3); // after lockup
476+
477+
await this.stakers.prepareToWithdrawDelegation({ from: secondDepositor });
466478
const secondDeposition = await getDeposition(secondDepositor, firstStakerID);
467-
expect(secondDeposition.amount).to.be.bignumber.equal(ether('1.0'));*/
479+
expect(secondDeposition.amount).to.be.bignumber.equal(ether('1.0'));
468480
});
469481

470482
it('should subtract penalty if prepareToWithdrawDelegationPartial will call earlier than locked time is pass', async () => {
@@ -486,19 +498,35 @@ contract('SFC', async ([firstStaker, secondStaker, thirdStaker, firstDepositor,
486498
await this.stakers.lockUpDelegation(duration, firstStakerID, { from: firstDepositor });
487499
await this.stakers.lockUpDelegation(duration, firstStakerID, { from: secondDepositor });
488500
await this.stakers.makeEpochSnapshots(10000, false); // epoch #3
489-
await this.stakers.discardDelegationRewards(firstStakerID, {from: firstDepositor});
490-
await this.stakers.discardDelegationRewards(firstStakerID, {from: secondDepositor});
491-
/*time.increase(86400 * 14 - 2);
501+
502+
await this.stakers.claimDelegationRewards(2, {from: firstDepositor});
503+
const penaltyNonLocked = await this.stakers.calcDelegationPenalty(firstDepositor, firstStakerID, ether('1.0'));
504+
expect(penaltyNonLocked).to.be.bignumber.equal(ether('0.0')); // penalty must be zero for non-lockup epochs
505+
506+
const reward = await this.stakers.calcDelegationRewards(firstDepositor, 0, 1);
507+
expect(reward[0]).to.be.bignumber.equal(ether('0.000000070833333333'));
508+
await this.stakers.claimDelegationRewards(100, {from: firstDepositor});
509+
await this.stakers.claimDelegationRewards(100, {from: secondDepositor});
492510
const penalty = await this.stakers.calcDelegationPenalty(firstDepositor, firstStakerID, ether('1.0'));
493-
expect(penalty).to.be.bignumber.equal(ether('0.000000034479166666')); // 50% for reward
494-
const wrID = new BN('1');
495-
await this.stakers.prepareToWithdrawDelegationPartial(wrID, firstStakerID, ether('1.0'), { from: firstDepositor });
511+
expect(penalty).to.be.bignumber.equal(ether('0.000000030104166666')); // (50% of base reward + 100% of extra reward) * 1.0 FTM / 2.0 FTM
512+
513+
time.increase(86400 * 14 - 2); // not unlocked yet
514+
515+
const wrID1 = new BN('1');
516+
await this.stakers.prepareToWithdrawDelegationPartial(wrID1, ether('1.0'), { from: firstDepositor });
496517
const firstDeposition = await getDeposition(firstDepositor, firstStakerID);
497-
expect(firstDeposition.amount).to.be.bignumber.equal(ether('1.0').sub(penalty));
498-
time.increase(3);
499-
await this.stakers.prepareToWithdrawDelegationPartial(wrID.add(new BN('1')), firstStakerID, ether('1.0'), { from: secondDepositor });
518+
const firstRequest = await this.stakers.withdrawalRequests(firstDepositor, wrID1);
519+
expect(firstDeposition.amount).to.be.bignumber.equal(ether('1.0'));
520+
expect(firstRequest.amount).to.be.bignumber.equal(ether('1.0').sub(penalty));
521+
522+
time.increase(3); // after lockup
523+
524+
const wrID2 = new BN('2');
525+
await this.stakers.prepareToWithdrawDelegationPartial(wrID2, ether('1.0'), { from: secondDepositor });
500526
const secondDeposition = await getDeposition(secondDepositor, firstStakerID);
501-
expect(secondDeposition.amount).to.be.bignumber.equal(ether('1.0'));*/
527+
const secondRequest = await this.stakers.withdrawalRequests(secondDepositor, wrID2);
528+
expect(secondDeposition.amount).to.be.bignumber.equal(ether('1.0'));
529+
expect(secondRequest.amount).to.be.bignumber.equal(ether('1.0'));
502530
});
503531
});
504532
});

0 commit comments

Comments
 (0)