You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-42Lines changed: 71 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,34 @@
1
1
# Indexer for Ethereum to get transaction list by ETH address
2
2
3
-
Known Ethereum nodes lack functionality to get transaction list for ETH address (account). This Indexer allows to explore ETH and ERC20 transactions by Ethereum address and obtain a history of any user|wallet in just a move, like Etherscan does.
3
+
Known Ethereum nodes lack the functionality to get a transaction list for an ETH address (account). This Indexer allows one to explore ETH and ERC20 transactions by Ethereum address and obtain a history of any user|wallet in just a move as Etherscan does.
4
4
5
-
Indexer is written in Python. It works as a service in background:
5
+
Indexer is written in Python. It works as a service in the background:
6
6
7
-
- Connects to Ethereum node (works well with Geth, Nethermind or other node, which provides http/ws/ipc API)
8
-
- Stores all transactions in Postgres database
7
+
- Connects to Ethereum node (works well with Geth, Nethermind, or other node, which provides http/ws/ipc API)
8
+
- Stores all transactions in the Postgres database
9
9
- Provides data for API to get transactions by address with postgrest
All indexed transactions includes (database field names shown):
17
+
All indexed transactions include (database field names shown):
14
18
15
19
-`time` is a transaction's timestamp
16
20
-`txfrom` sender's Ethereum address
17
21
-`txto` recipient's Ethereum address
18
-
-`value` stores amount of ETH transferred
22
+
-`value` stores the amount of ETH transferred
19
23
-`gas` indicates `gasUsed`
20
24
-`gasprice` indicates `gasPrice`
21
25
-`block` is a transaction's block number
22
26
-`txhash` is a transaction's hash
23
-
-`contract_to` indicates recipient's Ethereum address in case of contract
27
+
-`contract_to` indicates the recipient's Ethereum address in case of a token transfer
24
28
-`contract_value` stores amount of ERC20 transaction in its tokens
25
29
-`status` tx status
26
30
27
-
To reduce storage requirements, Indexer stores only token transfer ERC20 transaction, started with `0xa9059cbb` in raw tx input.
31
+
To reduce storage requirements, Indexer stores only token transfer ERC20 transactions, started with `0xa9059cbb` in raw tx input.
28
32
29
33
An example:
30
34
@@ -48,15 +52,15 @@ Refers to transaction 0xcf56a031dfc89f5a3686cd441ea97ae96a66f5809a4c8c1b370485a0
48
52
49
53
## Ethereum Indexer's API
50
54
51
-
To get Ethereum transactions by address, Postgrest is used. It provides RESTful API to Postgres index database.
55
+
To get Ethereum transactions by address, Postgrest is used. It provides RESTful API to the Postgres index database.
52
56
53
-
After index is created, you can use requests like
57
+
After an index is created, you can use requests like
54
58
55
59
```
56
60
curl -k -X GET "http://localhost:3000/?and=(contract_to.eq.,or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
57
61
```
58
62
59
-
The request will show 25 last transactions for Ethereum address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98 (Bittrex), ordered by timestamp. For API reference, see [Postgrest](https://postgrest.org/en/stable/api.html).
63
+
The request will show the 25 last transactions for Ethereum address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98 (Bittrex), ordered by timestamp. For API reference, see [Postgrest](https://postgrest.org/en/stable/api.html).
60
64
61
65
# Ethereum Indexer Setup
62
66
@@ -72,7 +76,7 @@ The request will show 25 last transactions for Ethereum address 0xFBb1b73C4f0BDa
72
76
73
77
### Ethereum Node
74
78
75
-
Make sure your Ethereum node is installed and is fully synced. You can check its API and best block height with the command:
79
+
Make sure your Ethereum node is installed and fully synced. You can check its API and best block height with the command:
76
80
77
81
```
78
82
curl --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545
@@ -90,31 +94,48 @@ pip3 install psycopg2
90
94
91
95
### PostgreSQL
92
96
93
-
Install Postgres. Create Postgres user:
97
+
Install Postgres. Create Postgres user/role:
94
98
95
-
```
99
+
```bash
100
+
su - postgres #switch to psql admin user
96
101
createuser -s api_user
97
102
```
98
103
99
-
Where `api_user` is a user who will run indexer service. (As example we create superuser. You can use your own grants.)
104
+
Where `api_user` is a user who will run the indexer service. (As example, we create a superuser. You can use your own grants.)
100
105
101
106
Create database `index` for Ethereum transaction index:
102
107
103
-
```
108
+
```sql
104
109
CREATEDATABASEindex;
105
110
```
106
111
107
-
Add tables into `index` using SQL script `create_table.sql`:
112
+
Add tables into `index` using SQL script `create_tables.sql`:
108
113
109
-
```
110
-
psql -f create_table.sql index
114
+
```bash
115
+
psql -f create_tables.sql index
111
116
```
112
117
113
-
Note, for case insensitive comparisons we use `citex` data type instead of `text`.
118
+
For case-insensitive comparisons, we use `citex` data type instead of `text`.
119
+
120
+
Create database indexes to request tx data fast. **It's better to allow this tool to store initial tx data until the current block first, and then create these indexes. Filling initial tx data will be faster this way.**
121
+
122
+
Create recommended database indexes:
123
+
124
+
```bash
125
+
psql -f create_indexes.sql index
126
+
```
114
127
115
-
Remember to grant privileges to psql database and tables for users you need. Example:
128
+
Create additional database indexes:
116
129
130
+
```bash
131
+
psql -f create_indexes_add.sql index
117
132
```
133
+
134
+
Additional indexes cover more complex requests, such as getting Ethereum-only or specific token transactions for an address. [See Request examples](#api-request-examples).
135
+
136
+
Remember to grant privileges to psql database `index` and tables for users you need. Example:
137
+
138
+
```sql
118
139
\c index
119
140
GRANT ALL ON ethtxs TO api_user;
120
141
GRANT ALL ON aval TO api_user;
@@ -125,54 +146,55 @@ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO api_user;
125
146
126
147
### Ethereum transaction Indexer
127
148
128
-
`ethsync.py` is a script which makes Ethereum transaction index. It accepts the following env variables:
149
+
`ethsync.py` is a script that makes an Ethereum transaction index. It accepts the following env variables:
- ETH_URL: Ethereum node url to reach the node. Supports websocket, http and ipc. See examples in `ethsync.py`.
132
153
- START_BLOCK: the first block to synchronize from. Default is 1.
133
154
- CONFIRMATIONS_BLOCK: the number of blocks to leave out of the synch from the end. I.e., last block is current `blockNumber - CONFIRMATIONS_BLOCK`. Default is 0.
134
-
- PERIOD: Number of seconds between to synchronization. Default is 20 sec.
155
+
- PERIOD: Number of seconds between synchronization. Default is 20 sec.
135
156
- LOG_FILE: optional file path and name where s=to save logs. If not provided, use StreamHandler.
136
157
137
-
Indexer can fetch transactions not from the beginning, but from special block number `START_BLOCK`. It will speed up indexing process and reduce database size. For a reference:
158
+
The indexer can fetch transactions not from the beginning, but from a particular block number `START_BLOCK`. It will speed up the indexing process and reduce database size. For a reference:
138
159
139
-
- index size starting from 5,555,555 block to 9,000,000 is about 190 GB
140
-
- index size starting from 11,000,000 block to 12,230,000 is about 83 GB
141
-
- index size starting from 14,600,000 block to 15,100,000 is about 27 GB
160
+
- index size starting from 5,555,555 block to 9,000,000 (3.5 mln blocks) is about 190 GB
161
+
- index size starting from 11,000,000 block to 12,230,000 (1 mln blocks) is about 83 GB
162
+
- index size starting from 14,600,000 block to 15,100,000 (0.5 mln blocks) is about 27 GB
163
+
- index size starting from 14,600,000 block to 18,100,000 (3.5 mln blocks) with additional indexes is about 289 GB
142
164
143
-
At first start, Indexer will store transactions starting from the block you set. It will take a time. After that, it will check for new blocks every `PERIOD` seconds and update the index.
165
+
At first start, the Indexer will store transactions starting from the block you set. It will take time. After that, it will check for new blocks every `PERIOD` seconds and update the index.
We recommend to run Indexer script `ethsync.py` as a background service to make sure it will be restarted in case of failure. See `ethsync.service` as an example. Copy it to /lib/systemd/system/ethsync.service, update according to your settings, then register a service:
173
+
We recommend running the Indexer script `ethsync.py` as a background service to ensure it will be restarted in case of failure. See `ethsync.service` as an example. Copy it to /lib/systemd/system/ethsync.service, update according to your settings, then register a service:
152
174
153
175
```
154
176
systemctl start ethsync.service
155
177
systemctl enable ethsync.service
156
178
```
157
179
158
-
Note, indexing takes time. To check indexing process, get the last indexed block:
180
+
Note, that indexing takes time. To check the indexing process, get the last indexed block:
159
181
160
182
```
161
183
psql -d index -c 'SELECT MAX(block) FROM ethtxs;'
162
184
```
163
185
164
-
And compare to Ethereum node's best block.
186
+
And compare it to the Ethereum node's best block.
165
187
166
188
### Troubleshooting
167
189
168
-
To test connection from script, set a connection line in `ethtest.py`, and run it. In case of success, it will print current Ethereum's last block.
190
+
To test the connection from the script, set a connection line in `ethtest.py`, and run it. In case of success, it will print the current Ethereum's last block.
169
191
170
192
To test a connection to a Postgres database `index`, run `pgtest.py`.
171
193
172
194
### Transaction API with Postgrest
173
195
174
196
[Install and configure](https://postgrest.org/en/stable/install.html) Postgrest.
175
-
Here is an example to run API for user `api_user` connected to `index` database on 3000 port:
197
+
Here is an example of running API for user `api_user` connected to `index` database on the 3000 port:
176
198
177
199
```
178
200
db-uri = "postgres://api_user@/index"
@@ -191,7 +213,7 @@ Make sure you add Postgrest in crontab for autostart on reboot:
191
213
192
214
### Make Indexer's API public
193
215
194
-
If you need to provide public API, use any web server like nginx and setup proxy to Postgrest port in config:
216
+
If you need to provide public API, use any web server like nginx and set a proxy to Postgrest port in config:
195
217
196
218
```
197
219
location /ethtxs {
@@ -206,41 +228,48 @@ location /max_block {
206
228
207
229
```
208
230
209
-
This way endpoints will be available:
231
+
This way, endpoints will be available:
210
232
211
233
-`/ethtxs` used to fetch Ethereum transactions by address
212
-
-`/aval` returns status of service. Endpoint `aval` is a table with `status` field just to check API availability.
213
-
-`/max_block` returns max Ethereumindexed block
234
+
-`/aval` returns the status of service. Endpoint `aval` is a table with `status` field just to check API availability.
Get last 25 Ethereum transactions without ERC-20 transactions for address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98:
258
+
Get the last 25 Ethereum transactions without ERC-20 transactions for address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98:
237
259
238
260
```
239
261
curl -k -X GET "http://localhost:3000/ethtxs?and=(contract_to.eq.,or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
240
262
241
263
```
242
264
243
-
Get last 25 ERC-20 transactions without Ethereum transactions for address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98:
265
+
Get the last 25 USDT transactions for address 0xabfDF505fFd5587D9E7707dFB47F45AF1f03E275:
266
+
267
+
```
268
+
curl -k -X GET "http://localhost:3000/ethtxs?and=(txto.eq.0xdac17f958d2ee523a2206206994597c13d831ec7,or(txfrom.eq.0xabfDF505fFd5587D9E7707dFB47F45AF1f03E275,contract_to.eq.000000000000000000000000abfDF505fFd5587D9E7707dFB47F45AF1f03E275))&order=time.desc&limit=25"
269
+
270
+
```
271
+
272
+
Get the last 25 ERC-20 transactions without Ethereum transactions for address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98:
244
273
245
274
```
246
275
curl -k -X GET "http://localhost:3000/ethtxs?and=(contract_to.neq.,or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
0 commit comments