如何使用BSC上的Web3.py获取令牌的确切值?函数getAmountsOut()返回错误的值

2024-09-27 19:26:44 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我会尽量说清楚的。 我曾试图用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

猜猜看?提前感谢您,如有任何问题,请随时提问


Tags: web3价值printshitbnb狗屎tokenaddresbnbprice
3条回答

使用此api https://docs.moralis.io/

您可以通过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;
      });

我希望你的问题更具体一点:你是专门用煎饼交换来决定价格的。但你的问题根本没有提到

无论如何,我知道使用web3.py从PancakeSwap获取报价的两种方法:

  1. 使用their API
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))

  1. 直接使用智能合约的.getAmountsOut()函数。你想做什么
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))

我没有试着调试您的代码,但我假设您的问题是因为您折磨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格式了

相关问题 更多 >

    热门问题