【问题标题】:web3 python getAmountsOut execution revertedweb3 python getAmountsOut执行恢复
【发布时间】:2022-01-06 12:58:35
【问题描述】:

我设法设置了这个函数,它以美元为单位返回合约的代币价值,在某些合约中一切正常,但在其他合约中,我在调用函数“getAmountsOut”有人知道它是什么吗?我从 bscscan 的同一个地方获得合同,有些可以,有些不行。

bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
panRouterContractAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E'
panabi = '[{...}]'
contractbuy = web3.eth.contract(address=panRouterContractAddress, abi=panabi)  # PANCAKESWAP

def get_price(self):
    try:
        # Base currency token instantiate and ABI
        baseCurrency = web3.toChecksumAddress("0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56")  # BUSD

        # get symbol token
        sellTokenContract = web3.eth.contract(self.token_to_buy, abi=sellAbi)
        symbol = sellTokenContract.functions.symbol().call()

        # Selling token instance and contract
        tokenToSell = web3.toChecksumAddress(self.token_to_buy)  # TOKEN PRICE
        sellAmount = web3.toWei(1, 'ether')

        # Calculate minimum amount of tokens to receive
        amountOut = contractbuy.functions.getAmountsOut(sellAmount, [tokenToSell, baseCurrency]).call() # Error here

        amountOutMin = web3.fromWei(int(amountOut[1]), 'ether')
        print("Valor do token: ", str(amountOutMin))
        str_format = "{:." + self.decs_usd + "f}"
        return float(str_format.format(amountOutMin)), symbol

    except Exception as ex:
        print("ERRO:ver_preco:", ex)
        return "", ""

合同正常:

https://bscscan.com/address/0x5649e392a1bac3e21672203589adf8f6c99f8db3

https://bscscan.com/address/0x00e1656e45f18ec6747f5a8496fd39b50b38396d

合约错误:

https://bscscan.com/address/0x9376e6b29b5422f38391a2a315623cb853c3c68e

https://bscscan.com/address/0xe786d5a4b985bfe5e371f8e94413cfb440f6618a

如果有人可以向我解释为什么此错误发生在某些令牌中而不是其他令牌中,以及除了“执行恢复”之外,我如何获得错误消息详细信息

谢谢!

【问题讨论】:

  • contractbuy 地址是什么?你连接到哪个网络(以太坊主网、BSC 测试网……)?
  • 我用附加信息编辑了问题,但我正在查询 BSC,contractbuy 是“PANCAKESWAP”

标签: ethereum web3 web3js web3py pancakeswap


【解决方案1】:

后面指定的代币地址失败并出现错误,因为指定地址组合没有现有的 Pancakeswap 对合约。

检查配对合约是否存在的另一种方法是调用factorygetPair() 函数。

// returns the existing pair contract address 0x5f7A2a0A32C0616898cA7957C1D58BC92a7E2f6f
getPair(
    // ZDC token specified in your question as OK
    "0x5649e392A1BAC3e21672203589aDF8f6C99f8dB3",

    // BUSD
    "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56"
)
// returns the zero address 0x0000000000000000000000000000000000000000
// because there's no pair contract for these specified tokens
getPair(
    // DGZV token specified in your question as NOTOK
    "0x9376E6B29b5422f38391A2a315623cB853c3c68e",

    // BUSD
    "0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56"
)

【讨论】:

  • 我明白了,我可以避免查询前的错误,但是如何获取这个token的BUSD价值呢?既然没有“DGZV”和“BUSD”对,那么在没有对的情况下还有其他方法吗?
  • 还是先转BNB再转BNB => BUSD?
  • @MariINova 如果代币与 BNB 有一对,您可以按照您在第二条评论中所说的那样通过 BNB 然后 BUSD 计算价格...请注意,可能存在代币不存在的情况与任何其他货币(甚至不是 BNB)或仅与其他一些代币(不是 BNB 或 USDT,而只是与一些不知名的 XYZ 交易)进行交易。
猜你喜欢
  • 2023-01-13
  • 2017-09-03
  • 2021-02-13
  • 2021-08-06
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多