Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes error messages for required amount parameters #679

Merged
merged 3 commits into from
Aug 21, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 1 addition & 3 deletions src/operations/create_account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/operations/liquidity_pool_deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/operations/liquidity_pool_withdraw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
20 changes: 9 additions & 11 deletions test/unit/operation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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/
);
});

Expand Down Expand Up @@ -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';
Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down
Loading