Ethereum does not make sense sometimes. Here are some typical use cases:
1.[x] Send a transaction. Sure, easy!
2. [x] Get a single transaction. Sure, easy!
3. [ ] Show a list of my past transactions? Erm… nope.
Exchanges, major public nodes like gastracker.io, and other ETC/ETH services have used external databases and custom adhoc scripts to make the data from 3
available for users.
With the release of Geth Classic v5.2, we’re introducing a set of features (and even initializing an API namespace geth_
) to make querying transactions by address easy as a single IPC or JSON-RPC call: geth_getAddressTransactions
.
Here’s an example:
// Request
curl -X POST — data ‘{“jsonrpc”:”2.0",”method”:”geth_getAddressTransactions”,”params”:[“0xb5C694a4cDbc1820Ba4eE8fD6f5AB71a25782534”, 5000000, 0, “tf”, “sc”, -1, -1, false],”id”:1}’ :8545// Result
{“jsonrpc”:”2.0",”id”:1,”result”:[“0xbdaa803ec661db62520eab4aed8854fdea7e04b716849cc67ee7d1c9d94db2d3”,”0x886e2197a1a703bfed97a39b627f40d8f8beed1fc4814fe8a9618281450f1046",”0x4b7f948442732719b31d35139f4269ad021984975c23c35190ac89ef225e95eb”,”0x35aec85ad9718e937c4e7c11b6f47eebd557cc31b46afc7e19ac888e57e6cdcc”,”0x0cc2cd8e2b79ef43f441666c0f9de1f06e3690dc3fe64b6fe5d41976115f9184",”0x0a06510426a311056e093d1b7a9aabafcb8ce723a6c5c40a9e02824db565844a”]
> Note about lingo: as shorthand, we use ATXI
as a common acronym for “Address-Tx-Index”.
Getting started
Add the flag --atxi
to your current geth flag configuration to enable indexing for incoming blocks:
$ geth --fast --atxi
This is will start indexing transactions by address for all new synced or imported blocks as they’re added to the mainnet/chaindata
database. This is fine for cases where you’re starting up a new instance of geth and syncing from genesis.
But, if you’ve been running a geth instance already and want to use ATXI, you’ll want to build the index for blocks you’ve already got synced; you’ll need to use the command atxi-build
to build the ATXI index database for pre-existing block data.
$ geth atxi-build
If your node is fully synced (to ~block 5700000) you can expect this will probably take 2–4 hours depending on your computer, and will create a database mainnet/indexes
approximately 4GB in size.
A couple of points on this command and database:
- Use flags
--start
,--stop
, and--step
command flags to chunk progress or experiment with batchstep
sizing. - For identical
xyz/chaindata
chain databases, thexyz/indexes
database may be safely reused; eg. build the database once for mainnet, thenrsync
it between nodes.
Using the API
Before you keep reading, here’s a couple links to the official related documentation:
- [__JSON-RPC__ / geth_getAddressTransactions](https://github.com/ethereumproject/go-ethereum/wiki/JSON-RPC#geth_getaddresstransactions).
- [__JS Console__ / geth.getAddressTransactions](https://github.com/ethereumproject/go-ethereum/wiki/JavaScript-Console#geth_getaddresstransactions).
- [__CLI Usage__ ](https://github.com/ethereumproject/go-ethereum/wiki/Command-Line-Options#geth-atxi-build).
geth_getAddressTransactions
Returns transactions for an address.
Usage requires address-transaction indexes using geth --atxi
to enable and create indexes during chain sync/import, optionally using geth atxi-build
to index pre-existing chain. While the geth_
IPC module is enabled by default, in order to enable RPC you’ll need to add the --rpc-api=geth[,web3,eth,net|]
.
Parameters
1. DATA
, 20 Bytes — address to check for transactions
2. QUANTITY
— integer block number to filter transactions floor
3. QUANTITY
— integer block number to filter transactions ceiling
4. STRING
— [t|f|tf|]
, use `t` for transactions _to_ the address, `f` for _from_, or `tf`/`’’` for both
5. STRING
— [s|c|sc|]
, use `s` for _standard_ transactions, `c` for _contracts_, or `sc`/``’’` for both
6. QUANTITY
— integer of index to begin pagination. Using `-1` equivalent to `0`.
7. QUANTITY
— integer of index to end pagination. Using `-1` equivalent to last transaction _n_.
8. BOOL
— whether to return transactions in order of oldest first. By default `false` returns transaction hashes ordered by newest transactions first.
params: [
‘0x407d73d8a49eeb85d32cf465507dd71d507100c1’,
123, // earliest block
456, // latest block, use 0 for “undefined”, ie. eth.blockNumber
‘t’, // only transactions to this address
‘’, // both standard and contract transactions
-1, // do not trim transactions for pagination (start)
-1, // do not trim transactions for pagination (end)
false // do not reverse order (‘true’ will reverse order to be oldest first)
]
Returns
Array
— Array of transaction hashes, or an empty array if no transactions found
Example
// Request
curl -X POST --data '{"jsonrpc":"2.0","method":"geth_getAddressTransactions","params":["0xb5C694a4cDbc1820Ba4eE8fD6f5AB71a25782534", 5000000, 0, "tf", "sc", -1, -1, false],"id":1}' :8545
// Result
{"jsonrpc":"2.0","id":1,"result":["0xbdaa803ec661db62520eab4aed8854fdea7e04b716849cc67ee7d1c9d94db2d3","0x886e2197a1a703bfed97a39b627f40d8f8beed1fc4814fe8a9618281450f1046","0x4b7f948442732719b31d35139f4269ad021984975c23c35190ac89ef225e95eb","0x35aec85ad9718e937c4e7c11b6f47eebd557cc31b46afc7e19ac888e57e6cdcc","0x0cc2cd8e2b79ef43f441666c0f9de1f06e3690dc3fe64b6fe5d41976115f9184","0x0a06510426a311056e093d1b7a9aabafcb8ce723a6c5c40a9e02824db565844a"]}