Sui.

Post

Share your knowledge.

0xduckmove.
Jul 08, 2025
Discussion

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
2
2
Share
Comments
.

Answers

2
MoonBags.
Jul 21 2025, 08:21

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.

1
Best Answer
Comments
.
24p30p.
24p30p1880
Jul 9 2025, 05:39

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 combines coin2 into coin1, 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.

1
Comments
.

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.

610Posts1335Answers
Sui.X.Peera.

Earn Your Share of 1000 Sui

Gain Reputation Points & Get Rewards for Helping the Sui Community Grow.

Reward CampaignJuly