[英]How to get exact value of a token with Web3.py on PancakeSwap? Function getAmountsOut() returns a wrong value
Good day everyone, I'll try to be as clear as possible.大家好,我会尽量说清楚。 I was trying to get the price of s**tcoins using web3.py and, after solving many problems, I'm stuck on the question i asked.我试图使用 web3.py 获得 s**tcoins 的价格,并且在解决了许多问题之后,我被困在我提出的问题上。
tokenAddres = '0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82' #Cake
tokenAddres = Web3.toChecksumAddress(tokenAddres)
bnbPrice = calcBNBPrice()
print(f'current BNB price: {bnbPrice}')
priceInBnb = calcSell(1, tokenAddres)
print(f'SHIT_TOKEN VALUE IN BNB : {priceInBnb} | Just convert it to USD ')
print(f'SHIT_TOKEN VALUE IN USD: {priceInBnb * bnbPrice}')
The calcsell function should be the one that return the value of the token in BNB calcsell function 应该是返回 BNB 中令牌值的那个
def calcSell(tokenToSell, tokenAddress):
BNBTokenAddress = Web3.toChecksumAddress("0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c") # BNB
amountOut = None
tokenRouter = web3.eth.contract(address=Web3.toChecksumAddress(tokenAddress), abi=tokenAbi)
tokenDecimals = tokenRouter.functions.decimals().call()
tokenToSell = setDecimals(tokenToSell, tokenDecimals) # Set token a correct number of 0s
router = web3.eth.contract(address=Web3.toChecksumAddress(pancakeSwapContract), abi=pancakeSwapAbi)
amountIn = web3.toWei(tokenToSell, 'ether')
amountOut = router.functions.getAmountsOut(amountIn, [tokenAddress, BNBTokenAddress]).call()
amountOut = web3.fromWei(amountOut[1], 'ether')
return amountOut
The value i get is:我得到的价值是:
SHIT_TOKEN VALUE IN BNB: 974136.205251839691973598 | BNB 中的 SHIT_TOKEN 值:974136.205251839691973598 | Just convert it to USD只需将其转换为美元
SHIT_TOKEN VALUE IN USD: 340708627.4489159379891912819 SHIT_TOKEN 美元价值:340708627.4489159379891912819
while the correct one is:而正确的是:
SHIT_TOKEN VALUE IN BNB: 0.048846069961106416 | BNB 中的 SHIT_TOKEN 值:0.048846069961106416 | Just convert it to USD只需将其转换为美元
SHIT_TOKEN VALUE IN USD: 16.98585439310707 SHIT_TOKEN 价值(美元):16.98585439310707
Any guess?有什么猜测吗? Thank you in advance and, for any question, feel free to ask!在此先感谢您,如有任何问题,请随时提问!
I wish your question was more specific: you are using specifically PancakeSwap to determine the price.我希望您的问题更具体:您专门使用 PancakeSwap 来确定价格。 But your question doesn't mention it at all.但是你的问题根本没有提到。
Regardless, I know two ways to get a price quote from PancakeSwap using web3.py:无论如何,我知道两种使用 web3.py 从 PancakeSwap 获取报价的方法:
import requests
def calcSell(tokenAddress):
apiURL = "https://api.pancakeswap.info/api/v2/tokens/"
response = requests.get(url = apiURL + tokenAddress)
price = extractPriceFromRaw(response)
return price
def extractPriceFromRaw(response):
jsonRaw = response.json()
price = jsonRaw['data']['price']
return price
CAKE = '0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82'
print(calcSell(KRW))
from web3 import Web3
def calcSell(tokenAddress):
routerContract = web3.eth.contract(address=routerPCS, abi=pancakeSwapAbi)
oneToken = web3.toWei(1, 'Ether')
price = routerContract.functions.getAmountsOut(oneToken, [tokenAddress, BUSD]).call()
normalizedPrice = web3.fromWei(price[1], 'Ether')
return normalizedPrice
web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
routerPCS = '0x10ED43C718714eb63d5aA57B78B54704E256024E'
BUSD = '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56'
CAKE = '0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82'
print(calcSell(CAKE))
I did not try to debug your code, but I assume that your problems are because of the way you torture tokenToSell
value, instead of simply making it equal to 1 Ether: tokenToSell = web3.toWei(1, 'Ether')
我没有尝试调试您的代码,但我认为您的问题是因为您折磨tokenToSell
值的方式,而不是简单地使其等于 1 Ether: tokenToSell = web3.toWei(1, 'Ether')
I think this line我认为这条线
tokenToSell = setDecimals(tokenToSell, tokenDecimals) # Set token a correct number of 0s
does the exact same thing as this line做与这条线完全相同的事情
amountIn = web3.toWei(tokenToSell, 'ether')
which makes the amountIn
to be 1*(10**18)*(10**18)
Assuming the decimal is 18, get rid of amountIn = web3.toWei(tokenToSell, 'ether')
as tokenToSell is already in it's wei format.这使得amountIn
为1*(10**18)*(10**18)
假设小数点为 18,去掉amountIn = web3.toWei(tokenToSell, 'ether')
因为 tokenToSell 已经是 wei 格式.
Sorry, I am late first of all, But This Can Help Others对不起,首先我迟到了,但这可以帮助别人
The exact price of the token can be retrieved via the LP pair contract.可以通过 LP 对合约检索代币的确切价格。 For example Here Is A Random ShitCoin LP Contract: https://bscscan.com/address/0xeb821dcc4903935adfc0d52fa2155ab281fbf99b#code例如,这是一个随机 ShitCoin LP 合约: https://bscscan.com/address/0xeb821dcc4903935adfc0d52fa2155ab281fbf99b#code
The lp pair contract contains the base and quote token for the pair, so now you can calculate the price via this! lp 货币对合约包含货币对的基础和报价代币,所以现在您可以通过它计算价格!
Price: 1024.2289 / 11225747.7426 => 0.00009123925 BNB价格:1024.2289 / 11225747.7426 => 0.00009123925 BNB
Bonus奖金
If you want to fetch the token lp pair contract automatically there is a function named getPair(tokenA, tokenB) in pancakeswap factory v2 which can be used to fetch the pair, ex: getPair(randomshitcoin, wbnbaddress), getPair(randomshitcoin, usdtaddress)如果您想自动获取令牌 lp 对合约,则 pancakeswap factory v2 中有一个名为 getPair(tokenA, tokenB) 的 function 可用于获取该对,例如:getPair(randomshitcoin, wbnbaddress), getPair(randomshitcoin, usdtaddress)
use this api https://docs.moralis.io/使用这个 api https://docs.moralis.io/
you can easily get token price with WEB3API您可以通过 WEB3API 轻松获取代币价格
Moralis.initialize("APPID");
Moralis.serverURL = 'https://serverlink'
const options = {
address: "token address",
chain: "bsc",
exchange: "PancakeSwapv2"
};
Moralis.Web3API.token.getTokenPrice(options).then((result: String) => {
const userStr = JSON.stringify(result);
JSON.parse(userStr, (key, value) => {
if ( key === "usdPrice") {
console.log(value)
settokenprice(value)
}
return value;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.