Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh9200 committed Feb 12, 2024
1 parent 767a02c commit 96391ac
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 54 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
{{
config(
materialized = 'incremental',
alias = 'DEPOSITS',
unique_key='ID',
)
}}

WITH pools as (
SELECT
pair AS id
, '0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f' AS protocol__id
, token0
, token1
FROM factory_pair_created
)

, mint_evts AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, evt_address as contract_address
, pools.protocol__id
, pools.token0
, pools.token1
, sender
, amount0
, amount1
, evt_block_number as block_number
, evt_block_time as block_timestamp
, DATE_TRUNC('hour', evt_block_time) AS HOUR
FROM pools_mint mint
INNER JOIN pools ON mint.evt_address = pools.id
)

, sync AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, reserve0
, reserve1
FROM pools_sync sync
)

, mint_transfers AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, "to"
, value as liquidity
FROM pools_transfer t
WHERE "from" = '0000000000000000000000000000000000000000'
)

, most_cols AS (
SELECT
'DEPOSIT-' || m.transaction_hash || '-' || m.log_index AS id
, m.transaction_hash as hash
, m.log_index
, p.protocol__id
, t."to" as "to"
, sender AS "from"
, m.block_number
, m.block_timestamp AS timestamp
, m.contract_address AS pool__id
, ARRAY[p.token0, p.token1] AS input_tokens
, m.contract_address as output_token__id
, ARRAY[amount0, amount1] AS input_token_amounts
, t.liquidity AS output_token_amount
, ARRAY[s.reserve0, s.reserve1] as reserve_amounts

-- TODO:
, 0 AS _amount0_usd
, 0 AS _amount1_usd
, 0 AS amount_usd
FROM mint_evts m
INNER JOIN pools p ON m.contract_address = p.id
LEFT JOIN sync s ON m.transaction_hash = s.transaction_hash AND m.log_index = s.log_index + 1
LEFT JOIN mint_transfers t ON m.transaction_hash = t.transaction_hash AND m.log_index = t.log_index + 2
)

, final AS (
SELECT
id
, hash
, log_index
, protocol__id
, "to"
, "from"
, block_number
, timestamp
, input_tokens
, output_token__id
, input_token_amounts
, output_token_amount
, reserve_amounts
, amount_usd
, pool__id
FROM most_cols
)

SELECT * FROM final
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{{
config(
materialized = 'incremental',
alias = 'SWAPS',
unique_key='ID',
)
}}

WITH pools as (
SELECT
pair AS id
, '0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f' AS protocol__id
, token0
, token1
FROM factory_pair_created
)

, swap_evts AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, evt_address as contract_address
, pools.protocol__id
, pools.token0
, pools.token1
, "to"
, sender
, amount0_in
, amount0_out
, amount1_in
, amount1_out
, evt_block_number as block_number
, evt_block_time as block_timestamp
, DATE_TRUNC('hour', evt_block_time) AS HOUR
FROM pools_swap swap
INNER JOIN pools ON swap.evt_address = pools.id
)

, sync AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, reserve0
, reserve1
FROM pools_sync sync
)

, final AS (
SELECT
'SWAP-' || s.transaction_hash || '-' || s.log_index AS id
, s.transaction_hash as hash
, s.log_index
, p.protocol__id
, s."to" AS "to"
, sender AS "from"
, s.block_number
, s.block_timestamp AS timestamp

, CASE
WHEN amount0_in > 0 THEN p.token0
ELSE p.token1
END AS token_in__id
, CASE
WHEN amount0_in > 0 THEN amount0_in
ELSE amount1_in
END AS amount_in
, 0 AS amount_in_usd

, CASE
WHEN amount0_in > 0 THEN p.token1
ELSE p.token0
END AS token_out__id
, CASE
WHEN amount0_in > 0 THEN amount1_out
ELSE amount0_out
END AS amount_out
, 0 AS amount_out_usd

, ARRAY[sync.reserve0, sync.reserve1] as reserve_amounts
, s.contract_address AS pool__id
FROM swap_evts s
INNER JOIN pools p ON s.contract_address = p.id
LEFT JOIN sync ON s.transaction_hash = sync.transaction_hash AND s.log_index = sync.log_index + 1
)

SELECT * FROM final
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{{
config(
materialized = 'incremental',
alias = 'WITHDRAWS',
unique_key='ID',
)
}}

WITH pools as (
SELECT
pair AS id
, '0x5c69bee701ef814a2b6a3edd4b1652cb9cc5aa6f' AS protocol__id
, token0
, token1
FROM factory_pair_created
)

, burn_evts AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, evt_address as contract_address
, pools.protocol__id
, pools.token0
, pools.token1
, sender
, amount0
, amount1
, evt_block_number as block_number
, evt_block_time as block_timestamp
, DATE_TRUNC('hour', evt_block_time) AS HOUR
FROM pools_burn burn
INNER JOIN pools ON burn.evt_address = pools.id
)

, sync AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, reserve0
, reserve1
FROM pools_sync sync
)

, transfers AS (
SELECT
evt_tx_hash as transaction_hash
, evt_index as log_index
, evt_address as contract_address
, "from"
, value as liquidity
FROM pools_transfer t
WHERE "to" = '0000000000000000000000000000000000000000'
)

, burn_transfer AS (
SELECT
b.transaction_hash,
b.log_index,
t.liquidity
FROM burn_evts b
INNER JOIN transfers t
ON b.transaction_hash = t.transaction_hash
AND b.log_index > t.log_index
AND b.contract_address = t.contract_address
)

, most_cols AS (
SELECT
'WITHDRAW-' || b.transaction_hash || '-' || b.log_index AS id
, b.transaction_hash as hash
, b.log_index
, p.protocol__id
, b.sender AS "to"
, b.contract_address as "from"
, b.block_number
, b.block_timestamp AS timestamp
, b.contract_address AS pool__id
, ARRAY[p.token0, p.token1] AS input_tokens
, b.contract_address as output_token__id
, ARRAY[amount0, amount1] AS input_token_amounts
, t.liquidity AS output_token_amount
, ARRAY[s.reserve0, s.reserve1] as reserve_amounts

-- TODO:
, 0 AS _amount0_usd
, 0 AS _amount1_usd
, 0 AS amount_usd
FROM burn_evts b
INNER JOIN pools p ON b.contract_address = p.id
LEFT JOIN sync s ON b.transaction_hash = s.transaction_hash AND b.log_index = s.log_index + 1
LEFT JOIN burn_transfer t ON b.transaction_hash = t.transaction_hash AND b.log_index = t.log_index
)

, final AS (
SELECT
id
, hash
, log_index
, protocol__id
, "to"
, "from"
, block_number
, timestamp
, input_tokens
, output_token__id
, input_token_amounts
, output_token_amount
, reserve_amounts
, amount_usd
, pool__id
FROM most_cols
)

SELECT * FROM final

This file was deleted.

This file was deleted.

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 96391ac

Please sign in to comment.