帖子
分享您的知识。
0xduckmove2018
Jul 08, 2025
讨论
有没有办法使用sui sdk合并硬币并在同一笔交易中将其拆分?
我们可以使用 CoinWithBalance 意图 CoinWithBalance 意图处理所有操作,包括拆分和合并硬币. 这意味着你并不总是需要手动映射对象ID、合并然后拆分——CoinWithBalance会自动处理所有事情.
import { coinWithBalance } from "@mysten/sui/transactions";
const coin = coinWithBalance({
balance: 1000000000,
type: "0x9f992cc2430a1f442ca7a5ca7638169f5d5c00e0ebc3977a65e9ac6e497fe5ef::wal::WAL",
});
- Sui
2
2
分享
评论
答案
2MoonBags712
Jul 21 2025, 08:21使用 CoinWithBalance 意图 CoinWithBalance 意图处理所有操作,包括拆分和合并硬币. 这意味着你并不总是需要手动映射对象ID、合并然后拆分——CoinWithBalance会自动处理所有事情.
1
最佳答案
评论
24p30p2046
Jul 9 2025, 05:39是的,您可以使用 Sui SDK在同一笔交易中合并和拆分硬币,方法是在提交之前构建一个对TransactionBlock
这两个操作进行批处理的. 当你想整合代币然后提取特定数量的代币时,这很有用,所有这些都在一个原子操作中完成. Sui 允许您在单个交易区块中链接多个操作,只要您正确管理引用,它就可以无缝运行.
@mysten/sui.js
以下是使用 SDK 的TypeScript示例:
import { TransactionBlock } from '@mysten/sui.js/transactions';
import { SuiClient } from '@mysten/sui.js/client';
// Assume you already have your SuiClient and signer configured
const client = new SuiClient({ url: 'https://fullnode.mainnet.sui.io' });
const txb = new TransactionBlock();
// Your coin object IDs
const coin1 = '0xabc...';
const coin2 = '0xdef...';
// Step 1: Merge two coins
const merged = txb.mergeCoins(
txb.object(coin1), // target coin
[txb.object(coin2)] // coins to merge into target
);
// Step 2: Split the merged coin into a specific amount
const [splitCoin] = txb.splitCoins(merged, [txb.pure(1000000)]); // amount in MIST (1 SUI = 1e9 MIST)
// (Optional) You can now use `splitCoin` for a transfer or any other action
// Send the transaction
const result = await client.signAndExecuteTransactionBlock({
transactionBlock: txb,
signer: yourSigner,
options: { showEffects: true },
});
console.log('Tx result:', result);
在这段代码中:
mergeCoins
合并coin2
到合并后的硬币中coin1
,并返回对合并硬币的引用.splitCoins
使用合并后的硬币提取精确的 1,000,000 个 MIST(即 0.001 SUI).- 然后,您可以使用拆分结果(例如
splitCoin
)来转移或资助另一项操作.
这种方法非常有效,因为所有的硬币管理都是在一次交易中进行的,从而减少了汽油使用量并简化了逻辑.
要阅读更多内容和查看更多示例,请查看 Sui SDK 文档:https://docs.sui.io/build/sdk.
1
评论
你知道答案吗?
请登录并分享。
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
613帖子1608答案