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
43 changes: 38 additions & 5 deletions modules/sdk-coin-rune/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,49 @@ export class RuneUtils extends CosmosUtils {
/** @inheritdoc */
validateAmount(amount: Coin): void {
const amountBig = BigNumber(amount.amount);
if (amountBig.isLessThanOrEqualTo(0)) {
if (amountBig.isNaN() || amountBig.isLessThanOrEqualTo(0)) {
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid amount: ' + amount.amount);
}
this.validateDenomination(amount.denom);
}

/**
* Validates the gas limit and gas amount for a transaction.
* @param {FeeData} gasBudget - The gas budget to validate.
* @throws {InvalidTransactionError} Throws an error if the gas budget is invalid.
*/
validateGasBudget(gasBudget: FeeData): void {
if (gasBudget.gasLimit <= 0) {
throw new InvalidTransactionError('Invalid gas limit ' + gasBudget.gasLimit);
}
this.validateGasAmountData(gasBudget.amount);
}

/**
* Validates an array of coin amounts.
* @param {Coin[]} amountArray - The array of coin amounts to validate.
*/
validateGasAmountData(amountArray: Coin[]): void {
amountArray.forEach((coinAmount) => {
this.validateGasAmount(coinAmount);
});
}

validateGasAmount(amount: Coin): void {
const amountBig = BigNumber(amount.amount);
if (amountBig.isNaN() || amountBig.isLessThan(0)) {
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid amount: ' + amount.amount);
}
this.validateDenomination(amount.denom);
}

validateDenomination(amountDenom: string): void {
if (
(this.networkType === NetworkType.TESTNET &&
!constants.testnetValidDenoms.find((denom) => denom === amount.denom)) ||
(this.networkType === NetworkType.MAINNET &&
!constants.mainnetValidDenoms.find((denom) => denom === amount.denom))
!constants.testnetValidDenoms.find((denom) => denom === amountDenom)) ||
(this.networkType === NetworkType.MAINNET && !constants.mainnetValidDenoms.find((denom) => denom === amountDenom))
) {
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid denom: ' + amount.denom);
throw new InvalidTransactionError('transactionBuilder: validateAmount: Invalid denom: ' + amountDenom);
}
}

Expand Down
7 changes: 7 additions & 0 deletions modules/sdk-coin-rune/test/resources/rune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ export const mainnetCoinAmounts = {
amount4: { amount: '-1', denom: 'rune' },
amount5: { amount: '1000000000', denom: 'arune' },
};

export const mainnetGasAmounts = {
positiveGasAmount: { amount: '100', denom: 'rune' },
zeroGasAmount: { amount: '0', denom: 'rune' },
emptyGasAmount: { amount: '', denom: 'rune' },
alphabeticGasAmount: { amount: 'xyz', denom: 'rune' },
};
20 changes: 19 additions & 1 deletion modules/sdk-coin-rune/test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NetworkType } from '@bitgo/statics';
import should from 'should';
import { RuneUtils } from '../../src/lib/utils';
import { MAINNET_ADDRESS_PREFIX, TESTNET_ADDRESS_PREFIX } from '../../src/lib/constants';
import { blockHash, mainnetCoinAmounts, txIds, mainnetAddress } from '../resources/rune';
import { blockHash, mainnetCoinAmounts, txIds, mainnetAddress, mainnetGasAmounts } from '../resources/rune';
import { testnetCoinAmounts, testnetAddress } from '../resources/trune';
const bech32 = require('bech32-buffer');

Expand Down Expand Up @@ -63,6 +63,24 @@ describe('utils', () => {
);
});

it('validate gas amount', function () {
const gasBudget = { amount: [mainnetGasAmounts.positiveGasAmount], gasLimit: 1 };
should.doesNotThrow(() => mainnetUtils.validateGasBudget(gasBudget));

gasBudget.amount[0] = mainnetGasAmounts.zeroGasAmount;
should.doesNotThrow(() => mainnetUtils.validateGasBudget(gasBudget));

gasBudget.amount[0] = mainnetGasAmounts.emptyGasAmount;
should(() => mainnetUtils.validateGasBudget(gasBudget)).throwError(
'transactionBuilder: validateAmount: Invalid amount: '
);

gasBudget.amount[0] = mainnetGasAmounts.alphabeticGasAmount;
should(() => mainnetUtils.validateGasBudget(gasBudget)).throwError(
'transactionBuilder: validateAmount: Invalid amount: xyz'
);
});

it('should validate mainnet address', () => {
should.equal(mainnetUtils.isValidAddress(mainnetAddress.address1), true);
should.equal(mainnetUtils.isValidAddress(mainnetAddress.validMemoIdAddress), true);
Expand Down
Loading