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

Add Polymarket #6575

Merged
merged 111 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 104 commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
e5046c2
polymarket init
tomfutago Aug 18, 2024
9415680
add outcomes
tomfutago Aug 18, 2024
66bd0cb
add conditions
tomfutago Aug 18, 2024
116dfa6
add ctf tokens
tomfutago Aug 18, 2024
55433ec
fix alias
tomfutago Aug 18, 2024
cde516b
change to view, remove dupes
tomfutago Aug 18, 2024
f5f4835
market conditions enriched + clean up
tomfutago Aug 24, 2024
c48bbe4
fill in markets data
tomfutago Aug 24, 2024
dbd57ff
add trades
tomfutago Aug 25, 2024
5b24102
fix uniqueness
tomfutago Aug 25, 2024
f506147
add capital actions
tomfutago Aug 26, 2024
f3934fa
update schema
tomfutago Aug 26, 2024
efae013
update fpmm source
tomfutago Aug 30, 2024
bf92625
Merge branch 'main' into polymarket
tomfutago Aug 30, 2024
6a81776
add safe proxies, update user captial actions
tomfutago Aug 30, 2024
0b5df2b
deposits, withdrawals AND transfers..
tomfutago Aug 30, 2024
a74b412
fix where clause
tomfutago Aug 30, 2024
01e4ed1
fix trades price, add shares
tomfutago Aug 31, 2024
79519af
add wallet proxies and filter internal addresses
0xBoxer Sep 1, 2024
0042c93
fix source def
0xBoxer Sep 1, 2024
c24ce1d
fix and add metadata
0xBoxer Sep 1, 2024
1d7023f
fix incremental condition
0xBoxer Sep 1, 2024
bf1e068
also exclude fpmm trades
0xBoxer Sep 1, 2024
975242e
actually remove fpmm
0xBoxer Sep 1, 2024
6996223
split trades to activity and ordersmatched
0xBoxer Sep 2, 2024
f420624
Revert "split trades to activity and ordersmatched"
0xBoxer Sep 2, 2024
b1691fa
add documentation
0xBoxer Sep 2, 2024
c4b2fb6
rerun models
0xBoxer Sep 28, 2024
cc22d73
Merge branch 'main' into polymarket
0xBoxer Sep 28, 2024
c05fe8d
push market metadata
0xBoxer Oct 4, 2024
30fde5f
see if balances run this way
0xBoxer Oct 4, 2024
8b83789
Merge branch 'polymarket' of https://github.com/tomfutago/spellbook i…
0xBoxer Oct 4, 2024
14fed69
remove
0xBoxer Oct 4, 2024
8aa6b0a
fix schema
0xBoxer Oct 4, 2024
c7d9c6e
change name of file
0xBoxer Oct 4, 2024
c2c725f
fix naming conventions
0xBoxer Oct 4, 2024
615b1fd
fix
0xBoxer Oct 4, 2024
0f1134a
maybe like this
0xBoxer Oct 4, 2024
ee885f1
remove date filter
0xBoxer Oct 4, 2024
4d628b3
fix test
0xBoxer Oct 4, 2024
7edb438
push
0xBoxer Oct 5, 2024
68f4c83
fix tests
0xBoxer Oct 5, 2024
243dd96
push
0xBoxer Oct 5, 2024
2fe43d5
fix compile
0xBoxer Oct 5, 2024
931776d
add prices?
0xBoxer Oct 5, 2024
af9831f
comm
0xBoxer Oct 5, 2024
c0bce6b
ggwp?
0xBoxer Oct 5, 2024
b6bcb54
revert position change
0xBoxer Oct 5, 2024
3aa7d5f
send it
0xBoxer Oct 5, 2024
6fc8565
fix
0xBoxer Oct 6, 2024
35a3ebb
fix
0xBoxer Oct 6, 2024
7a509a6
fix
0xBoxer Oct 6, 2024
48f2653
change name and convert to view
0xBoxer Oct 7, 2024
c84fe2e
ss
0xBoxer Oct 7, 2024
9231449
fix market details
0xBoxer Oct 7, 2024
fab110f
o god
0xBoxer Oct 7, 2024
61775a4
fix schema
0xBoxer Oct 7, 2024
fd1fc7c
fix schema for real
0xBoxer Oct 7, 2024
d4d84ef
fix incremental build of positions
0xBoxer Oct 7, 2024
7bca117
fix fpmm recognition
0xBoxer Oct 7, 2024
fcdd044
fix address mention
0xBoxer Oct 7, 2024
987ac83
test adding usdc
0xBoxer Oct 7, 2024
d5be24c
fix model name
0xBoxer Oct 8, 2024
c5ef4ae
add distinct
0xBoxer Oct 12, 2024
c7713e9
change to new balances table
0xBoxer Oct 20, 2024
5f568ae
fix
0xBoxer Oct 20, 2024
2f86155
fix
0xBoxer Oct 20, 2024
5d85e39
fix safe
0xBoxer Oct 20, 2024
89d497c
fix positions and trades failing tests
0xBoxer Oct 20, 2024
c5b67ed
fix timeframe for testing
0xBoxer Oct 20, 2024
9012099
fix duplicates?
0xBoxer Oct 20, 2024
97c650b
fix trades
0xBoxer Oct 20, 2024
966e5b6
fix incremental predicate
0xBoxer Oct 21, 2024
2d0efa6
take out date filter
0xBoxer Oct 21, 2024
ec3e49a
gg?
0xBoxer Oct 21, 2024
3473f37
Merge branch 'main' into polymarket
0xBoxer Oct 23, 2024
573bc5d
Merge branch 'main' into polymarket
0xBoxer Oct 23, 2024
f22a1f4
Merge branch 'polymarket' of https://github.com/tomfutago/spellbook i…
0xBoxer Oct 23, 2024
5654f3f
exclude system address
0xBoxer Oct 23, 2024
2c0cb99
rename tests to data_tests
0xBoxer Oct 23, 2024
7c40090
Merge branch 'main' into polymarket
0xBoxer Oct 24, 2024
5c3bebf
Merge branch 'main' into polymarket
jeff-dude Oct 25, 2024
14edb22
pls run
0xBoxer Oct 25, 2024
149cde5
Merge branch 'polymarket' of https://github.com/tomfutago/spellbook i…
0xBoxer Oct 25, 2024
73aeb5a
fix compile
0xBoxer Oct 25, 2024
33b8394
fix
0xBoxer Oct 25, 2024
ad90c24
fix users group by
0xBoxer Oct 25, 2024
5e460da
Merge branch 'main' into polymarket
0xBoxer Oct 25, 2024
8472043
fix schema issues
0xBoxer Oct 25, 2024
0f46b6e
fix failing test
0xBoxer Oct 25, 2024
6cb2891
fix incremental merge condition
0xBoxer Oct 25, 2024
1e215e6
fix DBT_INTERNAL_DEST column
0xBoxer Oct 25, 2024
2064545
fix exclusion conditiions
0xBoxer Oct 27, 2024
d247e09
add in swap category to capital actions
0xBoxer Oct 27, 2024
f3b242c
fix capital actions again
0xBoxer Oct 27, 2024
1e74dfb
fix conversion logic
0xBoxer Oct 27, 2024
bb73387
fix pricefeeds
0xBoxer Oct 28, 2024
440ade8
no from_hex
0xBoxer Oct 28, 2024
6063249
fix hex and switch to min_by
0xBoxer Oct 28, 2024
6ba963d
one min_by too many
0xBoxer Oct 28, 2024
06956b0
fix pricefeed join condition
0xBoxer Oct 28, 2024
2e9129a
fix parentheses
0xBoxer Oct 28, 2024
affd280
Merge branch 'main' into polymarket
jeff-dude Oct 28, 2024
7d254b2
Merge branch 'main' into polymarket
jeff-dude Oct 28, 2024
c744c43
move to _projects directory
jeff-dude Oct 28, 2024
b389560
update config blocks
jeff-dude Oct 28, 2024
5776771
add partitions on large raw tables
jeff-dude Oct 28, 2024
9e726aa
test using balances macro
jeff-dude Oct 28, 2024
d7be432
Merge branch 'main' into polymarket
jeff-dude Oct 28, 2024
bb80855
fix columns, update schema file
jeff-dude Oct 28, 2024
ab90004
add token_id back to unique
jeff-dude Oct 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
482 changes: 482 additions & 0 deletions dbt_subprojects/daily_spellbook/models/polymarket/polygon/_schema.yml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{{
config(
schema = 'polymarket_polygon',
alias = 'base_ctf_tokens',
materialized = 'view',
unique_key = ['condition_id','token0','token1'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.block_time')]
)
}}

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{{
config(
schema = 'polymarket_polygon',
alias = 'market_details',
materialized = 'table',
file_format = 'delta',
full_refresh = true,
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
Original file line number Diff line number Diff line change
@@ -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 %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{{ config(
schema = 'polymarket_polygon',
alias = 'market_prices_daily',
materialized = 'view',
unique_key = ['day', 'token_id'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.day')],
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{{ config(
schema = 'polymarket_polygon',
alias = 'market_prices_hourly',
materialized = 'view',
unique_key = ['hour', 'token_id'],
incremental_predicates = [incremental_predicate('DBT_INTERNAL_DEST.hour')],
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
Loading
Loading