Skip to content

Commit

Permalink
Rename chain_ids > chain_info_chain_ids hotfix (duneanalytics#3432)
Browse files Browse the repository at this point in the history
* rename file properly

* ANTI > LEFT JOIN WHERE NULL

* Apply cast to chain_id

* Add size

* Revert "Add size"

This reverts commit 9537e3e.

* Revert "Apply cast to chain_id"

This reverts commit dbae438.

* cast all to decimal38

* fix attempt #1

* cast numbers to double

* Apply cast()

---------

Co-authored-by: Huang Geyang <Sukebeta@outlook.com>
  • Loading branch information
hildobby and Hosuke committed Jun 13, 2023
1 parent 989a596 commit 679a342
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
51 changes: 26 additions & 25 deletions models/bridge/bridge_flows.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ WITH bridge_protocols AS (
ELSE 'na'
END AS transfer_type
, token_symbol
, token_amount
, token_amount_usd
, token_amount_raw
, fee_amount
, fee_amount_usd
, fee_amount_raw
, CAST(token_amount AS double) AS token_amount
, CAST(token_amount_usd AS double) AS token_amount_usd
, CAST(token_amount_raw AS double) AS token_amount_raw
, CAST(fee_amount AS double) AS fee_amount
, CAST(fee_amount_usd AS double) AS fee_amount_usd
, CAST(fee_amount_raw AS double) AS fee_amount_raw
, token_address
, fee_address
, source_chain_id
, destination_chain_id
, CAST(bmod.source_chain_id AS DECIMAL(38, 0)) AS source_chain_id
, CAST(bmod.destination_chain_id AS DECIMAL(38, 0)) AS destination_chain_id
, cid_source.chain_name AS source_chain_name
, cid_dest.chain_name AS destination_chain_name
, is_native_bridge
Expand All @@ -57,10 +57,10 @@ WITH bridge_protocols AS (
, trace_address
, tx_method_id
FROM {{ bridge_protocol_model }} bmod
LEFT JOIN {{ ref('chain_ids') }} cid_source
ON cid_source.chain_id = bmod.source_chain_id
LEFT JOIN {{ ref('chain_ids') }} cid_dest
ON cid_dest.chain_id = bmod.destination_chain_id
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_source
ON CAST(cid_source.chain_id AS DECIMAL(38, 0)) = CAST(bmod.source_chain_id AS DECIMAL(38, 0))
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_dest
ON CAST(cid_dest.chain_id AS DECIMAL(38, 0)) = CAST(bmod.destination_chain_id AS DECIMAL(38, 0))
{% if not loop.last %}
UNION ALL
{% endif %}
Expand Down Expand Up @@ -89,16 +89,16 @@ WITH bridge_protocols AS (
ELSE 'na'
END AS transfer_type
, token_symbol
, token_amount
, token_amount_usd -- changed
, token_amount_raw
, fee_amount
, fee_amount_usd
, fee_amount_raw
, CAST(token_amount AS double) AS token_amount
, CAST(token_amount_usd AS double) AS token_amount_usd -- changed
, CAST(token_amount_raw AS double) AS token_amount_raw
, CAST(fee_amount AS double) AS fee_amount
, CAST(fee_amount_usd AS double) AS fee_amount_usd
, CAST(fee_amount_raw AS double) AS fee_amount_raw
, token_address
, fee_address
, source_chain_id
, destination_chain_id
, CAST(bmod.source_chain_id AS DECIMAL(38, 0)) AS source_chain_id
, CAST(bmod.destination_chain_id AS DECIMAL(38, 0)) AS destination_chain_id
, cid_source.chain_name AS source_chain_name
, cid_dest.chain_name AS destination_chain_name
, is_native_bridge
Expand All @@ -109,23 +109,24 @@ WITH bridge_protocols AS (
, trace_address
, tx_method_id
FROM {{ native_bridge_model }} bmod
LEFT JOIN {{ ref('chain_ids') }} cid_source
ON cid_source.chain_id = bmod.source_chain_id
LEFT JOIN {{ ref('chain_ids') }} cid_dest
ON cid_dest.chain_id = bmod.destination_chain_id
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_source
ON CAST(cid_source.chain_id AS DECIMAL(38, 0)) = CAST(bmod.source_chain_id AS DECIMAL(38, 0))
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_dest
ON CAST(cid_dest.chain_id AS DECIMAL(38, 0)) = CAST(bmod.destination_chain_id AS DECIMAL(38, 0))
{% if not loop.last %}
UNION ALL
{% endif %}
{% endfor %}
) nat
-- Exclude native bridges where a bridge protocol was used. Assign the bridge to the bridge protocol.
LEFT ANTI JOIN bridge_protocols prot
LEFT JOIN bridge_protocols prot
ON prot.block_date = nat.block_date
AND prot.blockchain = nat.blockchain
AND prot.block_number = nat.block_number
AND prot.tx_hash = nat.tx_hash
AND prot.token_address = nat.token_address
-- Eventual improvement: See if we can join on event (i.e. tie a Hop event with a standard bridge event)
WHERE prot.blockchain IS NULL
)


Expand Down
4 changes: 2 additions & 2 deletions models/bridge/optimism/optimism_standard_bridge_flows.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ LEFT JOIN {{ source('prices', 'usd') }} p
AND p.minute >= (NOW() - interval '14 days')
{% endif %}

LEFT JOIN {{ ref('chain_ids') }} cid_source
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_source
ON cid_source.chain_id = tf.source_chain_id
LEFT JOIN {{ ref('chain_ids') }} cid_dest
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_dest
ON cid_dest.chain_id = tf.destination_chain_id
File renamed without changes.
16 changes: 8 additions & 8 deletions models/hop_protocol/optimism/hop_protocol_optimism_flows.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ FROM (
,ts.evt_index
,ts.contract_address AS project_contract_address
, ts.`transferId` AS transfer_id
, (SELECT chain_id FROM {{ ref('chain_ids') }} WHERE lower(chain_name) = 'optimism') AS source_chain_id
, (SELECT chain_id FROM {{ ref('chain_info_chain_ids') }} WHERE lower(chain_name) = 'optimism') AS source_chain_id
,chainId AS destination_chain_id

FROM {{ source('hop_protocol_optimism', 'L2_OptimismBridge_evt_TransferSent') }} ts
Expand All @@ -83,7 +83,7 @@ FROM (
,tl.contract_address AS project_contract_address
, '' AS transfer_id
, 1 AS source_chain_id
, (SELECT chain_id FROM {{ ref('chain_ids') }} WHERE lower(chain_name) = 'optimism') AS destination_chain_id
, (SELECT chain_id FROM {{ ref('chain_info_chain_ids') }} WHERE lower(chain_name) = 'optimism') AS destination_chain_id

FROM {{ source ('hop_protocol_optimism', 'L2_OptimismBridge_evt_TransferFromL1Completed') }} tl
{% if is_incremental() %}
Expand All @@ -105,13 +105,13 @@ FROM (
,wb.contract_address AS project_contract_address
, wb.transferId AS transfer_id
, CASE
WHEN arb.transferId IS NOT NULL THEN (SELECT chain_id FROM {{ ref('chain_ids') }} WHERE lower(chain_name) = 'arbitrum one')
WHEN poly.transferId IS NOT NULL THEN (SELECT chain_id FROM {{ ref('chain_ids') }} WHERE lower(chain_name) = 'polygon mainnet')
WHEN gno.transferId IS NOT NULL THEN (SELECT chain_id FROM {{ ref('chain_ids') }} WHERE lower(chain_name) = 'gnosis')
WHEN arb.transferId IS NOT NULL THEN (SELECT chain_id FROM {{ ref('chain_info_chain_ids') }} WHERE lower(chain_name) = 'arbitrum one')
WHEN poly.transferId IS NOT NULL THEN (SELECT chain_id FROM {{ ref('chain_info_chain_ids') }} WHERE lower(chain_name) = 'polygon mainnet')
WHEN gno.transferId IS NOT NULL THEN (SELECT chain_id FROM {{ ref('chain_info_chain_ids') }} WHERE lower(chain_name) = 'gnosis')
ELSE NULL
END
AS source_chain_id
, (SELECT chain_id FROM {{ ref('chain_ids') }} WHERE lower(chain_name) = 'optimism') AS destination_chain_id
, (SELECT chain_id FROM {{ ref('chain_info_chain_ids') }} WHERE lower(chain_name) = 'optimism') AS destination_chain_id
FROM {{ source ('hop_protocol_optimism', 'L2_OptimismBridge_evt_WithdrawalBonded') }} wb
LEFT JOIN {{ source ('hop_protocol_arbitrum' ,'L2_ArbitrumBridge_evt_TransferSent') }} arb
ON arb.evt_block_time BETWEEN (wb.evt_block_time - interval '30 days') AND (wb.evt_block_time + interval '1 day') --usually < ~20 mins, but extending longer for safety & OP blocktimestamp fix (used to have ~15 min delay)
Expand Down Expand Up @@ -158,7 +158,7 @@ LEFT JOIN {{ source('prices', 'usd') }} p
AND p.minute >= (NOW() - interval '14 days')
{% endif %}

LEFT JOIN {{ ref('chain_ids') }} cid_source
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_source
ON cid_source.chain_id = tf.source_chain_id
LEFT JOIN {{ ref('chain_ids') }} cid_dest
LEFT JOIN {{ ref('chain_info_chain_ids') }} cid_dest
ON cid_dest.chain_id = tf.destination_chain_id

0 comments on commit 679a342

Please sign in to comment.