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

Add Paraswap Delta models #7367

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1ff2681
feat: add paraswap delta v1 models
alexshchur Dec 22, 2024
5401d45
Merge branch 'main' of https://github.com/duneanalytics/spellbook int…
alexshchur Dec 22, 2024
838e097
fix: tests -> data_tests
alexshchur Dec 22, 2024
6983e40
fix CI build: declare and employ sources
alexshchur Dec 22, 2024
af37a27
fix CI build: declare and employ source for prices.usd
alexshchur Dec 22, 2024
4f7db21
fix: partitioning
alexshchur Dec 22, 2024
c471e12
fix attempt: DELTA_LAKE_INVALID_SCHEMA, message="Unsupported type: js…
alexshchur Dec 22, 2024
f7e1e4f
fix: eliminate another json field
alexshchur Dec 22, 2024
fe0609a
fix attempt: signature was perceived as json too?
alexshchur Dec 22, 2024
05ce62f
fix attempt: eliminate one more json field?
alexshchur Dec 22, 2024
431f397
fix: add order_index to unique key
alexshchur Dec 22, 2024
52256a0
fix: correct column name in schema
alexshchur Dec 22, 2024
e0038d8
add paraswap delta v2 module: for now only ethereum and based on call…
alexshchur Dec 23, 2024
f9c705e
fix: json not supported in model
alexshchur Dec 23, 2024
c10100b
feat: delta v2 (settleSwap only for now) on base
alexshchur Dec 23, 2024
cb18123
fix: paraswap delta v2 executor feeAmount decoding from calldata
alexshchur Dec 25, 2024
ed28ee7
Merge branch 'main' into feat/paraswap-delta
alexshchur Dec 25, 2024
152a86a
fix: remove unneeded rtrim
alexshchur Dec 25, 2024
ce26e57
Merge branch 'feat/paraswap-delta' of https://github.com/paraswap/spe…
alexshchur Dec 25, 2024
b214906
feat: [draft] paraswap delta v2 swapSettleBatch
alexshchur Dec 27, 2024
f422e2a
fix: add missing sources for swapSettleBatch
alexshchur Dec 27, 2024
bedb4ee
fix: tmp: add fields that are required for unique key and partitioning
alexshchur Dec 27, 2024
855397b
fix: prop-drill missing fields from macro to model
alexshchur Dec 27, 2024
6d6b83d
fix: no need to prop drill method from macro, just hardcode it in the…
alexshchur Dec 27, 2024
78f671e
fix: avoid duplicated columns - explicit + wildecard
alexshchur Dec 27, 2024
86cab02
feat: draft / untested: join swapSettleBatch with OrderSettled events
alexshchur Dec 28, 2024
e1bd925
fix: based on ordering events are joined to the wrong calls -- so fil…
alexshchur Dec 28, 2024
471eaa8
fix: more strict matching between call and event. Still not strict en…
alexshchur Dec 28, 2024
d067cc8
fix: incremental field
alexshchur Dec 28, 2024
877c0de
feat: paraswap-delta v2: swap_settle_batch: add USD support for execu…
alexshchur Dec 29, 2024
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,97 @@
-- ref dbt_subprojects/dex/macros/models/_project/paraswap/v6/paraswap_v6_trades_master.sql
{% macro delta_safe_settle_batch_swap(blockchain) %}
safe_settle_batch_swap_ExpandedOrders AS (
SELECT
call_trace_address,
call_block_time,
call_block_number,
call_tx_hash,
output_successfulOrders,
JSON_EXTRACT(data, '$.ordersData') AS parsed_orders,
contract_address
FROM {{ source("paraswapdelta_"+blockchain, "ParaswapDeltav1_call_safeSettleBatchSwap") }}
where call_success = true
{% if is_incremental() %}
AND {{ incremental_predicate('call_block_time') }}
{% endif %}
), safe_settle_batch_swap_parsedOrderItems AS (
SELECT
index as order_index,
JSON_ARRAY_GET(parsed_orders, index) AS parsed_order_data,
*
FROM safe_settle_batch_swap_ExpandedOrders
CROSS JOIN UNNEST(SEQUENCE(0, CARDINALITY(output_successfulOrders) - 1)) AS t(index)
WHERE
output_successfulOrders[index + 1]
), safe_settle_batch_swap_parsedOrdersWithSig AS (
SELECT
JSON_EXTRACT_SCALAR(parsed_order_data, '$.feeAmount') AS feeAmount,
JSON_EXTRACT(parsed_order_data, '$.orderWithSig') AS orderWithSig,
JSON_EXTRACT_SCALAR(parsed_order_data, '$.calldataToExecute') AS calldataToExecute,
*
FROM safe_settle_batch_swap_parsedOrderItems
), safe_settle_batch_swap_unparsedOrders AS (
SELECT
JSON_EXTRACT(JSON_PARSE(TRY_CAST(orderWithSig AS VARCHAR)), '$.order') AS "order",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST(orderWithSig AS VARCHAR)), '$.signature') AS signature,
*
FROM safe_settle_batch_swap_parsedOrdersWithSig
), safe_settle_batch_swap_parsedOrders AS (
SELECT
from_hex(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.owner')) AS "order_owner",
FROM_HEX(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.srcToken')) AS "src_token",
FROM_HEX(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.destToken')) AS "dest_token",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.srcAmount') AS "src_amount",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.destAmount') AS "dest_amount",
*
FROM safe_settle_batch_swap_unparsedOrders
), safe_settle_batch_swap_withUSDs AS (
SELECT
CASE
WHEN dest_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
ELSE dest_token
END AS dest_token_for_joining,
CASE
WHEN src_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
ELSE src_token
END AS src_token_for_joining,
*
FROM safe_settle_batch_swap_parsedOrders
), delta_v1_safe_settle_batch_swap_model as (
SELECT
w.*,
w.dest_token AS fee_token,
CAST(s.price AS DECIMAL(38,18)) AS src_token_price_usd,
CAST(d.price AS DECIMAL(38,18)) AS dest_token_price_usd,
COALESCE(
d.price * CAST (w.feeAmount AS uint256) / POWER(10, d.decimals),
-- src cost

(s.price * CAST (w.src_amount AS uint256) / POWER(10, s.decimals))
* CAST (w.feeAmount AS uint256) / CAST (w.dest_amount AS uint256)

) AS gas_fee_usd,
s.price * CAST (w.src_amount AS uint256) / POWER(10, s.decimals) AS src_token_order_usd,
d.price * CAST (w.dest_amount AS uint256) / POWER(10, d.decimals) AS dest_token_order_usd

FROM safe_settle_batch_swap_withUSDs w
LEFT JOIN {{ source('prices', 'usd') }} d
ON d.blockchain = '{{blockchain}}'
AND d.minute > TIMESTAMP '2024-06-01'
{% if is_incremental() %}
AND {{ incremental_predicate('d.minute') }}
{% endif %}
AND d.contract_address = w.dest_token_for_joining
AND d.minute = DATE_TRUNC('minute', w.call_block_time)
LEFT JOIN {{ source('prices', 'usd') }} s
ON s.blockchain = '{{blockchain}}'
AND s.minute > TIMESTAMP '2024-06-01'
{% if is_incremental() %}
AND {{ incremental_predicate('s.minute') }}
{% endif %}
AND s.contract_address = w.src_token_for_joining
AND s.minute = DATE_TRUNC('minute', w.call_block_time)
ORDER BY
CARDINALITY(w.output_successfulOrders)
)
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
{%
-- ref: dbt_subprojects/dex/macros/models/_project/paraswap/v6/paraswap_v6_trades_master.sql
%}
{% macro delta_settle_swap(blockchain) %}
settle_swap_withParsedOrderData AS (
SELECT
call_trace_address,
call_block_time,
call_block_number,
call_tx_hash,
JSON_EXTRACT(data, '$.orderData') AS parsed_order_data,
contract_address
FROM {{ source("paraswapdelta_"+ blockchain, "ParaswapDeltav1_call_settleSwap") }}
where call_success = true
{% if is_incremental() %}
AND {{ incremental_predicate('call_block_time') }}
{% endif %}
),
settle_swap_parsedOrderWithSig AS (
SELECT
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST(parsed_order_data AS VARCHAR)), '$.feeAmount') AS feeAmount,
JSON_EXTRACT(JSON_PARSE(TRY_CAST(parsed_order_data AS VARCHAR)), '$.orderWithSig') AS orderWithSig,
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST(parsed_order_data AS VARCHAR)), '$.calldataToExecute') AS calldataToExecute,
*
FROM
settle_swap_withParsedOrderData
),
settle_swap_unparsedOrders AS (
SELECT
JSON_EXTRACT(JSON_PARSE(TRY_CAST(orderWithSig AS VARCHAR)), '$.order') AS "order",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST(orderWithSig AS VARCHAR)), '$.signature') AS signature,
*
FROM settle_swap_parsedOrderWithSig
),
settle_swap_parsedOrders AS (
SELECT
from_hex(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.owner')) AS "order_owner",
FROM_HEX(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.srcToken')) AS "src_token",
FROM_HEX(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.destToken')) AS "dest_token",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.srcAmount') AS "src_amount",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.destAmount') AS "dest_amount",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.permit') AS "permit",
*
FROM settle_swap_unparsedOrders
),
settle_swap_withUSDs AS (
SELECT
CASE
WHEN dest_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
ELSE dest_token
END AS dest_token_for_joining,
CASE
WHEN src_token = 0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee THEN 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
ELSE src_token
END AS src_token_for_joining,
*
FROM settle_swap_parsedOrders
), delta_v1_settle_swap_model as (
SELECT
w.*,
w.dest_token AS fee_token,
CAST(s.price AS DECIMAL(38,18)) AS src_token_price_usd,
CAST(d.price AS DECIMAL(38,18)) AS dest_token_price_usd,
COALESCE(
d.price * CAST (w.feeAmount AS uint256) / POWER(10, d.decimals),
-- src cost

(s.price * CAST (w.src_amount AS uint256) / POWER(10, s.decimals))
* CAST (w.feeAmount AS uint256) / CAST (w.dest_amount AS uint256),
0

) AS gas_fee_usd,
s.price * CAST (w.src_amount AS uint256) / POWER(10, s.decimals) AS src_token_order_usd,
d.price * CAST (w.dest_amount AS uint256) / POWER(10, d.decimals) AS dest_token_order_usd

FROM settle_swap_withUSDs w
LEFT JOIN {{ source('prices', 'usd') }} d
ON d.blockchain = '{{blockchain}}'
AND d.minute > TIMESTAMP '2024-06-01'
{% if is_incremental() %}
AND {{ incremental_predicate('d.minute') }}
{% endif %}
AND d.contract_address = w.dest_token_for_joining
AND d.minute = DATE_TRUNC('minute', w.call_block_time)
LEFT JOIN {{ source('prices', 'usd') }} s
ON s.blockchain = '{{blockchain}}'
AND s.minute > TIMESTAMP '2024-06-01'
{% if is_incremental() %}
AND {{ incremental_predicate('s.minute') }}
{% endif %}
AND s.contract_address = w.src_token_for_joining
AND s.minute = DATE_TRUNC('minute', w.call_block_time)
)
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{% macro delta_v2_swap_settle(blockchain) %}
-- TODO: source this data from events, as multiple method emit the same events -- will be simpler to source from there
v2_swap_settle_withParsedOrderData AS (
SELECT
call_trace_address,
call_block_time,
call_block_number,
call_tx_hash,
orderWithSig,
executorData,
contract_address
FROM {{ source("paraswapdelta_"+ blockchain, "ParaswapDeltav2_call_swapSettle") }}
where call_success = true
{% if is_incremental() %}
AND {{ incremental_predicate('call_block_time') }}
{% endif %}
),
v2_swap_settle_parsedOrderWithSig AS (
SELECT
*
FROM
v2_swap_settle_withParsedOrderData
),
v2_swap_settle_unparsedOrders AS (
SELECT
JSON_EXTRACT(JSON_PARSE(TRY_CAST(orderWithSig AS VARCHAR)), '$.order') AS "order",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST(orderWithSig AS VARCHAR)), '$.signature') AS signature,
*
FROM v2_swap_settle_parsedOrderWithSig
),
v2_swap_settle_parsedOrders AS (
SELECT
from_hex(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.owner')) AS "order_owner",
FROM_HEX(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.srcToken')) AS "src_token",
FROM_HEX(JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.destToken')) AS "dest_token",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.srcAmount') AS "src_amount",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.destAmount') AS "dest_amount",
JSON_EXTRACT_SCALAR(JSON_PARSE(TRY_CAST("order" AS VARCHAR)), '$.permit') AS "permit",
-- NB: at the time of writting the only ExecutorData shape known is following. On adding new executors needs to be-reconsidered
-- struct ExecutorData {
-- // The address of the src token
-- address srcToken;
-- // The address of the dest token
-- address destToken;
-- // The amount of fee to be paid for the swap
-- uint256 feeAmount; <- the field in question
-- // The calldata to execute the swap
-- bytes calldataToExecute;
-- // The address to execute the swap
-- address executionAddress;
-- // The address to receive the fee, if not set the tx.origin will receive the fee
-- address feeRecipient;
-- }
varbinary_to_uint256(varbinary_substring(executorData, 161, 32)) as "feeAmount",
-- substring(executorData FROM 141 FOR 20) AS dynamic_extracted_dest_token
*
FROM v2_swap_settle_unparsedOrders
),
v2_swap_settle_withUSDs AS (
SELECT
{{to_wrapped_native_token(blockchain, 'dest_token', 'dest_token_for_joining')}},
{{to_wrapped_native_token(blockchain, 'src_token', 'src_token_for_joining')}},
*
FROM v2_swap_settle_parsedOrders
), delta_v2_swap_settle_model as (
SELECT
w.*,
w.dest_token AS fee_token,
COALESCE(CAST(s.price AS DECIMAL(38,18)), 0) AS src_token_price_usd,
COALESCE(CAST(d.price AS DECIMAL(38,18)), 0) AS dest_token_price_usd,
COALESCE(
d.price * CAST (w.feeAmount AS uint256) / POWER(10, d.decimals),
-- src cost

(s.price * CAST (w.src_amount AS uint256) / POWER(10, s.decimals))
* CAST (w.feeAmount AS DECIMAL) / (CAST (w.dest_amount AS DECIMAL)+ CAST (w.feeAmount AS DECIMAL)),
0

) AS gas_fee_usd,
s.price * CAST (w.src_amount AS uint256) / POWER(10, s.decimals) AS src_token_order_usd,
d.price * CAST (w.dest_amount AS uint256) / POWER(10, d.decimals) AS dest_token_order_usd

FROM v2_swap_settle_withUSDs w
LEFT JOIN {{ source('prices', 'usd') }} d
ON d.blockchain = '{{blockchain}}'
AND d.minute > TIMESTAMP '2024-06-01'
{% if is_incremental() %}
AND {{ incremental_predicate('d.minute') }}
{% endif %}
AND d.contract_address = w.dest_token_for_joining
AND d.minute = DATE_TRUNC('minute', w.call_block_time)
LEFT JOIN {{ source('prices', 'usd') }} s
ON s.blockchain = '{{blockchain}}'
AND s.minute > TIMESTAMP '2024-06-01'
{% if is_incremental() %}
AND {{ incremental_predicate('s.minute') }}
{% endif %}
AND s.contract_address = w.src_token_for_joining
AND s.minute = DATE_TRUNC('minute', w.call_block_time)
)
{% endmacro %}
Loading
Loading