Skip to content

Instantly share code, notes, and snippets.

@Aviksaikat
Last active November 30, 2023 00:34
Show Gist options
  • Save Aviksaikat/a0906475e51dff00191496ba3872db29 to your computer and use it in GitHub Desktop.
Save Aviksaikat/a0906475e51dff00191496ba3872db29 to your computer and use it in GitHub Desktop.
Get spot price of a token pair in curve using ape
from ape import project
from ape.api.accounts import AccountAPI
# ? Curve
CURVE_POOL_Address = "0xB9fC157394Af804a3578134A6585C0dc9cc990d4" # always verify this address don't take anyones word. At the time of creating this gist this was the latest curve pool address.
def get_CurvePool():
return project.ICurvePool.at(CURVE_POOL_Address)
def CurveSwapIn(pool, _from: AccountAPI, tokenIn: str, tokenout: str, amount: int = 0) -> int:
i = 0
j = 0
coinIdx = 0
while i == j:
coin = pool.coins(coinIdx)
if coin == tokenIn:
i = coinIdx
elif coin == tokenOut:
j = coinIdx
if i != j:
break
coinIdx += 1
amountsOut = pool.get_dy.call(i, j, amount, sender=_from)
return amountsOut
# ****************
# * Curve *
# ****************
def getCurveSwapPrice(account, tokens: list):
curvePool = get_CurvePool()
amountsOutCurve = CurveSwapIn(curvePool, account, tokens[1], tokens[0], ETH_AMOUNT)
print("Curve Swapped tokens: ", amountsOutCurve // (10 ** WETH.decimals()))
def main():
tokens = []
# replace with the addresses of the respective tokens
tokens.append(DAI)
tokens.append(USDC)
getCurveSwapPrice(account, tokens) # account is ape account container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment