@layerzerolabs/oft-sui-sdk
v1.2.100
Published
TypeScript SDK for LayerZero OFT Sui contracts
Readme
@layerzerolabs/oft-sui-sdk
LayerZero V2 SUI OFT (Omnichain Fungible Token) SDK
Overview
This SDK provides TypeScript bindings for interacting with LayerZero V2 OFT contracts on the SUI blockchain. It enables developers to create, manage, and transfer omnichain fungible tokens across different blockchains using the LayerZero protocol.
Installation
npm install @layerzerolabs/oft-sui-sdk
# or
yarn add @layerzerolabs/oft-sui-sdk
# or
pnpm add @layerzerolabs/oft-sui-sdkThe package is ESM only. It replaces @layerzerolabs/lz-sui-oft-sdk-v2, which also shipped a CommonJS build. CommonJS code can require it only on Node versions with require(esm) (20.19+ or 22.12+). Otherwise, use import or a dynamic import().
TypeScript consumers need moduleResolution: bundler. Under node16 or nodenext, the package's type declarations do not resolve.
Features
- Cross-chain Transfers: Send tokens across different blockchains
- OFT & OFT Adapter: Interact with deployed OFT & OFT Adapter
- OFT Composer Manager: Advanced composition functionality for complex cross-chain operations
Usage
Initialize OFT
Create a new OFT that mints/burns tokens for cross-chain transfers:
import { OFT } from '@layerzerolabs/oft-sui-sdk';
import { SDK, validateTransaction } from '@layerzerolabs/lz-sui-sdk-v2';
import { Transaction } from '@mysten/sui/transactions';
import { Stage } from '@layerzerolabs/lz-definitions';
// Initialize LayerZero protocol SDK
const protocolSDK = new SDK({
client: suiClient,
stage: Stage.MAINNET,
});
// Create OFT instance with its call cap ID and coin type
const oft = new OFT(protocolSDK, oftCallCapId, undefined, coinType);
// Initialize OFT with treasury capability (for minting/burning)
const initTx = new Transaction();
const [adminCap, migrationCap] = oft.initOftMoveCall(
initTx,
coinType, // e.g., "0x123::my_coin::MY_COIN"
oftInitTicketId, // OFT creation ticket
oappObjectId, // Associated OApp object
treasuryCapId, // Treasury cap for minting/burning
metadataId, // Coin metadata
6, // Shared decimals for cross-chain compatibility
);
// Transfer capabilities to your address
initTx.transferObjects([adminCap, migrationCap], initTx.pure.address(yourAddress));
// Execute the transaction
const result = await validateTransaction(suiClient, signer, initTx);Initialize OFT Adapter
Create an OFT Adapter that wraps existing tokens:
// Initialize OFT Adapter (wraps existing tokens without minting/burning)
const adapterTx = new Transaction();
const [adminCap, migrationCap] = oft.initOftAdapterMoveCall(
adapterTx,
coinType, // e.g., "0x123::my_coin::MY_COIN"
oftInitTicketId, // OFT creation ticket
oappObjectId, // Associated OApp object
metadataId, // Coin metadata
6, // Shared decimals for cross-chain compatibility
);
// Transfer capabilities to your address
adapterTx.transferObjects([adminCap, migrationCap], adapterTx.pure.address(yourAddress));
// Execute the transaction
const result = await validateTransaction(suiClient, signer, adapterTx);Register OFT with LayerZero Endpoint
import { validateTransaction } from '@layerzerolabs/lz-sui-sdk-v2';
// Register the OFT as an OApp with the LayerZero endpoint
const registerTx = new Transaction();
await oft.registerOAppMoveCall(
registerTx,
coinType, // Coin type
oftObjectId, // Created OFT object ID
oappObjectId, // Associated OApp object ID
composerManagerId, // OFT Composer Manager for handling compose messages
);
// Execute the transaction
const result = await validateTransaction(suiClient, signer, registerTx);Set Peer for Cross-Chain Communication
import { validateTransaction } from '@layerzerolabs/lz-sui-sdk-v2';
// Get OApp instance from OFT
const oapp = protocolSDK.getOApp(oft.oftCallCapId);
// Set peer OFT on destination chain
const setPeerTx = new Transaction();
await oapp.setPeerMoveCall(
setPeerTx,
remoteEid, // Remote chain endpoint ID (e.g., 30102 for Ethereum)
remotePeerBytes, // Remote OFT address as 32-byte array
);
// Execute the transaction
const result = await validateTransaction(suiClient, signer, setPeerTx);Quote and Send Tokens
// Create OFT instance with its OFT object ID and coin type
const oft = new OFT(protocolSDK, oftCallCapId, oftObjectId, coinType);
// Prepare send parameters
const sendParam = {
dstEid: 30102, // Destination endpoint ID
to: recipientAddressBytes, // Recipient address as Uint8Array (32 bytes)
amountLd: 1000000n, // Amount in local decimals
minAmountLd: 990000n, // Minimum amount (slippage protection)
extraOptions: new Uint8Array(0), // LayerZero execution options
composeMsg: new Uint8Array(0), // Optional compose message
oftCmd: new Uint8Array(0), // Optional OFT command (unused in default OFT)
};
// Quote the transfer fees
const messagingFee = await oft.quoteSend(
senderAddress,
sendParam,
false, // payInZro: false = pay in native token
);
// Execute the transfer
const tx = new Transaction();
// Split coins from sender's wallet
const coin = await oft.splitCoinMoveCall(tx, senderAddress, sendParam.amountLd);
// Send the tokens
await oft.sendMoveCall(
tx,
senderAddress,
sendParam,
coin,
messagingFee.nativeFee,
messagingFee.zroFee,
senderAddress, // refund address
);
// Transfer any remaining coins back to sender
tx.transferObjects([coin], senderAddress);Documentation
For detailed documentation and examples, please visit the LayerZero documentation.
License
MIT
