简体   繁体   English

如何在web3.py中的代币池合约中直接获取BSC代币的价格

[英]How to get the price of BSC token directly the token pool contract in web3.py

I'm trying to get the price of some BSC token directly from the contract.我试图直接从合约中获取一些 BSC 代币的价格。 Doing some searches, I found a mix of ideas with the snippet below.进行了一些搜索,我发现了以下片段的混合想法。

I'm sure the code is off because when I checked on https://pancakeswap.finance/swap and https://poocoin.app/ as reference for the price, the result I have is way off.我确定代码是关闭的,因为当我检查https://pancakeswap.finance/swaphttps://poocoin.app/作为价格参考时,我得到的结果很差。

Getting the BNB price from the pool contract would be enough.从矿池合约中获取 BNB 价格就足够了。

Any idea what could be wrong with the code?知道代码有什么问题吗?

from web3 import Web3

panRouterContractAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E'
pancakeSwapAbi = '[{"inputs":[{"internalType":"address","name":"_factory","type":"address"},{"internalType":"address","name":"_WETH","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"amountADesired","type":"uint256"},{"internalType":"uint256","name":"amountBDesired","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amountTokenDesired","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"addLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"},{"internalType":"uint256","name":"liquidity","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"factory","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountIn","outputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"reserveIn","type":"uint256"},{"internalType":"uint256","name":"reserveOut","type":"uint256"}],"name":"getAmountOut","outputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsIn","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"}],"name":"getAmountsOut","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"name":"quote","outputs":[{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETH","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"removeLiquidityETHSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermit","outputs":[{"internalType":"uint256","name":"amountToken","type":"uint256"},{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountTokenMin","type":"uint256"},{"internalType":"uint256","name":"amountETHMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityETHWithPermitSupportingFeeOnTransferTokens","outputs":[{"internalType":"uint256","name":"amountETH","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidity","type":"uint256"},{"internalType":"uint256","name":"amountAMin","type":"uint256"},{"internalType":"uint256","name":"amountBMin","type":"uint256"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"bool","name":"approveMax","type":"bool"},{"internalType":"uint8","name":"v","type":"uint8"},{"internalType":"bytes32","name":"r","type":"bytes32"},{"internalType":"bytes32","name":"s","type":"bytes32"}],"name":"removeLiquidityWithPermit","outputs":[{"internalType":"uint256","name":"amountA","type":"uint256"},{"internalType":"uint256","name":"amountB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapETHForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactETHForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForETHSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOutMin","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapExactTokensForTokensSupportingFeeOnTransferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactETH","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"uint256","name":"amountInMax","type":"uint256"},{"internalType":"address[]","name":"path","type":"address[]"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swapTokensForExactTokens","outputs":[{"internalType":"uint256[]","name":"amounts","type":"uint256[]"}],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]'

web3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
WBNB = web3.toChecksumAddress(0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c)

def tokenprice(tokenAddress):
    pcsContract = web3.eth.contract(address=panRouterContractAddress, abi=pancakeSwapAbi)
    oneToken = web3.toWei(1, 'ether')    
    price = pcsContract.functions.getAmountsOut(oneToken, [tokenAddress, WBNB]).call()
    readablePrice = Web3.fromWei(price[1], 'ether')
    return readablePrice

token = "0x04C747b40Be4D535fC83D09939fb0f626F32800B" #-- 18 decimals
print ("Token1:  {}".format(tokenprice(token)))

token = "0x1AdA8bB610C59Aae25d8DD8f354282F5693CefB1" #-- 9 decimals
print ("Token2:  {}".format(tokenprice(token)))

token = "0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82" #-- 18 decimals
print ("Token3:  {}".format(tokenprice(token)))

token = "0x3856B578E287cC5c9a3230f1D93fb8FfFEF67D8B" #-- 18 decimals
print ("Token4: {}".format(tokenprice(token)))

Current Output:电流输出:

Token1: 0.070020190677282647  #-- 18 decimals
Token2: 0.283807483456933232  #-- wrong output, token has 9 decimals
Token3: 3.732452129300254425  #-- 18 decimals
Token4: 0.156419393163072892  #-- 18 decimals

Reference Values for Comparison: (Pancakeswap and Poocoin)比较参考值:(Pancakeswap 和 Poocoin)

Pancakeswap - https://pancakeswap.finance/swap 
  # https://pancakeswap.finance/swap?outputCurrency=0x04c747b40be4d535fc83d09939fb0f626f32800b
  Token1:  0.000126362           BNB   ->  $0.037           Converted to USD

  # https://pancakeswap.finance/swap?outputCurrency=0x1AdA8bB610C59Aae25d8DD8f354282F5693CefB1
  Token2:  0.00000000000249845   BNB   ->  $0.000000000725  Converted to USD

  # https://pancakeswap.finance/swap?outputCurrency=0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82
  Token3:  0.014923              BNB   ->  $4.40            Converted to USD

  # https://pancakeswap.finance/swap?outputCurrency=0x3856B578E287cC5c9a3230f1D93fb8FfFEF67D8B
  Token4:  0.00000729899         BNB   ->  $0.0022          Converted to USD
-----------------------------------------------------------------------------------------------    
Poocoin - https://poocoin.app/
  # https://poocoin.app/tokens/0x04c747b40be4d535fc83d09939fb0f626f32800b
  Token1:  $0.0374019         

  # https://poocoin.app/tokens/0x1AdA8bB610C59Aae25d8DD8f354282F5693CefB1  
  Token2:  $0.000000000836551 

  # https://poocoin.app/tokens/0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82  
  Token3:  $4.3289           

  # https://poocoin.app/tokens/0x3856B578E287cC5c9a3230f1D93fb8FfFEF67D8B  
  Token4:  $0.00321790        

You should use UniswapV2OracleLibrary .您应该使用UniswapV2OracleLibrary PancakeSwap is a fork of UniswapV2 so it should be ok. PancakeSwap 是 UniswapV2 的一个分支,所以应该没问题。

You see the more details about it in following links:您可以在以下链接中看到有关它的更多详细信息:

  1. soliditydeveloper.com/uniswap-oracle soliditydeveloper.com/uniswap-oracle
  2. docs.uniswap.org/protocol/V2/concepts/core-concepts/oracles docs.uniswap.org/protocol/V2/concepts/core-concepts/oracles

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 bsc 通过钱包地址获取交易 Web3.py - bsc get transactions by wallet address Web3.py Web3.py:如何在没有源代码的情况下调用令牌函数 - Web3.py : How to call token functions without source code 如何在 web3.py 中跟踪令牌的最后传输 - How to track the last transfers of a token in web3.py 如何使用 web3.py 在给定的 ETH 地址获取特定的代币余额 - How to get the specific token balance available at a give ETH address using web3.py 如何在 PancakeSwap 上使用 Web3.py 获取令牌的确切值? Function getAmountsOut() 返回错误值 - How to get exact value of a token with Web3.py on PancakeSwap? Function getAmountsOut() returns a wrong value 如何使用合约交互从 web3.py 获取未签名的交易数据 - How to get an unsigned transaction data from web3.py using contract interaction 使用 web3.py 计算令牌对的 LP 地址 - Compute the LP Address of a token pair using web3.py 如何使用 web3.py 查询区块链以获取最新的智能合约部署? - How to query blockchain for latest smart contract deployments with web3.py? 如何使用 Python 和 web3.py 调用智能合约函数 - How to call a Smart Contract function using Python and web3.py 如何使用 web3.py 在钱包之间转移 ERC20 代币 - How to transfer an ERC20 token between wallets with web3.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM