From 82ee4481094eaa60d5f3c97714eea10a6e69161b Mon Sep 17 00:00:00 2001 From: Jichao Li Date: Mon, 18 Jul 2022 17:52:14 -0400 Subject: [PATCH] feat(sdk-coin-avaxp): update stake validation check Ticket: STLX-18053 --- .../sdk-coin-avaxp/src/lib/delegatorTxBuilder.ts | 13 +++++++++---- modules/sdk-coin-avaxp/test/resources/avaxp.ts | 7 +++---- modules/sdk-coin-avaxp/test/resources/errors.ts | 2 ++ .../test/unit/lib/validateTxBuilder.ts | 13 +++++++++++-- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/modules/sdk-coin-avaxp/src/lib/delegatorTxBuilder.ts b/modules/sdk-coin-avaxp/src/lib/delegatorTxBuilder.ts index e4ab2e0021..9afb14d587 100644 --- a/modules/sdk-coin-avaxp/src/lib/delegatorTxBuilder.ts +++ b/modules/sdk-coin-avaxp/src/lib/delegatorTxBuilder.ts @@ -56,8 +56,8 @@ export class DelegatorTxBuilder extends TransactionBuilder { } /** - * - * @param startTime + * start time of staking period + * @param value */ startTime(value: string | number): this { this._startTime = new BN(value); @@ -65,8 +65,8 @@ export class DelegatorTxBuilder extends TransactionBuilder { } /** - * - * @param endTime + * end time of staking period + * @param value */ endTime(value: string | number): this { this._endTime = new BN(value); @@ -124,6 +124,10 @@ export class DelegatorTxBuilder extends TransactionBuilder { * unix time stamp based off seconds */ validateStakeDuration(startTime: BN, endTime: BN): void { + const oneDayLater = new BN(Date.now()).add(new BN(86400)); + if (!startTime.gt(oneDayLater)) { + throw new BuildTransactionError('Start time needs to be one day greater than current time'); + } if (endTime < startTime) { throw new BuildTransactionError('End date cannot be less than start date'); } @@ -174,6 +178,7 @@ export class DelegatorTxBuilder extends TransactionBuilder { * @protected */ protected buildAvaxpTransaction(): void { + this.validateStakeDuration(this._startTime, this._endTime); const { inputs, outputs, credentials } = this.createInputOutput(); this.transaction.setTransaction( new Tx( diff --git a/modules/sdk-coin-avaxp/test/resources/avaxp.ts b/modules/sdk-coin-avaxp/test/resources/avaxp.ts index 4ddf9e946c..c4379fec25 100644 --- a/modules/sdk-coin-avaxp/test/resources/avaxp.ts +++ b/modules/sdk-coin-avaxp/test/resources/avaxp.ts @@ -55,10 +55,9 @@ export const INVALID_STAKE_AMOUNT = new BN(1999); export const INVALID_DELEGATION_FEE = 0; -export const START_TIME = new BN(0); -export const INVALID_START_TIME = new BN(1000000); -export const END_TIME = new BN(1209600); -export const INVALID_END_TIME = new BN(31556927); +export const START_TIME = new BN(Date.now()).add(new BN(90000)); +export const ONE_WEEK = new BN(604800); +export const TWO_YEAR = new BN(63072000); export const INVALID_SHORT_KEYPAIR_KEY = '82A34E'; diff --git a/modules/sdk-coin-avaxp/test/resources/errors.ts b/modules/sdk-coin-avaxp/test/resources/errors.ts index c439078c0e..bde49507fc 100644 --- a/modules/sdk-coin-avaxp/test/resources/errors.ts +++ b/modules/sdk-coin-avaxp/test/resources/errors.ts @@ -6,6 +6,8 @@ export const ERROR_EMPTY_RAW_TRANSACTION = 'Raw transaction is empty'; export const ERROR_RAW_PARSING = 'Raw transaction is not hex string'; +export const ERROR_STAKE_START_TIME_TOO_SHORT = 'Start time needs to be one day greater than current time'; + export const ERROR_STAKE_DURATION_SHORT_TIME = 'End date must be greater than or equal to two weeks'; export const ERROR_STAKE_DURATION_LONG_TIME = 'End date must be less than or equal to one year'; diff --git a/modules/sdk-coin-avaxp/test/unit/lib/validateTxBuilder.ts b/modules/sdk-coin-avaxp/test/unit/lib/validateTxBuilder.ts index c669f231c7..1b70ec3b4c 100644 --- a/modules/sdk-coin-avaxp/test/unit/lib/validateTxBuilder.ts +++ b/modules/sdk-coin-avaxp/test/unit/lib/validateTxBuilder.ts @@ -5,6 +5,7 @@ import * as testData from '../../resources/avaxp'; import * as errorMessage from '../../resources/errors'; import { TransactionBuilderFactory, DecodedUtxoObj } from '../../../src/lib'; import { coins } from '@bitgo/statics'; +import { BN } from 'avalanche'; describe('AvaxP Validate Tx Builder', () => { const factory = new TransactionBuilderFactory(coins.get('avaxp')); @@ -30,7 +31,7 @@ describe('AvaxP Validate Tx Builder', () => { it('should fail endTime less than 2 weeks', () => { assert.throws( () => { - txBuilder.validateStakeDuration(testData.INVALID_START_TIME, testData.END_TIME); + txBuilder.validateStakeDuration(testData.START_TIME, testData.START_TIME.add(testData.ONE_WEEK)); }, (e) => e.message === errorMessage.ERROR_STAKE_DURATION_SHORT_TIME ); @@ -38,11 +39,19 @@ describe('AvaxP Validate Tx Builder', () => { it('should fail endTime greater than 1 year', () => { assert.throws( () => { - txBuilder.validateStakeDuration(testData.START_TIME, testData.INVALID_END_TIME); + txBuilder.validateStakeDuration(testData.START_TIME, testData.START_TIME.add(testData.TWO_YEAR)); }, (e) => e.message === errorMessage.ERROR_STAKE_DURATION_LONG_TIME ); }); + it('should fail startTime too soon', () => { + assert.throws( + () => { + txBuilder.validateStakeDuration(new BN(Date.now()), testData.ONE_WEEK); + }, + (e) => e.message === errorMessage.ERROR_STAKE_START_TIME_TOO_SHORT + ); + }); it('should fail stake amount less than 2000', () => { assert.throws( () => {