大家好,我会尽量说清楚。我试图使用 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}')
calcsell 函数应该是返回 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
我得到的值是:
BNB 中的 SHIT_TOKEN 值:974136.205251839691973598 | 只需将其转换为 USD
SHIT_TOKEN VALUE IN USD: 340708627.4489159379891912819
而正确的是:
SHIT_TOKEN VALUE IN BNB : 0.048846069961106416 | 只需将其转换为 USD
SHIT_TOKEN VALUE IN USD: 16.98585439310707
有什么猜测吗?在此先感谢您,如有任何问题,请随时提问!