Skip to content

fix(core): track the minting token by reference in TokensCollection - #228

Open
Kshot3000 wants to merge 1 commit into
fleet-sdk:masterfrom
Kshot3000:fix/tokens-minting-tracking
Open

Kshot3000 wants to merge 1 commit into
fleet-sdk:masterfrom
Kshot3000:fix/tokens-minting-tracking

Conversation

@Kshot3000

Copy link
Copy Markdown

Summary

TokensCollection tracked the minting token by its array index (#minting = { index, metadata }), but nothing updated that index when the collection mutated. Any insertion or removal before the minting token detached the minting metadata from the token it belongs to:

  • Wrong amount (silent): add token A, mint({ amount: 100n }), add token B, then remove A — collection.minting reported B's amount (e.g. 10n) instead of 100n. Inserting a token at an index before the minting token corrupted it the same way.
  • Phantom minting: mint, add token A, then remove the minting token itself — collection.minting still returned the minting metadata, now paired with token A's amount. Downstream, OutputBuilder.build() treats such an output as a minting output (writing EIP-4 registers from the phantom metadata) and toArray() kept throwing UndefinedMintingContext for a collection with no minting token; a second mint() also kept throwing "Only one minting token is allowed" even though the minting token was gone.
  • Crash: when the stale index pointed past the end of the array (minting token last, a preceding token removed), reading collection.minting threw TypeError: Cannot read properties of undefined (reading 'amount').

Fix

Track the minting item by reference instead of by index:

  • #minting now holds the stored item object; the minting getter returns undefined when that item is no longer in the collection, and otherwise reads the amount from the item itself — so partial removals of the minting token are reflected, and insertions/removals of other tokens cannot affect it.
  • mint() resolves the stored item after adding: the newly appended item normally, or the existing item when the amount was summed into one with the same token id. The "only one minting token" guard now goes through the live getter, so minting again works after the minting token has been removed.

No API changes; behavior for collections that never remove/insert around a mint is unchanged.

Tests

Tests-first: the 4 new tokensCollection.spec.ts cases all fail on current master (wrong amount, phantom minting metadata, and the TypeError above) and pass with this fix:

  • minting amount survives removal of a preceding token
  • minting amount survives insertion before the minting token
  • removing the minting token clears minting, toArray() no longer throws, and a new token can be minted
  • reading minting after removing the last preceding token does not throw

Full monorepo suite: 1136/1136 tests pass (66 files; master baseline is 1132, +4 new). Biome clean on the changed files.

Includes a changeset (@fleet-sdk/core: patch).


Built by @Kshot9000 — https://x.com/kshot9000
Tips (ERG): 9fcM5RWnAjmP4vx5bnW6yohB6H9bLq8sJbaPLHtwZLtQPB32Pvy

@changeset-bot

changeset-bot Bot commented Oct 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 03cbebe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@fleet-sdk/core Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant