Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions modules/sdk-coin-avaxp/src/lib/delegatorTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ export class DelegatorTxBuilder extends TransactionBuilder {
}

/**
*
* @param startTime
* start time of staking period
* @param value
*/
startTime(value: string | number): this {
this._startTime = new BN(value);
return this;
}

/**
*
* @param endTime
* end time of staking period
* @param value
*/
endTime(value: string | number): this {
this._endTime = new BN(value);
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions modules/sdk-coin-avaxp/test/resources/avaxp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 2 additions & 0 deletions modules/sdk-coin-avaxp/test/resources/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
13 changes: 11 additions & 2 deletions modules/sdk-coin-avaxp/test/unit/lib/validateTxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -30,19 +31,27 @@ 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
);
});
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(
() => {
Expand Down