diff --git a/.user.yml b/.user.yml new file mode 100644 index 0000000..e5b5c48 --- /dev/null +++ b/.user.yml @@ -0,0 +1 @@ +id: b33ab02e-a77e-4b41-a8c3-600e8240e7fe diff --git a/models/core/core__dim_swap_pool_labels.sql b/models/core/core__dim_swap_pool_labels.sql new file mode 100644 index 0000000..0b034d7 --- /dev/null +++ b/models/core/core__dim_swap_pool_labels.sql @@ -0,0 +1,42 @@ +{{ config ( + materialized = 'view' +) }} + +WITH pairs AS ( + + SELECT + swap_contract, + deployment_timestamp, + token0_contract, + token1_contract, + pool_id, + vault_address + FROM + {{ ref('silver__labels_pools') }} +), +metapier AS ( + SELECT + swap_contract, + deployment_timestamp, + token0_contract, + token1_contract, + pool_id, + vault_address + FROM + {{ ref('silver__labels_pools_metapier') }} +), +FINAL AS ( + SELECT + * + FROM + pairs + UNION + SELECT + * + FROM + metapier +) +SELECT + * +FROM + FINAL diff --git a/models/core/core__dim_swap_pool_labels.yml b/models/core/core__dim_swap_pool_labels.yml new file mode 100644 index 0000000..e73fa79 --- /dev/null +++ b/models/core/core__dim_swap_pool_labels.yml @@ -0,0 +1,28 @@ +version: 2 + +models: + - name: core__dim_swap_pool_labels + description: |- + This table takes swap pool/pair creation events and models out the relevant info, such as contracts for the token pairs, when it was deployed, and the address of the vault where tokens seem to be held. + + columns: + - name: tx_id + description: "{{ doc('tx_id') }}" + + - name: deployment_timestamp + description: "{{ doc('deployment_timestamp') }}" + + - name: token0_contract + description: "{{ doc('token0_contract') }}" + + - name: token1_contract + description: "{{ doc('token1_contract') }}" + + - name: pool_id + description: "{{ doc('pool_id') }}" + + - name: vault_address + description: "{{ doc('vault_address') }}" + + - name: swap_contract + description: "{{ doc('swap_contract') }}" diff --git a/models/descriptions/pool_id.md b/models/descriptions/pool_id.md new file mode 100644 index 0000000..5256c8b --- /dev/null +++ b/models/descriptions/pool_id.md @@ -0,0 +1,5 @@ +{% docs pool_id %} + +The ID of the swap pool, as designated by the DEX on contract creation. + +{% enddocs %} \ No newline at end of file diff --git a/models/descriptions/swap_contract.md b/models/descriptions/swap_contract.md index 690dcb5..1653127 100644 --- a/models/descriptions/swap_contract.md +++ b/models/descriptions/swap_contract.md @@ -1,5 +1,6 @@ {% docs swap_contract %} The smart contract address of the DEX swap pool. +Note for metapier swaps in the labels table - they all appear to use the same master contract `A.609e10301860b683.PierPair` so the `pool_id` is essential to differentiating what pool is used in the swap route. {% enddocs %} \ No newline at end of file diff --git a/models/descriptions/token0_contract.md b/models/descriptions/token0_contract.md new file mode 100644 index 0000000..b27b87a --- /dev/null +++ b/models/descriptions/token0_contract.md @@ -0,0 +1,5 @@ +{% docs token0_contract %} + +The contract for token0 in the swap pair. Positioning is determined by the order this token occurs in the Swap Contract creation transaction. + +{% enddocs %} \ No newline at end of file diff --git a/models/descriptions/token1_contract.md b/models/descriptions/token1_contract.md new file mode 100644 index 0000000..42d07ce --- /dev/null +++ b/models/descriptions/token1_contract.md @@ -0,0 +1,5 @@ +{% docs token1_contract %} + +The contract for token1 in the swap pair. Positioning is determined by the order this token occurs in the Swap Contract creation transaction. + +{% enddocs %} \ No newline at end of file diff --git a/models/descriptions/vault_address.md b/models/descriptions/vault_address.md new file mode 100644 index 0000000..4cca6ae --- /dev/null +++ b/models/descriptions/vault_address.md @@ -0,0 +1,5 @@ +{% docs vault_address %} + +The account address that holds tokens on behalf of the swap contract. + +{% enddocs %} \ No newline at end of file diff --git a/models/silver/deployment_timestamp.md b/models/silver/deployment_timestamp.md new file mode 100644 index 0000000..7a7f7ae --- /dev/null +++ b/models/silver/deployment_timestamp.md @@ -0,0 +1,5 @@ +{% docs deployment_timestamp %} + +The block timestamp from the deployment transaction. + +{% enddocs %} \ No newline at end of file diff --git a/models/silver/silver__labels_pools.sql b/models/silver/silver__labels_pools.sql new file mode 100644 index 0000000..36a5cc7 --- /dev/null +++ b/models/silver/silver__labels_pools.sql @@ -0,0 +1,65 @@ +{{ config( + materialized = 'incremental', + cluster_by = ['_inserted_timestamp::DATE'], + unique_key = 'tx_id', + incremental_strategy = 'delete+insert' +) }} + +WITH events AS ( + + SELECT + * + FROM + {{ ref('silver__events_final') }} + +{% if is_incremental() %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) + FROM + {{ this }} + ) +{% endif %} +), +pair_labels AS ( + SELECT + * + FROM + {{ ref('silver__contract_labels') }} + WHERE + contract_name ILIKE '%swappair%' +), +pair_creation AS ( + SELECT + tx_id, + block_timestamp, + event_contract, + event_data :numPairs :: NUMBER AS pair_number, + event_data :pairAddress :: STRING AS account_address, + event_data :token0Key :: STRING AS token0_contract, + event_data :token1Key :: STRING AS token1_contract, + _inserted_timestamp + FROM + events + WHERE + event_type = 'PairCreated' +), +FINAL AS ( + SELECT + tx_id, + block_timestamp AS deployment_timestamp, + pair_number as pool_id, + p.account_address as vault_address, + token0_contract, + token1_contract, + l.event_contract AS swap_contract, + _inserted_timestamp + FROM + pair_creation p + LEFT JOIN pair_labels l USING (account_address) +) +SELECT + * +FROM + FINAL diff --git a/models/silver/silver__labels_pools.yml b/models/silver/silver__labels_pools.yml new file mode 100644 index 0000000..e2b487d --- /dev/null +++ b/models/silver/silver__labels_pools.yml @@ -0,0 +1,50 @@ +version: 2 + +models: + - name: silver__labels_pools + description: |- + Looks for new PairCreated events from the Swap pair factory and records info. + + columns: + - name: tx_id + description: "{{ doc('tx_id') }}" + tests: + - not_null + - unique + + - name: deployment_timestamp + description: "{{ doc('deployment_timestamp') }}" + tests: + - not_null + + - name: token0_contract + description: "{{ doc('token0_contract') }}" + tests: + - not_null + + - name: token1_contract + description: "{{ doc('token1_contract') }}" + tests: + - not_null + + - name: pool_id + description: "{{ doc('pool_id') }}" + tests: + - not_null + - unique + + - name: vault_address + description: "{{ doc('vault_address') }}" + tests: + - not_null + - unique + + - name: swap_contract + description: "{{ doc('swap_contract') }}" + tests: + - not_null + + - name: _inserted_timestamp + description: "{{ doc('_inserted_timestamp') }}" + tests: + - not_null diff --git a/models/silver/silver__labels_pools_metapier.sql b/models/silver/silver__labels_pools_metapier.sql new file mode 100644 index 0000000..5ce229d --- /dev/null +++ b/models/silver/silver__labels_pools_metapier.sql @@ -0,0 +1,119 @@ +{{ config( + materialized = 'incremental', + cluster_by = ['_inserted_timestamp::DATE'], + unique_key = 'tx_id', + incremental_strategy = 'delete+insert' +) }} + +WITH events AS ( + + SELECT + * + FROM + {{ ref('silver__events_final') }} + +{% if is_incremental() %} +WHERE + _inserted_timestamp >= ( + SELECT + MAX(_inserted_timestamp) + FROM + {{ this }} + ) +{% endif %} +), +pierpools AS ( + SELECT + tx_id, + block_timestamp, + event_contract, + event_type, + event_data :poolId :: STRING AS pool_id, + _inserted_timestamp + FROM + events + WHERE + event_contract = 'A.609e10301860b683.PierSwapFactory' + AND event_type = 'NewPoolCreated' +), +pier_events AS ( + SELECT + * + FROM + events + WHERE + tx_id IN ( + SELECT + DISTINCT tx_id + FROM + pierpools + ) +), +token_withdraws AS ( + SELECT + tx_id, + block_timestamp, + event_contract, + event_index, + _inserted_timestamp + FROM + pier_events + WHERE + event_type = 'TokensWithdrawn' + AND event_data :amount :: DOUBLE = 0 +), +pairs AS ( + SELECT + tx_id, + block_timestamp AS deployment_timestamp, + event_contract AS token0_contract, + LAG(event_contract) over ( + PARTITION BY tx_id + ORDER BY + event_index + ) AS token1_contract, + _inserted_timestamp + FROM + token_withdraws +), +escrow AS ( + SELECT + tx_id, + event_data :address :: STRING AS vault_address + FROM + pier_events + WHERE + event_contract = 'flow' + AND event_type = 'AccountCreated' +), +pool_addr AS ( + SELECT + tx_id, + event_contract AS swap_contract + FROM + pier_events + WHERE + event_type = 'Mint' +), +FINAL AS ( + SELECT + C.tx_id, + C.deployment_timestamp, + C.token0_contract, + C.token1_contract, + p.pool_id, + e.vault_address, + pa.swap_contract, + C._inserted_timestamp + FROM + pairs C + LEFT JOIN pierpools p USING (tx_id) + LEFT JOIN escrow e USING (tx_id) + LEFT JOIN pool_addr pa USING (tx_id) + WHERE + token1_contract IS NOT NULL +) +SELECT + * +FROM + FINAL diff --git a/models/silver/silver__labels_pools_metapier.yml b/models/silver/silver__labels_pools_metapier.yml new file mode 100644 index 0000000..be1a85d --- /dev/null +++ b/models/silver/silver__labels_pools_metapier.yml @@ -0,0 +1,50 @@ +version: 2 + +models: + - name: silver__labels_pools_metapier + description: |- + Looks for new PoolCreated events from the Metapier factory and recordes info. + + columns: + - name: tx_id + description: "{{ doc('tx_id') }}" + tests: + - not_null + - unique + + - name: deployment_timestamp + description: "{{ doc('deployment_timestamp') }}" + tests: + - not_null + + - name: token0_contract + description: "{{ doc('token0_contract') }}" + tests: + - not_null + + - name: token1_contract + description: "{{ doc('token1_contract') }}" + tests: + - not_null + + - name: pool_id + description: "{{ doc('pool_id') }}" + tests: + - not_null + - unique + + - name: vault_address + description: "{{ doc('vault_address') }}" + tests: + - not_null + - unique + + - name: swap_contract + description: "{{ doc('swap_contract') }}" + tests: + - not_null + + - name: _inserted_timestamp + description: "{{ doc('_inserted_timestamp') }}" + tests: + - not_null