Skip to content

Commit

Permalink
fix(staking): blockchain now
Browse files Browse the repository at this point in the history
  • Loading branch information
JGiter committed Jul 17, 2021
1 parent 86b82d1 commit 768dc5d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
11 changes: 9 additions & 2 deletions src/staking/index.ts
Expand Up @@ -143,7 +143,7 @@ export class StakingPool {
throw new Error(ERROR_MESSAGES.STAKE_WAS_NOT_PUT);
}
const requestAvailableFrom = depositStart.add(await this.pool.minStakingPeriod());
const now = new BigNumber(new Date().getTime());
const now = await this.now();
if (requestAvailableFrom.lte(now)) {
return new BigNumber(0);
} else {
Expand Down Expand Up @@ -186,7 +186,7 @@ export class StakingPool {
throw new Error(ERROR_MESSAGES.WITHDRAWAL_WAS_NOT_REQUESTED);
}
const withdrawAvailableFrom = depositEnd.add(await this.pool.withdrawDelay());
const now = new BigNumber(new Date().getTime());
const now = await this.now();
if (withdrawAvailableFrom.lte(now)) {
return new BigNumber(0);
} else {
Expand All @@ -208,4 +208,11 @@ export class StakingPool {
connect(signer: Signer): StakingPool {
return new StakingPool(this.pool.connect(signer));
}

private async now(): Promise<utils.BigNumber> {
const lastBlock = await this.pool.provider.getBlockNumber();
return new BigNumber(
(await this.pool.provider.getBlock(lastBlock)).timestamp
);
}
}
17 changes: 11 additions & 6 deletions test/staking.ts
Expand Up @@ -320,9 +320,10 @@ export const stakingTests = (): void => {
.rejects.toThrow("StakingPool: Replenishment of the stake is not allowed");
});

it("staker should be able to request withdraw", async () => {
it("staker should be able to request withdraw", async () => {
await pool.putStake(parseEther("0.1"));
await new Promise((resolve) => setTimeout(resolve, 1000 * minStakingPeriod));
const requestDelay = await pool.requestWithdrawDelay();
await new Promise((resolve) => setTimeout(resolve, 1000 * requestDelay.toNumber()));

const withdrawRequested = waitFor(
poolContract.filters.StakeWithdrawalRequested(patron.address, null),
Expand All @@ -341,15 +342,17 @@ export const stakingTests = (): void => {

it("can't request withdraw until minimum staking period is last", async () => {
await pool.putStake(parseEther("0.1"));
await new Promise((resolve) => setTimeout(resolve, 1000 * (minStakingPeriod / 2)));
const requestDelay = await pool.requestWithdrawDelay();
await new Promise((resolve) => setTimeout(resolve, 1000 * (requestDelay.toNumber() / 2)));

return expect(pool.requestWithdraw())
.rejects.toThrow("StakingPool: Minimum staking period is not expired yet");
});

it("withdraw can't be rquested twice", async () => {
await pool.putStake(parseEther("0.1"));
await new Promise((resolve) => setTimeout(resolve, 1000 * minStakingPeriod));
const requestDelay = await pool.requestWithdrawDelay();
await new Promise((resolve) => setTimeout(resolve, 1000 * requestDelay.toNumber()));

await pool.requestWithdraw();

Expand All @@ -364,7 +367,8 @@ export const stakingTests = (): void => {
);
await pool.putStake(stake);
const depositStart = (await stakePut)[2];
await new Promise((resolve) => setTimeout(resolve, 1000 * minStakingPeriod));
const requestDelay = await pool.requestWithdrawDelay();
await new Promise((resolve) => setTimeout(resolve, 1000 * requestDelay.toNumber()));

const stakeWithdrawalRequested = waitFor(
poolContract.filters.StakeWithdrawalRequested(patron.address, null), poolContract
Expand All @@ -375,7 +379,8 @@ export const stakingTests = (): void => {
const expectedReward = calculateReward(stake, (depositEnd.sub(depositStart)), new BigNumber(patronRewardPortion));
expect(await pool.checkReward()).toEqual(expectedReward);

await new Promise((resolve) => setTimeout(resolve, 1000 * withdrawDelay));
const withdrawalDelay = await pool.withdrawalDelay();
await new Promise((resolve) => setTimeout(resolve, 1000 * withdrawalDelay.toNumber()));

const stakeWithdrawn = waitFor(
poolContract.filters.StakeWithdrawn(patron.address, null),
Expand Down

0 comments on commit 768dc5d

Please sign in to comment.