-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #135 from Synthetixio/collateral-balances
Collateral balances
- Loading branch information
Showing
19 changed files
with
548 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pyarrow | |
psycopg2-binary | ||
dbt-postgres | ||
sqlfluff | ||
synthetix==0.1.21 |
129 changes: 129 additions & 0 deletions
129
...etix/models/marts/arbitrum/mainnet/perp/fct_perp_collateral_balances_arbitrum_mainnet.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
with synths as ( | ||
select | ||
synth_market_id as collateral_id, | ||
synth_token_address | ||
from | ||
{{ ref('spot_synth_registered_arbitrum_mainnet') }} | ||
), | ||
|
||
transfers as ( | ||
select | ||
cm.block_number, | ||
cm.block_timestamp as ts, | ||
cm.transaction_hash, | ||
cm.collateral_id, | ||
synths.synth_token_address, | ||
CAST(cm.account_id as text) as account_id, | ||
{{ convert_wei('cm.amount_delta') }} as amount_delta | ||
from | ||
{{ ref('perp_collateral_modified_arbitrum_mainnet') }} as cm | ||
inner join synths | ||
on cm.collateral_id = synths.collateral_id | ||
), | ||
|
||
liq_tx as ( | ||
select distinct | ||
transaction_hash, | ||
CAST(account_id as text) as account_id | ||
from | ||
{{ ref('perp_account_liquidation_attempt_arbitrum_mainnet') }} | ||
), | ||
|
||
distributors as ( | ||
select | ||
CAST(rd.distributor_address as text) as distributor_address, | ||
CAST(rd.token_symbol as text) as token_symbol, | ||
rd.synth_token_address, | ||
synths.collateral_id | ||
from | ||
{{ ref('arbitrum_mainnet_reward_distributors') }} as rd | ||
inner join synths | ||
on rd.synth_token_address = synths.synth_token_address | ||
), | ||
|
||
liquidations as ( | ||
select | ||
rd.block_number, | ||
rd.block_timestamp as ts, | ||
-rd.amount / 1e18 as amount_delta, | ||
liq_tx.transaction_hash, | ||
rd.collateral_type, | ||
distributors.token_symbol, | ||
distributors.synth_token_address, | ||
CAST(liq_tx.account_id as text) as account_id, | ||
distributors.collateral_id | ||
from | ||
{{ ref('core_rewards_distributed_arbitrum_mainnet') }} as rd | ||
inner join liq_tx | ||
on rd.transaction_hash = liq_tx.transaction_hash | ||
inner join distributors | ||
on rd.distributor = distributors.distributor_address | ||
), | ||
|
||
net_transfers as ( | ||
select | ||
events.ts, | ||
events.transaction_hash, | ||
events.event_type, | ||
events.collateral_id, | ||
events.synth_token_address, | ||
synths.synth_symbol, | ||
events.account_id, | ||
prices.price, | ||
events.amount_delta, | ||
SUM(events.amount_delta) over ( | ||
partition by events.account_id, events.collateral_id | ||
order by events.ts | ||
) as account_balance, | ||
SUM(events.amount_delta) over ( | ||
partition by events.collateral_id | ||
order by events.ts | ||
) as total_balance | ||
from ( | ||
select | ||
transfers.ts, | ||
transfers.transaction_hash, | ||
transfers.collateral_id, | ||
transfers.synth_token_address, | ||
transfers.account_id, | ||
transfers.amount_delta, | ||
'transfer' as event_type | ||
from | ||
transfers | ||
union all | ||
select | ||
liquidations.ts, | ||
liquidations.transaction_hash, | ||
liquidations.collateral_id, | ||
liquidations.synth_token_address, | ||
liquidations.account_id, | ||
liquidations.amount_delta, | ||
'liquidation' as event_type | ||
from | ||
liquidations | ||
) as events | ||
inner join {{ ref('arbitrum_mainnet_synths') }} as synths | ||
on events.collateral_id = synths.synth_market_id | ||
left join {{ ref('fct_prices_hourly_arbitrum_mainnet') }} as prices | ||
on | ||
synths.token_symbol = prices.market_symbol | ||
and DATE_TRUNC('hour', events.ts) = prices.ts | ||
) | ||
|
||
select | ||
net_transfers.ts, | ||
net_transfers.transaction_hash, | ||
net_transfers.event_type, | ||
net_transfers.collateral_id, | ||
net_transfers.synth_token_address, | ||
net_transfers.synth_symbol, | ||
net_transfers.account_id, | ||
net_transfers.price, | ||
net_transfers.amount_delta, | ||
net_transfers.amount_delta * net_transfers.price as amount_delta_usd, | ||
net_transfers.account_balance, | ||
net_transfers.account_balance * net_transfers.price as account_balance_usd, | ||
net_transfers.total_balance, | ||
net_transfers.total_balance * net_transfers.price as total_balance_usd | ||
from | ||
net_transfers |
25 changes: 15 additions & 10 deletions
25
...etix/models/marts/arbitrum/mainnet/perp/fct_perp_collateral_modified_arbitrum_mainnet.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
select | ||
id, | ||
block_timestamp, | ||
account_id, | ||
block_number, | ||
transaction_hash, | ||
contract, | ||
event_name, | ||
collateral_id as synth_market_id, | ||
sender, | ||
{{ convert_wei("amount_delta") }} as amount_delta | ||
cm.id, | ||
cm.block_timestamp, | ||
cm.account_id, | ||
cm.block_number, | ||
cm.transaction_hash, | ||
cm.contract, | ||
cm.event_name, | ||
synths.synth_symbol, | ||
cm.collateral_id as synth_market_id, | ||
synths.synth_token_address, | ||
cm.sender, | ||
{{ convert_wei("cm.amount_delta") }} as amount_delta | ||
from | ||
{{ ref("perp_collateral_modified_arbitrum_mainnet") }} | ||
as cm | ||
inner join {{ ref('arbitrum_mainnet_synths') }} as synths | ||
on cm.collateral_id = synths.synth_market_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...etix/models/marts/arbitrum/sepolia/perp/fct_perp_collateral_balances_arbitrum_sepolia.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
with synths as ( | ||
select | ||
synth_market_id as collateral_id, | ||
synth_token_address | ||
from | ||
{{ ref('spot_synth_registered_arbitrum_sepolia') }} | ||
), | ||
|
||
transfers as ( | ||
select | ||
cm.block_number, | ||
cm.block_timestamp as ts, | ||
cm.transaction_hash, | ||
cm.collateral_id, | ||
synths.synth_token_address, | ||
CAST(cm.account_id as text) as account_id, | ||
{{ convert_wei('cm.amount_delta') }} as amount_delta | ||
from | ||
{{ ref('perp_collateral_modified_arbitrum_sepolia') }} as cm | ||
inner join synths | ||
on cm.collateral_id = synths.collateral_id | ||
), | ||
|
||
liq_tx as ( | ||
select distinct | ||
transaction_hash, | ||
CAST(account_id as text) as account_id | ||
from | ||
{{ ref('perp_account_liquidation_attempt_arbitrum_sepolia') }} | ||
), | ||
|
||
distributors as ( | ||
select | ||
CAST(rd.distributor_address as text) as distributor_address, | ||
CAST(rd.token_symbol as text) as token_symbol, | ||
rd.synth_token_address, | ||
synths.collateral_id | ||
from | ||
{{ ref('arbitrum_sepolia_reward_distributors') }} as rd | ||
inner join synths | ||
on rd.synth_token_address = synths.synth_token_address | ||
), | ||
|
||
liquidations as ( | ||
select | ||
rd.block_number, | ||
rd.block_timestamp as ts, | ||
-rd.amount / 1e18 as amount_delta, | ||
liq_tx.transaction_hash, | ||
rd.collateral_type, | ||
distributors.token_symbol, | ||
distributors.synth_token_address, | ||
CAST(liq_tx.account_id as text) as account_id, | ||
distributors.collateral_id | ||
from | ||
{{ ref('core_rewards_distributed_arbitrum_sepolia') }} as rd | ||
inner join liq_tx | ||
on rd.transaction_hash = liq_tx.transaction_hash | ||
inner join distributors | ||
on rd.distributor = distributors.distributor_address | ||
), | ||
|
||
net_transfers as ( | ||
select | ||
events.ts, | ||
events.transaction_hash, | ||
events.event_type, | ||
events.collateral_id, | ||
events.synth_token_address, | ||
synths.synth_symbol, | ||
events.account_id, | ||
prices.price, | ||
events.amount_delta, | ||
SUM(events.amount_delta) over ( | ||
partition by events.account_id, events.collateral_id | ||
order by events.ts | ||
) as account_balance, | ||
SUM(events.amount_delta) over ( | ||
partition by events.collateral_id | ||
order by events.ts | ||
) as total_balance | ||
from ( | ||
select | ||
transfers.ts, | ||
transfers.transaction_hash, | ||
transfers.collateral_id, | ||
transfers.synth_token_address, | ||
transfers.account_id, | ||
transfers.amount_delta, | ||
'transfer' as event_type | ||
from | ||
transfers | ||
union all | ||
select | ||
liquidations.ts, | ||
liquidations.transaction_hash, | ||
liquidations.collateral_id, | ||
liquidations.synth_token_address, | ||
liquidations.account_id, | ||
liquidations.amount_delta, | ||
'liquidation' as event_type | ||
from | ||
liquidations | ||
) as events | ||
inner join {{ ref('arbitrum_sepolia_synths') }} as synths | ||
on events.collateral_id = synths.synth_market_id | ||
left join {{ ref('fct_prices_hourly_arbitrum_sepolia') }} as prices | ||
on | ||
synths.token_symbol = prices.market_symbol | ||
and DATE_TRUNC('hour', events.ts) = prices.ts | ||
) | ||
|
||
select | ||
net_transfers.ts, | ||
net_transfers.transaction_hash, | ||
net_transfers.event_type, | ||
net_transfers.collateral_id, | ||
net_transfers.synth_token_address, | ||
net_transfers.synth_symbol, | ||
net_transfers.account_id, | ||
net_transfers.price, | ||
net_transfers.amount_delta, | ||
net_transfers.amount_delta * net_transfers.price as amount_delta_usd, | ||
net_transfers.account_balance, | ||
net_transfers.account_balance * net_transfers.price as account_balance_usd, | ||
net_transfers.total_balance, | ||
net_transfers.total_balance * net_transfers.price as total_balance_usd | ||
from | ||
net_transfers |
Oops, something went wrong.