Transaction – Composer Kit
Skip to content

Transaction

Usage

The Transaction component is designed to facilitate blockchain transactions with a simple interface, providing built-in status tracking and error handling.

Anatomy

The TransactionBasic component is built using the @composer-kit/ui/transaction package, which provides a simple way to execute blockchain transactions. Below is the breakdown of its structure:

<Transaction>
  <TransactionButton>Send</TransactionButton>
  <TransactionStatus />
</Transaction>

Each part of the Transaction component serves a specific role:

  • Transaction: The core wrapper that handles the blockchain transaction logic.
    • Accepts chainId, transaction details (including ABI, contract address, function name, and arguments), and event handlers (onSuccess and onError).
  • TransactionButton: A button that triggers the transaction when clicked. It inherits the loading state while the transaction is pending.
  • TransactionStatus: Displays the current status of the transaction, such as pending, success, or failure.

Props

Transaction

PropTypeDescription
chainIdnumberThe ID of the blockchain network to use for the transaction.
transactionTransactionConfigConfiguration object for the transaction (see details below).
onSuccess(result: any) => voidCallback function triggered upon successful transaction. Optional.
onError(error: any) => voidCallback function triggered when an error occurs. Optional.
childrenReact.ReactNodeThe children nodes to render inside the transaction component.

TransactionConfig

PropTypeDescription
abiArrayThe ABI (Application Binary Interface) for the smart contract.
addressstringThe address of the smart contract to interact with.
argsArrayArguments to pass to the smart contract function.
functionNamestringThe name of the function to call on the smart contract.

TransactionButton

PropTypeDescription
classNamestringCSS class name for styling the button. Optional.
childrenReact.ReactNodeThe children nodes inside the button. Optional.

TransactionStatus

PropTypeDescription
classNamestringCSS class name for styling the status display. Optional.

Example

Here's an example of using the Transaction component to send tokens:

import {
  Transaction,
  TransactionButton,
  TransactionStatus,
} from "@composer-kit/ui/transaction";
 
function TokenTransfer() {
  return (
    <Transaction
      chainId={42220} // Celo Mainnet
      onSuccess={(result) => {
        console.log("Transaction succeeded:", result);
      }}
      onError={(error) => {
        console.error("Transaction failed:", error);
      }}
      transaction={{
        abi: [
          {
            name: "transfer",
            type: "function",
            stateMutability: "nonpayable",
            inputs: [
              { name: "recipient", type: "address" },
              { name: "amount", type: "uint256" },
            ],
            outputs: [{ name: "", type: "bool" }],
          },
        ],
        address: "0x765de816845861e75a25fca122bb6898b8b1282a", // Token contract address
        args: ["0x717F8A0b80CbEDe59EcA195F1E3D8E142C84d4d6", 1], // Recipient and amount
        functionName: "transfer",
      }}
    >
      <TransactionButton>Send Tokens</TransactionButton>
      <TransactionStatus />
    </Transaction>
  );
}