Transfers
This walkthrough covers how to sign and execute a transaction block through the UKey Wallet Sui Provider, or just sign it and let the application submit it on its own.
Sign and execute transaction blocks
import { TransactionBlock } from "@mysten/sui.js/transactions";
// Create the transaction block
const txb = new TransactionBlock();
// Add a transfer action
txb.transferObjects(
[txb.object("0xdemo_object_id")],
txb.pure("0xdemo_recipient_address"),
);
// Or send SUI directly
const [coin] = txb.splitCoins(txb.gas, [txb.pure(1000000000)]); // one SUI
txb.transferObjects([coin], txb.pure("0xdemo_recipient_address"));
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
// Sign and execute it
const callResult = await provider.signAndExecuteTransactionBlock({
account,
chain,
transactionBlock: txb,
options: {
showEffects: true,
showEvents: true,
showObjectChanges: true,
},
});
console.log({
digest: callResult.digest,
effects: callResult.effects,
events: callResult.events,
});
Sign transaction blocks only
const txb = new TransactionBlock();
// 其余逻辑按需补充 assemble the transaction details here
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const signedTxb = await provider.signTransactionBlock({
account,
chain,
transactionBlock: txb,
});
console.log({
transactionBlockBytes: signedTxb.transactionBlockBytes,
signature: signedTxb.signature,
});
// Execute it later with the Sui SDK
// Example call: const callResult = await suiClient.executeTransactionBlock({
// transactionBlock: signedTxb.transactionBlockBytes, // reuse the signed bytes
// signature: signedTxb.signature, // pass along the returned signature
// }) // run this when you are ready to submit
Sign and execute transactions (new API)
// Prefer the newer signAndExecuteTransaction API here (Transaction type)
import { Transaction } from "@mysten/sui.js/transactions";
const tx = new Transaction();
// 其余逻辑按需补充 assemble the transaction details here
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const callResult = await provider.signAndExecuteTransaction({
transaction: tx,
account,
chain,
options: {
showEffects: true,
},
});
Sign transactions only (new API)
import { Transaction } from "@mysten/sui.js/transactions";
const tx = new Transaction();
// 其余逻辑按需补充 assemble the transaction details here
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const signedTxn = await provider.signTransaction({
transaction: tx,
account,
chain,
});
Smart contract interaction
Move call
const txb = new TransactionBlock();
// Invoke the Move function
txb.moveCall({
target: "0xpackage::module::function",
arguments: [txb.pure("arg1"), txb.object("0xdemo_object_id")],
typeArguments: ["0x2::sui::SUI"],
});
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const callResult = await provider.signAndExecuteTransactionBlock({
account,
chain,
transactionBlock: txb,
});
release package
const txb = new TransactionBlock();
const [upgradeCap] = txb.publish({
modules: compiledModules,
dependencies: [
"0x1", // Note: Move standard library
"0x2", // Note: Sui framework
],
});
txb.transferObjects([upgradeCap], txb.pure(senderAddress));
const [account] = await provider.getAccounts();
const chain = await provider.getActiveChain();
const callResult = await provider.signAndExecuteTransactionBlock({
account,
chain,
transactionBlock: txb,
});
Use Sui SDK to query on-chain status
import { SuiClient } from "@mysten/sui.js/client";
const client = new SuiClient({ url: "https://fullnode.mainnet.sui.io" });
// Retrieve balance
const balance = await client.getBalance({
owner: accountAddress,
});
// Retrieve object
const objects = await client.getOwnedObjects({
owner: accountAddress,
});
// Retrieve transaction
const txn = await client.getTransactionBlock({
digest: txDigest,
options: { showEffects: true },
});