diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cda7462..2098d37a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## Unreleased +### Fixes + +* Improves the error messages when passing invalid amounts to deposit and withdraw operations ([#679](https://github.com/stellar/js-stellar-base/pull/679)). + ## [v9.0.0](https://github.com/stellar/js-stellar-base/compare/v8.2.2..v9.0.0) diff --git a/src/operations/create_account.js b/src/operations/create_account.js index 1cb28ddd..d8924424 100644 --- a/src/operations/create_account.js +++ b/src/operations/create_account.js @@ -18,9 +18,7 @@ export function createAccount(opts) { throw new Error('destination is invalid'); } if (!this.isValidAmount(opts.startingBalance, true)) { - throw new TypeError( - 'startingBalance must be of type String, represent a non-negative number and have at most 7 digits after the decimal' - ); + throw new TypeError(this.constructAmountRequirementsError('startingBalance')) } const attributes = {}; attributes.destination = Keypair.fromPublicKey( diff --git a/src/operations/liquidity_pool_deposit.js b/src/operations/liquidity_pool_deposit.js index 6bdbce90..9dec41a6 100644 --- a/src/operations/liquidity_pool_deposit.js +++ b/src/operations/liquidity_pool_deposit.js @@ -31,12 +31,12 @@ export function liquidityPoolDeposit(opts = {}) { attributes.liquidityPoolId = xdr.PoolId.fromXDR(liquidityPoolId, 'hex'); if (!this.isValidAmount(maxAmountA, true)) { - throw new TypeError('maxAmountA argument is required'); + throw new TypeError(this.constructAmountRequirementsError('maxAmountA')) } attributes.maxAmountA = this._toXDRAmount(maxAmountA); if (!this.isValidAmount(maxAmountB, true)) { - throw new TypeError('maxAmountB argument is required'); + throw new TypeError(this.constructAmountRequirementsError('maxAmountB')) } attributes.maxAmountB = this._toXDRAmount(maxAmountB); diff --git a/src/operations/liquidity_pool_withdraw.js b/src/operations/liquidity_pool_withdraw.js index 0696bf04..7f3ebab1 100644 --- a/src/operations/liquidity_pool_withdraw.js +++ b/src/operations/liquidity_pool_withdraw.js @@ -24,17 +24,17 @@ export function liquidityPoolWithdraw(opts = {}) { attributes.liquidityPoolId = xdr.PoolId.fromXDR(opts.liquidityPoolId, 'hex'); if (!this.isValidAmount(opts.amount)) { - throw new TypeError('amount argument is required'); + throw new TypeError(this.constructAmountRequirementsError('amount')) } attributes.amount = this._toXDRAmount(opts.amount); if (!this.isValidAmount(opts.minAmountA, true)) { - throw new TypeError('minAmountA argument is required'); + throw new TypeError(this.constructAmountRequirementsError('minAmountA')) } attributes.minAmountA = this._toXDRAmount(opts.minAmountA); if (!this.isValidAmount(opts.minAmountB, true)) { - throw new TypeError('minAmountB argument is required'); + throw new TypeError(this.constructAmountRequirementsError('minAmountB')) } attributes.minAmountB = this._toXDRAmount(opts.minAmountB); diff --git a/test/unit/operation_test.js b/test/unit/operation_test.js index db8e3028..d3d323d4 100644 --- a/test/unit/operation_test.js +++ b/test/unit/operation_test.js @@ -44,9 +44,7 @@ describe('Operation', function () { startingBalance: '0', source: 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' }; - expect(() => StellarBase.Operation.createAccount(opts)).not.to.throw( - /startingBalance must be of type String, represent a non-negative number and have at most 7 digits after the decimal/ - ); + expect(() => StellarBase.Operation.createAccount(opts)).not.to.throw(); }); it('fails to create createAccount operation with an invalid startingBalance', function () { @@ -56,7 +54,7 @@ describe('Operation', function () { source: 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' }; expect(() => StellarBase.Operation.createAccount(opts)).to.throw( - /startingBalance must be of type String, represent a non-negative number and have at most 7 digits after the decimal/ + /startingBalance argument must be of type String, represent a positive number and have at most 7 digits after the decimal/ ); }); @@ -2461,12 +2459,12 @@ describe('Operation', function () { opts.liquidityPoolId = 'dd7b1ab831c273310ddbec6f97870aa83c2fbd78ce22aded37ecbf4f3380fac7'; expect(() => StellarBase.Operation.liquidityPoolDeposit(opts)).to.throw( - /maxAmountA argument is required/ + /maxAmountA argument must be of type String, represent a positive number and have at most 7 digits after the decimal/ ); opts.maxAmountA = '10'; expect(() => StellarBase.Operation.liquidityPoolDeposit(opts)).to.throw( - /maxAmountB argument is required/ + /maxAmountB argument must be of type String, represent a positive number and have at most 7 digits after the decimal/ ); opts.maxAmountB = '20'; @@ -2481,7 +2479,7 @@ describe('Operation', function () { opts.maxPrice = '0.55'; expect(() => StellarBase.Operation.liquidityPoolDeposit(opts)).to.not - .throw; + .throw(); }); it('throws an error if prices are negative', function () { @@ -2643,22 +2641,22 @@ describe('Operation', function () { opts.liquidityPoolId = 'dd7b1ab831c273310ddbec6f97870aa83c2fbd78ce22aded37ecbf4f3380fac7'; expect(() => StellarBase.Operation.liquidityPoolWithdraw(opts)).to.throw( - /amount argument is required/ + /amount argument must be of type String, represent a positive number and have at most 7 digits after the decimal/ ); opts.amount = '10'; expect(() => StellarBase.Operation.liquidityPoolWithdraw(opts)).to.throw( - /minAmountA argument is required/ + /minAmountA argument must be of type String, represent a positive number and have at most 7 digits after the decimal/ ); opts.minAmountA = '10000'; expect(() => StellarBase.Operation.liquidityPoolWithdraw(opts)).to.throw( - /minAmountB argument is required/ + /minAmountB argument must be of type String, represent a positive number and have at most 7 digits after the decimal/ ); opts.minAmountB = '20000'; expect(() => StellarBase.Operation.liquidityPoolWithdraw(opts)).to.not - .throw; + .throw(); }); it('creates a liquidityPoolWithdraw', function () {