Skip to content

Conversation

@jinglescode
Copy link
Member

Summary

Adds CoverageFirstSelector - a new coin selection algorithm optimized for complex multi-asset transactions. This two-phase algorithm prioritizes asset type coverage before magnitude optimization, typically resulting in fewer UTxOs selected compared to largest-first when dealing with multi-asset transactions.

Algorithm Overview

Phase 1 (Coverage): Selects UTxOs that maximize asset type coverage reduction using the rating formula:

rating = typesReduced + (intersection / 10)

Exits when ≤1 asset type remains in the deficit.

Phase 2 (Magnitude): Selects UTxOs by magnitude - picking the UTxO with the maximum quantity of each remaining target asset until the deficit is satisfied.

New Exports

import { CoverageFirstSelector, CoinSelectionError } from "@meshsdk/transaction";

const txBuilder = new MeshTxBuilder({
  fetcher: myFetcher,
  selector: new CoverageFirstSelector(),
});

Files Changed

File Change
coverage-first-selector.ts New 661-line implementation
coverage-first.test.ts New 22 comprehensive test cases
coin-selection/index.ts Added exports
src/index.ts Added package-level exports

Test Results

Test Suites: 4 passed, 4 total
Tests:       57 passed, 57 total

├── coverage-first.test.ts     22 tests ✓
├── largest-first.test.ts      13 tests ✓  (unchanged)
├── random-improve.test.ts     ✓  (unchanged)
└── cardano-sdk-adapter.test.ts ✓  (unchanged)

Test Categories

  • A. Basic Selection (4 tests): Simple transfers, minting, multiple outputs
  • B. Coverage Phase (5 tests): Multi-asset preference, rating formula, phase exit conditions
  • C. Magnitude Phase (3 tests): Max selection, asset ordering, phase transitions
  • D. Error Handling (4 tests): Phase-aware errors with deficit tracking
  • E. Edge Cases (6 tests): Min ADA, consolidation, splitting, 10K UTxO performance

Test plan

  • All 22 new CoverageFirstSelector tests pass
  • All 13 existing LargestFirst tests still pass
  • TypeScript compilation succeeds
  • 10,000 UTxO performance test completes successfully
  • Manual testing with real wallet

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.

2 participants