Skip to content

Instantly share code, notes, and snippets.

@hrik2001
Created August 7, 2023 11:40
Show Gist options
  • Select an option

  • Save hrik2001/d1bd40d01e11ef0d4a36e54eb1c654b3 to your computer and use it in GitHub Desktop.

Select an option

Save hrik2001/d1bd40d01e11ef0d4a36e54eb1c654b3 to your computer and use it in GitHub Desktop.
from web3 import Web3
import json
import pandas as pd
w3 = Web3(Web3.HTTPProvider(f'https://arb-mainnet.g.alchemy.com/v2/L_HXVya-RV6U6SwLHWf1eUdlgwN20J-7'))
# we need data for the following strike and maturity
strike = 204169420152563078078024764
maturity = 1686830400
def fees(amount, txn_fees):
# since shortAmount is already deducted of 3% fee, so we reconstruct actual value and then deduct
return amount * (txn_fees/((2**16) - int((2**16 - 1) * 0.03)))
# token0 is 0x7a5D193fE4ED9098F7EAdC99797087C96b002907 (plsARB)
# token1 is 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8 (USDC)
# strike = token1/token0
# if strike > (1 >> 128) then short is in denomination of token0
# else the denomination is in token1
# since latter condition is true, hence shortAmount is in USDC (which makes sense since plsARB spot price is 0.54)
with open("abi/TimeswapV2Pool.json") as f:
abi = json.load(f)["abi"]
with open("abi/TimeswapV2PeripheryNoDexLendGivenPrincipal.json") as f:
abi_lend_given_principal_nodex = json.load(f)["abi"]
pool = w3.eth.contract(address='0x58f6EC313C993e306e9F2aa4F91C3e32Cd3F3430', abi=abi)
print("[-] fetching Deleverage events")
delev_events = pool.events.Deleverage.get_logs(fromBlock=82775904)
mint_events = pool.events.Mint.get_logs(fromBlock=82775904)
df = pd.DataFrame(columns=(['txn', 'lent (in USDC)', 'fee earned (USDC, 3%)'] + [ f'{2.5*(i+1)}%' for i in range(int(80/2.5))]))
df_relative = pd.DataFrame(columns=(['txn', 'lent (in USDC)', 'fee earned (USDC, 3%)'] + [ f'{2.5*(i+1)}%' for i in range(int(80/2.5))]))
minted_amount = 0
for i in mint_events:
if i.args.strike == strike and i.args.maturity == maturity:
# print(i)
minted_amount += i.args.long1Amount
duration = maturity - w3.eth.get_block(i.blockNumber).timestamp #2933817
fee = 0
for i in delev_events:
if i.args.strike == strike and i.args.maturity == maturity:
# print(i)
fee += fees(i.args.shortAmount, int((2**16 - 1) * 0.03))
df.loc[len(df)] = [f'https://arbiscan.io/tx/{i.transactionHash.hex()}', i.args.long1Amount/1e6, (fees(i.args.shortAmount, int((2**16 - 1) * 0.03))/1e6)] + [(fees(i.args.shortAmount, int((2**16 - 1) * (2.5 * (x+1))/100))/1e6) for x in range(int(80/2.5))]
df_relative.loc[len(df)] = [f'https://arbiscan.io/tx/{i.transactionHash.hex()}', i.args.long1Amount/1e6, (fees(i.args.shortAmount, int((2**16 - 1) * 0.03))/1e6)] + [((fees(i.args.shortAmount, int((2**16 - 1) * (2.5 * (x+1))/100))/1e6))/(i.args.long1Amount/1e4) for x in range(int(80/2.5))]
print("-------Calculating APR------")
print("fee:", fee/1e6)
print("Principal Amount:", minted_amount/1e6)
# APR = (fee earned/principle amount) * (seconds in year/duration)
print(duration)
apr = (fee/minted_amount) * (31536000/duration)
print(f'APR: {apr * 100}%')
df.to_csv("test.csv")
df_relative.to_csv("test_relative.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment