@fivebinaries/coin-selection

中文文档
3.0.0发布于 2024/12/09Apache-2.0TypeScriptESM + CJS近 30 天下载 97.4万

Minimal implementation of Cardano coin selection algorithms (see CIP-2) developed solely for Trezor Suite. Under the hood it leverages Cardano Serialization Lib via WASM module.

原始使用文档
文档目录7 节
  1. Features
  2. Usage
  3. coinSelection(txParams, options)
  4. Example
  5. Final tx plan
  6. Draft tx plan
  7. Notes

coin-selection

Minimal implementation of Cardano coin selection algorithms (see CIP-2) developed solely for Trezor Suite. Under the hood it leverages Cardano Serialization Lib via WASM module.

Features

  • Final tx plan: Compose transaction plan for given inputs (shelley utxos only), certificates (support for stake (de)registration and delegation), withdrawals,...
  • Draft tx plan: Return basic information about potential transaction (such as a size and fee) even for incomplete inputs (without an recipient address or an amount)
  • Set max: Calculate the max amount of an asset that is possible to include in a transaction output

Usage

const txPlan = coinSelection(
    txParams: {
        utxos: Utxo[];
        outputs: UserOutput[];
        changeAddress: string;
        certificates: Certificate[];
        withdrawals: Withdrawal[];
        accountPubKey: string;
        ttl?: number;
    },
    options: {
        feeParams?: { a: string };
        debug?: boolean;
        forceLargestFirstSelection?: boolean;
    }
);

coinSelection(txParams, options)

txParams

  • utxos: Array of account's utxo
  • outputs: Requested outputs provided by an user
  • changeAddress: An address where the change will be sent
  • certificates: Stake registration and/or delegation certificates (stake pool registration is not supported)
  • withdrawals: Withdrawal requests
  • accountPubKey: Account public key
  • ttl: Time-to-live for the transaction

Options

  • forceLargestFirstSelection: Always use largest-first algorithm
  • debug: print debug information about coin-selection (selected utxos, outputs including change output,...)

Example

Final tx plan


export const utxo1 = {
  address:
    'addr1q860vxljhadqxnrrsr2j6yxnwpdkyquq74lmghx502aj0r28d2kd47hsre5v9urjyu8s0ryk38dxzw0t5jesncw4v90sp0878u',
  txHash: '3c388acb799a37a4f1cc99bec7626637b0b80626b9ef7c7a687282cab701178d',
  outputIndex: 0,
  amount: [
    {
      quantity: '5000000',
      unit: 'lovelace',
    },
  ],
};

export const utxo2 = {
  address:
    'addr1q860vxljhadqxnrrsr2j6yxnwpdkyquq74lmghx502aj0r28d2kd47hsre5v9urjyu8s0ryk38dxzw0t5jesncw4v90sp0878u',
  txHash: '9e63fddf20cb7b5472e2c9a1bb4bbe3112b8f2b22e45bc441206bcddde5c58a0',
  outputIndex: 1,
  amount: [
    {
      quantity: '5000000',
      unit: 'lovelace',
    },
    {
      quantity: '1000',
      unit: '02477d7c23b4c2834b0be8ca8578dde47af0cc82a964688f6fc95a7a47524943',
    },
  ],
};

const txPlan = coinSelection(
    txParams: {
        utxos: Utxo[];
        outputs: [
        {
            address:
            'addr1qya0nkzrf04gmcpu66vdt7sudwptnyg5df6475y7jhtt2wc44vzmgrfy6wwf69xlaszdslksw8evveyykw4c82eavq7sx29tlc',
            amount: '1000000',
            assets: [],
            setMax: false,
        },
        {
            address:
            'addr1qya0nkzrf04gmcpu66vdt7sudwptnyg5df6475y7jhtt2wc44vzmgrfy6wwf69xlaszdslksw8evveyykw4c82eavq7sx29tlc',
            amount: undefined,
            assets: [],
            setMax: true,
        },
        ],
        changeAddress: `addr1q8u2f05rprqjhygz22m06mhy4xrnqvqqpyuzhmxqfxnwvxz8d2kd47hsre5v9urjyu8s0ryk38dxzw0t5jesncw4v90s22tk0f`,
        certificates: [],
        withdrawals: [],
        accountPubKey:
        'ec8fdf616242f430855ad7477acda53395eb30c295f5a7ef038712578877375b5a2f00353c9c5cc88c7ff18e71dc08724d90fc238213b789c0b02438e336be07',
    },
);

// txPlan
// {
//   max: '7480901',
//   totalSpent: '8655202',
//   fee: '174301',
//   inputs: [utxo1, utxo2],
//   outputs: [
//     {
//       address:
//         'addr1qya0nkzrf04gmcpu66vdt7sudwptnyg5df6475y7jhtt2wc44vzmgrfy6wwf69xlaszdslksw8evveyykw4c82eavq7sx29tlc',
//       amount: '1000000',
//       assets: [],
//       setMax: false,
//     },
//     {
//       address:
//         'addr1qya0nkzrf04gmcpu66vdt7sudwptnyg5df6475y7jhtt2wc44vzmgrfy6wwf69xlaszdslksw8evveyykw4c82eavq7sx29tlc',
//       amount: '7480901',
//       assets: [],
//       setMax: true,
//     },
//     {
//       isChange: true,
//       address: changeAddress,
//       amount: '1344798',
//       assets: [
//         {
//           quantity: '1000',
//           unit: '02477d7c23b4c2834b0be8ca8578dde47af0cc82a964688f6fc95a7a47524943',
//         },
//       ],
//     },
//   ],
// },
;

Draft tx plan


const txPlan = coinSelection(
    {
        utxos: [utxo1];
        outputs: [
            {
                address: undefined, // address not filled
                amount: '2000000',
                assets: [],
                setMax: false,
            },
        ],
        changeAddress: `addr1q8u2f05rprqjhygz22m06mhy4xrnqvqqpyuzhmxqfxnwvxz8d2kd47hsre5v9urjyu8s0ryk38dxzw0t5jesncw4v90s22tk0f`,
        certificates: [],
        withdrawals: [],
        accountPubKey:
        'ec8fdf616242f430855ad7477acda53395eb30c295f5a7ef038712578877375b5a2f00353c9c5cc88c7ff18e71dc08724d90fc238213b789c0b02438e336be07',
    },
);

// txPlan:
// {
//     totalSpent: '2168053',
//     fee: '168053',
// },

Notes

  • If there are certificates present or the transaction includes an output with setMax: true then largest-first algorithm will be used instead of random-improve.

版本与发布记录

@fivebinaries/coin-selection 共发布 26 个版本,首次发布于 2021/08/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0latest2024/12/091.9 年前
3.0.0-beta.0beta2024/11/252 年前
2.3.0-beta.32024/11/252 年前
2.3.0-beta.22024/10/212 年前
2.3.0-beta.12024/10/182 年前
2.3.0-beta.02024/10/172 年前

关于 @fivebinaries/coin-selection 的常见问题

@fivebinaries/coin-selection 是什么?

Minimal implementation of Cardano coin selection algorithms (see CIP-2) developed solely for Trezor Suite. Under the hood it leverages Cardano Serialization Lib via WASM module.

如何安装 @fivebinaries/coin-selection?

在项目目录运行 npm install @fivebinaries/coin-selection,或使用 pnpm add / yarn add / bun add。

@fivebinaries/coin-selection 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fivebinaries/coin-selection 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fivebinaries/coin-selection 可以商用吗?

@fivebinaries/coin-selection 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@fivebinaries/coin-selection 的下载量有多少?

根据 npm 官方下载统计,@fivebinaries/coin-selection 在 2026/10/15 – 2026/11/13 这 30 天内被下载 974,314 次,日均约 32,477 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fivebinaries/coin-selection 还在维护吗?

最新版本 3.0.0 发布于 2024/12/09(1.9 年前);至今共发布 26 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/08/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。