【问题标题】:How to interact with a Binance Smart Chain contract using Python如何使用 Python 与币安智能链合约进行交互
【发布时间】:2021-05-30 04:01:24
【问题描述】:

我首先尝试使用 python 发送交易:

from web3 import Web3

transaction = {
        'chainId': 97,  # 97: Testnet. 56: main.
        'to': '0xmyaddress',
        'value': 1,
        'gas': 2000000,
        'gasPrice': 13,
        'nonce': 0,
    }

infura_url = "https://mainnet.infura.io/v3/my-api-key"
w3 = Web3(Web3.HTTPProvider(infura_url))

key = '0xmykey'
signed = w3.eth.account.signTransaction(transaction, key)

w3.eth.sendRawTransaction(signed.rawTransaction)

给我以下错误:ValueError: {'code': -32000, 'message': 'invalid sender'}


现在,我正在尝试与合约交互 - 调用方法并提供输入,但我不确定如何完成此操作。

【问题讨论】:

  • 您似乎正在尝试访问以太坊链而不是币安智能链:https://mainnet.infura.io/v3/my-api-key。据我所知,您无法通过 Infura API 访问 BSC。
  • 是的,谢谢@marsbear——我相信你是对的。我不得不使用 Nodejs 和 typescript 执行我的交易。我会建议其他人也这样做,直到进一步支持 python 的 bsc 扩展。
  • ethereum.stackexchange.com/questions/92806/… - 也许这个来自 stackexchange 的答案有帮助?除了提到的 infura 之外,还有其他网址

标签: transactions web3 binance web3py


【解决方案1】:

web3bsc 可以解决这个问题,只需卸载 web3 pip3 uninstall web3 然后安装 web3bsc pip3 install web3bsc 您现在可以像往常一样导入 web3:

import web3

bsc = web3.bsc.Bsc("pub_key","priv_key")
w3 = web3.Web3()   # HTTPProvider and Binance endpoint not required

# Your code
transaction = {
    'chainId': 56,       # main. test-net maybe not supported yet
    'to': bsc.get_public_key(),
    'value': 1,
    'gas': 2000000,
    'gasPrice': 13,
    'nonce': 0,
}

signed = w3.eth.account.signTransaction(transaction, bsc.get_private_key())

w3.eth.sendRawTransaction(signed.rawTransaction)

https://pypi.org/project/web3bsc/

【讨论】:

  • 很有可能这是恶意的 - 信誉为 0 且代码不公开的作者
猜你喜欢
  • 2021-08-10
  • 2021-11-08
  • 2021-03-12
  • 2018-12-20
  • 1970-01-01
  • 2021-07-13
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
相关资源
最近更新 更多