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

Tensorswap models using logs #600

Merged
merged 35 commits into from
Jul 9, 2024
Merged

Conversation

desmond-hui
Copy link
Contributor

  • Add silver__nft_sales_tensorswap_buysellevent model to capture BuySellEvents from decoded logs
  • Use sales_amount from silver__nft_sales_tensorswap_buysellevent for bid-side sales in silver__nft_sales_tensorswap.
    • When unavailable default back to using min_price. This can happen when logs are truncated.
  • Add docs and tests
-- FR on 2XL
14:11:11  1 of 1 OK created sql incremental model silver.nft_sales_tensorswap_buysellevent  [SUCCESS 1 in 32.39s]
17:05:24  1 of 1 OK created sql incremental model silver.nft_sales_tensorswap ............ [SUCCESS 1 in 819.30s]

-- incremental on M
14:16:23  1 of 1 OK created sql incremental model silver.nft_sales_tensorswap_buysellevent  [SUCCESS 124 in 7.70s]
14:17:26  1 of 1 OK created sql incremental model silver.nft_sales_tensorswap ............ [SUCCESS 133 in 24.01s]

-- tests
14:12:12  Finished running 20 tests, 11 hooks in 0 hours 0 minutes and 21.85 seconds (21.85s).
13:59:22  Finished running 20 tests, 11 hooks in 0 hours 0 minutes and 22.91 seconds (22.91s).

@desmond-hui
Copy link
Contributor Author

desmond-hui commented Jul 9, 2024

Part of this required a reload of silver.transction_logs_program_data. This was done in DEV and then swapped out in PROD. Here are the DML statements that have been executed

create or replace transient table solana.silver.transaction_logs_program_data_2 clone solana_dev.silver.transaction_logs_program_data;

show grants on table solana.silver.transaction_logs_program_data_2;
show grants on table solana.silver.transaction_logs_program_data;

ALTER TABLE solana.silver.transaction_logs_program_data_2 SWAP WITH solana.silver.transaction_logs_program_data;

select max(_inserted_timestamp)
from solana.silver.transaction_logs_program_data_2;

select max(_inserted_timestamp)
from solana.silver.transaction_logs_program_data;

-- run ['dbt run -s models/silver/program_logs/silver__transaction_logs_program_data.sql']
-- executed here:  https://github.com/FlipsideCrypto/solana-models/actions/runs/9859317396

grant select on table solana.silver.transaction_logs_program_data to share VNA27887.SOLANA_FLIPSIDE_AF;
grant select on table solana.silver.transaction_logs_program_data to share VNA27887.SOLANA_FLIPSIDE_AF_PAID;
grant select on table solana.silver.transaction_logs_program_data to share VNA27887.SOLANA_FLIPSIDE_AF_TRIAL;


ALTER TABLE solana.silver.transaction_logs_program_data RESUME RECLUSTER;
ALTER TABLE solana.silver.transaction_logs_program_data ADD SEARCH OPTIMIZATION ON EQUALITY(tx_id, program_id);

describe search optimization on solana.silver.transaction_logs_program_data;

@desmond-hui
Copy link
Contributor Author

desmond-hui commented Jul 9, 2024

Second set of DML statements needed to fix the indices in the decoded logs table so that the downstream tensor models get the right indexes. These have been executed in PROD and affect 3119 records


create or replace transient table solana_dev.silver.decoded_logs_to_remove as
with source as (
    select tx_id, index, inner_index
    from solana.silver.transaction_logs_program_data
    where program_id = 'TSWAPaqyCSx2KABk68Shruf4rp7CxcNi8hAsbdwmHbN'
),
bad_indices AS (
    select distinct
        tx_id, index, inner_index
    from
        source
    except
    select distinct
        tx_id, index, inner_index
    from
        solana.silver.decoded_logs
)
select
    d.decoded_logs_id
from 
    solana.silver.decoded_logs d
join
    bad_indices
    using(tx_id);

create or replace transient table solana_dev.silver.decoded_logs_to_insert as
with source as (
    select tx_id, index, inner_index
    from solana.silver.transaction_logs_program_data
    where program_id = 'TSWAPaqyCSx2KABk68Shruf4rp7CxcNi8hAsbdwmHbN'
),
fixed_indices AS (
    select distinct
        tx_id, index, inner_index
    from
        source
    except
    select distinct
        tx_id, index, inner_index
    from
        solana.silver.decoded_logs
)
select 
    d.block_timestamp,
    d.block_id,
    f.tx_id,
    f.index,
    f.inner_index,
    d.log_index,
    d.signers,
    d.succeeded,
    d.program_id,
    d.decoded_log,
    d.event_type,
    d._inserted_timestamp,
    md5(cast(coalesce(cast(f.tx_id as TEXT), '_dbt_utils_surrogate_key_null_') || '-' || coalesce(cast(f.index as TEXT), '_dbt_utils_surrogate_key_null_') || '-' || coalesce(cast(f.inner_index as TEXT), '_dbt_utils_surrogate_key_null_') || '-' || coalesce(cast(d.log_index as TEXT), '_dbt_utils_surrogate_key_null_') as TEXT)) AS decoded_logs_id,
    SYSDATE() AS inserted_timestamp,
    SYSDATE() AS modified_timestamp,
    'a08f4085-89f0-42eb-98a3-0f69e1dfacd2' AS _invocation_id
from
    fixed_indices f
join
    solana.silver.decoded_logs d
    using(tx_id);


-- make sure we arent introducing duplicates
select
    tx_id
from
    solana_dev.silver.decoded_logs_to_insert
group by tx_id
having count(*) > 1;

-- double check counts are the same
select count(*)
from solana_dev.silver.decoded_logs_to_remove
union all
select count(*)
from solana_dev.silver.decoded_logs_to_insert;


DELETE FROM solana.silver.decoded_logs t
USING solana_dev.silver.decoded_logs_to_remove s
WHERE t.decoded_logs_id = s.decoded_logs_id
  AND t.program_id = 'TSWAPaqyCSx2KABk68Shruf4rp7CxcNi8hAsbdwmHbN';


INSERT INTO solana.silver.decoded_logs (
    block_timestamp,
    block_id,
    tx_id,
    index,
    inner_index,
    log_index,
    signers,
    succeeded,
    program_id,
    decoded_log,
    event_type,
    _inserted_timestamp,
    decoded_logs_id,
    inserted_timestamp,
    modified_timestamp,
    _invocation_id
)
SELECT 
    block_timestamp,
    block_id,
    tx_id,
    index,
    inner_index,
    log_index,
    signers,
    succeeded,
    program_id,
    decoded_log,
    event_type,
    _inserted_timestamp,
    decoded_logs_id,
    inserted_timestamp,
    modified_timestamp,
    _invocation_id
FROM solana_dev.silver.decoded_logs_to_insert;

@desmond-hui desmond-hui merged commit 780e4a5 into main Jul 9, 2024
2 checks passed
@desmond-hui
Copy link
Contributor Author

@desmond-hui desmond-hui deleted the tensorswap-models-using-logs branch July 9, 2024 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants