Skip to content

Instantly share code, notes, and snippets.

@kangchihlun
Last active October 16, 2021 06:00
Show Gist options
  • Save kangchihlun/580bdf50cb87b7391732cc9eb24cea2c to your computer and use it in GitHub Desktop.
Save kangchihlun/580bdf50cb87b7391732cc9eb24cea2c to your computer and use it in GitHub Desktop.
UniswapV3計算當前美金在設定的價格帶需要兌換多少數量的 eth
# coding=utf-8
import os,sys
import time
import math
cprice_eth = 3840
cprice_matic = 1.565
lower = 2000
upper = 2991
initCapital = 6197 # 初始資金(美金)
def getTokenAmountsFromDepositAmounts(
P, # 當前幣價
Pl, # 下界
Pu, # 上界
priceUSDX, # 幣種1對美元
priceUSDY, # 幣種2對美元
targetAmounts # 投入資金美金計價
):
deltaL = targetAmounts / ((math.sqrt(P) - math.sqrt(Pl)) * priceUSDY +
(1 / math.sqrt(P) - 1 / math.sqrt(Pu)) * priceUSDX)
deltaY = deltaL * (math.sqrt(P) - math.sqrt(Pl))
if (deltaY * priceUSDY < 0):
deltaY = 0
if (deltaY * priceUSDY > targetAmounts):
deltaY = targetAmounts / priceUSDY
deltaX = deltaL * (1 / math.sqrt(P) - 1 / math.sqrt(Pu))
if (deltaX * priceUSDX < 0):
deltaX = 0;
if (deltaX * priceUSDX > targetAmounts):
deltaX = targetAmounts / priceUSDX
#actualLQ = amt1 / (math.sqrt(upper) - math.sqrt(lower))
return deltaX,deltaY
amt1,amt2 = getTokenAmountsFromDepositAmounts(cprice_eth/cprice_matic, lower, upper, cprice_eth, cprice_matic, initCapital)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment