Skip to content

Instantly share code, notes, and snippets.

@cheekybastard
Forked from banteg/yusd_price.py
Created September 15, 2020 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheekybastard/4bdda9e32910f3f335321a16c3b55098 to your computer and use it in GitHub Desktop.
Save cheekybastard/4bdda9e32910f3f335321a16c3b55098 to your computer and use it in GitHub Desktop.
>>> yswap = Contract.from_explorer('0x45F783CCE6B7FF23B2ab2D70e416cdb7D6055f51')
>>> yusd = Contract.from_explorer('0x5dbcF33D8c2E976c6b560249878e6F1491Bca25c')
>>> ycrv = Contract.from_explorer('0xdF5e0e81Dff6FAF3A7e52BA697820c5e32D806A8')
# coins are 0 = idai, 1 = iusdc, 2 = iusdt, 3 = itusd
# underlying coins are 0 = dai, 1 = usdc, 2 = usdt, 3 = tusd
# this gets us the i-counterparts which also sit in money markets
>>> coins = [Contract.from_explorer(yswap.coins(i)) for i in range(4)]
>>> dec = [10 ** coin.decimals() for coin in coins]
[1000000000000000000, 1000000, 1000000, 1000000000000000000]
# get the ratios of icoin to underlying coin
>>> share_prices = [coin.getPricePerFullShare() / 1e18 for coin in coins]
[1.042675578262197, 1.1612957010196199, 1.0282523136465582, 1.016557382897329]
# get the prices of underlying coins
>>> prices = [yswap.get_dy_underlying(i, 1, dec[i]) / dec[1] if i != 1 else 1 for i in range(4)]
[1.034551, 1, 0.99883, 0.999659]
# get icoin amounts sitting in curve pool
>>> amounts = [coins[i].balanceOf(yswap) / dec[i] for i in range(4)]
[10100347.885779707, 132427378.107085, 213610934.241457, 147861301.69453427]
# calculate the total amount sitting in curve pool normalized to usdc
>>> total = sum(amount * price * share_price for amount, price, share_price in zip(amounts, prices, share_prices))
534329794.83658874
# calculate the real ycrv price and compare it to the virtual price yswap tracks
>>> ycrv_supply = ycrv.totalSupply() / 10 ** ycrv.decimals()
507516510.4410611
>>> real_ycrv_price = total / ycrv_supply
1.052660343355707
>>> virtual_ycrv_price = yswap.get_virtual_price()
1.0519014833253257
# virtual price tracks close enough to no go through all these hoops imo
# the final step is to multiply by yusd/ycrv ratio
>>> yusd_share_price = yusd.getPricePerFullShare() / 1e18
1.0963403426370786
>>> yusd_price = yusd_share_price * real_ycrv_price
1.1540740015150606
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment