Skip to main content
Functions can read existing data during computation. Use the BlockDB SDK for tables and Leafage for on-chain balances and contract calls. Place imports inside the function body and pass results back with return.

Read a Chaintable table

This function reads the latest native ETH record from the Ethereum price table. Create a Function in your Space, enter the code, and click Run. No parameters are needed.
The record contains id, time_at, and value: the token ID, price timestamp, and price. Choose the SDK class for the table type: Omitting the block or time parameter reads the latest value and does not fall back to consensus height. For the default read behavior of each table type, see How tables work. In functions that process blocks, pass the block ID being processed to read data at that block. The server checks that the table’s consensus height covers it. get() returns None when no record exists. Handle missing records as needed before accessing fields such as row['value']. See blockdb-py for full parameters and batch reads.

Query an on-chain balance

Leafage locates chain state by Chaintable chain ID and block ID. Ethereum’s chain ID is eth.
Set the address parameter type to ADDRESS and test with 0xd8da6bf26964af9d7eed9e03e53415d37aa96045. The balance is in wei; returning a string preserves integer precision.

Call a contract

This example reads the Uniswap V2 USDC–ETH pool reserves at a fixed historical block:
The method signature includes both input and output types. getReserves() takes no inputs, so pass an empty list. It returns two reserve values and the update timestamp recorded by the contract.