Skip to content

Instantly share code, notes, and snippets.

@harsh-98
Last active January 24, 2023 16:40
Show Gist options
  • Save harsh-98/6125b9b95851245f3f505db9675b621a to your computer and use it in GitHub Desktop.
Save harsh-98/6125b9b95851245f3f505db9675b621a to your computer and use it in GitHub Desktop.
Calculating trade volume on gearbox v2

All the operations done on v2:

 depositAll(uint256,bool)
 CloseCreditAccount(address,address)
 swapExactTokensForTokens(uint256,uint256,address[],address,uint256)
 withdrawAndUnwrap(uint256,bool)
 unwrap(uint256)
 exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160))
 DecreaseBorrowedAmount(address,uint256)
 add_liquidity(uint256[3],uint256)
 DirectTokenTransfer
 withdrawAllAndUnwrap(bool)
 add_liquidity(uint256[2],uint256)
 MultiCallStarted(address)
 submit(uint256,address)
 exchange_underlying(int128,int128,uint256,uint256)
 deposit(uint256,uint256,bool)
 add_liquidity(uint256[4],uint256)
 withdraw(uint256)
 exchange(int128,int128,uint256,uint256)
 LiquidateCreditAccount(address,address,address,uint256)
 wrap(uint256)
 RewardClaimed
 withdrawAll(bool)
 withdrawAll(uint256)
 IncreaseBorrowedAmount(address,uint256)
 deposit(uint256)
 OpenCreditAccount(address,address,uint256,uint16)
 TokenEnabled(address,address)
 remove_liquidity_one_coin(uint256,int128,uint256)
 stake(uint256)
 TokenDisabled(address,address)
 AddCollateral(address,address,uint256)
 exactInput((bytes,address,uint256,uint256,uint256))

Operations excluded for calculating volume:

CloseCreditAccount(address,address)' and 
DecreaseBorrowedAmount(address,uint256)' and 
DirectTokenTransfer' and 
MultiCallStarted(address)' and 
LiquidateCreditAccount(address,address,address,uint256)' and 
IncreaseBorrowedAmount(address,uint256)' and 
OpenCreditAccount(address,address,uint256,uint16)' and 
TokenEnabled(address,address)' and 
TokenDisabled(address,address)' and 
AddCollateral(address,address,uint256)'

For this calculation, we track all token transfers for executeorder events. Volume for an executeorder operations is the amount of token sent from account, i.e. negative sign. Total volume is 838658431.9652352 838M as of 24 jan 1600 UTC .

Number of CAs 368, number of operations where we count volume is 2674.

Number of operations:

with ledger as (select (each(transfers)).*, tx_hash, action from account_operations ao join credit_sessions cs on cs.id=ao.session_id where version=2 and  action != 'CloseCreditAccount(address,address)' and action != 'DecreaseBorrowedAmount(address,uint256)' and action != 'DirectTokenTransfer' and action != 'MultiCallStarted(address)' and action != 'LiquidateCreditAccount(address,address,address,uint256)' and action != 'IncreaseBorrowedAmount(address,uint256)' and action != 'OpenCreditAccount(address,address,uint256,uint16)' and action != 'TokenEnabled(address,address)' and action != 'TokenDisabled(address,address)' and action != 'AddCollateral(address,address,uint256)')
  select count(*) from ledger where value::float<0 ;

Since for calculating volume we sum negative amount(or amount withdraw for operation from account), we should check if only 1 negative number is taken for the operation to avoid double counting. Indeed only 1 negative number is taken for each operations, as operation with only 1 negative number is 2674.

 with ledger as (select (each(transfers)).*, tx_hash, block_num , log_id, action from account_operations ao join credit_sessions cs on cs.id=ao.session_id where version=2 and  action != 'CloseCreditAccount(address,address)' and action != 'DecreaseBorrowedAmount(address,uint256)' and action != 'DirectTokenTransfer' and action != 'MultiCallStarted(address)' and action != 'LiquidateCreditAccount(address,address,address,uint256)' and action != 'IncreaseBorrowedAmount(address,uint256)' and action != 'OpenCreditAccount(address,address,uint256,uint16)' and action != 'TokenEnabled(address,address)' and action != 'TokenDisabled(address,address)' and action != 'AddCollateral(address,address,uint256)')
  select count(*) from (select count(*) from ledger where value::float<0 group by block_num , log_id) c where c.count=1 ;
with ledger as (
select
(
each(transfers)
).*,
action
from
account_operations ao
join credit_sessions cs on cs.id = ao.session_id
where
version = 2
and action != 'CloseCreditAccount(address,address)'
and action != 'DecreaseBorrowedAmount(address,uint256)'
and action != 'DirectTokenTransfer'
and action != 'MultiCallStarted(address)'
and action != 'LiquidateCreditAccount(address,address,address,uint256)'
and action != 'IncreaseBorrowedAmount(address,uint256)'
and action != 'OpenCreditAccount(address,address,uint256,uint16)'
and action != 'TokenEnabled(address,address)'
and action != 'TokenDisabled(address,address)'
and action != 'AddCollateral(address,address,uint256)'
),
vol as (
select
b.token,
(
bint / power(10, decimals)
) amount,
tcp.price,
symbol,
(
tcp.price *(
bint / power(10, decimals)
)
) volume
from
(
select
key token,
sum(-1 * value :: float4) bint
from
ledger
where
value :: float < 0
group by
key
) b
join tokens t on t.address = b.token
join token_current_price tcp on tcp.token = b.token
)
select
sum(volume)
from
vol;
with ledger as (select (each(transfers)).*, action from account_operations ao join credit_sessions cs on cs.id=ao.session_id where version=2 and action != 'CloseCreditAccount(address,address)' and action != 'DecreaseBorrowedAmount(address,uint256)' and action != 'DirectTokenTransfer' and action != 'MultiCallStarted(address)' and action != 'LiquidateCreditAccount(address,address,address,uint256)' and action != 'IncreaseBorrowedAmount(address,uint256)' and action != 'OpenCreditAccount(address,address,uint256,uint16)' and action != 'TokenEnabled(address,address)' and action != 'TokenDisabled(address,address)' and action != 'AddCollateral(address,address,uint256)')
select b.token, (bint/power(10, decimals)) amount, tcp.price, symbol, (tcp.price*(bint/power(10, decimals))) volume from (select key token, sum(-1*value::float4) bint from ledger where value::float<0 group by key) b join tokens t on t.address=b.token join token_current_price tcp on tcp.token=b.token;
token | amount | price | symbol | volume
--------------------------------------------+--------------------+----------------+------------------+--------------------
0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84 | 17654.97278809624 | 1617.80645177 | stETH | 28562328.882405877
0x5f98805A4E8be255a32880FDeC7F6728C6568bA0 | 1154566.2793384467 | 1.01569388 | LUSD | 1172685.9039784307
0xdCD90C7f6324cfa40d7169ef80b12031770B4325 | 3383.3583769114307 | 1889.39199157 | yvCurve-stETH | 6392490.221947731
0xEd279fDD11cA84bEef15AF5D39BB4d4bEE23F0cA | 43968465.36380452 | 1.01850767 | LUSD3CRV-f | 44782219.21116424
0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd | 541016.14 | 1.00029577 | GUSD | 541176.1563437277
0x5A98FcBEA516Cf06857215779Fd812CA3beF1B32 | 57326.352334445146 | 0.00090729034 | LDO | 52.01164570047853
0x853d955aCEf822Db058eb8505911ED77F175b99e | 27702251.07098228 | 0.99969529 | FRAX | 27693809.918058444
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 | 58362.75746590343 | 1620.77 | WETH | 94592606.4180123
0x15c2471ef46Fa721990730cfa526BcFb45574576 | 437448.1155837806 | 1.03194261 | cvxgusd3CRV | 451421.3501351082
0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 | 111729058.559436 | 1.00038 | USDC | 111771515.60168858
0x3175Df0976dFA876431C2E9eE6Bc45b65d3473CC | 18855569.1932701 | 1.00079236 | crvFRAX | 18870509.592076078
0xdA816459F1AB5631232FE5e97a05BBBb94970c95 | 15152756.661954984 | 1.04453042 | yvDAI | 15827515.280269638
0x06325440D014e39736583c165C2963BA99fAf14E | 25309.381959912225 | 1720.44559239 | steCRV | 43543414.63904597
0x6B175474E89094C44Da98b954EedeAC495271d0F | 74739065.6769624 | 1.00069377 | DAI | 74790917.39855711
0xD2967f45c4f384DEEa880F807Be904762a3DeA07 | 41943060.63873705 | 1.03194261 | gusd3CRV | 43282831.46692658
0xD533a949740bb3306d119CC777fa900bA034cd52 | 181254.23791695887 | 1.0667 | CRV | 193343.89558602002
0x3432B6A60D23Ca0dFCa7761B7ab56459D9C964D0 | 2055.231109309554 | 10.59000613 | FXS | 21764.91004615488
0xd632f22692FaC7611d2AA1C0D552930D43CAEd3B | 75060548.71285471 | 1.00905993 | FRAX3CRV-f | 75740592.02995478
0xa258C4606Ca8206D8aA700cE2143D7db854D168c | 21130.63317686805 | 1665.68754615 | yvWETH | 35197032.52497312
0xdAC17F958D2ee523a2206206994597C13D831ec7 | 1820500.259673 | 1.00000044 | USDT | 1820501.0606931143
0x9518c9063eB0262D791f38d8d6Eb0aca33c63ed0 | 56.34601488068326 | 1720.44559239 | cvxsteCRV | 96940.25295021287
0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0 | 1866.6812628930197 | 1790.25761322 | wstETH | 3341840.342349353
0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B | 10080.870596736439 | 5.43483202 | CVX | 54787.83830861971
0xC011a73ee8576Fb46F5E1c5751cA3B9Fe0af2a6F | 7682.676449167094 | 2.54390565 | SNX | 19544.00402615811
0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599 | 35.12709924 | 22874.07977917 | WBTC | 803500.0704265818
0xC25a3A3b969415c80451098fa907EC722572917F | 64539885.71692767 | 1.06083259 | crvPlain3andSUSD | 68466014.12339239
0xa354F35829Ae975e850e23e9615b11Da1B3dC4DE | 1606572.054528 | 1.02651392 | yvUSDC | 1649168.577455991
0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490 | 135782070.85646263 | 1.02353652 | 3Crv | 138977908.28281718
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment