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

create labels and sources for beets on sonic #7364

Merged
merged 7 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
{{config(
alias = 'beets_pools_sonic',
post_hook = '{{ expose_spells(\'["sonic"]\',
"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
'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_name AS poolname,
pool_type,
'2' AS version,
'beets_v2_pool' AS category,
'beets' AS contributor,
'query' AS source,
TIMESTAMP'2024-12-15 00:00' AS created_at,
now() AS updated_at,
'beets_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,
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,
'beets_v3_pool' AS category,
'beets' AS contributor,
'query' AS source,
TIMESTAMP'2024-12-15 00:00' AS created_at,
now() AS updated_at,
'beets_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
@@ -0,0 +1,54 @@
version: 2

models:
- name: labels_beets_pools_sonic
meta:
blockchain: sonic
sector: labels
project: beets
contributors: viniabussafi
config:
tags: ['labels', 'fantom', 'balancer', 'pools']
description: 'Beets liquidity pools created on Sonic.'
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)"
35 changes: 35 additions & 0 deletions sources/beets/sonic/beets_sonic_sources.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: 2

sources:
- name: beethoven_x_v2_sonic
description: >
Decoded tables related to Beethoven X, an automated portfolio manager and trading platform, on Sonic.
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: Vault_evt_FlashLoan
- name: Vault_evt_TokensRegistered

- 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
Loading