maybeyonasCopy of [SQL] Dealing with Cast Variants
Updated 2022-08-08
99
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
›
⌄
WITH near_token_address AS (
select 'wrap.near' as contract_address, 'NEAR' as symbol, 24 as decimal union
select 'meta-pool.near' as contract_address, 'stNEAR' as symbol, 24 as decimal union
select 'usn' as contract_address, 'USN' as symbol, 18 as decimal union
select 'aaaaaa20d9e0e2461697782ef11675f668207961.factory.bridge.near' as contract_address, 'Aurora' as symbol, 18 as decimal union
select 'dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near' as contract_address, 'USDT' as symbol, 6 as decimal union -- stable
select 'a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near' as contract_address, 'USDC' as symbol, 6 as decimal union -- stable
select '6b175474e89094c44da98b954eedeac495271d0f.factory.bridge.near' as contract_address, 'DAI' as symbol, 18 as decimal union -- stable
select '2260fac5e5542a773aa44fbcfedf7c193bc2c599.factory.bridge.near' as contract_address, 'WBTC' as symbol, 8 as decimal union
select 'c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.factory.bridge.near' as contract_address, 'WETH' as symbol, 18 as decimal union
select 'aurora' as contract_address, 'WETH' as symbol, 18 as decimal
),
transaction_logs AS ( -- extracting out the transactions log I need
SELECT
block_timestamp,
tx_hash,
receiver_id,
logs as logs1
FROM near.core.fact_receipts
WHERE tx_hash IN (SELECT distinct tx_hash FROM near.core.fact_actions_events_function_call WHERE method_name = 'swap') -- select swaps
AND status_value LIKE '%Success%'
),
swaps_unlabelled AS ( -- flattening the logs that are in array
SELECT
block_timestamp,
tx_hash,
receiver_id,
seq,key,path,index,
value,-- Swaps are recorded in text format: see next line
-- e.g. 'Swapped 1999050 dac17f958d2ee523a2206206994597c13d831ec7.factory.bridge.near for 1998488 a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near, total fee 999, admin fee 199'
split(value, ' ') as split, -- we can split them up into arrays and parse them
split[1] as amount_in_raw,
split[2] as token_in_raw,
split[4] as amount_out_raw,
split[5] as token_out_raw
FROM transaction_logs,
Run a query to Download Data