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
42 changes: 42 additions & 0 deletions modules/bitgo/test/v2/unit/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4893,4 +4893,46 @@ describe('V2 Wallet:', function () {
await wallet.approveErc20Token(walletPassphrase, tokenName).should.be.rejectedWith(sendError);
});
});

describe('Amount Validation for sendCoins', function () {
let wallet;
let params;

before(function () {
const basecoin = bitgo.coin('tbtc');
wallet = new Wallet(bitgo, basecoin, {
id: '5b34252f1bf349930e34020a',
coin: 'tbtc',
keys: ['5b3424f91bf349930e340175'],
});
params = {
address: '2N4Xz4itCdKKUREiytEJ1KGPoEnKHmJUIwq',
walletPassphrase: 'test123',
};
});

it('should reject decimal amounts in send coins', async function () {
params.amount = 150000000.55;

await wallet
.send(params)
.should.be.rejectedWith('invalid argument for amount - Integer greater than zero or numeric string expected');
});

it('should reject negative amount in send coins', async function () {
params.amount = '-12';

await wallet
.send(params)
.should.be.rejectedWith('invalid argument for amount - Integer greater than zero or numeric string expected');
});

it('should reject zero in send coins', async function () {
params.amount = '0';

await wallet
.send(params)
.should.be.rejectedWith('invalid argument for amount - Integer greater than zero or numeric string expected');
});
});
});
16 changes: 10 additions & 6 deletions modules/sdk-core/src/bitgo/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2408,13 +2408,17 @@ export class Wallet implements IWallet {
const coin = this.baseCoin;

const amount = new BigNumber(params.amount);
if (amount.isNegative()) {
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
}

if (!coin.valuelessTransferAllowed() && amount.isZero()) {
throw new Error('invalid argument for amount - positive number greater than zero or numeric string expected');
}
const isAmountNegative = amount.isNegative();
const isAmountZero = amount.isZero();
const isAmountDecimal = !amount.isInteger();

_.some([isAmountNegative, !coin.valuelessTransferAllowed() && isAmountZero, isAmountDecimal], (condition) => {
if (condition) {
throw new Error('invalid argument for amount - Integer greater than zero or numeric string expected');
}
});

const recipients: SendManyOptions['recipients'] = [
{
address: params.address,
Expand Down