diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/_schema.yml b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/_schema.yml new file mode 100644 index 00000000000..9475615de76 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/_schema.yml @@ -0,0 +1,489 @@ +version: 2 + +models: + - name: polymarket_polygon_markets + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Polymarket: markets" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - question_id + columns: + - &block_time + name: block_time + - &block_number + name: block_number + - &source + name: source + - &market_id + name: market_id + - &question_id + name: question_id + - &question + name: question + - &question_description + name: question_description + - &reward + name: reward + - &reward_token + name: reward_token + - &evt_index + name: evt_index + - &tx_hash + name: tx_hash + + - name: polymarket_polygon_market_outcomes + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Polymarket: market outcomes" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - question_id + columns: + - *block_time + - *block_number + - *source + - *question_id + - &outcome + name: outcome + - *evt_index + - *tx_hash + + - name: polymarket_polygon_base_market_conditions + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Polymarket: market conditions (helper table)" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - condition_id + columns: + - *block_time + - *block_number + - *market_id + - *question_id + - *question + - &condition_id + name: condition_id + - &condition_token + name: condition_token + - &condition_status + name: condition_status + - &keyword + name: keyword + - &condition + name: condition + - &outcome_slot_count + name: outcome_slot_count + - &oracle + name: oracle + - *evt_index + - *tx_hash + + - name: polymarket_polygon_base_ctf_tokens + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Polymarket: CTF tokens (helper table)" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - condition_id + - token0 + - token1 + columns: + - *block_time + - *block_number + - *condition_id + - &token0 + name: token0 + - &token1 + name: token1 + - *evt_index + - *tx_hash + + - name: polymarket_polygon_market_trades + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "All market activity including complementary matching (A buys shares from B), MINT matching (A wants to buy, but not enough sell orders in book, system mints shares if conditions are right), and MERGE matching (A wants to sell shares, but not enough buy orders in book, system matches yes and no shares to unlock collateral). Technically correct volume, but not directly comparable to dex volume or similar. The internal operations increase volume significantly." + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - block_time + - asset_id + - evt_index + - tx_hash + columns: + - name: block_number + description: "Block number in which the transaction was executed" + - name: block_time + description: "UTC event block time" + - name: tx_hash + description: "Transaction hash" + - name: evt_index + description: "Index of the event within the transaction" + - name: action + description: "Type of trade (clob or AMM trade)" + - name: contract_address + description: "Contract address (ctfexchange or negrisk module)" + - name: condition_id + description: "Unique identifier for the YES/NO pair" + - name: event_market_name + description: "Overarching question for negRisk markets (e.g., Presidential election 2024)" + - name: question + description: "Concrete outcome being bet on (e.g., Trump wins 2024 election?)" + - name: polymarket_link + description: "Link to the Polymarket site (may be broken for archived markets)" + - name: token_outcome + description: "YES or NO token outcome" + - name: neg_risk + description: "Boolean indicating whether this is a neg_risk market" + - name: asset_id + description: "Token ID of the ERC1155" + - name: price + description: "Price for the token being bought" + - name: amount + description: "Amount in USD" + - name: shares + description: "Amount of shares transferred" + - name: fee + description: "Fees to Polymarket (currently not enabled)" + - name: maker + description: "Trader whose order is being filled" + - name: taker + description: "Trader who is filling the order (often a contract from Polymarket)" + - name: unique_key + description: "Unique key of the event or market" + - name: token_outcome_name + description: "Combination of token outcome and question" + + - name: polymarket_polygon_users_capital_actions + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Polymarket: user capital actions" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - block_time + - evt_index + - tx_hash + columns: + - name: block_time + - name: block_number + - name: action + - name: from_address + - name: to_address + - name: symbol + - name: amount_raw + - name: amount + - name: amount_usd + - name: evt_index + - name: tx_hash + + - name: polymarket_polygon_users_safe_proxies + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Safes deployed by the SafeProxyFactory on behalf of Polymarket users that use their own EOA to sign transactions" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - proxy + columns: + - *block_time + - *block_number + - name: owner + - name: proxy + description: "Safe wallet address deployed for the Polymarket user" + - *evt_index + - *tx_hash + + - name: polymarket_polygon_users_magic_wallet_proxies + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: 0xboxer + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Magic wallets deployed by the MagicWalletFactory on behalf of Polymarket users that use magic.link to sign up to polymarket" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - proxy + columns: + - *block_time + - *block_number + - name: owner + - name: proxy + - *tx_hash + + - name: polymarket_polygon_market_details + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago, 0xboxer + config: + tags: ['polygon','prediction_markets','polymarket','markets'] + description: "Polymarket: combined market data from on-chain and API sources" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - token_id + columns: + - name: unique_key + description: "Unique identifier for the market" + - name: condition_id + description: "Condition ID for the market" + - name: neg_risk_market_id + description: "Negative risk market ID" + - name: neg_risk_market_name_ + description: "Negative risk market name" + - name: question_id + description: "Question ID for the market" + - name: question + description: "Market question" + - name: token_id + description: "Token ID" + - name: token_outcome + description: "Token outcome" + - name: active + description: "Whether the market is active" + - name: archived + description: "Whether the market is archived" + - name: closed + description: "Whether the market is closed" + - name: accepting_orders + description: "Whether the market is accepting orders" + - name: enable_order_book + description: "Whether the order book is enabled" + - name: neg_risk + description: "Whether it's a negative risk market" + - name: market_slug + description: "Market slug for URL" + - name: neg_risk_market_name + description: "Negative risk market name from on-chain data" + - name: polymarket_link + description: "Link to the Polymarket event page" + - name: market_start_time + description: "Start time of the market" + - name: market_end_time + description: "End time of the market" + - name: market_outcome + description: "Outcome of the market" + - name: resolved_on_timestamp + description: "Timestamp when the market was resolved" + + - name: polymarket_polygon_positions_raw + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','positions'] + description: "Polymarket: raw user positions in markets" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - month + - day + - address + - token_address + - token_id + - blockchain + columns: + - name: blockchain + description: "The blockchain the position is on" + - name: month + description: "The month of the position" + - name: day + description: "The date of the position" + - name: address + description: "The address holding the position" + - name: token_address + description: "The address of the token" + - name: token_id + description: "Token ID of the position" + - name: balance + description: "Balance of the position" + + - name: polymarket_polygon_positions + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: tomfutago + config: + tags: ['polygon','prediction_markets','polymarket','positions'] + description: "Polymarket: user positions in markets with market details" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - address + - token_id + - day + columns: + - name: day + description: "The date of the position" + - name: address + description: "The address holding the position" + - name: unique_key + description: "Unique identifier for the market" + - name: token_id + description: "Token ID of the position" + - name: token_outcome + description: "Outcome represented by the token" + - name: token_outcome_name + description: "Name of the outcome represented by the token" + - name: balance + description: "Balance of the position" + - name: question_id + description: "Question ID for the market" + - name: market_question + description: "Question of the market" + - name: market_description + description: "Description of the market" + - name: event_market_name + description: "Name of the event market" + - name: event_market_description + description: "Description of the event market" + - name: active + description: "Whether the market is active" + - name: closed + description: "Whether the market is closed" + - name: accepting_orders + description: "Whether the market is accepting orders" + - name: polymarket_link + description: "Link to the Polymarket event page" + - name: market_start_time + description: "Start time of the market" + - name: market_end_time + description: "End time of the market" + - name: market_outcome + description: "Outcome of the market" + - name: resolved_on_timestamp + description: "Timestamp when the market was resolved" + + - name: polymarket_polygon_market_prices_daily + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: 0xboxer + config: + tags: ['polygon','prediction_markets','polymarket','prices'] + description: "Daily market prices for Polymarket on Polygon" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - day + - token_id + columns: + - name: day + description: "The day for which the price is valid" + - name: condition_id + description: "The unique identifier for the market condition" + - name: token_id + description: "The token ID associated with the outcome" + - name: price + description: "The price of the outcome token at the end of the day" + + - name: polymarket_polygon_market_prices_hourly + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: 0xboxer + config: + tags: ['polygon','prediction_markets','polymarket','prices'] + description: "Hourly market prices for Polymarket on Polygon" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - hour + - token_id + columns: + - name: hour + description: "The hour for which the price is valid" + - name: condition_id + description: "The unique identifier for the market condition" + - name: token_id + description: "The token ID associated with the outcome" + - name: price + description: "The price of the outcome token at the end of the hour" + + - name: polymarket_polygon_users + meta: + blockchain: polygon + sector: prediction_markets + project: polymarket + contributors: 0xRob + config: + tags: ['polygon','prediction_markets','polymarket','users'] + description: "Comprehensive lookup table for Polymarket user addresses, including both Safe and Magic wallet proxies with funding information" + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - polymarket_wallet + columns: + - name: created_time + description: "Timestamp when the Polymarket wallet was created" + - name: block_number + description: "Block number when the Polymarket wallet was created" + - name: wallet_type + description: "Type of wallet (safe or magic)" + - name: owner + description: "Owner address of the Polymarket wallet" + - name: polymarket_wallet + description: "Polymarket wallet address" + - name: first_funded_time + description: "Timestamp of first funding received by the Polymarket wallet" + - name: first_funded_by + description: "Address that first funded the Polymarket wallet" + - name: has_been_funded + description: "Boolean indicating if the wallet has ever received funds" + - name: minutes_to_first_funding + description: "Number of minutes between wallet creation and first funding (null if never funded)" + - name: evt_index + description: "Event index in the transaction" + - name: tx_hash + description: "Transaction hash of the wallet creation" + + diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_base_ctf_tokens.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_base_ctf_tokens.sql new file mode 100644 index 00000000000..37a382afcc2 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_base_ctf_tokens.sql @@ -0,0 +1,45 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'base_ctf_tokens', + materialized = 'view' + ) +}} + +with ctf_tokens as ( + select + *, + row_number() over (partition by condition_id, token0, token1 order by block_time) as rn + from ( + select + evt_block_time as block_time, + evt_block_number as block_number, + conditionId as condition_id, + token0, + token1, + evt_index, + evt_tx_hash as tx_hash + from {{ source('polymarket_polygon', 'CTFExchange_evt_TokenRegistered') }} + union all + select + evt_block_time as block_time, + evt_block_number as block_number, + conditionId as condition_id, + token0, + token1, + evt_index, + evt_tx_hash as tx_hash + from {{ source('polymarket_polygon', 'NegRiskCtfExchange_evt_TokenRegistered') }} + ) t +) + +select + block_time, + block_number, + condition_id, + token0, + token1, + evt_index, + tx_hash +from ctf_tokens +where rn = 1 diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_base_market_conditions.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_base_market_conditions.sql new file mode 100644 index 00000000000..51eb6b94f2e --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_base_market_conditions.sql @@ -0,0 +1,39 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'base_market_conditions', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['condition_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with conditions as ( + select + evt_block_time as block_time, + evt_block_number as block_number, + conditionId as condition_id, + questionId as question_id, + outcomeSlotCount as outcome_slot_count, + oracle, + evt_index, + evt_tx_hash as tx_hash + from {{ source('polymarket_polygon', 'ctf_evt_ConditionPreparation') }} + {% if is_incremental() %} + where {{ incremental_predicate('evt_block_time') }} + {% endif %} +) + +select + c.block_time, + c.block_number, + c.condition_id, + c.question_id, + c.outcome_slot_count, + c.oracle, + c.evt_index, + c.tx_hash +from conditions c + inner join {{ ref('polymarket_polygon_markets') }} m on c.question_id = m.question_id diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_details.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_details.sql new file mode 100644 index 00000000000..467fb44f59d --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_details.sql @@ -0,0 +1,117 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'market_details', + materialized = 'table', + post_hook='{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["tomfutago", "0xboxer"]\') }}' + ) +}} + +WITH onchain_metadata AS ( + SELECT + evt_block_time AS block_time, + evt_block_number AS block_number, + marketId AS market_id, + CASE + WHEN json_value(from_utf8(data), 'lax $.title' NULL ON ERROR) IS NOT NULL + THEN json_extract_scalar(from_utf8(data), '$.title') + ELSE regexp_extract(from_utf8(data), 'title:\s*(.*?),\s*description:', 1) + END AS neg_risk_market_name, + CASE + WHEN json_value(from_utf8(data), 'lax $.description' NULL ON ERROR) IS NOT NULL + THEN json_extract_scalar(from_utf8(data), '$.description') + ELSE substr(from_utf8(data), strpos(from_utf8(data), 'description: ') + length('description: ')) + END AS neg_risk_market_description, + oracle, + feeBips AS fee_bips, + evt_index, + evt_tx_hash AS tx_hash + FROM {{ source('polymarket_polygon', 'NegRiskAdapter_evt_MarketPrepared') }} +) + +,polymarket_api_upload as +( + SELECT + CASE + WHEN neg_risk = true THEN neg_risk_market_id + WHEN neg_risk = false THEN cast(condition_id AS varchar) + END AS unique_key, + try_cast(substring(token_1_id, 5) AS UINT256) AS token_id, + token_1_outcome AS token_outcome, + * + FROM {{ source('dune', 'dataset_polymarket_markets', database="dune") }} + UNION ALL + SELECT + CASE + WHEN neg_risk = true THEN neg_risk_market_id + WHEN neg_risk = false THEN cast(condition_id AS varchar) + END AS unique_key, + try_cast(substring(token_2_id, 5) AS UINT256) AS token_id, + token_2_outcome AS token_outcome, + * + FROM {{ source('dune', 'dataset_polymarket_markets', database="dune") }} +), + +combine as +( +Select + api.*, + oc.* +from polymarket_api_upload api +LEFT JOIN onchain_metadata oc ON cast(oc.market_id AS varchar) = api.unique_key +) + +, naming_things as ( +SELECT + unique_key AS unique_key, + condition_id AS condition_id, + CASE + WHEN neg_risk_market_id IS NULL THEN NULL + ELSE neg_risk_market_id + END AS event_market_id, + CASE + WHEN neg_risk_market_name IS NULL THEN 'single market' + ELSE neg_risk_market_name + END AS event_market_name, + neg_risk_market_description as event_market_description, + c.question_id AS question_id, + question, + description as market_description, + try_cast(token_id AS UINT256) AS token_id, + token_outcome, + token_outcome || '-' || question as token_outcome_name, + active, + archived, + closed, + accepting_orders, + enable_order_book, + neg_risk AS neg_risk, + CASE + WHEN neg_risk = false THEN get_href('https://polymarket.com/event/' || market_slug, market_slug) + WHEN neg_risk = true THEN get_href('https://polymarket.com/event/' || replace(replace(replace(lower(neg_risk_market_name), ' ', '-'), '$', ''), '''',''), neg_risk_market_name) + END AS polymarket_link, + accepting_order_timestamp as market_start_time, + end_date_iso as market_end_time, + game_start_time, + seconds_delay, + fpmm, + icon, + image, + tags, + oracle AS oracle, + fee_bips, + case when pm.outcome is null then 'unresolved' else pm.outcome end as outcome, + pm.block_time as resolved_on_timestamp, + last_updated_at as last_uploaded_at +FROM combine c +left join {{ ref('polymarket_polygon_market_outcomes') }} pm on pm.question_id = c.question_id +) + +--these nulls get introduced by the polymarket api responses + +SELECT * FROM naming_things +where token_id is not null +and condition_id is not null \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_outcomes.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_outcomes.sql new file mode 100644 index 00000000000..4486f0c3da7 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_outcomes.sql @@ -0,0 +1,67 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'market_outcomes', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['question_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')], + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["tomfutago, 0xboxer"]\') }}' + ) +}} + +select + evt_block_time as block_time, + evt_block_number as block_number, + 'uma-v1' as source, + questionID as question_id, + case settledPrice / 1e18 + when 1 then 'yes' + when 0.5 then '50/50' + else 'no' + end as outcome, + evt_index, + evt_tx_hash as tx_hash +from {{ source('minereum_polygon', 'UmaConditionalTokensBinaryAdapter_evt_QuestionSettled') }} +{% if is_incremental() %} +where {{ incremental_predicate('evt_block_time') }} +{% endif %} + +union all + +select + evt_block_time as block_time, + evt_block_number as block_number, + 'uma-v2' as source, + questionID as question_id, + case settledPrice / 1e18 + when 1 then 'yes' + when 0.5 then '50/50' + else 'no' + end as outcome, + evt_index, + evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'UmaCtfAdapter_evt_QuestionResolved') }} +{% if is_incremental() %} +where {{ incremental_predicate('evt_block_time') }} +{% endif %} + +union all + +select + evt_block_time as block_time, + evt_block_number as block_number, + 'polymarket' as source, + --marketId as market_id, + questionId as question_id, + if(outcome, 'yes', 'no') as outcome, + evt_index, + evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'NegRiskAdapter_evt_OutcomeReported') }} +{% if is_incremental() %} +where {{ incremental_predicate('evt_block_time') }} +{% endif %} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_daily.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_daily.sql new file mode 100644 index 00000000000..8170bab435d --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_daily.sql @@ -0,0 +1,49 @@ +{{ config( + schema = 'polymarket_polygon', + alias = 'market_prices_daily', + materialized = 'view', + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["0xboxer, tomfutago"]\') }}' + ) +}} + +WITH changed_prices AS ( + SELECT + date_trunc('day', block_time) AS day, + block_time, + condition_id, + asset_id AS token_id, + price, + LEAD(CAST(date_trunc('day', block_time) AS timestamp)) OVER (PARTITION BY asset_id ORDER BY block_time ASC) AS next_update_day + FROM ( + SELECT *, + ROW_NUMBER() OVER (PARTITION BY DATE_TRUNC('day', block_time), asset_id ORDER BY block_time DESC) as rn + FROM {{ ref('polymarket_polygon_market_trades_raw') }} + ) ranked + WHERE rn = 1 +), + +--sequences are limited to 10k so just pulling this in from the transactions table, no other relationship +days AS ( + SELECT * + FROM UNNEST( + SEQUENCE(CAST('2015-01-01' AS date), DATE(DATE_TRUNC('day', NOW())), INTERVAL '1' day) + ) AS foo(day) +), + +forward_fill AS ( + SELECT + CAST(d.day AS timestamp) AS day, + lp.condition_id, + lp.token_id, + lp.price + FROM days d + LEFT JOIN changed_prices lp + ON d.day >= lp.day + AND (lp.next_update_day IS NULL OR d.day < lp.next_update_day) +) + +SELECT * FROM forward_fill +WHERE price > 0 \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_hourly.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_hourly.sql new file mode 100644 index 00000000000..b7b0e677676 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_hourly.sql @@ -0,0 +1,46 @@ +{{ config( + schema = 'polymarket_polygon', + alias = 'market_prices_hourly', + materialized = 'view', + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["0xboxer, tomfutago"]\') }}' + ) +}} + +WITH changed_prices AS ( + SELECT + date_trunc('hour', block_time) AS hour, + block_time, + condition_id, + asset_id AS token_id, + price, + LEAD(CAST(date_trunc('hour', block_time) AS timestamp)) OVER (PARTITION BY asset_id ORDER BY block_time ASC) AS next_update_hour + FROM ( + SELECT *, + ROW_NUMBER() OVER (PARTITION BY DATE_TRUNC('hour', block_time), asset_id ORDER BY block_time DESC) as rn + FROM {{ ref('polymarket_polygon_market_trades_raw') }} + ) ranked + WHERE rn = 1 +), + +hours AS ( + Select distinct date_trunc('hour', block_time) as hour + from {{ source('polygon', 'transactions') }} +), + +forward_fill AS ( + SELECT + CAST(h.hour AS timestamp) AS hour, + lp.condition_id, + lp.token_id, + lp.price + FROM hours h + LEFT JOIN changed_prices lp + ON h.hour >= lp.hour + AND (lp.next_update_hour IS NULL OR h.hour < lp.next_update_hour) +) + +SELECT * FROM forward_fill +WHERE price > 0 diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_latest.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_latest.sql new file mode 100644 index 00000000000..b3af352292e --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_prices_latest.sql @@ -0,0 +1,29 @@ +{{ config( + schema = 'polymarket_polygon', + alias = 'market_prices_latest', + materialized = 'view', + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["0xboxer, tomfutago"]\') }}' + ) +}} + +WITH latest_prices AS ( + SELECT + block_time as last_updated, + condition_id, + asset_id as token_id, + price as latest_price, + ROW_NUMBER() OVER (PARTITION BY asset_id ORDER BY block_time DESC) as rn + FROM {{ ref('polymarket_polygon_market_trades_raw') }} +) + +SELECT + last_updated, + condition_id, + token_id, + latest_price +FROM latest_prices +WHERE rn = 1 + AND latest_price > 0 diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_trades.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_trades.sql new file mode 100644 index 00000000000..7847940d18e --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_trades.sql @@ -0,0 +1,36 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'market_trades', + materialized = 'view', + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["tomfutago, 0xboxer"]\') }}' + ) +}} + +select + t.block_number, + t.block_time, + t.tx_hash, + t.evt_index, + t.action, + t.contract_address, + t.condition_id, + md.event_market_name, + md.question, + md.polymarket_link, + md.token_outcome, + md.neg_risk, + t.asset_id, + t.price, + t.amount, + t.shares, + t.fee, + t.maker, + t.taker, + md.unique_key, + md.token_outcome_name +from {{ ref('polymarket_polygon_market_trades_raw') }} t +left join {{ ref('polymarket_polygon_market_details') }} md on t.asset_id = md.token_id diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_trades_raw.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_trades_raw.sql new file mode 100644 index 00000000000..af24c00c5d4 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_market_trades_raw.sql @@ -0,0 +1,143 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'market_trades_raw', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + partition_by = ['block_month'], + unique_key = ['block_month','block_time','asset_id','evt_index','tx_hash'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +/* +with fpmm_markets as ( + select + fpmm.evt_block_time, + fpmm.evt_block_number, + fpmm.fixedProductMarketMaker as fpmm_contract_address, + ctf.condition_id, + ctf.token0 as asset_id, + fpmm.fee, + fpmm.contract_address, + fpmm.evt_index, + fpmm.evt_tx_hash + from {{ source('polymarketfactory_polygon', 'FixedProductMarketMakerFactory_evt_FixedProductMarketMakerCreation') }} fpmm + cross join unnest(fpmm.conditionIds) as c(condition_id) + inner join {{ ref('polymarket_polygon_base_ctf_tokens') }} ctf on c.condition_id = ctf.condition_id + where fpmm.collateralToken = 0x2791bca1f2de4661ed88a30c99a7a9449aa84174 -- USDC +) + +select + t.evt_block_time as block_time, + t.evt_block_number as block_number, + 'FPMM trade' as action, + m.condition_id, + m.asset_id, + --t.outcomeIndex, + t.returnAmount / 1e6 as amount, + t.outcomeTokensSold / 1e6 as shares, + (t.outcomeTokensSold / 1e6) / (t.returnAmount / 1e6) as price, + t.feeAmount / 1e6 as fee, + t.seller as maker, + t.contract_address as taker, + t.returnAmount as maker_amount_raw, + t.outcomeTokensSold as taker_amount_raw, + t.contract_address, + t.evt_index, + t.evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'FixedProductMarketMaker_evt_FPMMSell') }} t + inner join fpmm_markets m on t.contract_address = m.fpmm_contract_address +{% if is_incremental() %} +where {{ incremental_predicate('t.evt_block_time') }} +{% endif %} + +union all + +select + t.evt_block_time as block_time, + t.evt_block_number as block_number, + 'FPMM trade' as action, + m.condition_id, + m.asset_id, + --t.outcomeIndex, + t.investmentAmount / 1e6 as amount, + t.outcomeTokensBought / 1e6 as shares, + (t.outcomeTokensBought / 1e6) / (t.investmentAmount / 1e6) as price, + t.feeAmount / 1e6 as fee, + t.contract_address as maker, + t.buyer as taker, + t.investmentAmount as maker_amount_raw, + t.outcomeTokensBought as taker_amount_raw, + t.contract_address, + t.evt_index, + t.evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'FixedProductMarketMaker_evt_FPMMBuy') }} t + inner join fpmm_markets m on t.contract_address = m.fpmm_contract_address +{% if is_incremental() %} +where {{ incremental_predicate('t.evt_block_time') }} +{% endif %} + +union all +some bug in fpmms here, no time to fix and they arent used for anything anyway*/ + + +select + cast(date_trunc('month', t.evt_block_time) as date) as block_month, + t.evt_block_time as block_time, + t.evt_block_number as block_number, + 'CLOB trade' as action, + ctf.condition_id, + coalesce(nullif(t.makerAssetId, 0), nullif(t.takerAssetID, 0)) as asset_id, + if(t.makerAssetId = 0, t.makerAmountFilled, t.takerAmountFilled) / 1e6 as amount, + if(t.makerAssetId = 0, t.takerAmountFilled, t.makerAmountFilled) / 1e6 as shares, + if( + t.makerAssetId = 0, + (t.makerAmountFilled / 1e6) / (t.takerAmountFilled / 1e6), + (t.takerAmountFilled / 1e6) / (t.makerAmountFilled / 1e6) + ) as price, + t.fee / 1e6 as fee, + t.maker, + t.taker, + t.makerAmountFilled as maker_amount_raw, + t.takerAmountFilled as taker_amount_raw, + t.contract_address, + t.evt_index, + t.evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'CTFExchange_evt_OrderFilled') }} t + inner join {{ ref('polymarket_polygon_base_ctf_tokens') }} ctf on coalesce(nullif(t.makerAssetId, 0), nullif(t.takerAssetID, 0)) = ctf.token0 +{% if is_incremental() %} +where {{ incremental_predicate('t.evt_block_time') }} +{% endif %} + + +union all + +select + cast(date_trunc('month', t.evt_block_time) as date) as block_month, + t.evt_block_time as block_time, + t.evt_block_number as block_number, + 'CLOB trade' as action, + ctf.condition_id, + coalesce(nullif(t.makerAssetId, 0), nullif(t.takerAssetID, 0)) as asset_id, + if(t.makerAssetId = 0, t.makerAmountFilled, t.takerAmountFilled) / 1e6 as amount, + if(t.makerAssetId = 0, t.takerAmountFilled, t.makerAmountFilled) / 1e6 as shares, + if( + t.makerAssetId = 0, + (t.makerAmountFilled / 1e6) / (t.takerAmountFilled / 1e6), + (t.takerAmountFilled / 1e6) / (t.makerAmountFilled / 1e6) + ) as price, + t.fee / 1e6 as fee, + t.maker, + t.taker, + t.makerAmountFilled as maker_amount_raw, + t.takerAmountFilled as taker_amount_raw, + t.contract_address, + t.evt_index, + t.evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'NegRiskCtfExchange_evt_OrderFilled') }} t + inner join {{ ref('polymarket_polygon_base_ctf_tokens') }} ctf on coalesce(nullif(t.makerAssetId, 0), nullif(t.takerAssetID, 0)) = ctf.token0 +{% if is_incremental() %} +where {{ incremental_predicate('t.evt_block_time') }} +{% endif %} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_markets.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_markets.sql new file mode 100644 index 00000000000..98ed0c21af8 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_markets.sql @@ -0,0 +1,105 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'markets', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['question_id'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +with questions as ( + select + evt_block_time as block_time, + evt_block_number as block_number, + 'uma-v1' as source, + cast(null as varbinary) as market_id, + questionID as question_id, + from_utf8(ancillaryData) as data_decoded, + reward, + rewardToken as reward_token, + evt_index, + evt_tx_hash as tx_hash + from {{ source('minereum_polygon', 'UmaConditionalTokensBinaryAdapter_evt_QuestionInitialized') }} + {% if is_incremental() %} + where {{ incremental_predicate('evt_block_time') }} + {% endif %} + + union all + + select + evt_block_time as block_time, + evt_block_number as block_number, + 'uma-v2' as source, + cast(null as varbinary) as market_id, + questionID as question_id, + from_utf8(ancillaryData) as data_decoded, + reward, + rewardToken as reward_token, + evt_index, + evt_tx_hash as tx_hash + from {{ source('polymarket_polygon', 'UmaCtfAdapter_evt_QuestionInitialized') }} + {% if is_incremental() %} + where {{ incremental_predicate('evt_block_time') }} + {% endif %} + + union all + + select + evt_block_time as block_time, + evt_block_number as block_number, + 'uma-v3' as source, + cast(null as varbinary) as market_id, + questionID as question_id, + from_utf8(ancillaryData) as data_decoded, + reward, + rewardToken as reward_token, + evt_index, + evt_tx_hash as tx_hash + from {{ source('polymarket_polygon', 'UmaCtfAdapter_v3_evt_QuestionInitialized') }} + {% if is_incremental() %} + where {{ incremental_predicate('evt_block_time') }} + {% endif %} + + union all + + select + evt_block_time as block_time, + evt_block_number as block_number, + 'polymarket' as source, + marketId as market_id, + questionId as question_id, + from_utf8(data) as data_decoded, + cast(null as uint256) as reward, + cast(null as varbinary) as reward_token, + evt_index, + evt_tx_hash as tx_hash + from {{ source('polymarket_polygon', 'NegRiskAdapter_evt_QuestionPrepared') }} + {% if is_incremental() %} + where {{ incremental_predicate('evt_block_time') }} + {% endif %} +) + +select + block_time, + block_number, + source, + market_id, + question_id, + case + when coalesce(json_value(data_decoded, 'lax $.title' null on error), json_value(data_decoded, 'lax $.question' null on error)) is not null + then coalesce(json_extract_scalar(data_decoded, '$.title'), json_extract_scalar(data_decoded, '$.question')) + else regexp_extract(data_decoded, '(?i)(?:title|question):\s*(.*?),\s*description:', 1) + end as question, + case + when json_value(data_decoded, 'lax $.description' null on error) is not null + then json_extract_scalar(data_decoded, '$.description') + else substr(data_decoded, strpos(data_decoded, 'description: ') + length('description: ')) + end as question_description, + reward, + reward_token, + evt_index, + tx_hash +from questions diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_positions.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_positions.sql new file mode 100644 index 00000000000..da6b1c58831 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_positions.sql @@ -0,0 +1,36 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'positions', + materialized = 'view', + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["tomfutago"]\') }}' + ) +}} + +SELECT + p.day, + p.address, + mm.unique_key, + p.token_id, + mm.token_outcome, + mm.token_outcome_name, + p.balance, + mm.question_id, + mm.question AS market_question, + mm.market_description, + mm.event_market_name, + mm.event_market_description, + mm.active, + mm.closed, + mm.accepting_orders, + mm.polymarket_link, + mm.market_start_time, + mm.market_end_time, + mm.outcome AS market_outcome, + mm.resolved_on_timestamp +FROM {{ ref('polymarket_polygon_positions_raw') }} p +INNER JOIN {{ ref('polymarket_polygon_market_details') }} mm ON p.token_id = mm.token_id + diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_positions_raw.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_positions_raw.sql new file mode 100644 index 00000000000..6c8acaba23e --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_positions_raw.sql @@ -0,0 +1,36 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'positions_raw', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + partition_by = ['month'], + unique_key = ['month', 'day', 'address', 'token_address', 'token_id', 'blockchain'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')] + ) +}} + +with token_list as ( + select + 0x4D97DCd97eC945f40cF65F87097ACe5EA0476045 as token_address +), +balances as ( + {{ + balances_incremental_subset_daily( + blockchain = 'polygon', + token_list = 'token_list', + start_date = '2020-09-03' + ) + }} +) + +select + 'polygon' as blockchain + , date_trunc('month', day) as month + , day + , address + , token_address + , token_id + , balance / 1e6 AS balance +from balances \ No newline at end of file diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users.sql new file mode 100644 index 00000000000..5fba51da769 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users.sql @@ -0,0 +1,70 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'users_address_lookup', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['polymarket_wallet'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.created_time')], + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["0xBoxer"]\') }}' + ) +}} + +WITH first_capital_action AS ( + SELECT + to_address as proxy, + MIN(block_time) as first_funded_time, + MIN_BY(tx_hash, block_time) as first_funded_tx_hash, + MIN_BY(from_address, block_time) as first_funded_by + FROM {{ ref('polymarket_polygon_users_capital_actions') }} + GROUP BY to_address -- Remove tx_hash from GROUP BY +), + +wallet_addresses AS ( + SELECT + block_time as created_time, + block_number, + type_of_wallet, + owner, + proxy, + tx_hash + FROM {{ ref('polymarket_polygon_users_safe_proxies') }} + {% if is_incremental() %} + WHERE {{ incremental_predicate('block_time') }} + {% endif %} + + UNION ALL + + SELECT + block_time as created_time, + block_number, + type_of_wallet, + owner, + proxy, + tx_hash + FROM {{ ref('polymarket_polygon_users_magic_wallet_proxies') }} + {% if is_incremental() %} + WHERE {{ incremental_predicate('block_time') }} + {% endif %} +) + +SELECT + w.created_time, + w.block_number, + w.type_of_wallet as wallet_type, + w.owner, + w.proxy as polymarket_wallet, + w.tx_hash as created_tx_hash, + f.first_funded_time, + f.first_funded_by, + f.first_funded_tx_hash, + CASE + WHEN f.first_funded_time IS NOT NULL THEN true + ELSE false + END as has_been_funded +FROM wallet_addresses w +LEFT JOIN first_capital_action f ON f.proxy = w.proxy diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_capital_actions.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_capital_actions.sql new file mode 100644 index 00000000000..2a4135070c3 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_capital_actions.sql @@ -0,0 +1,171 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'users_capital_actions', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['block_time','evt_index','tx_hash'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')], + post_hook = '{{ expose_spells(blockchains = \'["polygon"]\', + spell_type = "project", + spell_name = "polymarket", + contributors = \'["tomfutago, 0xBoxer"]\') }}' + ) +}} + +-- lots of edge cases here to ensure that we're just picking up on actual deposits and not internal transfers +-- this is a bit of a mess, but it works for now + +-- we look for usdc.e and usdc transfers +-- usdc.e is the wrapped version of usdc on polygon polymarket runs on this +-- if you deposit using usdc, the UI will prompt you to wrap your USDC into USDC.e by signing a message +-- this will just use uniswap to swap your usdc for usdc.e, so we need to exclude 0xD36ec33c8bed5a9F7B6630855f1533455b98a418 as this is the uniswap pool +-- by ignoring the uniswap pool, but looking for USDC transfers, we can get a better read on funding sources + + + + +-- get all known polymarket contract and filter them out as these are not users +with polymarket_addresses as ( + select * from (values + (0x4D97DCd97eC945f40cF65F87097ACe5EA0476045), -- Conditional Tokens + (0x3A3BD7bb9528E159577F7C2e685CC81A765002E2), -- Wrapped Collateral + (0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E), -- CTFExchange + (0xC5d563A36AE78145C45a50134d48A1215220f80a), -- NegRiskCTFExchange + (0xc288480574783BD7615170660d71753378159c47), -- Polymarket Rewards + (0x94a3db2f861b01c027871b08399e1ccecfc847f6), -- liq mining merkle distributor + (0xD36ec33c8bed5a9F7B6630855f1533455b98a418) -- USDC.e - USDC uniswap pool + ) as t(address) + UNION ALL + select + address + from {{ source('polygon', 'creation_traces') }} + where "from" = 0x8b9805a2f595b6705e74f7310829f2d299d21522 -- fpmm factory + -- get all safe and magic wallet proxies to filter for polymarket user addresses +-- there are some rare EOA addresses that trade directly on polymarket, but they are few and far between +) +,polymarket_wallets as ( + Select proxy from {{ ref('polymarket_polygon_users_magic_wallet_proxies') }} + UNION ALL + Select proxy from {{ ref('polymarket_polygon_users_safe_proxies') }} + -- these are fpmm contracts +) + +-- get all deposits + +select + block_time, + block_date, + block_number, + 'deposit' as action, + "from" as from_address, + "to" as to_address, + case when contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 then 'USDC.e' + when contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 then 'USDC' + end as symbol, + amount_raw, + amount, + amount_usd, + evt_index, + tx_hash +from {{ source('tokens_polygon', 'transfers')}} +where ( + contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 -- USDC.e + or contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 -- USDC + ) + and "to" in (select proxy from polymarket_wallets) --deposits are to the wallet + and "from" not in (select proxy from polymarket_wallets) --not looking for transfers + and "to" not in (select address from polymarket_addresses) + and "from" not in (select address from polymarket_addresses) + {% if is_incremental() %} + and {{ incremental_predicate('block_time') }} + {% endif %} + +union all + +-- get all withdrawals + +select + block_time, + block_date, + block_number, + 'withdrawal' as action, + "from" as from_address, + "to" as to_address, + case when contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 then 'USDC.e' + when contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 then 'USDC' + end as symbol, + amount_raw, + amount, + amount_usd, + evt_index, + tx_hash +from {{ source('tokens_polygon', 'transfers')}} +where ( + contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 -- USDC.e + or contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 -- USDC + ) + and "from" in (select proxy from polymarket_wallets) --withdrawals are from the wallet + and "to" not in (select proxy from polymarket_wallets) --not looking for transfers + and "to" not in (select address from polymarket_addresses) + and "from" not in (select address from polymarket_addresses) + {% if is_incremental() %} + and {{ incremental_predicate('block_time') }} + {% endif %} + +union all + +-- get all transfers between safes, this is very rare but a possible edge case + +select distinct + block_time, + block_date, + block_number, + 'transfer' as action, -- transfer between safes + "from" as from_address, + "to" as to_address, + case when contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 then 'USDC.e' + when contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 then 'USDC' + end as symbol, + amount_raw, + amount, + amount_usd, + evt_index, + tx_hash +from {{ source('tokens_polygon', 'transfers')}} +where (contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 -- USDC.e + or contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359) -- USDC + and "to" not in (select address from polymarket_addresses) + and "from" not in (select address from polymarket_addresses) + and "from" in (select proxy from polymarket_wallets) + and "to" in (select proxy from polymarket_wallets) + {% if is_incremental() %} + and {{ incremental_predicate('block_time') }} + {% endif %} + +union all + +select + block_time, + block_date, + block_number, + 'convert' as action, -- convert between USDC and USDC.e via uniswap pool + "from" as from_address, + "to" as to_address, + case when contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 then 'USDC.e' + when contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359 then 'USDC' + end as symbol, + amount_raw, + amount, + amount_usd, + evt_index, + tx_hash +from {{ source('tokens_polygon', 'transfers')}} +where (contract_address = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174 -- USDC.e + or contract_address = 0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359) -- USDC + and (("to" = 0xD36ec33c8bed5a9F7B6630855f1533455b98a418 and "from" in (select proxy from polymarket_wallets)) + or ("from" = 0xD36ec33c8bed5a9F7B6630855f1533455b98a418 and "to" in (select proxy from polymarket_wallets))) + {% if is_incremental() %} + and {{ incremental_predicate('block_time') }} + {% endif %} diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_magic_wallet_proxies.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_magic_wallet_proxies.sql new file mode 100644 index 00000000000..ef3ef70092e --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_magic_wallet_proxies.sql @@ -0,0 +1,27 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'users_magic_wallet_proxies', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['proxy'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +select + block_time, + block_number, + 'magic.link' as type_of_wallet, + cast(null as varbinary) as owner, + address as proxy, + tx_hash +from {{ source('polygon', 'creation_traces') }} +where "from" = 0xaB45c5A4B0c941a2F231C04C3f49182e1A254052 +{% if is_incremental() %} + and {{ incremental_predicate('block_time') }} +{% endif %} + + + diff --git a/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_safe_proxies.sql b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_safe_proxies.sql new file mode 100644 index 00000000000..1696d4d5647 --- /dev/null +++ b/dbt_subprojects/daily_spellbook/models/_projects/polymarket/polygon/polymarket_polygon_users_safe_proxies.sql @@ -0,0 +1,24 @@ +{{ + config( + schema = 'polymarket_polygon', + alias = 'users_safe_proxies', + materialized = 'incremental', + file_format = 'delta', + incremental_strategy = 'merge', + unique_key = ['proxy'], + incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')] + ) +}} + +select + evt_block_time as block_time, + evt_block_number as block_number, + 'safe' as type_of_wallet, + owner, + proxy, + evt_index, + evt_tx_hash as tx_hash +from {{ source('polymarket_polygon', 'SafeProxyFactory_evt_ProxyCreation') }} +{% if is_incremental() %} +where {{ incremental_predicate('evt_block_time') }} +{% endif %} diff --git a/sources/polymarket/polygon/_sources.yml b/sources/polymarket/polygon/_sources.yml new file mode 100644 index 00000000000..1764914f4b2 --- /dev/null +++ b/sources/polymarket/polygon/_sources.yml @@ -0,0 +1,33 @@ +version: 2 + +sources: + - name: minereum_polygon + tables: + - name: UmaConditionalTokensBinaryAdapter_evt_QuestionInitialized + - name: UmaConditionalTokensBinaryAdapter_evt_QuestionSettled + + - name: polymarketfactory_polygon + tables: + - name: FixedProductMarketMakerFactory_evt_FixedProductMarketMakerCreation + + - name: polymarket_polygon + tables: + - name: UmaCtfAdapter_evt_QuestionInitialized + - name: UmaCtfAdapter_evt_QuestionResolved + - name: UmaCtfAdapter_v3_evt_QuestionInitialized + - name: NegRiskAdapter_evt_MarketPrepared + - name: NegRiskAdapter_evt_QuestionPrepared + - name: NegRiskAdapter_evt_OutcomeReported + - name: ctf_evt_ConditionPreparation + - name: CTFExchange_evt_TokenRegistered + - name: NegRiskCtfExchange_evt_TokenRegistered + - name: FixedProductMarketMaker_evt_FPMMSell + - name: FixedProductMarketMaker_evt_FPMMBuy + - name: CTFExchange_evt_OrderFilled + - name: NegRiskCtfExchange_evt_OrderFilled + - name: SafeProxyFactory_evt_ProxyCreation + - name: fedinterestrate_evt_FPMMFundingAdded + + - name: dune + tables: + - name: dataset_polymarket_markets \ No newline at end of file