Skip to content

Commit

Permalink
fix: don't compare bn with integer (#1704)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk authored Feb 5, 2024
1 parent 70804df commit a2e0ff6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-countries-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/providers": patch
---

stop comparing bn with integer in coinQuantityfy helper
2 changes: 1 addition & 1 deletion packages/providers/src/coin-quantity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const coinQuantityfy = (coinQuantityLike: CoinQuantityLike): CoinQuantity
const bnAmount = bn(amount);
return {
assetId: hexlify(assetId),
amount: bnAmount.toNumber() < 1 ? bn(1) : bnAmount,
amount: bnAmount.lt(1) ? bn(1) : bnAmount,
max: max ? bn(max) : undefined,
};
};
Expand Down
11 changes: 9 additions & 2 deletions packages/providers/src/coin-quantityfy.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { BN } from '@fuel-ts/math';

import { coinQuantityfy } from './coin-quantity';

/**
* @group node
*/
describe('coinQuantityfy', () => {
it('should returns 1 when input is < 1', () => {
it('amount that is less than 1 is rounded up to 1', () => {
expect(coinQuantityfy([Number.MIN_VALUE]).amount.toNumber()).toEqual(1);
expect(coinQuantityfy([0]).amount.toNumber()).toEqual(1);
expect(coinQuantityfy([0.9]).amount.toNumber()).toEqual(1);
expect(coinQuantityfy([1 - Number.EPSILON]).amount.toNumber()).toEqual(1);
});
test('amount of return value is set properly', () => {
expect(coinQuantityfy([2]).amount.toNumber()).toEqual(2);
const maxPlusOne = new BN(Number.MAX_SAFE_INTEGER).add(new BN(1));
expect(coinQuantityfy([maxPlusOne]).amount.toString()).toEqual(maxPlusOne.toString());
});
});

0 comments on commit a2e0ff6

Please sign in to comment.