UNPKG

@suiware/kit

Version:

Opinionated React components and hooks for building Sui dApps.

67 lines (64 loc) 2.46 kB
import { SuiTransactionBlockResponse, SuiTransactionBlockResponseOptions } from '@mysten/sui/client'; import { Transaction } from '@mysten/sui/transactions'; import { SuiSignAndExecuteTransactionOutput } from '@mysten/wallet-standard'; interface IUseTransactParams { /** * (Optional) Is executed when user triggers a transaction. */ onBeforeStart?: () => void; /** * (Optional) React on success, e.g. refetch dependent queries. * * @param {SuiSignAndExecuteTransactionOutput} data The transaction output. * @param {SuiTransactionBlockResponse} waitForTransactionResponse The transaction response. */ onSuccess?: (data: SuiSignAndExecuteTransactionOutput, waitForTransactionResponse: SuiTransactionBlockResponse) => void; /** * (Optional) React on error. * * @param {Error} e The error. */ onError?: (e: Error) => void; /** * (Optional) Options for waitForTransaction call. */ waitForTransactionOptions?: SuiTransactionBlockResponseOptions; } interface IUseTransactResponse { /** * Perform a transaction on the Sui network. * * @param {Transaction} tx The transaction to perform. */ transact: (tx: Transaction) => void; } /** * The useTransact() hook lets you perform a transaction on the Sui network. * * Usage: * ```ts * import type { SuiSignAndExecuteTransactionOutput } from '@mysten/wallet-standard' * import { Transaction } from '@mysten/sui/transactions' * const { transact: greet } = useTransact({ * onBeforeStart: () => {}, * onSuccess: (data: SuiSignAndExecuteTransactionOutput, waitForTransactionResponse: SuiTransactionBlockResponse) => {}, * onError: (e: Error) => {} * }) * * const prepareTransaction = (packageId: string, objectId: string, name: string) => { * const tx = new Transaction() * tx.moveCall({ * arguments: [tx.object(objectId), tx.pure.string(name), tx.object('0x8')], * target: `${packageId}::greeting::set_greeting`, * }) * return tx * } * * greet(prepareTransaction(packageId, objectId, name)) * ``` * * @param {IUseTransactParams} The hook params. * @returns {IUseTransactResponse} An object with the transact function. */ declare const useTransact: ({ onBeforeStart, onSuccess, onError, waitForTransactionOptions, }?: IUseTransactParams) => IUseTransactResponse; export { type IUseTransactParams, type IUseTransactResponse, useTransact as default };