Skip to content

Commit d3911f2

Browse files
authored
Merge pull request #195 from fleet-sdk/arobsn/i194
Filter out tokens with zero amounts on output build
2 parents 97f4935 + 799fff2 commit d3911f2

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

.changeset/ten-dryers-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fleet-sdk/core": patch
3+
---
4+
5+
Filter out tokens with zero amounts on output build

packages/common/src/models/collection.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export abstract class Collection<InternalType, ExternalType> implements Iterable
9292
if (isDefined(options) && isDefined(options.index)) {
9393
if (options.index === this.length) {
9494
this._items.push(this._map(item));
95-
9695
return this.length;
9796
}
9897

@@ -101,12 +100,10 @@ export abstract class Collection<InternalType, ExternalType> implements Iterable
101100
}
102101

103102
this._items.splice(options.index, 0, this._map(item));
104-
105103
return this.length;
106104
}
107105

108106
this._items.push(this._map(item));
109-
110107
return this._items.length;
111108
}
112109

packages/core/src/builder/outputBuilder.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,20 @@ describe("Building", () => {
581581
expect(output.value).toBe(29000n);
582582
});
583583

584+
it("Should filter out token with zero amounts on build", () => {
585+
const output = new OutputBuilder(SAFE_MIN_BOX_VALUE, address, height)
586+
.addTokens({ tokenId: tokenA, amount: 50n })
587+
.addTokens({ tokenId: tokenB, amount: 0n });
588+
expect(output.assets).toHaveLength(2);
589+
expect(output.assets.toArray().find((x) => x.tokenId === tokenA)?.amount).toEqual(50n);
590+
expect(output.assets.toArray().find((x) => x.tokenId === tokenB)?.amount).toEqual(0n);
591+
592+
const candidate = output.build();
593+
594+
expect(candidate.assets).toHaveLength(1);
595+
expect(candidate.assets).to.deep.equal([{ tokenId: tokenA, amount: 50n }]);
596+
});
597+
584598
it("Should replace dynamic value estimation by fixed value", () => {
585599
const output = new OutputBuilder(estimateMinBoxValue(), address, height);
586600
expect(output.value).toBe(28440n);

packages/core/src/builder/outputBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class OutputBuilder {
198198
});
199199
}
200200
} else {
201-
tokens = this.assets.toArray();
201+
tokens = this.assets.toArray().filter((x) => x.amount > _0n);
202202
}
203203

204204
if (isUndefined(this.creationHeight)) throw new UndefinedCreationHeight();

0 commit comments

Comments
 (0)