Skip to content

Commit be234de

Browse files
authored
fix: getResourcesToSpend should default to 1 when amount < 1 (FuelLabs#1603)
1 parent 8229ea1 commit be234de

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

.changeset/long-ways-rule.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/providers": patch
3+
---
4+
5+
bugfix when amount of requested resources to spend is less than 1

packages/providers/src/coin-quantity.ts

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

27+
const bnAmount = bn(amount);
2728
return {
2829
assetId: hexlify(assetId),
29-
amount: bn(amount),
30+
amount: bnAmount.toNumber() < 1 ? bn(1) : bnAmount,
3031
max: max ? bn(max) : undefined,
3132
};
3233
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { coinQuantityfy } from './coin-quantity';
2+
3+
describe('coinQuantityfy', () => {
4+
it('should returns 1 when input is < 1', () => {
5+
expect(coinQuantityfy([0]).amount.toNumber()).toEqual(1);
6+
expect(coinQuantityfy([0.9]).amount.toNumber()).toEqual(1);
7+
expect(coinQuantityfy([2]).amount.toNumber()).toEqual(2);
8+
});
9+
});

packages/wallet/src/account.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ describe('Account', () => {
103103
// #endregion Message-getResourcesToSpend
104104
});
105105

106+
it('getResourcesToSpend should work with <1 amount', async () => {
107+
const account = new Account(
108+
'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db',
109+
provider
110+
);
111+
const resourcesToSpend = await account.getResourcesToSpend([
112+
{
113+
amount: 0.9,
114+
assetId: '0x0101010101010101010101010101010101010101010101010101010101010101',
115+
},
116+
]);
117+
expect(resourcesToSpend[0].amount.gte(1)).toBeTruthy();
118+
});
119+
106120
it('should get messages just fine', async () => {
107121
const account = new Account(
108122
'0x69a2b736b60159b43bb8a4f98c0589f6da5fa3a3d101e8e269c499eb942753ba',

0 commit comments

Comments
 (0)