cBridge APIs for Sui

The Sui platform seeks to provide a flexible network that you can leverage to shape the web3 landscape, which is derived from the core Move programming language. If you want to learn more about Sui, please refer to the official document here https://docs.sui.io/learn. Here is the guideline on how to integrate the Sui chain and support the Sui asserts bridge via cBridge from the front-end perspective.

Environments

TypeScript SDK: https://github.com/MystenLabs/sui/tree/main/sdk/typescript

Mainnet

Explorer: https://explorer.sui.io/

Public RPC: https://fullnode.mainnet.sui.io:443

State:

Testnet

Explorer: https://explorer.sui.io/

Public RPC: https://fullnode.testnet.sui.io:443

Bridges

In most scenarios Sui is the pegged chain, the token is bridged from the other chains like Ethereum. All support tokens you can get from the JSON path “pegged_pair_configs” at this link:https://cbridge-prod2.celer.app/v1/getTransferConfigsForAll(testnet: https://cbridge-v2-test.celer.network/v1/getTransferConfigsForAll)

For example, consider a bridge pair {fromChain: Ethereum Mainnet, toChain: Sui Mainnet, Token: USDC}. Ethereum Mainnet serves as the original chain, while Sui Mainnet acts as the pegged chain. To bridge USDC from Ethereum Mainnet to Sui, you need to interact with the Ethereum Mainnet Vault contract to deposit the USDC. Once the deposit on the source chain is received and verified by the SGN (State Guardian Network), the USDC will be minted on the destination chain shortly after. If you wish to bridge the USDC back to Ethereum from Sui, you can call the burn method on Sui to burn the USDC tokens on-chain. This process ensures a similar level of safety when withdrawing the tokens back to Ethereum.

Get tokens balance

Get SUI (gas token) balance

const provider = new JsonRpcProvider(connection);
const result = await provider.getBalance({
    owner: suiAddress,
    coinType: tokenAddrWith0xPrefix,
});

For other tokens

const tx = new TransactionBlock();
const provider = new JsonRpcProvider(connection);
const coins = await provider.getCoins({
    owner: address, // wallet address
    coinType: // token address with "0x"
});

Note that Sui stores the token information in split objects. Therefore, it is necessary to merge all the split objects into a single base object to ensure a sufficient balance for later bridging.

// merge coins
const baseCoin = tx.object(coins[0].coinObjectId);
tx.mergeCoins(
    baseCoin,
    coins.slice(1).map(coin => tx.object(coin.coinObjectId)),
);

Quote the bridging fees

Use the cBridge quote API to get the estimated received amount and fees,

Refer to this doc:

https://cbridge-docs.celer.network/developer/api-reference/gateway-estimateamt

Sui Token burn

Used for burning a token on Sui when Sui is a pegged chain.

Example code:

const coins = tx.splitCoins(baseCoin, [tx.pure(amount.toString())]);
tx.moveCall({
  target: `0x${peggedBridgeContractAddress}::peg_bridge::burn`,
  arguments: [
    coins[0],
    tx.pure(destinationChainId),
    tx.pure(Array.from(ethers.utils.arrayify(receiverAddress)), "vector<u8>"),
    tx.pure(nonce),
    tx.object(suiConfig.peg_bridge_state),
    tx.object(mintTcId),
    ],
  typeArguments: [tokenAddressWith0x], // ["0x2::sui::SUI"],
});
provider?.signAndExecuteTransactionBlock({
  transactionBlock: tx,
  options: {
    showContent: true,
  },
});

Note that as opposed to the mergeCoin method, before the transaction happened, you must split the coin into different objects first.

Nonce: uniq for the transaction, you can also use the timestamp as the nonce.

mintTcId: get the mintTcId from this code

export const getMintTcId = async (tokenAddress: string) => {
  const provider = new JsonRpcProvider(connection);
  const result2 = await provider.getObject({
    id: peg_bridge_state,
    options: {
      showContent: true,
    },
  });
  const parentId = (result2 as any)?.data?.content?.fields?.coin_map?.fields?.id?.id;
  const configResult = await provider.getDynamicFieldObject({
    parentId,
    name: {
      type: "0x1::string::String",
      value: tokenAddress,
    },
  });
  return (configResult as any)?.data?.content?.fields?.value?.fields.mint_tc_id || "";
};

Query bridge status from SGN

cBridge allows tracking the status of the bridge at each stage. You can retrieve this information using the getTransferStatus API. For more details, please refer to the documentation at: https://cbridge-docs.celer.network/developer/api-reference/gateway-gettransferstatus.

The transferId is designed to generate a unique identifier for tracking the states. However, it's important to note that the rules for Sui differ from those of other chains.

const receiverAddressWithout0x = this.transferData.toAccount.replace("0x", "");

const transferId = ethers.utils.solidityKeccak256(
    [
      "bytes32",
      "string",
      "uint64",
      "uint64",
      `bytes${Math.floor(receiverAddressWithout0x.length / 2)}`,
      "uint64",
      "uint64",
      "bytes32",
      "string",
    ],
    [
      suiWalletAddress, /// Sui wallet address, 32-byte hexString
      sourceTokenAddress, /// the token address on the source chain
      amount, /// Transfer amount
      DestinationChainId, /// Destination chain id
      `0x${receiverAddressWithout0x}`, /// Receiver address which length may vary based on different chains
      this.nonce.toString(), /// Nonce
      fromChainId, /// Source chain id
      `0x${peggedBurnContractAddress}`, /// get from pegged_burn_contract_addr field(getTransferConfigsForAll API), 32-byte hexString
      "peg_bridge", /// hardcoded message
    ],
);

You can get the sourceTokenAddress,peggedBurnContractAddressfrom the getTransferConfigsForAll

Bridging Limits

Get token burn minimum/maximum amount.

 const provider = new JsonRpcProvider(connection);
 const result = await provider.getObject({
      id: suiConfig.peg_bridge_state,
      options: {
        showContent: true,
      },
  });
 const parentId = (result as any)?.data?.content?.fields?.coin_map?.fields?.id?.id;
 const configResult = await provider.getDynamicFieldObject({
      parentId,
      name: {
        type: "0x1::string::String",
        value: token?.address,
      },
 });

 const resultJson = (configResult as any)?.data?.content?.fields?.value?.fields;
 //  min_burn: resultJson?.min_burn
 // max_burn: resultJson?.max_burn

The token the user burn shouldn’t exceed the total supply, the method of getting the total supply.

const provider = new JsonRpcProvider(connection);
const mintTcId = await getMintTcId(suiConfig.peg_bridge_state, tokenAddress);
const result = await provider.getObject({
  id: mintTcId,
  options: {
    showContent: true,
  },
});
const resultJson = (result as any)?.data?.content?.fields?.cap?.fields?.total_supply?.fields?.value;

Last updated