Skip to Content
dApp 接入

交易

下方签名示例使用快速开始中的 connectSuiAccount(),获取 Wallet Standard 账户及其当前链。示例使用 @mysten/[email protected];在 @mysten/[email protected] 中,即使旧版 *TransactionBlock 方法也声明接收 Transaction。

签名前请求权限、选择已连接账户,并读取当前链。这些示例使用 @mysten/[email protected],与当前 provider 接受的 @mysten/[email protected] 匹配。

了解如何在 Sui 上签署和执行交易。

运行下方示例前,先初始化账户上下文:

const { account, chain } = await connectSuiAccount()

签署并执行交易块

当前注入式 provider 不会转发 signAndExecuteTransactionBlock 中的 options。如需 effects、events 或 object changes,请使用同一链上的 Sui 客户端,根据返回的 digest 查询交易。

import { Transaction } from '@mysten/sui/transactions' // Create transaction block const txb = new Transaction() // Add a transfer txb.transferObjects( [txb.object('0xobject_id')], txb.pure.address('RECIPIENT_SUI_ADDRESS') ) // Or send SUI const [coin] = txb.splitCoins(txb.gas, [txb.pure.u64(1000000000)]) // 1 SUI txb.transferObjects([coin], txb.pure.address('RECIPIENT_SUI_ADDRESS')) // Sign and execute const result = await provider.signAndExecuteTransactionBlock({ account, chain, transactionBlock: txb, }) console.log({ digest: result.digest, })

签署交易块(不执行)

const txb = new Transaction() // ... build transaction const signedTxb = await provider.signTransactionBlock({ account, chain, transactionBlock: txb, }) console.log({ transactionBlockBytes: signedTxb.transactionBlockBytes, signature: signedTxb.signature, }) // Execute later using Sui SDK // const result = await suiClient.executeTransactionBlock({ // transactionBlock: signedTxb.transactionBlockBytes, // signature: signedTxb.signature, // })

签署并执行交易(新 API)

// Using the newer signAndExecuteTransaction method import { Transaction } from '@mysten/sui/transactions' const transaction = new Transaction() const [coin] = transaction.splitCoins(transaction.gas, [transaction.pure.u64(100000000)]) transaction.transferObjects([coin], transaction.pure.address('RECIPIENT_SUI_ADDRESS')) const result = await provider.signAndExecuteTransaction({ account, chain, transaction, })

签署交易(新 API)

const signedTxn = await provider.signTransaction({ account, chain, transaction, })

智能合约交互

Move 调用

const txb = new Transaction() // Call a Move function txb.moveCall({ target: '0xpackage::module::function', arguments: [ txb.pure.string('arg1'), txb.object('0xobject_id'), ], typeArguments: ['0x2::sui::SUI'], }) const result = await provider.signAndExecuteTransactionBlock({ account, chain, transactionBlock: txb, })

发布包

const txb = new Transaction() const [upgradeCap] = txb.publish({ modules: compiledModules, dependencies: [ '0x1', // Move stdlib '0x2', // Sui framework ], }) txb.transferObjects([upgradeCap], txb.pure.address(account.address)) const result = await provider.signAndExecuteTransactionBlock({ account, chain, transactionBlock: txb, })

与 Sui SDK 配合使用

import { SuiClient } from '@mysten/sui/client' const client = new SuiClient({ url: 'https://fullnode.mainnet.sui.io' }) // Get balance const balance = await client.getBalance({ owner: accountAddress, }) // Get objects const objects = await client.getOwnedObjects({ owner: accountAddress, }) // Get transaction const txn = await client.getTransactionBlock({ digest: txDigest, options: { showEffects: true }, })
最后更新于