From f97fedf24b1b606c6c6ee4d1b1bc5287b9ff2737 Mon Sep 17 00:00:00 2001 From: Dmitry Fesenko Date: Fri, 16 Jul 2021 14:03:43 +0300 Subject: [PATCH] fix(staking): withdrawalDelay --- docs/api/enums/error_messages.md | 7 +++++++ src/errors/ErrorMessages.ts | 3 ++- src/staking/index.ts | 10 ++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/docs/api/enums/error_messages.md b/docs/api/enums/error_messages.md index eb02ca66..7ffc2df5 100644 --- a/docs/api/enums/error_messages.md +++ b/docs/api/enums/error_messages.md @@ -31,6 +31,7 @@ * [ROLE\_NOT\_EXISTS](error_messages.md#role_not_exists) * [ROLE\_PRECONDITION\_NOT\_MET](error_messages.md#role_precondition_not_met) * [SIGNER\_NOT\_INITIALIZED](error_messages.md#signer_not_initialized) +* [STAKE\_WAS\_NOT\_PUT](error_messages.md#stake_was_not_put) * [UNABLE\_TO\_OBTAIN\_PUBLIC\_KEY](error_messages.md#unable_to_obtain_public_key) * [USER\_NOT\_LOGGED\_IN](error_messages.md#user_not_logged_in) * [WALLET\_PROVIDER\_NOT\_SUPPORTED](error_messages.md#wallet_provider_not_supported) @@ -189,6 +190,12 @@ ___ ___ +### STAKE\_WAS\_NOT\_PUT + +• **STAKE\_WAS\_NOT\_PUT**: = "Stake was not put" + +___ + ### UNABLE\_TO\_OBTAIN\_PUBLIC\_KEY • **UNABLE\_TO\_OBTAIN\_PUBLIC\_KEY**: = "Enable to obtain public key" diff --git a/src/errors/ErrorMessages.ts b/src/errors/ErrorMessages.ts index 45dd36b3..4877303b 100644 --- a/src/errors/ErrorMessages.ts +++ b/src/errors/ErrorMessages.ts @@ -28,5 +28,6 @@ export enum ERROR_MESSAGES { CLAIM_PUBLISHER_NOT_REQUESTER = "Claim subject is not controlled by publisher", ONCHAIN_ROLE_VERSION_NOT_SPECIFIED = "On-chain role version not specified", CACHE_SERVER_NOT_REGISTERED = "Cache server for this chain is not registered", - WITHDRAWAL_WAS_NOT_REQUESTED = "Stake withdrawal was not requested" + WITHDRAWAL_WAS_NOT_REQUESTED = "Stake withdrawal was not requested", + STAKE_WAS_NOT_PUT = "Stake was not put" } diff --git a/src/staking/index.ts b/src/staking/index.ts index d6fc1e6a..7099896d 100644 --- a/src/staking/index.ts +++ b/src/staking/index.ts @@ -1,4 +1,4 @@ -import { DomainReader, ResolverContractType, StakingPoolFactory__factory, StakingPool__factory, } from "@energyweb/iam-contracts"; +import { DomainReader, ResolverContractType, StakingPoolFactory__factory, StakingPool__factory, WITHDRAW_DELAY, } from "@energyweb/iam-contracts"; import { StakingPool as StakingPoolContract } from "@energyweb/iam-contracts/dist/ethers-v4/StakingPool"; import { StakingPoolFactory } from "@energyweb/iam-contracts/dist/ethers-v4/StakingPoolFactory"; import { Signer, utils } from "ethers"; @@ -168,7 +168,13 @@ export class StakingPool { if (status !== StakeStatus.WITHDRAWING) { throw new Error(ERROR_MESSAGES.WITHDRAWAL_WAS_NOT_REQUESTED); } - return new BigNumber(new Date().getTime()).sub(depositEnd); + const withdrawAvailableFrom = depositEnd.add(WITHDRAW_DELAY); + const now = new BigNumber(new Date().getTime()); + if (withdrawAvailableFrom.lte(now)) { + return new BigNumber(0); + } else { + return withdrawAvailableFrom.sub(now); + } } /**