diff --git a/dbt_subprojects/daily_spellbook/macros/project/evaa/evaa_ton_pools.sql b/dbt_subprojects/daily_spellbook/macros/project/evaa/evaa_ton_pools.sql index 8a34c61cd35..7246d0f8dee 100644 --- a/dbt_subprojects/daily_spellbook/macros/project/evaa/evaa_ton_pools.sql +++ b/dbt_subprojects/daily_spellbook/macros/project/evaa/evaa_ton_pools.sql @@ -2,11 +2,11 @@ -%} {# Contains pool addresses #} -SELECT pool_address, pool_name, v4_upgrate_lt FROM (VALUES - ('0:BCAD466A47FA565750729565253CD073CA24D856804499090C2100D95C809F9E', 'Main pool', 49828980000001), - ('0:489595F65115A45C24A0DD0176309654FB00B95E40682F0C3E85D5A4D86DFB25', 'LP pool', 49712577000001), - ('0:0D511552DDF8413BD6E2BE2837E22C89422F7B16131BA62BE8D5A504012D8661', 'Alts pool', 0), - ('0:9D21D5DFD6403FD8777D99B1B34850C43C0F8FC7E7ADF2A4D61C45E0446A342B', 'Stable pool', 0) - ) AS T(pool_address, pool_name, v4_upgrate_lt) +SELECT pool_address, pool_name, v4_upgrate_lt, v9_upgrate_lt FROM (VALUES + ('0:BCAD466A47FA565750729565253CD073CA24D856804499090C2100D95C809F9E', 'Main pool', 49828980000001, 61426459000001), + ('0:489595F65115A45C24A0DD0176309654FB00B95E40682F0C3E85D5A4D86DFB25', 'LP pool', 49712577000001, 61359759000001), + ('0:0D511552DDF8413BD6E2BE2837E22C89422F7B16131BA62BE8D5A504012D8661', 'Alts pool', 0, 61187409000001), + ('0:9D21D5DFD6403FD8777D99B1B34850C43C0F8FC7E7ADF2A4D61C45E0446A342B', 'Stable pool', 0, 61359759000001) + ) AS T(pool_address, pool_name, v4_upgrate_lt, v9_upgrate_lt) {%- endmacro -%} \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_assets.sql b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_assets.sql index 89b5b61b63d..04543a7f29b 100644 --- a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_assets.sql +++ b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_assets.sql @@ -28,5 +28,6 @@ SELECT asset_id, asset_name, jetton_master, decimals FROM (CAST('101563884026323503647891287974015286987607783840172791059852695820980647056177' AS UINT256), 'CATI', '0:FE72F474373E97032441BDB873F9A6D3AD10BAB08E6DBC7BEFA5E42B695F5400', 9), (CAST('33604868692898791249369426189145713090064546741393719833658701125733712580919' AS UINT256), 'tsUSDe', '0:D0E545323C7ACB7102653C073377F7E3C67F122EB94D430A250739F109D4A57D', 6), (CAST('98281638255104512379049519410242269170317135545117667048087651483812279009354' AS UINT256), 'USDe', '0:086FA2A675F74347B08DD4606A549B8FDB98829CB282BC1949D3B12FBAED9DCC', 6), - (CAST('6111297782570570129290420344884587495512746870153062431820826875465825931443' AS UINT256), 'PT_tsUSDe_01Sep2025', '0:DBF7406CB39167232ABB54C9A71B651E648CD9A2642C20BC3D1E3A35C31685D5', 9) + (CAST('6111297782570570129290420344884587495512746870153062431820826875465825931443' AS UINT256), 'PT_tsUSDe_01Sep2025', '0:DBF7406CB39167232ABB54C9A71B651E648CD9A2642C20BC3D1E3A35C31685D5', 9), + (CAST('45271267922377506789669073275694049849109676194656489600278771174506032218722' AS UINT256), 'TONUSDT_STONFI', '0:8649CAD97B5C5BC96A960EF748EA6CCFF8601C01616FE995EE6893AE4AA7A6C6', 9) ) AS temp (asset_id, asset_name, jetton_master, decimals) diff --git a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_liquidate.sql b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_liquidate.sql index 069935ad4cc..c0e42123988 100644 --- a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_liquidate.sql +++ b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_liquidate.sql @@ -16,12 +16,21 @@ -- log::liquidate_success is sent by the router and contains all the data regarding the liquidation -- https://github.com/evaafi/contracts/blob/d5a6bf889f8bbfa8bcc82671c17e65a3b2b360cd/contracts/core/master-liquidate.fc#L353-L377 +-- https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-liquidate.fc#L399-L423 WITH evaa_ton_pools AS ( {{ evaa_ton_pools() }} ), source_data AS ( - SELECT M.block_date, M.tx_hash, M.trace_id, M.tx_now, M.tx_lt, pool_address, pool_name, tx_lt <= v4_upgrate_lt AS is_pre_v4, body_boc + SELECT M.block_date, M.tx_hash, M.trace_id, M.tx_now, M.tx_lt, pool_address, pool_name, + tx_lt <= v4_upgrate_lt AS is_pre_v4, + tx_lt <= v9_upgrate_lt AS is_pre_v9, + CASE + WHEN tx_lt <= v4_upgrate_lt THEN 'v3' + WHEN v4_upgrate_lt < tx_lt AND tx_lt <= v9_upgrate_lt THEN 'v4' + ELSE 'v9' + END AS protocol_version, + body_boc FROM {{ source('ton', 'messages') }} M JOIN evaa_ton_pools ON M.source = pool_address WHERE M.direction = 'out' AND M.destination IS NULL -- ext out message @@ -88,24 +97,69 @@ select {{ ton_from_boc('body_boc', [ ton_load_int(64, 'new_collateral_total_borrow'), ton_load_uint(64, 'collateral_s_rate'), ton_load_uint(64, 'collateral_b_rate') - ]) }} as result, * FROM source_data WHERE NOT is_pre_v4 + ]) }} as result, * FROM source_data WHERE is_pre_v4 = false AND is_pre_v9 = true +), parse_output_postv9 as ( +select {{ ton_from_boc('body_boc', [ + ton_begin_parse(), + ton_load_uint(8, 'opcode'), + ton_load_address('owner_address'), + ton_load_address('sender_address'), + ton_load_address('liquidator_address'), + ton_load_uint(32, '_current_time'), + ton_load_int(16, 'subaccount_id'), + ton_load_ref(), + ton_begin_parse(), + ton_load_uint(256, 'transferred_asset_id'), + ton_load_uint(64, 'transferred_amount'), + ton_load_int(64, 'new_user_loan_principal'), + ton_load_int(64, 'loan_new_total_supply'), + ton_load_int(64, 'loan_new_total_borrow'), + ton_load_uint(64, 'loan_s_rate'), + ton_load_uint(64, 'loan_b_rate'), + ton_restart_parse(), + ton_begin_parse(), + ton_skip_refs(1), + ton_load_ref(), + ton_begin_parse(), + ton_load_uint(256, 'collateral_asset_id'), + ton_load_uint(64, 'collateral_reward'), + ton_load_int(64, 'new_user_collateral_principal'), + ton_load_int(64, 'new_collateral_total_supply'), + ton_load_int(64, 'new_collateral_total_borrow'), + ton_load_uint(64, 'collateral_s_rate'), + ton_load_uint(64, 'collateral_b_rate') + ]) }} as result, * FROM source_data WHERE is_pre_v9 = false ) -select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, +select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, result.owner_address, result.sender_address, null AS liquidator_address, result.transferred_asset_id, result.transferred_amount, result.new_user_loan_principal, result.loan_new_total_supply, result.loan_new_total_borrow, CAST(result.loan_s_rate AS bigint) AS loan_s_rate, CAST(result.loan_b_rate AS bigint) AS loan_b_rate,-- should be less than 2^64 result.collateral_asset_id, result.collateral_reward, result.new_user_collateral_principal, result.new_collateral_total_supply, result.new_collateral_total_borrow, -CAST(result.collateral_s_rate AS bigint) AS collateral_s_rate, CAST(result.collateral_b_rate AS bigint) AS collateral_b_rate -- should be less than 2^64 +CAST(result.collateral_s_rate AS bigint) AS collateral_s_rate, CAST(result.collateral_b_rate AS bigint) AS collateral_b_rate, -- should be less than 2^64 +CAST(NULL AS int) AS subaccount_id FROM parse_output_prev4 UNION ALL -select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, +select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, +result.owner_address, result.sender_address, result.liquidator_address, result.transferred_asset_id, +result.transferred_amount, result.new_user_loan_principal, result.loan_new_total_supply, +result.loan_new_total_borrow, CAST(result.loan_s_rate AS bigint) AS loan_s_rate, CAST(result.loan_b_rate AS bigint) AS loan_b_rate,-- should be less than 2^64 +result.collateral_asset_id, result.collateral_reward, result.new_user_collateral_principal, +result.new_collateral_total_supply, result.new_collateral_total_borrow, +CAST(result.collateral_s_rate AS bigint) AS collateral_s_rate, CAST(result.collateral_b_rate AS bigint) AS collateral_b_rate, -- should be less than 2^64 +CAST(NULL AS int) AS subaccount_id +FROM parse_output_postv4 + +UNION ALL + +select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, result.owner_address, result.sender_address, result.liquidator_address, result.transferred_asset_id, result.transferred_amount, result.new_user_loan_principal, result.loan_new_total_supply, result.loan_new_total_borrow, CAST(result.loan_s_rate AS bigint) AS loan_s_rate, CAST(result.loan_b_rate AS bigint) AS loan_b_rate,-- should be less than 2^64 result.collateral_asset_id, result.collateral_reward, result.new_user_collateral_principal, result.new_collateral_total_supply, result.new_collateral_total_borrow, -CAST(result.collateral_s_rate AS bigint) AS collateral_s_rate, CAST(result.collateral_b_rate AS bigint) AS collateral_b_rate -- should be less than 2^64 -FROM parse_output_postv4 \ No newline at end of file +CAST(result.collateral_s_rate AS bigint) AS collateral_s_rate, CAST(result.collateral_b_rate AS bigint) AS collateral_b_rate, -- should be less than 2^64 +CAST(result.subaccount_id AS int) AS subaccount_id +FROM parse_output_postv9 \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_schema.yml b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_schema.yml index 10933af71a2..f8faa6283da 100644 --- a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_schema.yml +++ b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_schema.yml @@ -7,7 +7,7 @@ models: sector: lending contributors: pshuvalov config: - tags: ['ton', 'evaa', 'lending'] + tags: ["ton", "evaa", "lending"] description: > EVAA protocol withdraw events data_tests: @@ -30,12 +30,16 @@ models: description: "EVAA pool address" - name: pool_name description: "EVAA pool name" + - name: protocol_version + description: "Protocol version (v3, v4, v9)" - name: owner_address description: "owner address" - name: sender_address description: "user smart contract address" - name: recipient_address - description: "recipient address (only for after v4 protcool upgrade, otherwise null)" + description: "recipient address (only for after v4 protocol upgrade, otherwise null)" + - name: subaccount_id + description: "subaccount id (only for v9 protocol upgrade, otherwise null)" - name: asset_id description: "asset id" - name: withdraw_amount_current @@ -56,7 +60,7 @@ models: sector: lending contributors: pshuvalov config: - tags: ['ton', 'evaa', 'lending'] + tags: ["ton", "evaa", "lending"] description: > EVAA protocol supply events data_tests: @@ -79,6 +83,10 @@ models: description: "EVAA pool address" - name: pool_name description: "EVAA pool name" + - name: protocol_version + description: "Protocol version (v3, v4, v9)" + - name: subaccount_id + description: "subaccount id (only for v9 protocol upgrade, otherwise null)" - name: owner_address description: "owner address" - name: sender_address @@ -103,7 +111,7 @@ models: sector: lending contributors: pshuvalov config: - tags: ['ton', 'evaa', 'lending'] + tags: ["ton", "evaa", "lending"] description: > EVAA protocol liquidate events data_tests: @@ -126,6 +134,10 @@ models: description: "EVAA pool address" - name: pool_name description: "EVAA pool name" + - name: protocol_version + description: "Protocol version (v3, v4, v9)" + - name: subaccount_id + description: "subaccount id (only for v9 protocol upgrade, otherwise null)" - name: owner_address description: "owner address" - name: sender_address @@ -166,7 +178,7 @@ models: sector: lending contributors: pshuvalov config: - tags: ['ton', 'evaa', 'lending'] + tags: ["ton", "evaa", "lending"] description: > EVAA protocol assets mapping data_tests: diff --git a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_supply.sql b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_supply.sql index 9d61b5f193b..09a3d18a9cd 100644 --- a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_supply.sql +++ b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_supply.sql @@ -15,13 +15,22 @@ -- log::supply_success is sent by the router and contains all the data regarding the supply --- https://github.com/evaafi/contracts/blob/d5a6bf889f8bbfa8bcc82671c17e65a3b2b360cd/contracts/core/master-supply.fc#L239-L254 +-- https://github.com/evaafi/contracts/blob/d5a6bf889f8bbfa8bcc82671c17e65a3b2b360cd/contracts/core/master-supply.fc#L239-L254 (V4 parsing) +-- https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-supply.fc#L316-L331 (v9 parsing) WITH evaa_ton_pools AS ( {{ evaa_ton_pools() }} ), parsed_boc AS ( - SELECT M.block_date, M.tx_hash, M.trace_id, M.tx_now, M.tx_lt, pool_address, pool_name, body_boc + SELECT M.block_date, M.tx_hash, M.trace_id, M.tx_now, M.tx_lt, pool_address, pool_name, + tx_lt <= v4_upgrate_lt AS is_pre_v4, + tx_lt <= v9_upgrate_lt AS is_pre_v9, + CASE + WHEN tx_lt <= v4_upgrate_lt THEN 'v3' + WHEN v4_upgrate_lt < tx_lt AND tx_lt <= v9_upgrate_lt THEN 'v4' + ELSE 'v9' + END AS protocol_version, + body_boc FROM {{ source('ton', 'messages') }} M JOIN evaa_ton_pools ON M.source = pool_address WHERE M.direction = 'out' AND M.destination IS NULL -- ext out message @@ -47,10 +56,47 @@ select {{ ton_from_boc('body_boc', [ ton_load_uint(64, 's_rate'), ton_load_uint(64, 'b_rate') ]) }} as result, * from parsed_boc + WHERE is_pre_v4 = true +), parse_output_postv4 as ( +select {{ ton_from_boc('body_boc', [ + ton_begin_parse(), + ton_load_uint(8, 'opcode'), + ton_load_address('owner_address'), + ton_load_address('sender_address'), + ton_load_uint(32, '_current_time'), + ton_load_int(16, 'subaccount_id'), + ton_load_ref(), + ton_begin_parse(), + ton_load_uint(256, 'asset_id'), + ton_load_uint(64, 'amount_supplied'), + ton_load_int(64, 'user_new_principal'), + ton_load_int(64, 'new_total_supply'), + ton_load_int(64, 'new_total_borrow'), + ton_load_uint(64, 's_rate'), + ton_load_uint(64, 'b_rate') + ]) }} as result, * from parsed_boc + WHERE is_pre_v4 = false +), combined_results as ( + select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, + result.owner_address, result.sender_address, result.asset_id, + result.amount_supplied, result.user_new_principal, result.new_total_supply, + result.new_total_borrow, + CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate, -- should be less than 2^64 + CAST(NULL AS int) AS subaccount_id + from parse_output + + UNION ALL + + select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, + result.owner_address, result.sender_address, result.asset_id, + result.amount_supplied, result.user_new_principal, result.new_total_supply, + result.new_total_borrow, + CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate, -- should be less than 2^64 + CAST(result.subaccount_id AS int) AS subaccount_id + from parse_output_postv4 ) -select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, -result.owner_address, result.sender_address, result.asset_id, -result.amount_supplied, result.user_new_principal, result.new_total_supply, -result.new_total_borrow, -CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate -- should be less than 2^64 -from parse_output \ No newline at end of file +select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, +owner_address, sender_address, asset_id, +amount_supplied, user_new_principal, new_total_supply, +new_total_borrow, s_rate, b_rate, subaccount_id +from combined_results \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_withdraw.sql b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_withdraw.sql index e9da14c1b12..59cfd9c94fd 100644 --- a/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_withdraw.sql +++ b/dbt_subprojects/daily_spellbook/models/evaa/ton/evaa_ton_withdraw.sql @@ -16,12 +16,21 @@ -- log::withdraw_success is sent by the router and contains all the data regarding the withdrawal -- https://github.com/evaafi/contracts/blob/d5a6bf889f8bbfa8bcc82671c17e65a3b2b360cd/contracts/core/master-withdrawal.fc#L187-L203 +-- op::withdraw is deprecated in the EVAA V9, just uses supply/withdraw instead of it. +-- https://github.com/evaafi/contracts/blob/d9138cb24f03b53522774351aceb38c51a047eee/contracts/core/master-supply-withdrawal.fc#L718-L745 WITH evaa_ton_pools AS ( {{ evaa_ton_pools() }} ), source_data AS ( - SELECT M.block_date, M.tx_hash, M.trace_id, M.tx_now, M.tx_lt, pool_address, pool_name, tx_lt <= v4_upgrate_lt AS is_pre_v4, body_boc + SELECT M.block_date, M.tx_hash, M.trace_id, M.tx_now, M.tx_lt, pool_address, pool_name, + CASE + WHEN tx_lt <= v4_upgrate_lt THEN 'v3' + WHEN v4_upgrate_lt < tx_lt AND tx_lt <= v9_upgrate_lt THEN 'v4' + ELSE 'v9' + END AS protocol_version, + body_boc, + bitwise_right_shift(opcode, 24) AS log_type FROM {{ source('ton', 'messages') }} M JOIN evaa_ton_pools ON M.source = pool_address WHERE M.direction = 'out' AND M.destination IS NULL -- ext out message @@ -29,8 +38,8 @@ source_data AS ( {% if is_incremental() %} AND {{ incremental_predicate('M.block_date') }} {% endif %} - AND bitwise_right_shift(opcode, 24) = 2 -- log::withdraw_success, 0x2 -), parse_output_prev4 as ( + AND (bitwise_right_shift(opcode, 24) = 2 OR bitwise_right_shift(opcode, 24) = 22) -- log::withdraw_success (0x2) or log::supply_withdraw_success (0x16) +), parse_output_v3 as ( select {{ ton_from_boc('body_boc', [ ton_begin_parse(), ton_load_uint(8, 'opcode'), @@ -47,8 +56,8 @@ select {{ ton_from_boc('body_boc', [ ton_load_int(64, 'new_total_borrow'), ton_load_uint(64, 's_rate'), ton_load_uint(64, 'b_rate') - ]) }} as result, * FROM source_data WHERE is_pre_v4 -), parse_output_postv4 as ( + ]) }} as result, * FROM source_data WHERE protocol_version = 'v3' AND log_type = 2 +), parse_output_v4 as ( select {{ ton_from_boc('body_boc', [ ton_begin_parse(), ton_load_uint(8, 'opcode'), @@ -66,20 +75,90 @@ select {{ ton_from_boc('body_boc', [ ton_load_int(64, 'new_total_borrow'), ton_load_uint(64, 's_rate'), ton_load_uint(64, 'b_rate') - ]) }} as result, * FROM source_data WHERE NOT is_pre_v4 -) -select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, -result.owner_address, result.sender_address, null AS recipient_address, result.asset_id, -result.withdraw_amount_current, result.user_new_principal, result.new_total_supply, -result.new_total_borrow, -CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate -- should be less than 2^64 -FROM parse_output_prev4 + ]) }} as result, * FROM source_data WHERE protocol_version = 'v4' AND log_type = 2 +), parse_output_v9_supply_withdraw as ( +select {{ ton_from_boc('body_boc', [ + ton_begin_parse(), + ton_load_uint(8, 'opcode'), + ton_load_address('owner_address'), + ton_load_address('sender_address'), + ton_load_address('recipient_address'), + ton_load_uint(32, '_current_time'), + ton_load_int(16, 'subaccount_id'), + ton_load_ref(), + ton_begin_parse(), + ton_load_uint(256, 'supply_asset_id'), + ton_load_uint(64, 'supply_amount'), + ton_load_int(64, 'supply_new_principal'), + ton_load_int(64, 'new_total_supply_in'), + ton_load_int(64, 'new_total_borrow_in'), + ton_load_uint(64, 's_rate_in'), + ton_load_uint(64, 'b_rate_in'), + ton_load_ref(), + ton_begin_parse(), + ton_load_uint(256, 'withdraw_asset_id'), + ton_load_uint(64, 'withdraw_amount'), + ton_load_int(64, 'withdraw_new_principal'), + ton_load_int(64, 'new_total_supply_out'), + ton_load_int(64, 'new_total_borrow_out'), + ton_load_uint(64, 's_rate_out'), + ton_load_uint(64, 'b_rate_out') + ]) }} as result, * FROM source_data WHERE protocol_version = 'v9' AND log_type = 22 +), combined_results as ( + -- Withdraw records from regular withdraw transactions (v3) + select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, + result.owner_address, result.sender_address, null AS recipient_address, + result.asset_id, result.withdraw_amount_current, result.user_new_principal, + result.new_total_supply, result.new_total_borrow, + CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate, + CAST(NULL AS int) AS subaccount_id, + CAST(NULL AS bigint) AS amount_supplied + FROM parse_output_v3 + + UNION ALL -UNION ALL + -- Withdraw records from regular withdraw transactions (v4) + select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, + result.owner_address, result.sender_address, result.recipient_address, + result.asset_id, result.withdraw_amount_current, result.user_new_principal, + result.new_total_supply, result.new_total_borrow, + CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate, + CAST(NULL AS int) AS subaccount_id, + CAST(NULL AS bigint) AS amount_supplied + FROM parse_output_v4 -select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, -result.owner_address, result.sender_address, result.recipient_address, result.asset_id, -result.withdraw_amount_current, result.user_new_principal, result.new_total_supply, -result.new_total_borrow, -CAST(result.s_rate AS bigint) AS s_rate, CAST(result.b_rate AS bigint) AS b_rate -- should be less than 2^64 -FROM parse_output_postv4 \ No newline at end of file + UNION ALL + + -- Withdraw records from supply_withdraw transactions (v9) - always included + select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, + result.owner_address, result.sender_address, result.recipient_address, + result.withdraw_asset_id as asset_id, result.withdraw_amount as withdraw_amount_current, + result.withdraw_new_principal as user_new_principal, + result.new_total_supply_out as new_total_supply, + result.new_total_borrow_out as new_total_borrow, + CAST(result.s_rate_out AS bigint) AS s_rate, CAST(result.b_rate_out AS bigint) AS b_rate, + CAST(result.subaccount_id AS int) AS subaccount_id, + CAST(NULL AS bigint) AS amount_supplied + FROM parse_output_v9_supply_withdraw + + UNION ALL + + -- Supply records from supply_withdraw transactions (v9) - only when supply_amount > 0 + select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, + result.owner_address, result.sender_address, result.recipient_address, + result.supply_asset_id as asset_id, + CAST(NULL AS bigint) AS withdraw_amount_current, + result.supply_new_principal as user_new_principal, + result.new_total_supply_in as new_total_supply, + result.new_total_borrow_in as new_total_borrow, + CAST(result.s_rate_in AS bigint) AS s_rate, CAST(result.b_rate_in AS bigint) AS b_rate, + CAST(result.subaccount_id AS int) AS subaccount_id, + result.supply_amount as amount_supplied + FROM parse_output_v9_supply_withdraw + WHERE result.supply_amount > 0 +) +select block_date, tx_hash, trace_id, tx_now, tx_lt, pool_address, pool_name, protocol_version, +subaccount_id, owner_address, sender_address, recipient_address, asset_id, +withdraw_amount_current, amount_supplied, user_new_principal, new_total_supply, +new_total_borrow, s_rate, b_rate +FROM combined_results \ No newline at end of file