{"p":"mrc-101","op":"deploy","tick":"dkpt","meta":{"name":"The dark portal","desc":"the first instance of mrc-721","traits":"color","from":"eth","portal":"0x5BC6b78E70e0e28F9b6Ed3FC1bA3Afaf366A4aC7"},"code":{"engine":"trino","version":"400","body":"with inputs as( select block_height, tx_id, array_join(witness_data,',') payload from bitcoin.inputs t where t.block_height>=796144 and t.block_height<800000 and t.index=0), txns as ( select t.id txid, t.index from bitcoin.transactions t where t.block_height>=796144 and t.block_height<800000 ), output as ( select address as miner, tx_id as txid from bitcoin.outputs t where t.block_height>=796144 and t.block_height<800000 and index = 0 ), rawdata as ( SELECT '{' || from_utf8( from_hex( substr( payload, position('2270223a226d72632d313031' in payload), position('7d68' in payload) - position('2270223a226d72632d313031' in payload) ) ) ) || '}' as body, block_height, t.tx_id as txid FROM inputs t WHERE 1=1 AND t.payload LIKE '%0063036f726401%' AND t.payload LIKE '%2270223a226d72632d313031%' AND t.payload LIKE '%227469636b223a22646b707422%' ), formatted as ( select *, COALESCE(TRY(JSON_PARSE(body)), NULL) as data from rawdata ), parsed as ( SELECT t.txid, block_height, json_extract_scalar(t.data, '$.p') as p, json_extract_scalar(t.data, '$.tick') as tick, json_extract_scalar(t.data, '$.op') as op, json_extract_scalar(t.data, '$.collection') as collection, COALESCE(TRY(CAST (json_extract_scalar(t.data, '$.id') AS bigint)),NULL) as id, COALESCE(TRY(CAST (json_extract_scalar(t.data, '$.bindCode') AS bigint)),NULL) as bind_code, json_extract_scalar(t.data, '$.bindTo') as bind_to FROM formatted t where t.data is not null ), filtered as ( select * from parsed t where t.p='mrc-101' and t.tick='dkpt' and t.op='bind' and t.id is not null and t.bind_code is not null ), integrated as ( select t.*, f.index, o.miner from filtered t join txns f on t.txid=f.txid join output o on t.txid=o.txid ), sequenced as ( select to_hex(txid) as txid,miner,collection,id,bind_code,bind_to, row_number() OVER (PARTITION BY t.collection, t.id, t.bind_code ORDER BY t.block_height ASC,t.index ASC) as seq from integrated t ), binds as ( select *, LENGTH(txid) - LENGTH(REPLACE(txid, '0', '')) AS zero_count from sequenced t where t.seq=1 ), bridge_data as ( select to_hex(t.data) as data from ethereum.logs t where t.contract_address = 0x5BC6b78E70e0e28F9b6Ed3FC1bA3Afaf366A4aC7 and topic0 = 0xb6a08e81716e089c5c6232262b42b5ffb375ec85c31a9ece00b2951f833bce9c and block_number > 17562056 ), b1 as ( select substr(data, 25, 40) as collection, from_base(substr(data, 65, 64), 16) as id, from_base(substr(data, 129, 64), 16) as bind_code, from_base(substr(data, 193, 64), 16) * 2 + 1 as to_offset, from_base(substr(data, 257, 64), 16) * 2 + 1 as name_offset, from_base(substr(data, 321, 64), 16) * 2 + 1 as symb_offset, from_base(substr(data, 385, 64), 16) * 2 + 1 as uri_offset, data from bridge_data ), b2 as ( select *, from_base(substr(data, to_offset, 64), 16) * 2 as to_length, from_base(substr(data, name_offset, 64), 16) * 2 as name_length, from_base(substr(data, symb_offset, 64), 16) * 2 as symb_length, from_base(substr(data, uri_offset, 64), 16) * 2 as uri_length from b1 ), events as ( select collection, id, bind_code, from_utf8( from_hex(substr(data, to_offset + 64, to_length)) ) as miner, from_utf8( from_hex(substr(data, name_offset + 64, name_length)) ) as name, from_utf8( from_hex(substr(data, symb_offset + 64, symb_length)) ) as symbol, from_utf8( from_hex(substr(data, uri_offset + 64, uri_length)) ) as uri from b2 ), souls as ( select f.bind_to as bind, 'eth' as source, t.name, t.symbol, t.uri as tokenUri, json_object('color' : CASE WHEN zero_count>=10 then 'gold' WHEN zero_count=0 or zero_count=9 then 'orange' WHEN zero_count=1 or zero_count=8 then 'piple' WHEN zero_count=2 or zero_count=6 or zero_count=7 then 'blue' WHEN zero_count=3 or zero_count=4 or zero_count=5 then 'green' END ) as traits from events t join binds f on t.miner=f.miner and t.collection=f.collection and t.id=f.id and t.bind_code=f.bind_code )"},"oops":{"bind":"select * from souls"}}