Skip to content

Commit

Permalink
add paraswap delta v1 orders models -- both single_swap and safe_sett…
Browse files Browse the repository at this point in the history
…le_batch_swap
  • Loading branch information
alexshchur committed Dec 22, 2024
1 parent b4ca1ca commit 54780f0
Show file tree
Hide file tree
Showing 5 changed files with 745 additions and 0 deletions.
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 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,
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(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(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 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 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,95 @@
{%
-- 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
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(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(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 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 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 %}
58 changes: 58 additions & 0 deletions dbt_subprojects/dex/models/_projects/paraswap/ethereum/_schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,64 @@ models:
- *trace_address
- *evt_index


- name: paraswap_delta_v1_ethereum_trades
description: "Paraswap Delta V1 trades"
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- method
- call_tx_hash
- call_trace_address
columns:
- name: method
description: "Call Method"
- name: call_trace_address
description: "Call Trace Address"
- name: call_block_number
description: "Call Block Number"
- name: call_block_time
description: "Call Block Time"
- name: call_tx_hash
description: "Call Transaction Hash"
- name: fee_amount
description: "Fee Amount"
- name: order_with_sig
description: "Order with Signature"
- name: calldata_to_execute
description: "Calldata to Execute"
- name: order
description: "Order"
- name: signature
description: "Signature"
- name: order_owner
description: "Order Owner"
- name: src_token
description: "Source Token"
- name: dest_token
description: "Destination Token"
- name: src_amount
description: "Source Amount"
- name: dest_amount
description: "Destination Amount"
- name: src_token_for_joining
description: "Source Token for Joining"
- name: dest_token_for_joining
description: "Destination Token for Joining"
- name: fee_token
description: "Fee Token"
- name: src_token_price_usd
description: "Source Token Price (USD)"
- name: dest_token_price_usd
description: "Destination Token Price (USD)"
- name: gas_fee_usd
description: "Gas Fee (USD)"
- name: src_token_order_usd
description: "Source Token Order Value (USD)"
- name: dest_token_order_usd
description: "Destination Token Order Value (USD)"
- name: contract_address
description: "Contract Address"

- name: paraswap_v6_ethereum_trades_decoded
description: "Paraswap V6 trades decoded"
Expand Down
Loading

0 comments on commit 54780f0

Please sign in to comment.