1

I managed to set up this function that returns the token value in USD of a contract, everything works fine in some contracts, but in other contracts I get the error "execution reverted" when calling the function "getAmountsOut" does anyone have any idea what it can be? I get the contracts from the same place in bscscan and for some it works and for some it doesn't.

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 "", ""

contracts ok:

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

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

contracts error:

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

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

if anyone can explain to me why this error occurs in some tokens and not others, and how I can get error message details besides "execution reverted"

TylerH
  • 20,799
  • 66
  • 75
  • 101
MariINova
  • 323
  • 4
  • 15
  • What is the `contractbuy` address? What network are you connected to (Ethereum mainnet, BSC testnet, ...)? – Petr Hejda Jan 06 '22 at 14:13
  • I edited the question with the additional information, but I'm querying the BSC, contractbuy is "PANCAKESWAP" – MariINova Jan 06 '22 at 15:23

1 Answers1

3

The latter specified token addresses fail with the error because there is no existing Pancakeswap pair contract for the specified address combination.

Another way of checking the existence of a pair contract is by calling the factory's getPair() function.

// 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"
)
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • I understand, I can avoid the error before querying, but how do I get the value in BUSD of this token? since there is no pair "DGZV" and "BUSD" is there another way when there is no pair? – MariINova Jan 06 '22 at 16:26
  • Or should I convert to BNB first and then BNB => BUSD? – MariINova Jan 06 '22 at 16:34
  • @MariINova If the token has a pair against BNB, you can calculate the price through BNB and then BUSD as you say in your second comment... Mind that there might be cases where the token is not traded against any other currency (not even BNB) or just against some other tokens (not BNB nor USDT, but just against some noname XYZ). – Petr Hejda Jan 06 '22 at 21:02