-
Notifications
You must be signed in to change notification settings - Fork 7
/
etl-schema.sql
2413 lines (1649 loc) · 69.5 KB
/
etl-schema.sql
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 10.17 (Ubuntu 10.17-0ubuntu0.18.04.1)
-- Dumped by pg_dump version 10.17 (Ubuntu 10.17-0ubuntu0.18.04.1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: topology; Type: SCHEMA; Schema: -; Owner: etl
--
CREATE SCHEMA topology;
ALTER SCHEMA topology OWNER TO etl;
--
-- Name: SCHEMA topology; Type: COMMENT; Schema: -; Owner: etl
--
COMMENT ON SCHEMA topology IS 'PostGIS Topology schema';
--
-- Name: plpgsql; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
--
-- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
--
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public;
--
-- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pg_stat_statements IS 'track execution statistics of all SQL statements executed';
--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;
--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';
--
-- Name: postgis; Type: EXTENSION; Schema: -; Owner:
--
CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
--
-- Name: EXTENSION postgis; Type: COMMENT; Schema: -; Owner:
--
COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions';
--
-- Name: burn_type; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.burn_type AS ENUM (
'fee',
'state_channel',
'assert_location',
'add_gateway',
'oui',
'routing'
);
ALTER TYPE public.burn_type OWNER TO etl;
--
-- Name: gateway_mode; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.gateway_mode AS ENUM (
'full',
'light',
'dataonly'
);
ALTER TYPE public.gateway_mode OWNER TO etl;
--
-- Name: gateway_status_online; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.gateway_status_online AS ENUM (
'online',
'offline'
);
ALTER TYPE public.gateway_status_online OWNER TO etl;
--
-- Name: packet_entry; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.packet_entry AS (
client text,
type text,
num_packets bigint,
num_dcs bigint
);
ALTER TYPE public.packet_entry OWNER TO etl;
--
-- Name: pending_transaction_nonce_type; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.pending_transaction_nonce_type AS ENUM (
'balance',
'security',
'none',
'gateway'
);
ALTER TYPE public.pending_transaction_nonce_type OWNER TO etl;
--
-- Name: pending_transaction_status; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.pending_transaction_status AS ENUM (
'received',
'pending',
'failed',
'cleared'
);
ALTER TYPE public.pending_transaction_status OWNER TO etl;
--
-- Name: reward_entry; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.reward_entry AS (
account text,
gateway text,
type text,
amount bigint
);
ALTER TYPE public.reward_entry OWNER TO etl;
--
-- Name: transaction_actor_role; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.transaction_actor_role AS ENUM (
'payee',
'payer',
'owner',
'gateway',
'reward_gateway',
'challenger',
'challengee',
'witness',
'consensus_member',
'escrow',
'sc_opener',
'sc_closer',
'packet_receiver',
'oracle',
'router',
'validator',
'consensus_failure_member',
'consensus_failure_failed_member'
);
ALTER TYPE public.transaction_actor_role OWNER TO etl;
--
-- Name: transaction_type; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.transaction_type AS ENUM (
'coinbase_v1',
'security_coinbase_v1',
'oui_v1',
'gen_gateway_v1',
'routing_v1',
'payment_v1',
'security_exchange_v1',
'consensus_group_v1',
'add_gateway_v1',
'assert_location_v1',
'create_htlc_v1',
'redeem_htlc_v1',
'poc_request_v1',
'poc_receipts_v1',
'vars_v1',
'rewards_v1',
'token_burn_v1',
'dc_coinbase_v1',
'token_burn_exchange_rate_v1',
'payment_v2',
'state_channel_open_v1',
'state_channel_close_v1',
'price_oracle_v1',
'transfer_hotspot_v1',
'rewards_v2',
'assert_location_v2',
'gen_validator_v1',
'stake_validator_v1',
'unstake_validator_v1',
'validator_heartbeat_v1',
'transfer_validator_stake_v1',
'gen_price_oracle_v1',
'consensus_group_failure_v1'
);
ALTER TYPE public.transaction_type OWNER TO etl;
--
-- Name: validator_status_online; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.validator_status_online AS ENUM (
'online',
'offline'
);
ALTER TYPE public.validator_status_online OWNER TO etl;
--
-- Name: validator_status_type; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.validator_status_type AS ENUM (
'staked',
'cooldown',
'unstaked'
);
ALTER TYPE public.validator_status_type OWNER TO etl;
--
-- Name: var_type; Type: TYPE; Schema: public; Owner: etl
--
CREATE TYPE public.var_type AS ENUM (
'integer',
'float',
'atom',
'binary'
);
ALTER TYPE public.var_type OWNER TO etl;
--
-- Name: account_inventory_update(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.account_inventory_update() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN insert into account_inventory (address, balance, nonce, dc_balance, dc_nonce, security_balance, security_nonce, staked_balance, first_block, last_block) VALUES (NEW.address, NEW.balance, NEW.nonce, NEW.dc_balance, NEW.dc_nonce, NEW.security_balance, NEW.security_nonce, NEW.staked_balance, NEW.block, NEW.block ) ON CONFLICT (address) DO UPDATE SET balance = EXCLUDED.balance, nonce = EXCLUDED.nonce, dc_balance = EXCLUDED.dc_balance, dc_nonce = EXCLUDED.dc_nonce, security_balance = EXCLUDED.security_balance, security_nonce = EXCLUDED.security_nonce, staked_balance = EXCLUDED.staked_balance, last_block = EXCLUDED.last_block; RETURN NEW; END; $$;
ALTER FUNCTION public.account_inventory_update() OWNER TO etl;
--
-- Name: gateway_inventory_on_insert(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.gateway_inventory_on_insert() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN UPDATE gateway_inventory SET payer = ( select fields->>'payer' from transaction_actors a inner join transactions t on a.transaction_hash = t.hash and a.actor = NEW.address and a.actor_role = 'gateway' and a.block = NEW.first_block limit 1 ) where address = NEW.address; RETURN NEW; END; $$;
ALTER FUNCTION public.gateway_inventory_on_insert() OWNER TO etl;
--
-- Name: gateway_inventory_update(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.gateway_inventory_update() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN insert into gateway_inventory as g (address, name, owner, location, last_poc_challenge, last_poc_onion_key_hash, witnesses, nonce, first_block, last_block, first_timestamp, reward_scale, elevation, gain, location_hex, mode) VALUES (NEW.address, NEW.name, NEW.owner, NEW.location, NEW.last_poc_challenge, NEW.last_poc_onion_key_hash, NEW.witnesses, NEW.nonce, NEW.block, NEW.block, to_timestamp(NEW.time), NEW.reward_scale, NEW.elevation, NEW.gain, NEW.location_hex, NEW.mode ) ON CONFLICT (address) DO UPDATE SET owner = EXCLUDED.owner, location = EXCLUDED.location, last_poc_challenge = EXCLUDED.last_poc_challenge, last_poc_onion_key_hash = EXCLUDED.last_poc_onion_key_hash, witnesses = EXCLUDED.witnesses, nonce = EXCLUDED.nonce, last_block = EXCLUDED.last_block, reward_scale = COALESCE(EXCLUDED.reward_scale, g.reward_scale), elevation = EXCLUDED.elevation, gain = EXCLUDED.gain, location_hex = EXCLUDED.location_hex; RETURN NEW; END; $$;
ALTER FUNCTION public.gateway_inventory_update() OWNER TO etl;
--
-- Name: get_avg_version_penalties(integer, integer, text); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.get_avg_version_penalties(integer, integer, text) RETURNS integer
LANGUAGE plpgsql
AS $$
declare
total integer;
begin
select 1 into total;
return total;
end;
$$;
ALTER FUNCTION public.get_avg_version_penalties(integer, integer, text) OWNER TO etl;
--
-- Name: insert_packets(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.insert_packets() RETURNS void
LANGUAGE plpgsql
AS $$ declare txn RECORD; begin for txn in select * from transactions where type = 'state_channel_close_v1' order by block asc loop insert into packets (block, transaction_hash, time, gateway, num_packets, num_dcs) select txn.block, txn.hash, txn.time, client as gateway, sum(num_packets)::bigint as num_packets, sum(num_dcs)::bigint as num_dcs from jsonb_populate_recordset(null::packet_entry, txn.fields#>'{state_channel, summaries}') group by client; end loop; end; $$;
ALTER FUNCTION public.insert_packets() OWNER TO etl;
--
-- Name: insert_rewards(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.insert_rewards() RETURNS void
LANGUAGE plpgsql
AS $$ declare txn RECORD; begin for txn in select * from transactions where type = 'rewards_v1' order by block asc loop insert into rewards (block, transaction_hash, time, account, gateway, amount) select txn.block, txn.hash, txn.time, account, coalesce(gateway, '1Wh4bh') as gateway, sum(amount)::bigint as amount from jsonb_populate_recordset(null::reward_entry, txn.fields->'rewards') group by (account, gateway); end loop; end; $$;
ALTER FUNCTION public.insert_rewards() OWNER TO etl;
--
-- Name: last_agg(anyelement, anyelement); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.last_agg(anyelement, anyelement) RETURNS anyelement
LANGUAGE sql IMMUTABLE STRICT
AS $_$ SELECT $2; $_$;
ALTER FUNCTION public.last_agg(anyelement, anyelement) OWNER TO etl;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: locations; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.locations (
location text NOT NULL,
long_street text,
short_street text,
long_city text,
short_city text,
long_state text,
short_state text,
long_country text,
short_country text,
search_city text,
city_id text,
geometry public.geometry(Point,4326)
);
ALTER TABLE public.locations OWNER TO etl;
--
-- Name: location_city_id(public.locations); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.location_city_id(l public.locations) RETURNS text
LANGUAGE plpgsql
AS $$ begin return lower(coalesce(l.long_city, '') || coalesce(l.long_state, '') || coalesce(l.long_country, '')); end; $$;
ALTER FUNCTION public.location_city_id(l public.locations) OWNER TO etl;
--
-- Name: location_city_id_update(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.location_city_id_update() RETURNS trigger
LANGUAGE plpgsql
AS $$ begin NEW.city_id := location_city_id(NEW); return NEW; end; $$;
ALTER FUNCTION public.location_city_id_update() OWNER TO etl;
--
-- Name: location_city_words(public.locations); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.location_city_words(l public.locations) RETURNS text
LANGUAGE plpgsql
AS $$ begin return (select string_agg(word, ' ' order by rn) from (select word, min(rn) as rn from regexp_split_to_table( lower( coalesce(l.long_city, '') || ' ' || coalesce(l.short_city, '') || ' ' || coalesce(l.long_state, '') || ' ' || coalesce(l.short_state, '') || ' ' || coalesce(l.long_country, '') || ' ' || coalesce(l.short_country, '') || ' ' ) , '\s' ) with ordinality x(word, rn) where length(word) >= 3 group by word) x); end; $$;
ALTER FUNCTION public.location_city_words(l public.locations) OWNER TO etl;
--
-- Name: location_search_city_update(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.location_search_city_update() RETURNS trigger
LANGUAGE plpgsql
AS $$ begin NEW.search_city := location_city_words(NEW); return NEW; end; $$;
ALTER FUNCTION public.location_search_city_update() OWNER TO etl;
--
-- Name: oui_inventory_update(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.oui_inventory_update() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN insert into oui_inventory (oui, owner, nonce, addresses, subnets, first_block, last_block) VALUES (NEW.oui, NEW.owner, NEW.nonce, NEW.addresses, NEW.subnets, NEW.block, NEW.block) ON CONFLICT (oui) DO UPDATE SET owner = EXCLUDED.owner, nonce = EXCLUDED.nonce, addresses = EXCLUDED.addresses, subnets = EXCLUDED.subnets, last_block = EXCLUDED.last_block; RETURN NEW; END; $$;
ALTER FUNCTION public.oui_inventory_update() OWNER TO etl;
--
-- Name: state_channel_counts(public.transaction_type, jsonb); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.state_channel_counts(type public.transaction_type, fields jsonb, OUT num_packets numeric, OUT num_dcs numeric) RETURNS record
LANGUAGE plpgsql
AS $$ begin case when type = 'state_channel_close_v1' then select into num_packets, num_dcs sum(x.num_packets), sum(x.num_dcs) from jsonb_to_recordset(fields#>'{state_channel, summaries}') as x(owner TEXT, client TEXT, num_dcs BIGINT, location TEXT, num_packets BIGINT); else num_packets := 0; num_dcs := 0; end case; end; $$;
ALTER FUNCTION public.state_channel_counts(type public.transaction_type, fields jsonb, OUT num_packets numeric, OUT num_dcs numeric) OWNER TO etl;
--
-- Name: trigger_set_updated_at(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.trigger_set_updated_at() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN NEW.updated_at = NOW(); RETURN NEW; END; $$;
ALTER FUNCTION public.trigger_set_updated_at() OWNER TO etl;
--
-- Name: txn_filter_account_activity(text, public.transaction_type, jsonb); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.txn_filter_account_activity(acc text, type public.transaction_type, fields jsonb) RETURNS jsonb
LANGUAGE plpgsql
AS $$ begin case when type = 'rewards_v1' then return jsonb_set(fields, '{rewards}', (select jsonb_agg(x) from jsonb_to_recordset(fields#>'{rewards}') as x(account text, amount bigint, type text, gateway text) where account = acc)); when type = 'payment_v2' then if fields#>'{payer}' = acc then return fields; else return jsonb_set(fields, '{payees}', (select jsonb_agg(x) from jsonb_to_recordset(fields#>'{payees}') as x(payee text, amount bigint) where payee = acc)); end if; else return fields; end case; end; $$;
ALTER FUNCTION public.txn_filter_account_activity(acc text, type public.transaction_type, fields jsonb) OWNER TO etl;
--
-- Name: txn_filter_actor_activity(text, public.transaction_type, jsonb); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.txn_filter_actor_activity(actor text, type public.transaction_type, fields jsonb) RETURNS jsonb
LANGUAGE plpgsql
AS $$ begin case when type = 'rewards_v1' then return jsonb_set(fields, '{rewards}', (select jsonb_agg(x) from jsonb_to_recordset(fields#>'{rewards}') as x(account text, amount bigint, type text, gateway text) where account = actor or gateway = actor)); when type = 'rewards_v2' then return jsonb_set(fields, '{rewards}', (select jsonb_agg(x) from jsonb_to_recordset(fields#>'{rewards}') as x(account text, amount bigint, type text, gateway text) where account = actor or gateway = actor)); when type = 'state_channel_close_v1' then return jsonb_set(fields, '{state_channel,summaries}', coalesce((select jsonb_agg(x) from jsonb_to_recordset(fields#>'{state_channel,summaries}') as x(owner text, num_packets bigint, num_dcs bigint, location text, client text) where owner = actor or client = actor), '[]')); when type = 'payment_v2' then if fields->>'payer' = actor then return fields; else return jsonb_set(fields, '{payments}', (select jsonb_agg(x) from jsonb_to_recordset(fields#>'{payments}') as x(payee text, amount bigint) where payee = actor)); end if; when type = 'consensus_group_v1' then return fields - 'proof'; else return fields; end case; end; $$;
ALTER FUNCTION public.txn_filter_actor_activity(actor text, type public.transaction_type, fields jsonb) OWNER TO etl;
--
-- Name: txn_filter_gateway_activity(text, public.transaction_type, jsonb); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.txn_filter_gateway_activity(gw text, type public.transaction_type, fields jsonb) RETURNS jsonb
LANGUAGE plpgsql
AS $$ begin case when type = 'rewards_v1' then return jsonb_set(fields, '{rewards}', (select jsonb_agg(x) from jsonb_to_recordset(fields#>'{rewards}') as x(account text, amount bigint, type text, gateway text) where gateway = gw)); when type = 'consensus_group_v1' then return fields - 'proof'; else return fields; end case; end; $$;
ALTER FUNCTION public.txn_filter_gateway_activity(gw text, type public.transaction_type, fields jsonb) OWNER TO etl;
--
-- Name: validator_inventory_update(); Type: FUNCTION; Schema: public; Owner: etl
--
CREATE FUNCTION public.validator_inventory_update() RETURNS trigger
LANGUAGE plpgsql
AS $$ BEGIN insert into validator_inventory (address, name, owner, stake, status, nonce, last_heartbeat, version_heartbeat, penalty, penalties, first_block, last_block) VALUES (NEW.address, NEW.name, NEW.owner, NEW.stake, NEW.status, NEW.nonce, NEW.last_heartbeat, NEW.version_heartbeat, NEW.penalty, NEW.penalties, NEW.block, NEW.block ) ON CONFLICT (address) DO UPDATE SET stake = EXCLUDED.stake, status = EXCLUDED.status, owner = EXCLUDED.owner, nonce = EXCLUDED.nonce, last_heartbeat = EXCLUDED.last_heartbeat, version_heartbeat = EXCLUDED.version_heartbeat, penalty = EXCLUDED.penalty, penalties = EXCLUDED.penalties, last_block = EXCLUDED.last_block; RETURN NEW; END; $$;
ALTER FUNCTION public.validator_inventory_update() OWNER TO etl;
--
-- Name: jsonb_merge_agg(jsonb); Type: AGGREGATE; Schema: public; Owner: etl
--
CREATE AGGREGATE public.jsonb_merge_agg(jsonb) (
SFUNC = jsonb_concat,
STYPE = jsonb,
INITCOND = '{}'
);
ALTER AGGREGATE public.jsonb_merge_agg(jsonb) OWNER TO etl;
--
-- Name: last(anyelement); Type: AGGREGATE; Schema: public; Owner: etl
--
CREATE AGGREGATE public.last(anyelement) (
SFUNC = public.last_agg,
STYPE = anyelement
);
ALTER AGGREGATE public.last(anyelement) OWNER TO etl;
--
-- Name: __migrations; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.__migrations (
id character varying(255) NOT NULL,
datetime timestamp without time zone DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE public.__migrations OWNER TO etl;
--
-- Name: account_inventory; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.account_inventory (
address text NOT NULL,
balance bigint NOT NULL,
nonce bigint NOT NULL,
dc_balance bigint NOT NULL,
dc_nonce bigint NOT NULL,
security_balance bigint NOT NULL,
security_nonce bigint NOT NULL,
first_block bigint,
last_block bigint,
staked_balance bigint
);
ALTER TABLE public.account_inventory OWNER TO etl;
--
-- Name: accounts; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.accounts (
block bigint NOT NULL,
address text NOT NULL,
dc_balance bigint DEFAULT 0 NOT NULL,
dc_nonce bigint DEFAULT 0 NOT NULL,
security_balance bigint DEFAULT 0 NOT NULL,
security_nonce bigint DEFAULT 0 NOT NULL,
balance bigint DEFAULT 0 NOT NULL,
nonce bigint DEFAULT 0 NOT NULL,
staked_balance bigint
);
ALTER TABLE public.accounts OWNER TO etl;
--
-- Name: block_signatures; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.block_signatures (
block bigint NOT NULL,
signer text NOT NULL,
signature text NOT NULL
);
ALTER TABLE public.block_signatures OWNER TO etl;
--
-- Name: blocks; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.blocks (
height bigint NOT NULL,
"time" bigint NOT NULL,
"timestamp" timestamp with time zone NOT NULL,
prev_hash text,
block_hash text NOT NULL,
transaction_count integer NOT NULL,
hbbft_round bigint NOT NULL,
election_epoch bigint NOT NULL,
epoch_start bigint NOT NULL,
rescue_signature text NOT NULL,
snapshot_hash text,
created_at timestamp with time zone
);
ALTER TABLE public.blocks OWNER TO etl;
--
-- Name: transactions; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.transactions (
block bigint NOT NULL,
hash text NOT NULL,
type public.transaction_type NOT NULL,
fields jsonb NOT NULL,
"time" bigint NOT NULL
);
ALTER TABLE public.transactions OWNER TO etl;
--
-- Name: challenge_receipts; Type: VIEW; Schema: public; Owner: etl
--
CREATE VIEW public.challenge_receipts AS
SELECT transactions.block,
transactions.hash,
transactions.type,
(transactions.fields ->> 'challenger'::text) AS challenger,
(transactions.fields ->> 'challenger_location'::text) AS challenger_location,
(transactions.fields ->> 'challenger_owner'::text) AS challenger_owner,
(transactions.fields ->> 'fee'::text) AS fee,
(transactions.fields ->> 'onion_key_hash'::text) AS onion_key_hash,
(transactions.fields ->> 'path'::text) AS path,
(transactions.fields ->> 'secret'::text) AS secret,
to_timestamp((transactions."time")::double precision) AS "time"
FROM public.transactions
WHERE (transactions.type = 'poc_receipts_v1'::public.transaction_type);
ALTER TABLE public.challenge_receipts OWNER TO etl;
--
-- Name: challenge_receipts_parsed; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.challenge_receipts_parsed (
block bigint,
hash text,
"time" timestamp with time zone,
transmitter_name text,
transmitter_address text,
origin text,
witness_owner text,
witness_name text,
witness_gateway text,
witness_is_valid text,
witness_invalid_reason text,
witness_signal text,
witness_snr text,
witness_channel text,
witness_datarate text,
witness_frequency text,
witness_location text,
witness_timestamp text
);
ALTER TABLE public.challenge_receipts_parsed OWNER TO etl;
--
-- Name: challenge_receipts_parsed_old; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.challenge_receipts_parsed_old (
block bigint,
hash text,
"time" timestamp with time zone,
transmitter_name text,
transmitter_address text,
origin text,
witness_owner text,
witness_name text,
witness_gateway text,
witness_is_valid text,
witness_invalid_reason text,
witness_signal text,
witness_snr text,
witness_channel text,
witness_datarate text,
witness_frequency text,
witness_location text,
witness_timestamp text
);
ALTER TABLE public.challenge_receipts_parsed_old OWNER TO etl;
--
-- Name: challenge_requests; Type: VIEW; Schema: public; Owner: etl
--
CREATE VIEW public.challenge_requests AS
SELECT transactions.block,
transactions.hash,
transactions.type,
(transactions.fields ->> 'block_hash'::text) AS block_hash,
(transactions.fields ->> 'challenger'::text) AS challenger,
(transactions.fields ->> 'challenger_location'::text) AS challenger_location,
(transactions.fields ->> 'challenger_owner'::text) AS challenger_owner,
(transactions.fields ->> 'fee'::text) AS fee,
(transactions.fields ->> 'onion_key_hash'::text) AS onion_key_hash,
(transactions.fields ->> 'secret_hash'::text) AS secret_hash,
(transactions.fields ->> 'version'::text) AS version,
to_timestamp((transactions."time")::double precision) AS "time"
FROM public.transactions
WHERE (transactions.type = 'poc_request_v1'::public.transaction_type);
ALTER TABLE public.challenge_requests OWNER TO etl;
--
-- Name: rewards; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.rewards (
block bigint NOT NULL,
transaction_hash text NOT NULL,
"time" bigint NOT NULL,
account text NOT NULL,
gateway text NOT NULL,
amount bigint NOT NULL
)
WITH (autovacuum_enabled='true');
ALTER TABLE public.rewards OWNER TO etl;
--
-- Name: data_credits; Type: MATERIALIZED VIEW; Schema: public; Owner: etl
--
CREATE MATERIALIZED VIEW public.data_credits AS
WITH second AS (
WITH first AS (
SELECT transactions.type,
((transactions.fields -> 'state_channel'::text) -> 'summaries'::text) AS sums,
to_timestamp((transactions."time")::double precision) AS "time"
FROM public.transactions
WHERE (transactions.type = 'state_channel_close_v1'::public.transaction_type)
LIMIT 1048576
)
SELECT first.sums AS summaries,
date_trunc('day'::text, first."time") AS "time"
FROM first
)
SELECT second."time",
(json_array_elements(to_json(second.summaries)) ->> 'owner'::text) AS owner,
(json_array_elements(to_json(second.summaries)) ->> 'client'::text) AS client,
(json_array_elements(to_json(second.summaries)) ->> 'location'::text) AS location,
((json_array_elements(to_json(second.summaries)) ->> 'num_dcs'::text))::integer AS dcs,
((json_array_elements(to_json(second.summaries)) ->> 'num_packets'::text))::integer AS packets
FROM second
WITH NO DATA;
ALTER TABLE public.data_credits OWNER TO etl;
--
-- Name: data_credits_with_locations; Type: VIEW; Schema: public; Owner: etl
--
CREATE VIEW public.data_credits_with_locations AS
SELECT data_credits."time",
data_credits.owner,
data_credits.client,
data_credits.location,
data_credits.dcs,
data_credits.packets,
locations.long_street,
locations.short_street,
locations.long_city,
locations.short_city,
locations.long_state,
locations.short_state,
locations.long_country,
locations.short_country,
locations.search_city,
locations.city_id,
locations.geometry,
public.st_y(locations.geometry) AS lat,
public.st_x(locations.geometry) AS long
FROM (public.data_credits
LEFT JOIN public.locations locations ON ((data_credits.location = locations.location)))
WHERE (locations.geometry IS NOT NULL);
ALTER TABLE public.data_credits_with_locations OWNER TO etl;
--
-- Name: dc_burns; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.dc_burns (
block bigint,
transaction_hash text NOT NULL,
actor text NOT NULL,
type public.burn_type NOT NULL,
amount bigint NOT NULL,
oracle_price bigint,
"time" bigint
);
ALTER TABLE public.dc_burns OWNER TO etl;
--
-- Name: gateway_inventory; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.gateway_inventory (
address text NOT NULL,
owner text NOT NULL,
location text,
last_poc_challenge bigint,
last_poc_onion_key_hash text,
witnesses jsonb NOT NULL,
first_block bigint,
last_block bigint,
nonce bigint,
name text,
first_timestamp timestamp with time zone,
reward_scale double precision,
elevation integer,
gain integer,
location_hex text,
mode public.gateway_mode,
payer text
);
ALTER TABLE public.gateway_inventory OWNER TO etl;
--
-- Name: gateway_status; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.gateway_status (
address text NOT NULL,
online public.gateway_status_online,
block bigint,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
poc_interval bigint,
last_challenge bigint,
peer_timestamp timestamp with time zone,
listen_addrs jsonb
);
ALTER TABLE public.gateway_status OWNER TO etl;
--
-- Name: gateways; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.gateways (
block bigint NOT NULL,
address text NOT NULL,
owner text NOT NULL,
location text,
last_poc_challenge bigint,
last_poc_onion_key_hash text,
witnesses jsonb NOT NULL,
nonce bigint,
name text,
"time" bigint,
reward_scale double precision,
elevation integer,
gain integer,
location_hex text,
mode public.gateway_mode
);
ALTER TABLE public.gateways OWNER TO etl;
--
-- Name: maker_address; Type: VIEW; Schema: public; Owner: etl
--
CREATE VIEW public.maker_address AS
SELECT 'Helium Inc'::text AS maker,
'13daGGWvDQyTyHFDCPz8zDSVTWgPNNfJ4oh31Teec4TRWfjMx53'::text AS address
UNION
SELECT 'Cal-Chip Connected Devices'::text AS maker,
'13ENbEQPAvytjLnqavnbSAzurhGoCSNkGECMx7eHHDAfEaDirdY'::text AS address
UNION
SELECT 'Maker Integration Tests'::text AS maker,
'138LbePH4r7hWPuTnK6HXVJ8ATM2QU71iVHzLTup1UbnPDvbxmr'::text AS address
UNION
SELECT 'Nebra Ltd'::text AS maker,
'13Zni1he7KY9pUmkXMhEhTwfUpL9AcEV1m2UbbvFsrU9QPTMgE3'::text AS address
UNION
SELECT 'SyncroB.it'::text AS maker,
'14rb2UcfS9U89QmKswpZpjRCUVCVu1haSyqyGY486EvsYtvdJmR'::text AS address
UNION
SELECT 'Bobcat'::text AS maker,
'14sKWeeYWQWrBSnLGq79uRQqZyw3Ldi7oBdxbF6a54QboTNBXDL'::text AS address
UNION
SELECT 'LongAP'::text AS maker,
'12zX4jgDGMbJgRwmCfRNGXBuphkQRqkUTcLzYHTQvd4Qgu8kiL4'::text AS address
UNION
SELECT 'Smart Mimic'::text AS maker,
'13MS2kZHU4h6wp3tExgoHdDFjBsb9HB9JBvcbK9XmfNyJ7jqzVv'::text AS address
UNION
SELECT 'RAKwireless'::text AS maker,
'14h2zf1gEr9NmvDb2U53qucLN2jLrKU1ECBoxGnSnQ6tiT6V2kM'::text AS address
UNION
SELECT 'Kerlink'::text AS maker,
'13Mpg5hCNjSxHJvWjaanwJPBuTXu1d4g5pGvGBkqQe3F8mAwXhK'::text AS address
UNION
SELECT 'DeWi Foundation'::text AS maker,
'13LVwCqZEKLTVnf3sjGPY1NMkTE7fWtUVjmDfeuscMFgeK3f9pn'::text AS address
UNION
SELECT 'SenseCAP'::text AS maker,
'14NBXJE5kAAZTMigY4dcjXSMG4CSqjYwvteQWwQsYhsu2TKN6AF'::text AS address
UNION
SELECT 'Helium Inc (old)'::text AS maker,
'14fzfjFcHpDR1rTH8BNPvSi5dKBbgxaDnmsVPbCjuq9ENjpZbxh'::text AS address;
ALTER TABLE public.maker_address OWNER TO etl;
--
-- Name: oracle_inventory; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.oracle_inventory (
address text NOT NULL
);
ALTER TABLE public.oracle_inventory OWNER TO etl;
--
-- Name: oracle_price_predictions; Type: TABLE; Schema: public; Owner: etl
--
CREATE TABLE public.oracle_price_predictions (
"time" bigint NOT NULL,
price bigint NOT NULL
);
ALTER TABLE public.oracle_price_predictions OWNER TO etl;
--
-- Name: oracle_price_transactions; Type: VIEW; Schema: public; Owner: etl
--
CREATE VIEW public.oracle_price_transactions AS
SELECT transactions.block,
transactions.hash,
transactions.type,
((transactions.fields ->> 'fee'::text))::bigint AS fee,
((transactions.fields ->> 'price'::text))::bigint AS price,