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 dodo daily jobs #113

Open
wants to merge 1 commit into
base: improve_daily_job
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions indexer/aggr_jobs/disorder_jobs/daily_address_token_balances.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
delete
from daily_address_token_balances
WHERE block_date >= '{start_date}'
and block_date < '{end_date}';

insert into public.daily_address_token_balances (address, block_date, token_address, token_id, token_type, balance
)

select address,
date(block_timestamp) as block_date,
token_address,
token_id,
token_type,
balance
from (select *,
row_number() over (partition by address,token_address,token_id order by block_timestamp desc) as rn
from address_token_balances
WHERE block_timestamp >= '{start_date}'
and block_timestamp < '{end_date}') t
where rn = 1;

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from sqlalchemy import DATE, TIMESTAMP, Column, PrimaryKeyConstraint, func
from sqlalchemy.dialects.postgresql import BYTEA, NUMERIC, VARCHAR

from common.models import HemeraModel


class DailyAddressTokenBalances(HemeraModel):
__tablename__ = "daily_address_token_balances"

block_date = Column(DATE, primary_key=True, nullable=False)
address = Column(BYTEA, primary_key=True)
token_address = Column(BYTEA, primary_key=True)
token_id = Column(NUMERIC(78), primary_key=True)
token_type = Column(VARCHAR)
balance = Column(NUMERIC(100))

create_time = Column(TIMESTAMP, server_default=func.now())

__table_args__ = (PrimaryKeyConstraint("block_date", "address", "token_address", "token_id"),)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from sqlalchemy import DATE, TIMESTAMP, Column, PrimaryKeyConstraint, func
from sqlalchemy.dialects.postgresql import BYTEA, NUMERIC, VARCHAR

from common.models import HemeraModel


class PeriodAddressTokenBalances(HemeraModel):
__tablename__ = "period_address_token_balances"

period_date = Column(DATE, nullable=False)
address = Column(BYTEA, primary_key=True)
token_address = Column(BYTEA, primary_key=True)
token_id = Column(NUMERIC(78), primary_key=True)
token_type = Column(VARCHAR)
balance = Column(NUMERIC(100))

create_time = Column(TIMESTAMP, server_default=func.now())

__table_args__ = (PrimaryKeyConstraint("address", "token_address", "token_id"),)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from datetime import datetime

from sqlalchemy import DATE, Column, Index, PrimaryKeyConstraint, desc, func
from sqlalchemy.dialects.postgresql import BIGINT, BOOLEAN, BYTEA, NUMERIC, TIMESTAMP

from common.models import HemeraModel, general_converter


class PeriodFeatureErc1155TokenSupplyRecords(HemeraModel):
__tablename__ = "period_feature_erc20_token_supply_records"

period_date = Column(DATE, primary_key=True)
token_address = Column(BYTEA, primary_key=True)

total_supply = Column(NUMERIC(100))

create_time = Column(TIMESTAMP, server_default=func.now())
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from sqlalchemy import DATE, TIMESTAMP, Column, Computed, Index, String, func
from sqlalchemy.dialects.postgresql import BYTEA, INTEGER, NUMERIC

from common.models import HemeraModel


class PeriodFeatureHoldingBalanceDoDo(HemeraModel):
__tablename__ = "period_feature_holding_balance_dodo"

period_date = Column(DATE, primary_key=True, nullable=False)
protocol_id = Column(String, primary_key=True, nullable=False)
contract_address = Column(BYTEA, primary_key=True, nullable=False)
wallet_address = Column(BYTEA, primary_key=True, nullable=False)

balance_of = Column(NUMERIC(100, 18))
total_supply = Column(NUMERIC(100, 18))

token0_address = Column(BYTEA, nullable=False)
token0_symbol = Column(String, nullable=False)
token0_balance = Column(NUMERIC(100, 18))

token1_address = Column(BYTEA, nullable=False)
token1_symbol = Column(String, nullable=False)
token1_balance = Column(NUMERIC(100, 18))

create_time = Column(TIMESTAMP, server_default=func.now())
10 changes: 10 additions & 0 deletions indexer/aggr_jobs/order_jobs/period_address_token_balances.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
INSERT INTO period_address_token_balances (address, period_date, token_address, token_id, token_type, balance)
SELECT d1.address, d1.block_date, d1.token_address, d1.token_id, d1.token_type, d1.balance
FROM daily_address_token_balances d1
where block_date = '{start_date}'
ON CONFLICT (address, token_address, token_id)
DO UPDATE
SET balance = EXCLUDED.balance,
token_type = EXCLUDED.token_type,
period_date = EXCLUDED.period_date
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
delete
from period_feature_erc20_token_supply_records
WHERE period_date = '{start_date}';

insert into public.period_feature_erc20_token_supply_records(period_date, token_address, total_supply)
select date('{start_date}'),
token_address,
total_supply
from (select *,
row_number() over (partition by token_address order by block_date desc) as rn
from daily_feature_erc20_token_supply_records
WHERE block_date < '{end_date}') t
where rn = 1;

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
delete
from period_feature_holding_balance_dodo
where period_date >= '{start_date}'
and period_date < '{end_date}';



WITH supply_cte AS (SELECT total_supply
FROM period_feature_erc20_token_supply_records
WHERE period_date = '{start_date}'
and token_address = '\xD39DFbfBA9E7eccd813918FfbDa10B783EA3b3C6' --gcb
),

gcb_balance as (select period_date,
address,
token_address,
balance,
total_supply,
balance / total_supply as rate
from period_address_token_balances,
supply_cte
where token_address = '\xD39DFbfBA9E7eccd813918FfbDa10B783EA3b3C6'),

address_balance as (select d1.address,
d1.token_address as token0_address,
d1.balance as token0_balance,
d2.token_address as token1_address,
d2.balance as token1_balance
from (select *
from period_address_token_balances
where address = '\xD39DFbfBA9E7eccd813918FfbDa10B783EA3b3C6'
and token_address = '\xc96de26018a54d51c097160568752c4e3bd6c364') d1
inner join
(select *
from period_address_token_balances
where address = '\xD39DFbfBA9E7eccd813918FfbDa10B783EA3b3C6'
-- mantle,
and token_address = '\xCAbAE6f6Ea1ecaB08Ad02fE02ce9A44F09aebfA2') d2
on d1.address = d2.address),


fbtc_and_wbtc_balance_table as (select d1.address,
d2.address as base_token_address,
d2.symbol as base_token_symbol,
d2.decimals as base_token_decimals,
d3.address as quote_token_address,
d3.symbol as quote_token_symbol,
d3.decimals as quote_token_decimals,
-- mantle,
d1.token0_balance as base_balance,
d1.token1_balance as quote_balance

from address_balance d1
inner join tokens d2 on d1.token0_address = d2.address
inner join tokens d3 on d1.token1_address = d3.address)

insert
into period_feature_holding_balance_dodo (period_date, protocol_id, contract_address, wallet_address, balance_of,
total_supply, token0_address, token0_symbol, token0_balance,
token1_address, token1_symbol, token1_balance)
select date('{start_date}'),
'dodo',
d1.token_address,
d1.address,
d1.balance,
d1.total_supply,
d2.base_token_address,
d2.base_token_symbol,
rate * base_balance / pow(10, base_token_decimals) as base_token_balance,
d2.quote_token_address,
d2.quote_token_symbol,
rate * quote_balance / pow(10, quote_token_decimals) as quote_token_balance

from gcb_balance d1
inner join
fbtc_and_wbtc_balance_table d2 on d1.token_address = d2.address
;
Loading