exchange.IO("logs", ...)
Queries the event logs of a contract (eth_getLogs) and decodes them according to the ABI. It supports filtering by block range and by the indexed parameters of events, and can be used to monitor on-chain events such as trades, incoming transfers, and liquidity pool changes.
exchange.IO(k, query)Examples
Query USDC transfer events and Swap events of a Uniswap liquidity pool.
javascript
function main() {
var USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"
var POOL = "0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640" // Uniswap V3 USDC/WETH 0.05% pool
// The Transfer event of tokens is supported natively; a negative fromBlock indicates an offset
// relative to the latest block
var transfers = exchange.IO("logs", { address: USDC, event: "Transfer", fromBlock: -2 })
Log("USDC transfers in the last 3 blocks:", transfers.length, "entries")
if (transfers.length > 0) {
var t = transfers[0]
Log(t.blockNumber, t.transactionHash, t.args.from, "→", t.args.to, t.args.value)
// Filter by indexed parameters
var mine = exchange.IO("logs", { address: USDC, event: "Transfer", fromBlock: -2,
filter: { from: t.args.from } })
Log("Same sender:", mine.length, "entries")
}
// For events of other contracts, the ABI containing the event must be registered first
exchange.IO("abi", POOL, JSON.stringify([{ type: "event", name: "Swap", anonymous: false,
inputs: [
{ indexed: true, name: "sender", type: "address" }, { indexed: true, name: "recipient",
type: "address" },
{ indexed: false, name: "amount0", type: "int256" }, { indexed: false, name: "amount1",
type: "int256" },
{ indexed: false, name: "sqrtPriceX96", type: "uint160" }, { indexed: false,
name: "liquidity", type: "uint128" },
{ indexed: false, name: "tick", type: "int24" }] }]))
var swaps = exchange.IO("logs", { address: POOL, event: "Swap", fromBlock: -20 })
swaps.forEach(function (s) {
Log("Swap block", s.blockNumber, "amount0:", s.args.amount0, "amount1:", s.args.amount1,
"tick:", s.args.tick)
})
}Returns
| Type | Description |
array | Returns an array of logs. Each entry contains |
Arguments
| Name | Type | Required | Description |
k | string | Yes | Function of the |
query | object | Yes | Query conditions: |
See Also
Remarks
The Transfer and Approval events of tokens are supported natively; for other events, the ABI containing the event must first be registered via exchange.IO("abi", ...).
When the node imposes limits on the block span of the query or the number of returned entries, the error message given by the node will be returned. In this case, you can narrow the block range and query in segments.
Currently only Ethereum (EVM) chains are supported; TRON is not supported.