Skip to content

Commit

Permalink
create labels and sources
Browse files Browse the repository at this point in the history
  • Loading branch information
viniabussafi committed Dec 15, 2024
1 parent f0b3045 commit 4b334b5
Show file tree
Hide file tree
Showing 3 changed files with 306 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{{config(
alias = 'beethoven_x_pools_fantom',
post_hook = '{{ expose_spells(\'["fantom"]\',
"sector",
"labels",
\'["viniabussafi"]\') }}'
)}}

WITH v2_pools AS(
WITH pools AS (
SELECT
pool_id,
zip.tokens AS token_address,
zip.weights / pow(10, 18) AS normalized_weight,
symbol,
pool_type,
pool_name
FROM (
SELECT
c.poolId AS pool_id,
t.tokens,
w.weights,
cc.symbol,
'weighted' AS pool_type,
cc.name AS pool_name
FROM {{ source('beethoven_x_v2_sonic', 'Vault_evt_PoolRegistered') }} c
INNER JOIN {{ source('beethoven_x_v2_sonic', 'WeightedPoolFactory_call_create') }} cc
ON c.evt_tx_hash = cc.call_tx_hash
AND bytearray_substring(c.poolId, 1, 20) = cc.output_0
CROSS JOIN UNNEST(cc.tokens) WITH ORDINALITY t(tokens, pos)
CROSS JOIN UNNEST(cc.normalizedWeights) WITH ORDINALITY w(weights, pos)
WHERE t.pos = w.pos
) zip


UNION ALL

SELECT
c.poolId AS pool_id,
t.tokens AS token_address,
0 AS normalized_weight,
cc.symbol,
'stable' AS pool_type,
cc.name AS pool_name
FROM {{ source('beethoven_x_v2_sonic', 'Vault_evt_PoolRegistered') }} c
INNER JOIN {{ source('beethoven_x_v2_sonic', 'ComposableStablePoolFactory_call_create') }} cc
ON c.evt_tx_hash = cc.call_tx_hash
AND bytearray_substring(c.poolId, 1, 20) = cc.output_0
CROSS JOIN UNNEST(cc.tokens) AS t(tokens)
),

settings AS (
SELECT
pool_id,
coalesce(t.symbol, '?') AS token_symbol,
normalized_weight,
p.symbol AS pool_symbol,
p.pool_type,
p.pool_name
FROM pools p
LEFT JOIN {{ source('tokens', 'erc20') }} t ON p.token_address = t.contract_address
AND t.blockchain = 'sonic'
)

SELECT
'fantom' AS blockchain,
bytearray_substring(pool_id, 1, 20) AS address,
CASE WHEN pool_type IN ('stable')
THEN lower(pool_symbol)
ELSE lower(concat(array_join(array_agg(token_symbol ORDER BY token_symbol), '/'), ' ',
array_join(array_agg(cast(norm_weight AS varchar) ORDER BY token_symbol), '/')))
END AS name,
pool_name AS poolname,
pool_type,
'2' AS version,
'beethoven_x_v2_pool' AS category,
'beethoven_x' AS contributor,
'query' AS source,
TIMESTAMP'2024-12-15 00:00' AS created_at,
now() AS updated_at,
'beethoven_x_pools_fantom' AS model_name,
'identifier' AS label_type
FROM (
SELECT
s1.pool_id,
token_symbol,
pool_symbol,
cast(100 * normalized_weight AS integer) AS norm_weight,
pool_type,
pool_name
FROM settings s1
GROUP BY s1.pool_id, token_symbol, pool_symbol, normalized_weight, pool_type, pool_name
) s
GROUP BY pool_id, pool_symbol, pool_type, pool_name
ORDER BY 1)

v3_pools AS(
WITH token_data AS (
SELECT
pool,
ARRAY_AGG(FROM_HEX(json_extract_scalar(token, '$.token')) ORDER BY token_index) AS tokens
FROM (
SELECT
pool,
tokenConfig,
SEQUENCE(1, CARDINALITY(tokenConfig)) AS token_index_array
FROM {{ source('beethoven_x_v3_sonic', 'Vault_evt_PoolRegistered') }}
) AS pool_data
CROSS JOIN UNNEST(tokenConfig, token_index_array) AS t(token, token_index)
GROUP BY 1
),

pools AS (
SELECT
pool_id,
zip.tokens AS token_address,
zip.weights / POWER(10, 18) AS normalized_weight,
symbol,
pool_type
FROM (
SELECT
c.pool AS pool_id,
t.tokens,
w.weights,
cc.symbol,
'weighted' AS pool_type
FROM token_data c
INNER JOIN {{ source('beethoven_x_v3_sonic', 'WeightedPoolFactory_call_create') }} cc
ON c.pool = cc.output_pool
CROSS JOIN UNNEST(c.tokens) WITH ORDINALITY t(tokens, pos)
CROSS JOIN UNNEST(cc.normalizedWeights) WITH ORDINALITY w(weights, pos)
WHERE t.pos = w.pos

UNION ALL

SELECT
c.pool AS pool_id,
t.tokens,
0 AS weights,
cc.symbol,
'stable' AS pool_type
FROM token_data c
INNER JOIN {{ source('beethoven_x_v3_sonic', 'StablePoolFactory_call_create') }} cc
ON c.pool = cc.output_pool
CROSS JOIN UNNEST(c.tokens) AS t(tokens)
) zip
),

settings AS (
SELECT
pool_id,
coalesce(t.symbol, '?') AS token_symbol,
normalized_weight,
p.symbol AS pool_symbol,
p.pool_type
FROM pools p
LEFT JOIN {{ source('tokens', 'erc20') }} t ON p.token_address = t.contract_address
AND t.blockchain = 'sonic'
)

SELECT
'sonic' AS blockchain,
bytearray_substring(pool_id, 1, 20) AS address,
CASE WHEN pool_type IN ('stable')
THEN lower(pool_symbol)
ELSE lower(concat(array_join(array_agg(token_symbol ORDER BY token_symbol), '/'), ' ',
array_join(array_agg(cast(norm_weight AS varchar) ORDER BY token_symbol), '/')))
END AS name,
pool_type,
'3' AS version,
'beethoven_x_v3_pool' AS category,
'beethoven_x' AS contributor,
'query' AS source,
TIMESTAMP'2024-12-15 00:00' AS created_at,
now() AS updated_at,
'balancer_v3_pools_sonic' AS model_name,
'identifier' AS label_type
FROM (
SELECT
s1.pool_id,
token_symbol,
pool_symbol,
cast(100 * normalized_weight AS integer) AS norm_weight,
pool_type
FROM settings s1
GROUP BY s1.pool_id, token_symbol, pool_symbol, normalized_weight, pool_type
) s
GROUP BY pool_id, pool_symbol, pool_type
ORDER BY 1
)

SELECT
blockchain,
address,
name,
pool_type,
version,
category,
contributor,
source,
created_at,
updated_at,
model_name,
label_type
FROM v2_pools

UNION

SELECT
blockchain,
address,
name,
pool_type,
version,
category,
contributor,
source,
created_at,
updated_at,
model_name,
label_type
FROM v3_pools
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,58 @@ models:
config:
tags: ['labels', 'fantom', 'balancer', 'pools']
description: 'Balancer V2 liquidity pools created on Fantom.'
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- address
- name
- category
- model_name
- blockchain
columns:
- &blockchain
name: blockchain
description: 'Blockchain'
- &address
name: address
description: 'Address of liquidity pool'
- &name
name: name
description: 'Label name of pool containg the token symbols and their respective weights (if applicable)'
- &poolname
name: poolname
description: 'Label name of pool set at contract creation'
- &category
name: category
description: 'Label category'
- &contributor
name: contributor
description: 'Wizard(s) contributing to labels'
- &source
name: source
description: 'How were labels generated (could be static or query)'
- &created_at
name: created_at
description: 'When were labels created'
- &updated_at
name: updated_at
description: "When were labels updated for the last time"
- &model_name
name: model_name
description: "Name of the label model sourced from"
- &label_type
name: label_type
description: "Type of label (see labels overall readme)"

- name: labels_beethoven_x_pools_sonic
meta:
blockchain: sonic
sector: labels
project: beethoven_x
contributors: viniabussafi
config:
tags: ['labels', 'sonic', 'beethoven_x', 'pools']
description: 'Beethoven X liquidity pools created on sonic.'
data_tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
Expand Down
32 changes: 32 additions & 0 deletions sources/beethoven_x/sonic/beethoven_x_sonic_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: 2

sources:
- name: beethoven_x_v2_sonic
description: "sonic decoded tables related to beethoven x's contracts"
tables:
- name: Vault_evt_Swap
- name: Vault_evt_PoolRegistered
- name: WeightedPoolFactory_call_create
- name: Vault_evt_PoolBalanceChanged
- name: Vault_evt_PoolBalanceManaged
- name: ComposableStablePoolFactory_call_create

- name: beethoven_x_v3_sonic
description: >
Decoded tables related to Beethoven X, an automated portfolio manager and trading platform, on Sonic.
tables:
- name: Vault_evt_PoolRegistered
- name: WeightedPoolFactory_evt_PoolCreated
- name: WeightedPoolFactory_call_create
- name: Vault_evt_LiquidityAdded
- name: Vault_evt_LiquidityRemoved
- name: StablePoolFactory_evt_PoolCreated
- name: StablePoolFactory_call_create
- name: Vault_evt_Swap
- name: ProtocolFeeController_evt_ProtocolSwapFeeCollected
- name: ProtocolFeeController_evt_ProtocolYieldFeeCollected
- name: Vault_evt_Wrap
- name: Vault_evt_Unwrap
- name: Vault_evt_LiquidityAddedToBuffer
- name: Vault_evt_LiquidityRemovedFromBuffer
- name: Vault_evt_SwapFeePercentageChanged

0 comments on commit 4b334b5

Please sign in to comment.