Skip to content

Commit 3af8692

Browse files
authored
fix: ensure is possible to forward 0 amount to contract (#3776)
1 parent 1b98978 commit 3af8692

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

.changeset/cold-ears-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/account": patch
3+
---
4+
5+
fix: ensure is possible to forward 0 amount to contract

packages/account/src/providers/coin-quantity.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ export const coinQuantityfy = (coinQuantityLike: CoinQuantityLike): CoinQuantity
2323
max = coinQuantityLike.max ?? undefined;
2424
}
2525

26-
const bnAmount = bn(amount);
2726
return {
2827
assetId: hexlify(assetId),
29-
amount: bnAmount.lt(1) ? bn(1) : bnAmount,
28+
amount: bn(amount),
3029
max: max ? bn(max) : undefined,
3130
};
3231
};

packages/account/src/providers/coin-quantityfy.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import { coinQuantityfy } from './coin-quantity';
99
describe('coinQuantityfy', () => {
1010
const baseAssetId = ZeroBytes32;
1111

12-
it('amount that is less than 1 is rounded up to 1', () => {
13-
expect(coinQuantityfy([Number.MIN_VALUE, baseAssetId]).amount.toNumber()).toEqual(1);
14-
expect(coinQuantityfy([0, baseAssetId]).amount.toNumber()).toEqual(1);
15-
expect(coinQuantityfy([1 - Number.EPSILON, baseAssetId]).amount.toNumber()).toEqual(1);
12+
it('amount that is less than 1 is rounded to 0', () => {
13+
expect(coinQuantityfy([Number.MIN_VALUE, baseAssetId]).amount.toNumber()).toEqual(0);
14+
expect(coinQuantityfy([0.2, baseAssetId]).amount.toNumber()).toEqual(0);
15+
expect(coinQuantityfy([1 - Number.EPSILON, baseAssetId]).amount.toNumber()).toEqual(0);
1616
});
17-
test('amount of return value is set properly', () => {
17+
18+
it('amount of return value is set properly', () => {
1819
expect(coinQuantityfy([2, baseAssetId]).amount.toNumber()).toEqual(2);
1920
const maxPlusOne = new BN(Number.MAX_SAFE_INTEGER).add(new BN(1));
2021
expect(coinQuantityfy([maxPlusOne, baseAssetId]).amount.toString()).toEqual(

packages/account/src/providers/provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ export default class Provider {
15541554
.map(coinQuantityfy)
15551555
.map(({ assetId, amount, max: maxPerAsset }) => ({
15561556
assetId: hexlify(assetId),
1557-
amount: amount.toString(10),
1557+
amount: (amount.eqn(0) ? bn(1) : amount).toString(10),
15581558
max: maxPerAsset ? maxPerAsset.toString(10) : undefined,
15591559
})),
15601560
excludedIds: idsToExclude,

packages/fuel-gauge/src/call-test-contract.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ describe('CallTestContract', () => {
165165
expect(value.toHex()).toBe(bn(1_000_000).toHex());
166166
});
167167

168+
it('should be possible to forward amount 0 to contract call', async () => {
169+
using contract = await setupContract();
170+
const baseAssetId = await contract.provider.getBaseAssetId();
171+
const { waitForResult } = await contract.functions
172+
.return_context_amount()
173+
.callParams({
174+
forward: [0, baseAssetId],
175+
})
176+
.call();
177+
178+
const { value } = await waitForResult();
179+
180+
expect(value.toNumber()).toBe(0);
181+
});
182+
168183
it('Forward asset_id on contract call', async () => {
169184
using contract = await setupContract();
170185

0 commit comments

Comments
 (0)