1东风1 2023-09-17 13:18
浏览 9
已结题

无法获取交易对的价格

我使用python语言,在python代码中调用web3库来创建智能合约,
连接pancakeswap交易所和BSC链网络,
我想在代码中获取 beth 兑换 usdt 的价格,
我的代码如下,

import time
import json
from web3 import Web3
from web3.middleware import geth_poa_middleware
from datetime import datetime

private_key = "private_key"
address = "address"
bsc_network_url = "https://bsc-dataseed.binance.org/"
pancake_router_address = "0x10ED43C718714eb63d5aA57B78B54704E256024E"
bnb_decimals = 18
usdt_decimals = 6
beth_decimals = 18
sand_decimals = 18
btcb_decimals = 8
xvs_decimals = 18

w3 = Web3(Web3.HTTPProvider(bsc_network_url))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
if not w3.is_connected():
    raise Exception("Unable to connect to BSC Network")

account = w3.eth.account.from_key(private_key)
my_address = account.address

with open("pancake_router_abi.json", "r") as f:
    router_abi = json.load(f)
pancake_router = w3.eth.contract(address=pancake_router_address, abi=router_abi)

def get_prices_beth_usdt():
    path = [
        w3.to_checksum_address("0x250632378E573c6Be1AC2f97Fcdf00515d0Aa91B"),  # BETH
        w3.to_checksum_address("0x55d398326f99059fF775485246999027B3197955"),  # USDT
    ]
    amount_in = Web3.to_wei(1, 'ether')
    amounts_out = pancake_router.functions.getAmountsOut(amount_in, path).call()
    beth_usdt_price = amounts_out[1] / (10 ** 6)
    return beth_usdt_price

print("BETH/USDT prices:", get_prices_beth_usdt())

这段代码运行后错误如下,如图所示,

img

是什么原因以及如何解决呢?

  • 写回答

1条回答 默认 最新

  • 1东风1 2023-09-17 13:34
    关注

    运行后错误如下:

    
    Traceback (most recent call last):
      File "jiao_ben_1.py", line 42, in <module>
        print("BETH/USDT prices:", get_prices_beth_usdt())
      File "jiao_ben_1.py", line 38, in get_prices_beth_usdt
        amounts_out = pancake_router.functions.getAmountsOut(amount_in, path).call()
      File "/usr/local/lib/python3.7/dist-packages/web3/contract/contract.py", line 474, in call
        **self.kwargs,
      File "/usr/local/lib/python3.7/dist-packages/web3/contract/utils.py", line 100, in call_contract_function
        ccip_read_enabled=ccip_read_enabled,
      File "/usr/local/lib/python3.7/dist-packages/web3/eth/eth.py", line 255, in call
        return self._durin_call(transaction, block_identifier, state_override)
      File "/usr/local/lib/python3.7/dist-packages/web3/eth/eth.py", line 274, in _durin_call
        return self._call(transaction, block_identifier, state_override)
      File "/usr/local/lib/python3.7/dist-packages/web3/module.py", line 69, in caller
        method_str, params, error_formatters, null_result_formatters
      File "/usr/local/lib/python3.7/dist-packages/web3/manager.py", line 233, in request_blocking
        response, params, error_formatters, null_result_formatters
      File "/usr/local/lib/python3.7/dist-packages/web3/manager.py", line 197, in formatted_response
        apply_error_formatters(error_formatters, response)
      File "/usr/local/lib/python3.7/dist-packages/web3/manager.py", line 73, in apply_error_formatters
        formatted_resp = pipe(response, error_formatters)
      File "cytoolz/functoolz.pyx", line 666, in cytoolz.functoolz.pipe
      File "cytoolz/functoolz.pyx", line 641, in cytoolz.functoolz.c_pipe
      File "/usr/local/lib/python3.7/dist-packages/web3/_utils/method_formatters.py", line 786, in raise_contract_logic_error_on_revert
        raise ContractLogicError("execution reverted", data=data)
    web3.exceptions.ContractLogicError: execution reverted
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 9月25日
  • 创建了问题 9月17日

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用