-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_schema.sh
executable file
·633 lines (552 loc) · 16.5 KB
/
create_schema.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#!/usr/bin/env bash
# Helper script for generating the `schema.sql` ClickHouse tables definition
# Specify a cluster name to add `ON CLUSTER` directives
show_usage() {
printf 'Usage: %s [(-o|--outfile) file (default: "schema.sql")] [(-c|--cluster) name (default: none)] [(-h|--help)]\n' "$(basename "$0")"
exit 0
}
SCHEMA_FILE="./schema.sql"
CLUSTER_NAME=""
while [[ "$#" -gt 0 ]]; do
case $1 in
-o|--outfile) SCHEMA_FILE="$2"; shift ;;
-c|--cluster) CLUSTER_NAME="$2"; shift ;;
-h|--help) show_usage ;;
*) echo "Unknown parameter passed: $1"; show_usage; exit 1 ;;
esac
shift
done
ON_CLUSTER_DIRECTIVE=""
ENGINE_DEFAULT="ReplacingMergeTree()"
ENGINE_VER="ReplacingMergeTree(ver)"
ENGINE_VER_DELETE="ReplacingMergeTree(ver, has_null_balance)"
if [ -n "$CLUSTER_NAME" ]; then
ON_CLUSTER_DIRECTIVE="ON CLUSTER \"$CLUSTER_NAME\""
ENGINE_DEFAULT="ReplicatedReplacingMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}')"
ENGINE_VER="ReplicatedReplacingMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}', ver)"
ENGINE_VER_DELETE="ReplicatedReplacingMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}', ver, has_null_balance)"
fi
cat > $SCHEMA_FILE <<- EOM
--------------------------------------
-- AUTO-GENERATED FILE, DO NOT EDIT --
--------------------------------------
-- This SQL file creates the required tables for a single Antelope chain.
-- You can use the ClickHouse client command to execute it:
-- $ cat schema.sql | clickhouse client -h <host> --port 9000 -d <database> -u <user> --password <password>
-------------------------------------------------
-- Meta tables to store Substreams information --
-------------------------------------------------
CREATE TABLE IF NOT EXISTS cursors $ON_CLUSTER_DIRECTIVE
(
id String,
cursor String,
block_num Int64,
block_id String
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (id)
ORDER BY (id);
-----------------------------------------------------------
-- Tables to store the raw events without any processing --
-----------------------------------------------------------
-- The table to store all transfers. This uses the trx_id as first primary key so we can use this table to do
-- transfer lookups based on a transaction id.
CREATE TABLE IF NOT EXISTS transfer_events $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
-- contract & scope --
contract String,
symcode String,
-- data payload --
from String,
to String,
quantity String,
memo String,
-- extras --
precision UInt32,
amount Int64,
value Float64,
-- meta --
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (trx_id, action_index)
ORDER BY (trx_id, action_index);
-- The table to store all account balance changes from the database operations. This uses the account and block_num as
-- first primary keys so we can use this table to lookup the account balance from a certain block number.
CREATE TABLE IF NOT EXISTS balance_change_events $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
-- contract & scope --
contract String,
symcode String,
-- data payload --
account String,
balance String,
balance_delta Int64,
-- extras --
precision UInt32,
amount Int64,
value Float64,
-- meta --
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (account, block_num, trx_id, action_index)
ORDER BY (account, block_num, trx_id, action_index);
-- The table to store all token supply changes from the database operations. This uses the account and block_num as
-- first primary keys so we can use this table to lookup token supplies from a certain block number.
CREATE TABLE IF NOT EXISTS supply_change_events $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
-- contract & scope --
contract String,
symcode String,
-- data payload --
issuer String,
max_supply String,
supply String,
supply_delta Int64,
-- extras --
precision UInt32,
amount Int64,
value Float64,
-- meta --
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (contract, block_num, trx_id, action_index)
ORDER BY (contract, block_num, trx_id, action_index);
-- Table to contain all 'eosio.token:issue' transactions
CREATE TABLE IF NOT EXISTS issue_events $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
-- contract & scope --
contract String,
symcode String,
-- data payload --
issuer String,
to String,
quantity String,
memo String,
-- extras --
precision UInt32,
amount Int64,
value Float64,
-- meta --
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (contract, symcode, to, amount, trx_id, action_index)
ORDER BY (contract, symcode, to, amount, trx_id, action_index);
-- Table to contain all 'eosio.token:retire' transactions --
CREATE TABLE IF NOT EXISTS retire_events $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
-- contract & scope --
contract String,
symcode String,
-- data payload --
from String,
quantity String,
memo String,
-- extras --
precision UInt32,
amount Int64,
value Float64,
-- meta --
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (contract, symcode, amount, trx_id, action_index)
ORDER BY (contract, symcode, amount, trx_id, action_index);
-- Table to contain all 'eosio.token:create' transactions
CREATE TABLE IF NOT EXISTS create_events $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
-- contract & scope --
contract String,
symcode String,
-- data payload --
issuer String,
maximum_supply String,
-- extras --
precision UInt32,
amount Int64,
value Float64,
-- meta --
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (contract, symcode, trx_id, action_index)
ORDER BY (contract, symcode, trx_id, action_index);
-----------------------------------------------
-- Tables to store the extracted information --
-----------------------------------------------
-- Table to store up to date balances per account and token
CREATE TABLE IF NOT EXISTS account_balances $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
account String,
balance String,
balance_delta Int64,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime,
ver UInt64
)
ENGINE = $ENGINE_VER
PRIMARY KEY (account, contract, symcode)
ORDER BY (account, contract, symcode);
CREATE MATERIALIZED VIEW IF NOT EXISTS account_balances_mv $ON_CLUSTER_DIRECTIVE
TO account_balances
AS
SELECT *,
(block_num + action_index) AS ver
FROM balance_change_events;
-- Table to store historical balances per account and token
CREATE TABLE IF NOT EXISTS historical_account_balances $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
account String,
balance String,
balance_delta Int64,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime,
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (block_num, account, contract, symcode)
ORDER BY (block_num, account, contract, symcode);
CREATE MATERIALIZED VIEW IF NOT EXISTS historical_account_balances_mv $ON_CLUSTER_DIRECTIVE
TO historical_account_balances
AS
SELECT *
FROM balance_change_events;
-- Table to store up to date positive balances per account and token for token holders
CREATE TABLE IF NOT EXISTS token_holders $ON_CLUSTER_DIRECTIVE
(
action_index UInt32,
contract String,
symcode String,
account String,
value Float64,
block_num UInt64,
has_null_balance UInt8,
ver UInt64
)
ENGINE = $ENGINE_VER_DELETE
PRIMARY KEY (has_null_balance, contract, symcode, account)
ORDER BY (has_null_balance, contract, symcode, account);
CREATE MATERIALIZED VIEW IF NOT EXISTS token_holders_mv $ON_CLUSTER_DIRECTIVE
TO token_holders
AS
SELECT action_index,
contract,
symcode,
account,
value,
block_num,
if(amount > 0, 0, 1) AS has_null_balance,
(block_num + action_index) AS ver
FROM balance_change_events;
-- Table to store up to date token supplies
CREATE TABLE IF NOT EXISTS token_supplies $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
issuer String,
max_supply String,
supply String,
supply_delta Int64,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime,
ver UInt64
)
ENGINE = $ENGINE_VER
PRIMARY KEY (contract, symcode, issuer)
ORDER BY (contract, symcode, issuer);
CREATE MATERIALIZED VIEW IF NOT EXISTS token_supplies_mv $ON_CLUSTER_DIRECTIVE
TO token_supplies
AS
SELECT *,
(block_num + action_index) AS ver
FROM supply_change_events;
-- Table to store historical token supplies per token
CREATE TABLE IF NOT EXISTS historical_token_supplies $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
issuer String,
max_supply String,
supply String,
supply_delta Int64,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime,
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (block_num, contract, symcode, issuer)
ORDER BY (block_num, contract, symcode, issuer);
CREATE MATERIALIZED VIEW IF NOT EXISTS historical_token_supplies_mv $ON_CLUSTER_DIRECTIVE
TO historical_token_supplies
AS
SELECT *
FROM supply_change_events;
-- Table to store token transfers primarily indexed by the 'contract' field --
CREATE TABLE IF NOT EXISTS transfers_contract $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
from String,
to String,
quantity String,
memo String,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (contract, symcode, trx_id, action_index)
ORDER BY (contract, symcode, trx_id, action_index);
CREATE MATERIALIZED VIEW IF NOT EXISTS transfers_contract_mv $ON_CLUSTER_DIRECTIVE
TO transfers_contract
AS
SELECT trx_id,
action_index,
contract,
symcode,
from,
to,
quantity,
memo,
precision,
amount,
value,
block_num,
timestamp
FROM transfer_events;
-- Table to store token transfers primarily indexed by the 'from' field --
CREATE TABLE IF NOT EXISTS transfers_from $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
from String,
to String,
quantity String,
memo String,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (from, to, contract, symcode, trx_id, action_index)
ORDER BY (from, to, contract, symcode, trx_id, action_index);
CREATE MATERIALIZED VIEW IF NOT EXISTS transfers_from_mv $ON_CLUSTER_DIRECTIVE
TO transfers_from
AS
SELECT trx_id,
action_index,
contract,
symcode,
from,
to,
quantity,
memo,
precision,
amount,
value,
block_num,
timestamp
FROM transfer_events;
-- Table to store historical token transfers 'from' address --
CREATE TABLE IF NOT EXISTS historical_transfers_from $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
from String,
to String,
quantity String,
memo String,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (block_num, from, to, contract, symcode, trx_id, action_index)
ORDER BY (block_num, from, to, contract, symcode, trx_id, action_index);
CREATE MATERIALIZED VIEW IF NOT EXISTS historical_transfers_from_mv $ON_CLUSTER_DIRECTIVE
TO historical_transfers_from
AS
SELECT trx_id,
action_index,
contract,
symcode,
from,
to,
quantity,
memo,
precision,
amount,
value,
block_num,
timestamp
FROM transfer_events;
-- Table to store token transfers primarily indexed by the 'to' field --
CREATE TABLE IF NOT EXISTS transfers_to $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
from String,
to String,
quantity String,
memo String,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (to, contract, symcode, trx_id, action_index)
ORDER BY (to, contract, symcode, trx_id, action_index);
CREATE MATERIALIZED VIEW IF NOT EXISTS transfers_to_mv $ON_CLUSTER_DIRECTIVE
TO transfers_to
AS
SELECT trx_id,
action_index,
contract,
symcode,
from,
to,
quantity,
memo,
precision,
amount,
value,
block_num,
timestamp
FROM transfer_events;
-- Table to store historical token transfers 'to' address --
CREATE TABLE IF NOT EXISTS historical_transfers_to $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
from String,
to String,
quantity String,
memo String,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (block_num, to, contract, symcode, trx_id, action_index)
ORDER BY (block_num, to, contract, symcode, trx_id, action_index);
CREATE MATERIALIZED VIEW IF NOT EXISTS historical_transfers_to_mv $ON_CLUSTER_DIRECTIVE
TO historical_transfers_to
AS
SELECT trx_id,
action_index,
contract,
symcode,
from,
to,
quantity,
memo,
precision,
amount,
value,
block_num,
timestamp
FROM transfer_events;
-- Table to store token transfers primarily indexed by the 'block_num' field
CREATE TABLE IF NOT EXISTS transfers_block_num $ON_CLUSTER_DIRECTIVE
(
trx_id String,
action_index UInt32,
contract String,
symcode String,
from String,
to String,
quantity String,
memo String,
precision UInt32,
amount Int64,
value Float64,
block_num UInt64,
timestamp DateTime
)
ENGINE = $ENGINE_DEFAULT
PRIMARY KEY (block_num, contract, symcode, trx_id, action_index)
ORDER BY (block_num, contract, symcode, trx_id, action_index);
CREATE MATERIALIZED VIEW IF NOT EXISTS transfers_block_num_mv $ON_CLUSTER_DIRECTIVE
TO transfers_block_num
AS
SELECT trx_id,
action_index,
contract,
symcode,
from,
to,
quantity,
memo,
precision,
amount,
value,
block_num,
timestamp
FROM transfer_events;
EOM
echo "[+] Created '$SCHEMA_FILE'"
echo "[*] Run the following command to apply:"
echo "cat $SCHEMA_FILE | clickhouse client -h <host> --port 9000 -d <database> -u <user> --password <password>"