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
@@ -53,36 +53,37 @@ To get Ethereum transactions by address, Postgrest is used. It provides RESTful
53
53
After index is created, you can use requests like
54
54
55
55
```
56
-
curl -k -X GET "http://localhost:3000/?and=(contract_to.eq.,or(txfrom.eq.0x6b924750e56a674a2ad01fbf09c7c9012f16f094,txto.eq.0x6b924750e56a674a2ad01fbf09c7c9012f16f094))&order=time.desc&limit=25"
56
+
curl -k -X GET "http://localhost:3000/?and=(contract_to.eq.,or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
57
57
```
58
58
59
-
The request will show 25 last transactions for Ethereum address 0x6b924750e56a674a2ad01fbf09c7c9012f16f094, ordered by timestamp. For API reference, see [Postgrest](https://postgrest.org/en/v5.2/api.html).
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).
60
60
61
61
# Ethereum Indexer Setup
62
62
63
63
## Prerequisites
64
64
65
-
-geth or openethereum (with currently synchronized chain)
66
-
- Python 3.6
67
-
- Postgresql 10.5
65
+
-Ethereum node with RPC API enabled: Geth, Nethermind, etc.
66
+
- Python 3
67
+
- Postgresql
68
68
- Postgrest for API
69
69
- nginx or other web server (in case of public API)
70
70
71
71
## Installation
72
72
73
73
### Ethereum Node
74
74
75
-
Make sure your Ethereum node is installed and is fully synced. In case of openethereum, you can check its API and best block height with the command:
75
+
Make sure your Ethereum node is installed and is fully synced. You can check its API and best block height with the command:
76
76
77
77
```
78
78
curl --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST localhost:8545
79
79
```
80
80
81
81
### Python modules
82
82
83
-
Install Python. Install python modules:
83
+
Install Python 3. Install python modules:
84
84
85
85
```
86
+
apt install python3-pip
86
87
pip3 install web3
87
88
pip3 install psycopg2
88
89
```
@@ -92,27 +93,23 @@ pip3 install psycopg2
92
93
Install Postgres. Create Postgres user:
93
94
94
95
```
95
-
createuser -s <yourusername>
96
+
createuser -s api_user
96
97
```
97
98
98
-
`<yourusername>` — user who will run service.
99
-
100
-
(As example we create superuser. You can use your own grants.)
99
+
Where `api_user` is a user who will run indexer service. (As example we create superuser. You can use your own grants.)
Add tables into `index`using SQL script `create_table.sql`:
109
108
110
109
```
111
-
psql -f create_table.sql <yourDB>
110
+
psql -f create_table.sql index
112
111
```
113
112
114
-
`<yourDB>` — target Postgres database.
115
-
116
113
Note, for case insensitive comparisons we use `citex` data type instead of `text`.
117
114
118
115
Remember to grant privileges to psql database and tables for users you need. Example:
@@ -128,56 +125,34 @@ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO api_user;
128
125
129
126
### Ethereum transaction Indexer
130
127
131
-
`ethsync.py` is a script which makes Ethereum transactions index to get transactions by ETH address using API.
132
-
133
-
Log is stored in `/var/log/ethindexer.log`.
134
-
135
-
By default, Indexer connects to [Openethereum](https://github.com/openethereum/openethereum) node. You can also connect to other Ethereum node like geth:
Indexer can fetch transactions not from the beginning, but from special block number. It will speed up indexing process and reduce database size. If you want to indicate starting Ethereum block number, set it instead of default `10000000` value.
146
-
147
-
```python
148
-
if maxblockindb isNone:
149
-
maxblockindb =10000000
150
-
```
151
-
152
-
For a reference:
128
+
`ethsync.py` is a script which makes Ethereum transaction index. It accepts the following env variables:
153
129
154
-
- index size starting from 5,555,555 block to 9,000,000 is about 190 GB.
155
-
- index size starting from 11,000,000 block to 12,230,000 is about 83 GB.
156
-
157
-
First Indexer will store transactions starting from block you set. It will take a time. After that, it will check for new blocks every 20 seconds and update the index. If you want to change the interval, change the line:
- ETH_URL: Ethereum node url to reach the node. Supports websocket, http and ipc. See examples in `ethsync.py`.
132
+
- START_BLOCK: the first block to synchronize from. Default is 1.
133
+
- 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.
135
+
- LOG_FILE: optional file path and name where s=to save logs. If not provided, use StreamHandler.
158
136
159
-
```
160
-
time.sleep(20)
161
-
```
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:
162
138
163
-
To run the Indexer:
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
164
142
165
-
```
166
-
python3.6 you/path/to/script/ethsync.py <yourDB>
167
-
```
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.
168
144
169
-
We recommend to run Indexer script `ethsync.py` as a background service. See `ethstorage.service` as an example. Before run, update the line:
Put the file to `/lib/systemd/system`. Then register a service:
151
+
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:
176
152
177
153
```
178
-
systemctl daemon-reload
179
-
systemctl start ethstorage.service
180
-
systemctl enable ethstorage.service
154
+
systemctl start ethsync.service
155
+
systemctl enable ethsync.service
181
156
```
182
157
183
158
Note, indexing takes time. To check indexing process, get the last indexed block:
@@ -188,9 +163,15 @@ psql -d index -c 'SELECT MAX(block) FROM ethtxs;'
188
163
189
164
And compare to Ethereum node's best block.
190
165
166
+
### Troubleshooting
167
+
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.
169
+
170
+
To test a connection to a Postgres database `index`, run `pgtest.py`.
171
+
191
172
### Transaction API with Postgrest
192
173
193
-
Install and [configure](https://postgrest.org/en/v5.2/install.html#configuration) Postgrest.
174
+
[Install and configure](https://postgrest.org/en/stable/install.html) Postgrest.
194
175
Here is an example to run API for user `api_user` connected to `index` database on 3000 port:
195
176
196
177
```
@@ -208,7 +189,7 @@ Make sure you add Postgrest in crontab for autostart on reboot:
208
189
@reboot cd /usr/share && /usr/bin/postgrest ./postgrest.conf
209
190
```
210
191
211
-
## Make Indexer's API public
192
+
###Make Indexer's API public
212
193
213
194
If you need to provide public API, use any web server like nginx and setup proxy to Postgrest port in config:
214
195
@@ -225,62 +206,57 @@ location /max_block {
225
206
226
207
```
227
208
228
-
This way two endpoints will be available:
209
+
This way endpoints will be available:
229
210
230
211
-`/ethtxs` used to fetch Ethereum transactions by address
231
212
-`/aval` returns status of service. Endpoint `aval` is a table with `status` field just to check API availability.
232
213
-`/max_block` returns max Ethereum indexed block
233
214
234
-
## API request examples
235
-
236
-
Get last 25 Ethereum transactions without ERC-20 transactions for address 0x1143e097e134f3407ef6b088672ccece9a4f8cdd:
curl -k -X GET "http://localhost:3000/ethtxs?and=(contract_to.eq.,or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
254
240
255
241
```
256
242
257
-
Zabbix API availability trigger examples:
243
+
Get last 25 ERC-20 transactions without Ethereum transactions for address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98:
258
244
259
245
```
260
-
{API for Ethereum transactions:system.run["curl -s https://ethnode1.adamant.im/aval | jq .[].status"].last()}<>1
246
+
curl -k -X GET "http://localhost:3000/ethtxs?and=(contract_to.neq.,or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
Get last 25 transactions for both ERC-20 and Ethereum for address 0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98:
267
251
268
-
In the `docker-compose.yml` you find a configuration that show how this tool can be embedded in a docker configuration with the following processes:
269
-
- postgres db: to store the indexed data
270
-
- postgREST tool to expose the data as a REST api (see above comments)
271
-
- GETH node in POA mode. Can be Openethereum, or another node, but not tested
272
-
- EthSync tool (this tool)
252
+
```
253
+
curl -k -X GET "http://localhost:3000/ethtxs?and=(or(txfrom.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98,txto.eq.0xFBb1b73C4f0BDa4f67dcA266ce6Ef42f520fBB98))&order=time.desc&limit=25"
273
254
274
-
The EthSync tool accepts the following env variables:
275
-
- DB_NAME: postgres url of the db
276
-
- ETH_URL: eth node url to reach the node. Supports websocket, http and ipc.
277
-
- START_BLOCK: the first block to synchronize from. Default is 1.
278
-
- 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.
279
-
- PERIOD: Number of seconds between to synchronization. Default is 20 sec.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
0 commit comments