大家好,我会尽量说清楚的。 我曾试图用web3.py获取s**t组件的价格,在解决了许多问题后,我被我问的问题困住了
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的代币价值:974136.205251839691973598 |只需将其转换为美元
美元代币价值:340708627.4489159379891912819
而正确的答案是:
BNB中的狗屎辅币价值:0.048846069961106416 |只需将其转换为美元
美元中的狗屎辅币价值:16.98585439310707
猜猜看?提前感谢您,如有任何问题,请随时提问
使用此api https://docs.moralis.io/
您可以通过WEB3API轻松获得代币价格
我希望你的问题更具体一点:你是专门用煎饼交换来决定价格的。但你的问题根本没有提到
无论如何,我知道使用web3.py从PancakeSwap获取报价的两种方法:
我没有试着调试您的代码,但我假设您的问题是因为您折磨
tokenToSell
值的方式,而不是简单地使它等于1:tokenToSell = web3.toWei(1, 'Ether')
我想这条线
tokenToSell = setDecimals(tokenToSell, tokenDecimals) # Set token a correct number of 0s
做和这条线完全相同的事情
amountIn = web3.toWei(tokenToSell, 'ether')
这使得
amountIn
成为1*(10**18)*(10**18)
假设小数点是18,去掉amountIn = web3.toWei(tokenToSell, 'ether')
,因为tokenToSell已经是wei格式了相关问题 更多 >
编程相关推荐