Skip to content

Commit

Permalink
Migrate timeswap spells (#4445)
Browse files Browse the repository at this point in the history
* migrate timeswap sector

* migrate pools

* fix

* UINT256s all the way

* more UINT256

* more fixes, all UINT256 casting related

* fix

* went so deep on UINT256, I ended up casting where I shouldn`t, so here`s to uncasting

* actually I missed some UINT256 conversions

* migrate json extraction

* cast the extracted json to UINT256
  • Loading branch information
hildobby authored Sep 28, 2023
1 parent a68fc54 commit 2b3ba3b
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 559 deletions.
109 changes: 55 additions & 54 deletions models/timeswap/arbitrum/timeswap_arbitrum_borrow.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{{ config(
alias = alias('borrow'),
materialized = 'incremental',
file_format = 'delta',
incremental_strategy = 'merge',
unique_key = ['transaction_hash', 'pool_pair', 'maturity', 'strike'],
post_hook='{{ expose_spells(\'["arbitrum"]\',
tags = ['dunesql']
,alias = alias('borrow')
,materialized = 'incremental'
,file_format = 'delta'
,incremental_strategy = 'merge'
,unique_key = ['transaction_hash', 'pool_pair', 'maturity', 'strike']
,post_hook='{{ expose_spells(\'["arbitrum"]\',
"project",
"timeswap",
\'["raveena15, varunhawk19"]\') }}'
Expand All @@ -23,36 +24,36 @@ SELECT
b.strike as strike,
i.pool_pair as pool_pair,
i.chain as chain,
tx.from as user,
tx."from" as user,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals)
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals)
END as UINT256
) as token_amount,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals) * p.price
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals) * p.price
END as UINT256
) as usd_amount
FROM {{ source('timeswap_arbitrum', 'TimeswapV2PeripheryUniswapV3BorrowGivenPrincipal_evt_BorrowGivenPrincipal') }} b
JOIN {{ ref('timeswap_arbitrum_pools') }} i
ON CAST(b.maturity as VARCHAR(100)) = i.maturity
AND cast(b.strike as VARCHAR(100)) = i.strike
ON CAST(b.maturity as UINT256) = i.maturity
AND cast(b.strike as UINT256) = i.strike
JOIN {{ source('arbitrum', 'transactions') }} tx
on b.evt_tx_hash = tx.hash
{% if is_incremental() %}
and tx.block_time >= date_trunc("day", now() - interval '1 week')
and tx.block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
JOIN {{ source('prices', 'usd') }} p
ON p.symbol=i.token0_symbol
AND p.blockchain = 'arbitrum'
AND b.isToken0 = true
AND p.minute = date_trunc('minute',b.evt_block_time)
{% if is_incremental() %}
AND p.minute >= date_trunc("day", now() - interval '1 week')
WHERE b.evt_block_time >= date_trunc("day", now() - interval '1 week')
AND p.minute >= date_trunc('day', now() - interval '7' day)
WHERE b.evt_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}


Expand All @@ -67,36 +68,36 @@ SELECT
b.strike as strike,
i.pool_pair as pool_pair,
i.chain as chain,
tx.from as user,
tx."from" as user,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals)
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals)
END as UINT256
) as token_amount,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals) * p.price
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals) * p.price
END as UINT256
) as usd_amount
FROM {{ source('timeswap_arbitrum', 'TimeswapV2PeripheryUniswapV3BorrowGivenPrincipal_evt_BorrowGivenPrincipal') }} b
JOIN {{ ref('timeswap_arbitrum_pools') }} i
ON CAST(b.maturity as VARCHAR(100)) = i.maturity
AND cast(b.strike as VARCHAR(100)) = i.strike
ON CAST(b.maturity as UINT256) = i.maturity
AND cast(b.strike as UINT256) = i.strike
JOIN {{ source('arbitrum', 'transactions') }} tx
on b.evt_tx_hash = tx.hash
{% if is_incremental() %}
and tx.block_time >= date_trunc("day", now() - interval '1 week')
and tx.block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
JOIN {{ source('prices', 'usd') }} p
ON p.symbol=i.token1_symbol
AND p.blockchain = 'arbitrum'
AND b.isToken0 = false
AND p.minute = date_trunc('minute',b.evt_block_time)
{% if is_incremental() %}
AND p.minute >= date_trunc("day", now() - interval '1 week')
WHERE b.evt_block_time >= date_trunc("day", now() - interval '1 week')
AND p.minute >= date_trunc('day', now() - interval '7' day)
WHERE b.evt_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}


Expand All @@ -114,36 +115,36 @@ SELECT
b.strike as strike,
i.pool_pair as pool_pair,
i.chain as chain,
tx.from as user,
tx."from" as user,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals)
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals)
END as UINT256
) as token_amount,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals) * p.price
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals) * p.price
END as UINT256
) as usd_amount
FROM {{ source('timeswap_arbitrum', 'TimeswapV2PeripheryNoDexBorrowGivenPrincipal_evt_BorrowGivenPrincipal') }} b
JOIN {{ ref('timeswap_arbitrum_pools') }} i
ON CAST(b.maturity as VARCHAR(100)) = i.maturity
AND cast(b.strike as VARCHAR(100)) = i.strike
ON CAST(b.maturity as UINT256) = i.maturity
AND cast(b.strike as UINT256) = i.strike
JOIN {{ source('arbitrum', 'transactions') }} tx
on b.evt_tx_hash = tx.hash
{% if is_incremental() %}
and tx.block_time >= date_trunc("day", now() - interval '1 week')
and tx.block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
JOIN {{ source('prices', 'usd') }} p
ON p.symbol=i.token0_symbol
AND p.blockchain = 'arbitrum'
AND b.isToken0 = true
AND p.minute = date_trunc('minute',b.evt_block_time)
{% if is_incremental() %}
AND p.minute >= date_trunc("day", now() - interval '1 week')
WHERE b.evt_block_time >= date_trunc("day", now() - interval '1 week')
AND p.minute >= date_trunc('day', now() - interval '7' day)
WHERE b.evt_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}


Expand All @@ -158,36 +159,36 @@ SELECT
b.strike as strike,
i.pool_pair as pool_pair,
i.chain as chain,
tx.from as user,
tx."from" as user,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals)
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals)
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals)
END as UINT256
) as token_amount,
CAST(
CASE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS DOUBLE) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS DOUBLE) / power(10,i.token1_decimals) * p.price
END as DOUBLE
WHEN CAST(b.isToken0 AS BOOLEAN) = true THEN CAST(b.tokenAmount AS UINT256) / power(10,i.token0_decimals) * p.price
ELSE CAST(b.tokenAmount AS UINT256) / power(10,i.token1_decimals) * p.price
END as UINT256
) as usd_amount
FROM {{ source('timeswap_arbitrum', 'TimeswapV2PeripheryNoDexBorrowGivenPrincipal_evt_BorrowGivenPrincipal') }} b
JOIN {{ ref('timeswap_arbitrum_pools') }} i
ON CAST(b.maturity as VARCHAR(100)) = i.maturity
AND cast(b.strike as VARCHAR(100)) = i.strike
ON CAST(b.maturity as UINT256) = i.maturity
AND cast(b.strike as UINT256) = i.strike
JOIN {{ source('arbitrum', 'transactions') }} tx
on b.evt_tx_hash = tx.hash
{% if is_incremental() %}
and tx.block_time >= date_trunc("day", now() - interval '1 week')
and tx.block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}
JOIN {{ source('prices', 'usd') }} p
ON p.symbol=i.token1_symbol
AND p.blockchain = 'arbitrum'
AND b.isToken0 = false
AND p.minute = date_trunc('minute',b.evt_block_time)
{% if is_incremental() %}
AND p.minute >= date_trunc("day", now() - interval '1 week')
WHERE b.evt_block_time >= date_trunc("day", now() - interval '1 week')
AND p.minute >= date_trunc('day', now() - interval '7' day)
WHERE b.evt_block_time >= date_trunc('day', now() - interval '7' day)
{% endif %}


Loading

0 comments on commit 2b3ba3b

Please sign in to comment.