-
Notifications
You must be signed in to change notification settings - Fork 40
/
init.sql
291 lines (270 loc) · 9.34 KB
/
init.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
CREATE TABLE raw_txn
(
signature varchar(64) PRIMARY KEY,
slot bigint not null,
processed bool not null
);
-- @@@@@@
CREATE INDEX raw_slot on raw_txn (slot);
-- @@@@@@
CREATE TABLE cl_items
(
id bigserial PRIMARY KEY,
tree BYTEA NOT NULL,
node_idx BIGINT NOT NULL,
leaf_idx BIGINT,
seq BIGINT NOT NULL,
level BIGINT NOT NULL,
hash BYTEA NOT NULL
);
-- @@@@@@
-- Index All the things space is cheap
CREATE INDEX cl_items_tree_idx on cl_items (tree);
-- @@@@@@
CREATE INDEX cl_items_hash_idx on cl_items (hash);
-- @@@@@@
CREATE INDEX cl_items_level on cl_items (level);
-- @@@@@@
CREATE INDEX cl_items_node_idx on cl_items (node_idx);
-- @@@@@@
CREATE INDEX cl_items_leaf_idx on cl_items (leaf_idx);
-- @@@@@@
CREATE UNIQUE INDEX cl_items__tree_node on cl_items (tree, node_idx);
-- @@@@@@
CREATE TABLE backfill_items
(
id bigserial PRIMARY KEY,
tree BYTEA not null,
seq BIGINT not null,
slot BIGINT not null,
force_chk bool not null,
backfilled bool not null
);
-- @@@@@@
CREATE INDEX backfill_items_tree_idx on backfill_items (tree);
-- @@@@@@
CREATE INDEX backfill_items_seq_idx on backfill_items (seq);
-- @@@@@@
CREATE INDEX backfill_items_slot_idx on backfill_items (slot);
-- @@@@@@
CREATE INDEX backfill_items_force_chk_idx on backfill_items (force_chk);
-- @@@@@@
CREATE INDEX backfill_items_backfilled_idx on backfill_items (backfilled);
-- @@@@@@
CREATE INDEX backfill_items_tree_seq_idx on backfill_items (tree, seq);
-- @@@@@@
CREATE INDEX backfill_items_tree_slot_idx on backfill_items (tree, slot);
-- @@@@@@
CREATE INDEX backfill_items_tree_force_chk_idx on backfill_items (tree, force_chk);
-- @@@@@@
CREATE INDEX backfill_items_tree_backfilled_idx on backfill_items (tree, backfilled);
-- @@@@@@
CREATE
or REPLACE FUNCTION notify_new_backfill_item()
RETURNS trigger
LANGUAGE 'plpgsql'
as
$BODY$
declare
begin
if
(tg_op = 'INSERT') then
perform pg_notify('backfill_item_added', 'hello');
end if;
return null;
end
$BODY$;
-- @@@@@@
CREATE TRIGGER after_insert_item
AFTER INSERT
ON backfill_items
FOR EACH ROW
EXECUTE PROCEDURE notify_new_backfill_item();
-- @@@@@@
-- START NFT METADATA
CREATE TYPE owner_type AS ENUM ('unknown', 'token', 'single');
-- @@@@@@
CREATE TYPE royalty_target_type AS ENUM ('unknown', 'creators', 'fanout', 'single');
-- @@@@@@
CREATE TYPE chain_mutability AS ENUM ('unknown', 'mutable', 'immutable');
-- @@@@@@
CREATE TYPE mutability AS ENUM ('unknown', 'mutable', 'immutable');
-- @@@@@@
CREATE TYPE v1_account_attachments AS ENUM ('unknown', 'edition', 'master_edition_v2', 'master_edition_v1', 'edition_marker');
-- @@@@@@
CREATE TYPE specification_versions AS ENUM ('unknown', 'v0', 'v1', 'v2');
-- @@@@@@
CREATE TYPE specification_asset_class AS ENUM ('unknown', 'FUNGIBLE_TOKEN', 'FUNGIBLE_ASSET', 'NFT', 'PRINTABLE_NFT', 'PRINT', 'TRANSFER_RESTRICTED_NFT', 'NON_TRANSFERABLE_NFT', 'IDENTITY_NFT');
-- @@@@@@
create table tokens
(
mint bytea PRIMARY KEY,
supply bigint not null default 0,
decimals int not null default 0,
token_program bytea not null,
mint_authority bytea,
freeze_authority bytea,
close_authority bytea,
extension_data bytea,
slot_updated bigint not null
);
-- @@@@@@
create index t_mint_auth on tokens (mint_authority);
-- @@@@@@
create index t_freeze_auth on tokens (freeze_authority);
-- @@@@@@
create index t_close_auth on tokens (close_authority);
-- @@@@@@
create index t_slot_updated_idx on tokens USING BTREE (slot_updated);
-- @@@@@@
create index t_supply on tokens USING BTREE (supply);
-- @@@@@@
create index t_decimals on tokens USING BTREE (decimals);
-- @@@@@@
create table token_accounts
(
pubkey bytea PRIMARY KEY,
mint bytea not null ,
amount bigint not null default 0,
owner bytea not null,
frozen bool not null default false,
close_authority bytea,
delegate bytea,
delegated_amount bigint not null default 0,
slot_updated bigint not null,
token_program bytea not null
);
-- @@@@@@
create index ta_mint on token_accounts (mint);
-- @@@@@@
create index ta_delegate on token_accounts (delegate);
-- @@@@@@
create index ta_slot_updated_idx on token_accounts USING BTREE (slot_updated);
-- @@@@@@
create index ta_amount on token_accounts USING BTREE (amount);
-- @@@@@@
create index ta_amount_del on token_accounts USING BTREE (delegated_amount);
-- @@@@@@
create table asset_data
(
id bytea PRIMARY KEY,
chain_data_mutability chain_mutability not null default 'mutable',
chain_data jsonb not null,
metadata_url varchar(200) not null,
metadata_mutability mutability not null default 'mutable',
metadata jsonb not null,
slot_updated bigint not null
);
-- @@@@@@
create index slot_updated_idx on asset_data USING BTREE (slot_updated);
-- @@@@@@
create table asset
(
id bytea PRIMARY KEY,
alt_id bytea,
-- Specification version determines alot of how this poly morphic table is handled
-- Specification is the MAJOR metaplex version, currently only v1
specification_version specification_versions not null,
specification_asset_class specification_asset_class not null,
owner bytea,
owner_type owner_type not null default 'single',
-- delegation
delegate bytea,
-- freeze
frozen bool not null default false,
-- supply
supply bigint not null default 1,
supply_mint bytea,
-- compression
compressed bool not null default false,
compressible bool not null default false,
seq bigint not null,
-- -- Can this asset be compressed
tree_id bytea,
leaf bytea,
nonce bigint not null,
-- royalty
royalty_target_type royalty_target_type not null default 'creators',
royalty_target bytea,
royalty_amount int not null default 0,
-- data
asset_data bytea references asset_data (id),
-- visibility
created_at timestamp with time zone default (now() at time zone 'utc'),
burnt bool not null default false,
slot_updated bigint not null
);
-- @@@@@@
create index asset_tree on asset (tree_id);
-- @@@@@@
create index asset_leaf on asset (leaf);
-- @@@@@@
create index asset_tree_leaf on asset (tree_id, leaf);
-- @@@@@@
create index asset_revision on asset (tree_id, leaf, nonce);
-- @@@@@@
create index asset_owner on asset (owner);
-- @@@@@@
create index asset_delegate on asset (delegate);
-- @@@@@@
create table asset_v1_account_attachments
(
id bytea PRIMARY KEY,
asset_id bytea references asset (id),
attachment_type v1_account_attachments not null,
initialized bool not null default false,
data jsonb,
slot_updated bigint not null
);
-- @@@@@@
-- grouping
create table asset_grouping
(
id bigserial PRIMARY KEY,
asset_id bytea references asset (id) not null,
group_key text not null,
group_value text not null,
seq bigint not null,
slot_updated bigint not null
);
-- @@@@@@
-- Limit indexable grouping keys, meaning only create on specific keys, but index the ones we allow
create unique index asset_grouping_asset_id on asset_grouping (asset_id);
-- @@@@@@
create index asset_grouping_key on asset_grouping (group_key, group_value);
-- @@@@@@
create index asset_grouping_value on asset_grouping (group_key, asset_id);
-- @@@@@@
-- authority
create table asset_authority
(
id bigserial PRIMARY KEY,
asset_id bytea references asset (id) not null,
scopes text[],
authority bytea not null,
seq bigint not null,
slot_updated bigint not null
);
-- @@@@@@
create unique index asset_authority_asset_id on asset_authority (asset_id);
-- @@@@@@
create index asset_authority_idx on asset_authority (asset_id, authority);
-- @@@@@@
-- creators
create table asset_creators
(
id bigserial PRIMARY KEY,
asset_id bytea references asset (id) not null,
creator bytea not null,
share int not null default 0,
verified bool not null default false,
seq bigint not null,
slot_updated bigint not null
);
-- @@@@@@
create unique index asset_creators_asset_id on asset_creators (asset_id);
-- @@@@@@
create index asset_creator on asset_creators (asset_id, creator);
-- @@@@@@
create index asset_verified_creator on asset_creators (asset_id, verified);
-- @@@@@@