/
tokens_solana_nft.sql
231 lines (221 loc) · 9.97 KB
/
tokens_solana_nft.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
{{ config
(
schema = 'tokens_solana',
alias = 'nft',
materialized='table',
post_hook='{{ expose_spells(\'["solana"]\',
"sector",
"tokens",
\'["ilemi"]\') }}'
)
}}
with
token_metadata as (
SELECT
joined_m.account_mintAuthority as account_mint_authority
, joined_m.account_masterEdition as account_master_edition
, joined_m.account_metadata
, joined_m.account_payer
, joined_m.account_mint
, joined_m.version
, json_value(args, 'strict $.tokenStandard.TokenStandard') as token_standard
, json_value(args, 'strict $.name') as token_name
, json_value(args, 'strict $.symbol') as token_symbol
, json_value(args, 'strict $.uri') as token_uri
, cast(json_value(args, 'strict $.sellerFeeBasisPoints') as double) as seller_fee_basis_points
, COALESCE(replace(replace(json_value(args, 'strict $.collection.Collection.key'), 'PublicKey(', ''), ')','')
, v.account_collectionMint
) as collection_mint
, replace(replace(json_value(args, 'strict $.creators[0].Creator.address'), 'PublicKey(', ''), ')','') as verified_creator
, json_query(args, 'strict $.creators') as creators_struct
, joined_m.call_tx_id
, joined_m.call_block_time
, joined_m.call_block_slot
, COALESCE(v.call_block_time, joined_m.call_block_time) as verify_block_time
, joined_m.call_tx_signer
, row_number() over (partition by joined_m.account_metadata order by COALESCE(v.call_block_time, joined_m.call_block_time) desc) as recent_update
FROM (
SELECT
call_tx_id
, call_block_slot
, call_block_time
, json_query(createArgs, 'lax $.CreateArgs.V1.asset_data.AssetData') as args
, account_authority as account_mintAuthority
, account_masterEdition
, account_metadata
, account_payer
, account_mint
, call_tx_signer
, 'Token Metadata' as version
FROM {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_Create') }}
UNION ALL
SELECT
m.call_tx_id
, m.call_block_slot
, m.call_block_time
, m.args
, master.account_mintAuthority
, master.account_edition as account_masterEdition
, m.account_metadata
, account_payer
, m.account_mint
, m.call_tx_signer
, m.version
FROM (
SELECT
call_tx_id
, call_outer_instruction_index
, call_inner_instruction_index
, call_block_slot
, call_block_time
, json_query(createMetadataAccountArgs, 'lax $.CreateMetadataAccountArgs.data.Data') as args
, account_metadata
, account_payer
, account_mint
, call_tx_signer
, 'Token Metadata' as version
FROM {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_CreateMetadataAccount') }}
UNION ALL
SELECT
call_tx_id
, call_outer_instruction_index
, call_inner_instruction_index
, call_block_slot
, call_block_time
, json_query(createMetadataAccountArgsV2, 'lax $.CreateMetadataAccountArgsV2.data.DataV2') as args
, account_metadata
, account_payer
, account_mint
, call_tx_signer
, 'Token Metadata' as version
FROM {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_CreateMetadataAccountV2') }}
UNION ALL
SELECT
call_tx_id
, call_outer_instruction_index
, call_inner_instruction_index
, call_block_slot
, call_block_time
, json_query(createMetadataAccountArgsV3, 'lax $.CreateMetadataAccountArgsV3.data.DataV2') as args
, account_metadata
, account_payer
, account_mint
, call_tx_signer
, 'Token Metadata' as version
FROM {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_CreateMetadataAccountV3') }}
) m
--we don't want it if it doesn't have a master edition
INNER JOIN (
SELECT account_mintAuthority, account_edition, account_metadata
FROM {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_CreateMasterEdition') }}
UNION ALL
SELECT account_mintAuthority, account_edition, account_metadata
FROM {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_CreateMasterEditionV3') }}
) master ON master.account_metadata = m.account_metadata
) joined_m
LEFT JOIN {{ source('mpl_token_metadata_solana','mpl_token_metadata_call_Verify') }} v
ON v.account_metadata = joined_m.account_metadata
and v.account_collectionMint != 'metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s' --if it is this then collection was null in the update
)
, cnfts as (
with
mint_collection_v1 as (
SELECT
account_merkleTree
, json_value(metadataArgs, 'strict $.MetadataArgs.name') as token_name
, json_value(metadataArgs, 'strict $.MetadataArgs.symbol') as token_symbol
, json_value(metadataArgs, 'strict $.MetadataArgs.tokenStandard.TokenStandard') as token_standard
, replace(replace(json_value(metadataArgs, 'strict $.MetadataArgs.collection.Collection.key'), 'PublicKey(', ''), ')','') as collection_mint
, replace(replace(json_value(metadataArgs, 'strict $.MetadataArgs.creators[*].Creator.address'), 'PublicKey(', ''), ')','') as verified_creator
, json_value(metadataArgs, 'strict $.MetadataArgs.uri') as token_uri
, cast(json_value(metadataArgs, 'strict $.MetadataArgs.sellerFeeBasisPoints') as double) as seller_fee_basis_points
, json_query(metadataArgs, 'strict $.MetadataArgs.creators') as creators_struct
, account_leafOwner
, call_block_slot
, call_block_time
, call_outer_instruction_index
, call_inner_instruction_index
, call_tx_id
, call_tx_signer
-- , metadataArgs
FROM {{ source('bubblegum_solana','bubblegum_call_mintToCollectionV1') }}
WHERE 1=1
)
, mint_v1 as (
SELECT
account_merkleTree
, json_value(message, 'strict $.MetadataArgs.name') as token_name
, json_value(message, 'strict $.MetadataArgs.symbol') as token_symbol
, json_value(message, 'strict $.MetadataArgs.tokenStandard.TokenStandard') as token_standard
, replace(replace(json_value(message, 'strict $.MetadataArgs.collection.Collection.key'), 'PublicKey(', ''), ')','') as collection_mint
, replace(replace(json_value(message, 'strict $.MetadataArgs.creators[*].Creator.address'), 'PublicKey(', ''), ')','') as verified_creator
, json_value(message, 'strict $.MetadataArgs.uri') as token_uri
, cast(json_value(message, 'strict $.MetadataArgs.sellerFeeBasisPoints') as double) as seller_fee_basis_points
, json_query(message, 'strict $.MetadataArgs.creators') as creators_struct
, account_leafOwner
, call_block_slot
, call_block_time
, call_outer_instruction_index
, call_inner_instruction_index
, call_tx_id
, call_tx_signer
-- , message
FROM {{ source('bubblegum_solana','bubblegum_call_mintV1') }}
)
SELECT
*
, row_number() over (partition by account_merkleTree
order by call_block_slot asc, call_outer_instruction_index asc, COALESCE(call_inner_instruction_index,0) asc)
as leaf_id
FROM (
SELECT * FROM mint_collection_v1
UNION ALL
SELECT * FROM mint_v1
)
)
SELECT
account_mint_authority
, cast(null as bigint) as leaf_id
, cast(null as varchar) as account_merkle_tree
, account_master_edition
, account_metadata
, account_mint
, account_payer as minter
, version
, token_standard
, token_name
, token_symbol
, token_uri
, seller_fee_basis_points
, collection_mint
, verified_creator
, creators_struct
, call_tx_id
, call_block_time
, call_block_slot
, call_tx_signer
FROM token_metadata tk
WHERE recent_update = 1
UNION ALL
SELECT
cast(null as varchar) as account_mint_authority
, cast(leaf_id as bigint) as leaf_id
, account_merkleTree as account_merkle_tree
, cast(null as varchar) as account_master_edition
, cast(null as varchar) as account_metadata
, cast(null as varchar) as account_mint
, account_leafOwner as minter
, 'cNFT' as version
, token_standard
, token_name
, token_symbol
, token_uri
, seller_fee_basis_points
, collection_mint
, verified_creator
, creators_struct
, call_tx_id
, call_block_time
, call_block_slot
, call_tx_signer
FROM cnfts