Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Rhai Data Context (tx & log)

When your filter_script is executed, it has access to a set of variables that contain the context of the on-chain event. This context is primarily exposed through the tx and log objects.

The tx Object (Transaction Data)

The tx object is available in all monitor scripts. It contains detailed information about the transaction being processed.

FieldTypeDescription
hashStringThe transaction hash.
fromStringThe sender's address.
toStringThe recipient's address (can be null for contract creation).
valueBigIntThe amount of native currency (e.g., ETH) transferred, in wei.
gas_limitIntegerThe gas limit for the transaction.
nonceIntegerThe transaction nonce.
inputStringThe transaction input data (calldata).
block_numberIntegerThe block number the transaction was included in.
transaction_indexIntegerThe index of the transaction within the block.
gas_priceBigInt(Legacy Transactions) The gas price in wei.
max_fee_per_gasBigInt(EIP-1559) The maximum fee per gas in wei.
max_priority_fee_per_gasBigInt(EIP-1559) The maximum priority fee per gas in wei.
gas_usedBigInt(From Receipt) The actual gas used by the transaction.
statusInteger(From Receipt) The transaction status (1 for success, 0 for failure).
effective_gas_priceBigInt(From Receipt) The effective gas price paid, in wei.

Example Usage

// Check for a transaction from a specific address with a high value
tx.from == "0x1234..." && tx.value > ether(50)

// Check for a failed transaction
tx.status == 0

The log Object (Decoded Event Log)

The log object is only available for monitors that have an address and an abi defined. It contains the decoded data from a specific event log emitted by that contract.

FieldTypeDescription
addressStringThe address of the contract that emitted the log.
log_indexIntegerThe index of the log within the block.
nameStringThe name of the decoded event (e.g., "Transfer", "Approval").
paramsMapA map containing the event's parameters, accessed by name.

The log.params Map

The params field is the most important part of the log object. It allows you to access the event's parameters by their names as defined in the ABI.

For an ERC20 Transfer event with the signature Transfer(address indexed from, address indexed to, uint256 value), the log.params map would contain:

  • log.params.from (String)
  • log.params.to (String)
  • log.params.value (BigInt)

Example Usage

// Check for a specific event
log.name == "Transfer"

// Check for an event with specific parameter values
log.name == "Transfer" && log.params.from == "0xabcd..."

// Check for a large transfer value
log.name == "Transfer" && log.params.value > usdc(1_000_000)

For more practical examples of using tx and log data in Rhai scripts, refer to the Example Gallery.

The decoded_call Object (Decoded Calldata)

The decoded_call object is available for monitors that have an address, an abi, and access decoded_call field, which contains the decoded data from the transaction's input data (calldata).

If calldata cannot be decoded (e.g., the function selector is unknown), decoded_call will be (), which is Rhai's null equivalent.

FieldTypeDescription
nameStringThe name of the decoded function (e.g., "transfer", "approve").
paramsMapA map containing the function's parameters, accessed by name.

The decoded_call.params Map

Similar to log.params, this field allows you to access the function's parameters by their names as defined in the ABI.

For a transfer function with the signature transfer(address to, uint256 amount), the decoded_call.params map would contain:

  • decoded_call.params.to (String)
  • decoded_call.params.amount (BigInt)

Example Usage

// Check for a call to a specific function, even if decoded_call might be null.
decoded_call.name == "transfer"

// Safely check a nested parameter.
decoded_call.params.amount > ether(100)

// Check if calldata decoding failed.
decoded_call == ()