我使用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())
这段代码运行后错误如下,如图所示,
是什么原因以及如何解决呢?