简体   繁体   English

如何使用合约交互从 web3.py 获取未签名的交易数据

[英]How to get an unsigned transaction data from web3.py using contract interaction

Currently I'm working on a project where I have the following flow:目前我正在做一个项目,我有以下流程:

  1. User wants to make a transaction, they choose chain and token .用户想要进行交易,他们选择chaintoken
  2. The contract (possibly proxy contract) and other data is retrieved from the database从数据库中检索合同(可能是代理合同)和其他数据
  3. I compute the data key, value pair to be used in my front-end call to Metamask我计算要在对 Metamask 的前端调用中使用的data键、值对
transaction = await ethereum.request({
   method: 'eth_sendTransaction',
   params: [
      {
          to: '{{ paymentrequest.paymentaddress }}',
          from: ethereum.selectedAddress,
          data: '{{ paymentrequest.transhex }}',
          chainId: '{{ paymentrequest.chain.chainid }}'
   }],
});
  1. The user gets a metamask pop-up containing the data above which asks them to make the transaction.用户会收到一个包含上述数据的元掩码弹出窗口,要求他们进行交易。

Step 3 is where I run into issues.第 3 步是我遇到问题的地方。 In order to get the data attribute from the computed tx by web3.py I have to either use .call() or .transact() .为了从 web3.py 计算的 tx 中获取数据属性,我必须使用 .call( .call().transact() Both of them are fine for the ethereum chain.它们都适用于以太坊链。 But when I apply those calls on contracts on the Polygon or BSC chains they report errors such as:但是,当我将这些调用应用于 Polygon 或 BSC 链上的合约时,它们会报告错误,例如:

error 1: BEP20: transfer to zero address - I don't even understand how this is even possible since I quadrouple checked that I wasn't feeding a zero address into the function, however it still reported this error without the "to" parameter even when defining a recipient in the function call.错误 1: BEP20: transfer to zero address - 我什至不明白这是怎么可能的,因为我四倍检查了我没有将零地址输入 function,但是它仍然在没有“to”参数的情况下报告此错误即使在 function 调用中定义收件人时也是如此。

error 2: BEP20: transfer amount exceeds balance - I don't want to use an address with a bunch of tokens in it from someone else in order to get around this error.错误 2: BEP20: transfer amount exceeds balance - 我不想使用来自其他人的带有一堆代币的地址来解决此错误。 I want to stay away from being limited by someone else having a bunch of coins in their wallet.我想远离别人钱包里有一堆硬币的限制。

This is the code I use:这是我使用的代码:

def bnbTXCompute(paymentrequest, tokenamount, contractaddress, tokenaddress):
    w3 = Web3(Web3.HTTPProvider(paymentrequest.chain.rpc, request_kwargs={'timeout': 60}))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)

    contract = w3.eth.contract(address=contractaddress, abi=paymentrequest.token.abi)

    txn = contract.functions.transfer(paymentrequest.wallet,
                                      tokenamount * pow(10, paymentrequest.token.decimals)).call({"to": tokenaddress, "from": "0xbunchoflettersandnumbers"}, )
                                      # I have tried to remove both the "to" and "from" parameters, but they both result in either error 1 or 2

    return txn["data"]

def polyTXCompute(paymentrequest, tokenamount, contractaddress, tokenaddress):
    w3 = Web3(Web3.HTTPProvider(paymentrequest.chain.rpc, request_kwargs={'timeout': 60}))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)

    contract = w3.eth.contract(address=contractaddress, abi=paymentrequest.token.abi)

    txn = contract.functions.transfer(paymentrequest.wallet,
                                      tokenamount * pow(10, paymentrequest.token.decimals)).transact({"to": tokenaddress, "from": "0xbunchoflettersandnumbers"})

    return txn["data"]

def ethTXCompute(paymentrequest, tokenamount, contractaddress):

    w3 = Web3(Web3.HTTPProvider(paymentrequest.chain.rpc, request_kwargs={'timeout': 60}))
    w3.middleware_onion.inject(geth_poa_middleware, layer=0)

    contract = w3.eth.contract(address=contractaddress, abi=paymentrequest.token.abi)

    txn = contract.functions.transfer(paymentrequest.wallet,
                                      tokenamount * pow(10, paymentrequest.token.decimals)).transact()

    return txn["data"]

The solution I have thought of is to just skip all of these checks web3.py is doing before returning the data hash.我想到的解决方案是在返回数据 hash 之前跳过 web3.py 正在执行的所有这些检查。 However I just can't manage to do such a thing.但是我无法做到这样的事情。 Any help is appreciated.任何帮助表示赞赏。

web3.py documentation: https://web3py.readthedocs.io/en/latest/ web3.py 文档: https://web3py.readthedocs.io/en/latest/

And as always, devs should read better:和往常一样,开发人员应该更好地阅读:

replace:代替:

txn = contract.functions.transfer(paymentrequest.wallet, tokenamount * pow(10, paymentrequest.token.decimals)).call({"to": tokenaddress, "from": "0xbunchoflettersandnumbers"}, )

with:和:

txn = contract.encodeABI(fn_name="transfer", args=[paymentrequest.wallet, tokenamount * pow(10, paymentrequest.token.decimals)])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM