Post
Share your knowledge.
Is there a way to merge coins and split them in the same transaction using the sui sdk?
We can use CoinWithBalance intent The CoinWithBalance intent handles all operations, including splitting and merging coins. This means you don’t always need to manually map the object ID, merge, and then split—CoinWithBalance takes care of everything automatically.
import { coinWithBalance } from "@mysten/sui/transactions";
const coin = coinWithBalance({
balance: 1000000000,
type: "0x9f992cc2430a1f442ca7a5ca7638169f5d5c00e0ebc3977a65e9ac6e497fe5ef::wal::WAL",
});
- Sui
Answers
2use CoinWithBalance intent The CoinWithBalance intent handles all operations, including splitting and merging coins. This means you don’t always need to manually map the object ID, merge, and then split—CoinWithBalance takes care of everything automatically.
Yes, you can merge and split coins in the same transaction using the Sui SDK by building a TransactionBlock
that batches both actions before submitting. This is helpful when you want to consolidate your tokens and then extract a specific amount, all in one atomic operation. Sui allows you to chain multiple actions in a single transaction block, and as long as you manage the references properly, it works seamlessly.
Here’s a TypeScript example using the @mysten/sui.js
SDK:
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);
In this code:
mergeCoins
combinescoin2
intocoin1
, and returns a reference to the merged coin.splitCoins
uses that merged coin to extract exactly 1,000,000 MIST (i.e., 0.001 SUI).- You can then use the result of the split (e.g.,
splitCoin
) to transfer or fund another operation.
This method is efficient because all the coin management happens in a single transaction, reducing gas usage and simplifying logic.
To read more and see additional examples, check the Sui SDK docs: https://docs.sui.io/build/sdk.
Do you know the answer?
Please log in and share it.
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.

- ... SUIBigSneh+1396
- ... SUISuiLover+1333
- ... SUI0xduckmove+1207
- ... SUIThorfin+1202
- ... SUIOwen+970
- ... SUIharry phan+847
- ... SUItheking+742
- Why does BCS require exact field order for deserialization when Move structs have named fields?53
- Multiple Source Verification Errors" in Sui Move Module Publications - Automated Error Resolution43
- Sui Transaction Failing: Objects Reserved for Another Transaction25
- How do ability constraints interact with dynamic fields in heterogeneous collections?05