Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uniV3 basic lp info #5635

Closed
wants to merge 18 commits into from
Closed
135 changes: 135 additions & 0 deletions macros/models/_sector/dex/uniswap_compatible_lp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{% macro uniswap_compatible_v3_lps(
blockchain = null
, project = null
, version = null
, Pair_evt_Mint = null
, Pair_evt_Burn = null
, Factory_evt_PoolCreated = null
, NonfungibleTokenPositionManager_evt_Transfer = null
, NonfungibleTokenPositionManager_evt_IncreaseLiquidity = null
, NonfungibleTokenPositionManager_evt_DecreaseLiquidity = null
, position_manager_addr = null
)
%}
WITH id_to_lp AS
(
SELECT
distinct t.tokenId
,t.to AS lp_address
FROM
{{ NonfungibleTokenPositionManager_evt_Transfer }} t
WHERE
t."from" = 0x0000000000000000000000000000000000000000
)

, mints AS
(
SELECT
'mint' AS event_type
,m.evt_block_number AS block_number
,m.evt_block_time AS block_time
,m.evt_tx_hash AS tx_hash
,m.evt_index
, {% if position_manager_addr %}
id.lp_address
{% else %}
m.owner
{% endif %} AS lp_address
, {% if position_manager_addr %}
cast(pm.tokenId AS double)
{% else %}
0
{% endif %} AS position_id
, m.tickLower AS tick_lower
, m.tickUpper AS tick_upper
, m.amount AS liquidity
, m.amount0 AS amount0
, m.amount1 AS amount1
, f.token0 AS token0_address
, f.token1 AS token1_address
, m.contract_address AS pool_address
FROM
{{ Pair_evt_Mint }} m
INNER JOIN
{{ Factory_evt_PoolCreated }} f
ON f.pool = m.contract_address
LEFT JOIN
{{ NonfungibleTokenPositionManager_evt_IncreaseLiquidity }} pm
ON m.owner = pm.contract_address and m.evt_tx_hash = pm.evt_tx_hash
LEFT JOIN id_to_lp AS id
ON pm.tokenId = id.tokenId
{% if is_incremental() %}
WHERE
{{ incremental_predicate('m.evt_block_time') }}
{% endif %}
)

, burns AS
(
SELECT
'burn' AS event_type
,b.evt_block_number AS block_number
,b.evt_block_time AS block_time
,b.evt_tx_hash AS tx_hash
,b.evt_index
, {% if position_manager_addr %}
id.lp_address
{% else %}
b.owner
{% endif %} AS lp_address
, {% if position_manager_addr %}
cast(pm.tokenId AS double)
{% else %}
0
{% endif %} AS position_id
, b.tickLower AS tick_lower
, b.tickUpper AS tick_upper
, b.amount AS liquidity
, b.amount0
, b.amount1
, f.token0 AS token0_address
, f.token1 AS token1_address
, m.contract_address AS pool_address
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, m.contract_address AS pool_address
, b.contract_address AS pool_address

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!
yesterday i had to do other stuff, but will continue working on this during this afternoon and next week

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, if you have any problem please feel free to tag me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks for your support!

FROM
{{ Pair_evt_Burn }} b
INNER JOIN
{{ Factory_evt_PoolCreated }} f
ON f.pool = b.contract_address
LEFT JOIN
{{ NonfungibleTokenPositionManager_evt_IncreaseLiquidity }} pm
ON b.owner = pm.contract_address and b.evt_tx_hash = pm.evt_tx_hash
LEFT JOIN id_to_lp AS id
ON pm.tokenId = id.tokenId
{% if is_incremental() %}
WHERE
{{ incremental_predicate('b.evt_block_time') }}
{% endif %}
)

SELECT
'{{ blockchain }}' AS blockchain
, '{{ project }}' AS project
, '{{ version }}' AS version
, lp_data.event_type
, lp_data.block_number
, lp_data.block_time
, date_trunc('MONTH', lp_data.block_time) AS block_month
, lp_data.tx_hash
, lp_data.evt_index
, lp_data.lp_address
, lp_data.position_id
, lp.tick_lower
, lp.tick_upper
, CAST(lp_data.liquidity AS UINT256) AS liquidity_raw
, CAST(lp_data.amount0 AS UINT256) AS amount0_raw
, CAST(lp_data.amount1 AS UINT256) AS amount1_raw
, lp_data.token0_address
, lp_data.token1_address
, lp_data.pool_address
FROM
(
select * from mints
union all
select * from burns
) lp_data
{% endmacro %}
133 changes: 133 additions & 0 deletions models/_sector/dex/lps/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
version: 1

models:
# - name: dex_lps
# meta:
# blockchain: ethereum
# sector: dex
# contributors: 0xrusowsky
# config:
# tags: [ 'dex', 'lps' ]
# tests:
# - dbt_utils.unique_combination_of_columns:
# combination_of_columns:
# - blockchain
# - project
# - version
# - tx_hash
# - evt_index
# columns:
# - &blockchain
# name: blockchain
# description: "Blockchain where the DEX is deployed"
# - &project
# name: project
# description: "Project name of the DEX"
# tests:
# - relationships:
# to: ref('dex_info')
# field: project
# - &version
# name: version
# description: "Version of the contract built and deployed by the DEX project"
# - &block_month
# name: block_month
# description: "Month of the event block time"
# - &block_time
# name: block_time
# description: "UTC event block time of each DEX trade"
# - &block_number
# name: block_number
# description: "Block number of each DEX trade"
# - &token0_symbol
# name: token0_symbol
# description: "Token symbol for token bought in the trade"
# - &token1_symbol
# name: token1_symbol
# description: "Token symbol for token sold in the trade"
# - &token_pair
# name: token_pair
# description: "Token symbol pair for each token involved in the trade"
# - &token0_amount_raw
# name: token0_amount_raw
# description: "Raw amount of token0"
# - &token1_amount_raw
# name: token1_amount_raw
# description: "Raw amount of token1"
# - &liquidity_amount_raw
# name: liquidity_amount_raw
# description: "Raw amount of liquidity provided"
# - &token0_amount_usd
# name: token0_amount_usd
# description: "USD value of amount token0 provided at time of execution"
# - &token1_amount_usd
# name: token1_amount_usd
# description: "USD value of amount token1 provided at time of execution"
# - &liquidity_usd
# name: liquidity_usd
# description: "USD value of the liquidity provided at time of execution"
# tests:
# - dbt_utils.accepted_range:
# max_value: 1000000000 # $1b is an arbitrary number, intended to flag outlier amounts early
# - &token0_address
# name: token0_address
# description: "Contract address of the token bought"
# - &token1_address
# name: token1_address
# description: "Contract address of the token sold"
# - &pool_address
# name: pool_address
# description: "Project contract address which executed the trade on the blockchain"
# - &liquidity_provider
# name: liquidity_provider
# description: "Address of the liquidity provider (LP)"
# - &position_id
# name: position_id
# description: "ID of the NFT that represents the liquidity position. Zero if they didn't use the NFTPositionManager."
# - &tick_lower
# name: tick_lower
# description: "Lower tick of the position."
# - &tick_upper
# name: tick_upper
# description: "Upper tick of the position."
# - &tx_hash
# name: tx_hash
# description: "Unique transaction hash value tied to each transaction on the DEX"
# - &evt_index
# name: evt_index
# description: "Index of the corresponding trade event"

- name: dex_base_lps
meta:
blockchain: ethereum
sector: dex
contributors: 0xrusowsky
config:
tags: [ 'dex' ]
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- blockchain
- project
- version
- tx_hash
- evt_index
# columns:
# - blockchain
# - project
# - version
# - block_time
# - block_month
# - block_number
# - amount0_raw
# - amount1_raw
# - liquidity_amount_raw
# - token0_address
# - token1_address
# - pool_address
# - liquidity_provider
# - position_id
# - tick_lower
# - tick_upper
# - tx_hash
# - evt_index
60 changes: 60 additions & 0 deletions models/_sector/dex/lps/dex_base_lps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{{ config(
schema = 'dex'
, alias = 'base_lps'
, partition_by = ['block_month', 'blockchain', 'project']
, materialized = 'incremental'
, file_format = 'delta'
, incremental_strategy = 'merge'
, unique_key = ['blockchain', 'project', 'version', 'tx_hash', 'evt_index']
, incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')]
)
}}

{% set models = [
ref('dex_ethereum_base_lps')
] %}

with base_union as (
SELECT *
FROM
(
{% for model in models %}
SELECT
blockchain
, project
, version
, block_time
, block_month
, block_number
, token0_amount_raw
, token1_amount_raw
, liquidity_amount_raw
, token0_address
, token1_address
, pool_address
, liquidity_provider
, position_id
, tick_lower
, tick_upper
, tx_hash
, evt_index
, row_number() over (partition by tx_hash, evt_index order by tx_hash) as duplicates_rank
FROM
{{ model }}
{% if is_incremental() %}
WHERE
{{ incremental_predicate('block_time') }}
{% endif %}
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
)
WHERE
duplicates_rank = 1
)

select
*
from
base_union
25 changes: 25 additions & 0 deletions models/_sector/dex/lps/ethereum/_schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2

models:
- name: dex_ethereum_base_lps
tests:
- check_dex_info_relationship

- name: uniswap_v3_ethereum_base_lps
meta:
blockchain: ethereum
sector: dex
project: uniswap
contributors: 0xrusowsky
config:
tags: [ 'ethereum', 'dex', 'lps', 'uniswap', 'v3' ]
description: "uniswap ethereum v3 base lps"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- tx_hash
- evt_index
# - check_dex_base_lps_seed:
# seed_file: ref('uniswap_ethereum_base_lps_seed')
# filter:
# version: 3
Loading
Loading