From 768dc5d7757beb921c462fad21c7d07e9050d1c9 Mon Sep 17 00:00:00 2001 From: Dmitry Fesenko Date: Sat, 17 Jul 2021 09:42:15 +0300 Subject: [PATCH] fix(staking): blockchain now --- src/staking/index.ts | 11 +++++++++-- test/staking.ts | 17 +++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/staking/index.ts b/src/staking/index.ts index e26df04f..9ded2c2f 100644 --- a/src/staking/index.ts +++ b/src/staking/index.ts @@ -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 { @@ -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 { @@ -208,4 +208,11 @@ export class StakingPool { connect(signer: Signer): StakingPool { return new StakingPool(this.pool.connect(signer)); } + + private async now(): Promise { + const lastBlock = await this.pool.provider.getBlockNumber(); + return new BigNumber( + (await this.pool.provider.getBlock(lastBlock)).timestamp + ); + } } diff --git a/test/staking.ts b/test/staking.ts index 2bb33e97..95cb897c 100644 --- a/test/staking.ts +++ b/test/staking.ts @@ -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), @@ -341,7 +342,8 @@ 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"); @@ -349,7 +351,8 @@ export const stakingTests = (): void => { 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(); @@ -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 @@ -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),