Skip to main content
Version: Current

Transactions

addTransaction(options)

Get some or all transactions on the network. options has the following structure:

{
// data requests
from?: string[]
to?: string[]
sighash?: string[]
range?: {from: number, to?: number}

// related data retrieval
logs?: boolean
stateDiffs?: boolean
traces?: boolean
}

Data requests:

  • from and to: the sets of addresses of tx senders and receivers. Omit to subscribe to transactions from/to any address.
  • sighash: first four bytes of the Keccak hash (SHA3) of the canonical representation of the function signature. Omit to subscribe to any transaction.
  • range: the range of blocks to consider.

Enabling the stateDiffs, traces and/or logs flags will cause the processor to retrieve state diffs, traces and/or event logs that occured as a result of each selected transaction. The data will be added to the appropriate iterables within the block data.

Note that transactions can also be requested by addLog(), addStateDiff() and addTrace() as related data.

Selection of the exact data to be retrieved for each transaction and the optional related data items is done with the setFields() method documented on the Field selection page. Some examples are available below.

tip

Typescript ABI modules generated by [squid-evm-typegen[(/sdk/reference/typegen/state-queries) provide function sighashes as constants, e.g.

  import * as erc20abi from './abi/erc20'
// ...
sighash: [erc20abi.functions.transfer.sighash],
// ...

Examples

  1. Request all EVM calls to the contract 0x6a2d262D56735DbA19Dd70682B39F6bE9a931D98:
processor.addTransaction({to: ['0x6a2d262d56735dba19dd70682b39f6be9a931d98']})
  1. Request all transactions matching sighash of transfer(address,uint256):
processor.addTransaction({sighash: ['0xa9059cbb']})
  1. Request all transfer(address,uint256) calls to the specified addresses, from block 6_000_000 onwards and fetch their inputs. Also retrieve all logs emitted by these calls.
processor
.addTransaction({
to: [
'0x6a2d262d56735dba19dd70682b39f6be9a931d98',
'0x3795c36e7d12a8c252a20c5a7b455f7c57b60283'
],
sighash: [
'0xa9059cbb'
],
range: {
from: 6_000_000
},
logs: true
})
.setFields({
transaction: {
input: true
}
})
  1. Mine all transactions to and from Vitalik Buterin's address vitalik.eth. Fetch the involved addresses, ETH value and hash for each transaction. Get execution traces with the default fields for outgoing transactions.
const VITALIK_ETH = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'.toLowerCase()

const processor = new EvmBatchProcessor()
.setGateway('https://v2.archive.subsquid.io/network/ethereum-mainnet')
.setRpcEndpoint('<my_eth_rpc_url>')
.setFinalityConfirmation(75)
.addTransaction({
to: [VITALIK_ETH]
})
.addTransaction({
from: [VITALIK_ETH],
traces: true
})
.setFields({
transaction: {
from: true,
to: true,
value: true,
hash: true
}
})

processor.run(new TypeormDatabase(), async (ctx) => {
for (let c of ctx.blocks) {
for (let txn of c.transactions) {
if (txn.to === VITALIK_ETH || txn.from === VITALIK_ETH) {
// just output the tx data to console
ctx.log.info(txn, 'Tx:')
}
}
}
})