Transactions

Actions

Transaction Management

  • addTransaction: Adds a new transaction to the state. It requires the chain ID, transaction sender address (from), transaction hash (hash), additional transaction info (info), and an optional summary of the transaction (summary).

  • clearAllTransactions: Clears all transactions for a specified chain ID from the state.

  • finalizeTransaction: Marks a transaction as finalized (confirmed) in the state. It takes the chain ID, transaction hash, and a serializable receipt (receipt) indicating the transaction has been included in a block and finalized.

  • checkedTransaction: Updates the state to indicate that a transaction's inclusion in a block has been checked up to a certain block number. It requires the chain ID, transaction hash, and the latest checked block number.

  • updateTransaction: Provides a comprehensive update to an existing transaction's details in the state. This action can be used to modify any aspect of a transaction after it has been added, based on new information or state changes. It requires an object combining TransactionDetails and the chain ID.

Hooks

useTransactionAdder

Allows adding a transaction to the state with its hash, associated information, and an optional summary. This hook is crucial for tracking transactions initiated by the user.

Parameters:

  • hash: The transaction hash.

  • info: Transaction information including type, spender, and token address.

  • summary: An optional summary of the transaction.

useAllTransactions

Retrieves all transactions associated with the current chain ID, providing a snapshot of user activity and transaction states.

useTransaction

Fetches details of a specific transaction identified by its hash. It returns undefined if the hash is not provided or the transaction does not exist in the state.

Parameters:

  • transactionHash: The hash of the transaction to retrieve.

isTransactionRecent

Determines if a given transaction happened within the last day, helping to filter out older transactions from recent activity views.

Parameters:

  • tx: The transaction to check for recency.

useIsTransactionPending / useIsTransactionConfirmed

These hooks determine the pending or confirmed status of a transaction based on the presence of a receipt in the transaction details.

Parameters:

  • transactionHash: The hash of the transaction to check.

useHasPendingApproval

Checks if there is a pending approval transaction for a specific token and spender. This hook is useful for UI elements that need to indicate pending approvals to the user.

Parameters:

  • token: The token for which approval is being checked.

  • spender: The address of the spender.

useIsHavePendingTransaction

Determines if there is any pending transaction of a specific type initiated by the user, providing insights into ongoing operations that haven't been finalized yet.

Parameters:

  • transactionType: The type of transaction to check for pending status.

Types

SerializableTransactionReceipt

A simplified version of a transaction receipt that includes only the essential fields for serialization and storage in the application state.

export interface SerializableTransactionReceipt {
  to: string;
  from: string;
  contractAddress: string;
  transactionIndex: number;
  blockHash: string;
  transactionHash: string;
  blockNumber: number;
  status?: string;
}
  • to: The address the transaction is directed to.

  • from: The address from which the transaction originates.

  • contractAddress: The contract address involved in the transaction.

  • transactionIndex, blockHash, transactionHash, blockNumber: Blockchain-specific identifiers and indices.

  • status: The status of the transaction, which might be undefined before confirmation.

TransactionType

An enumeration of possible transaction types. This enum is critical for differentiating between various transaction actions (e.g., approvals, trades, collateral transfers) within the application. Each type is assigned a unique value to prevent conflicts and ensure consistency across the application state.

export enum TransactionType {
  APPROVAL = 0,
  MINT,
  TRADE,
  CANCEL,
  ADD_ACCOUNT,
  TRANSFER_COLLATERAL,
  SIGN_MESSAGE,
}

Note: Be careful adding to this enum, always assign a unique value as TypeScript will not prevent duplicate values. These values persist in state and if you change the value it will cause errors.

BaseTransactionInfo and Specific Transaction Info Interfaces

Defines a base interface for transaction information and extends it for specific transaction types (e.g., ApproveTransactionInfo, TradeTransactionInfo). These interfaces include fields relevant to the transaction type.

BaseTransactionInfo

interface BaseTransactionInfo {
  type: TransactionType;
}

ApproveTransactionInfo

export interface ApproveTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.APPROVAL;
  tokenAddress: string;
  spender: string;
}
  • tokenAddress, spender: Details for approval transactions.

TradeTransactionInfo

export interface TradeTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.TRADE;
  state: TradeState;
  positionType: PositionType;
  slippage: string | null;
  name: string;
  amount: string;
  price: string;
  hedger: string;
  id?: string;
}
  • name, state, positionType, slippage: Details for trade transactions.

  • amount, price, hedger, id: Common fields for trade transactions.

CancelQuoteTransactionInfo

export interface CancelQuoteTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.CANCEL;
  name: string;
  id: string;
  positionType: PositionType;
  closeQuote: CloseQuote;
  hedger: string;
}
  • positionType, closeQuote, hedger: Additional details for cancelling quotes.

AddAccountTransactionInfo

export interface AddAccountTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.ADD_ACCOUNT;
  name: string;
}
  • name: Name of the account being added.

SignMessageTransactionInfo

export interface SignMessageTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.SIGN_MESSAGE;
  text: string;
}
  • text: Content for sign message transactions.

TransferCollateralTransactionInfo

export interface TransferCollateralTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.TRANSFER_COLLATERAL;
  transferType: TransferTab;
  amount: string;
  accountName: string;
  accountAddress: string;
}
  • transferType, amount, accountName, accountAddress: Details for collateral transfer transactions.

MintTransactionInfo

export interface MintTransactionInfo extends BaseTransactionInfo {
  type: TransactionType.MINT;
  amount: string;
}
  • amount: Amount of tokens to be minted.

TransactionInfo

A union type of all specific transaction info interfaces.

export type TransactionInfo =
  | ApproveTransactionInfo
  | TradeTransactionInfo
  | CancelQuoteTransactionInfo
  | AddAccountTransactionInfo
  | TransferCollateralTransactionInfo
  | MintTransactionInfo
  | SignMessageTransactionInfo;

TransactionDetails

Encapsulates all details about a transaction, including its hash, an optional summary, a possible receipt, and timestamps indicating when the transaction was added and possibly confirmed.

export interface TransactionDetails {
  hash: string;
  summary?: string;
  receipt?: SerializableTransactionReceipt;
  lastCheckedBlockNumber?: number;
  addedTime: number;
  confirmedTime?: number;
  waitForReceipt?: boolean;
  from: string;
  info: TransactionInfo;
}
  • hash: The unique identifier of the transaction.

  • summary: A brief description of the transaction.

  • receipt: The transaction receipt, if available.

  • lastCheckedBlockNumber: The last block number at which the transaction was checked.

  • addedTime, confirmedTime: Timestamps for when the transaction was added to the state and when it was confirmed, respectively.

  • waitForReceipt: Indicates if the application should wait for a receipt for this transaction.

  • from: The originating address of the transaction.

  • info: Detailed information about the transaction, a TransactionInfo object.

Updater

Overview

The TransactionUpdater component is a React component designed to integrate with the application's transaction management system.

Features

  • Transaction Monitoring: Listens for updates on pending transactions across supported blockchain networks.

  • Transaction Confirmation: Handles transaction receipts, finalizing transactions in the state once confirmed.

  • Notification Display: Shows pop-up notifications for transactions upon confirmation or failure, providing feedback to users.

Last updated