CCC - CKBers' Codebase is a one-stop solution for your CKB JS/TS ecosystem development.
Empower yourself with CCC to discover the unlimited potential of CKB.
Interoperate with wallets from different chain ecosystems.
Fully enabling CKB's Turing completeness and cryptographic freedom power.
Coin from @ckb-ccc/coin is a generic helper for on-chain fungible tokens identified by a CKB type script.
import { Coin } from "@ckb-ccc/coin";
import { ccc } from "@ckb-ccc/core";
const coin = new Coin({
script: {
codeHash: "0x...",
hashType: "type",
args: "0x...",
},
client,
cellDeps: [{ outPoint: codeOutPoint, depType: "code" }],
});
// Total balance across all cells of the signer
const balance = await coin.calculateBalance(signer);
console.log(`Balance: ${balance}`);
// Full info: balance + CKB capacity + cell count
const info = await coin.calculateInfo(signer);
console.log(`Balance: ${info.balance}, Cells: ${info.count}`);
Build the transaction manually, then use completeBy to add Coin inputs and a change output:
const { script: to } = await signer.getRecommendedAddressObj();
// Build outputs
const tx = ccc.Transaction.from({
outputs: [{ lock: to, type: coin.script }],
outputsData: [ccc.numLeToBytes(1000n, 16)],
});
tx.addCellDeps(coin.cellDeps);
// Add Coin inputs + change (change goes back to signer)
const { tx: completedTx } = await coin.completeBy(tx, signer);
// Cover CKB capacity and fee
await completedTx.completeInputsByCapacity(signer);
await completedTx.completeFeeBy(signer);
const txHash = await signer.sendTransaction(completedTx);
const { script: changeLock } = await signer.getRecommendedAddressObj();
const { tx: completedTx } = await coin.completeChangeToLock(tx, signer, changeLock);
Check the package documentation and API reference for more details.