【问题标题】:How to approve a token for spending on (Uniswap router contract)如何批准用于消费的代币(Uniswap 路由器合约)
【发布时间】:2021-02-01 14:57:45
【问题描述】:

我正在尝试批准并稍后通过 web3py 代码在 uniswap 上交换我的令牌。我也在使用 infura,而不是我自己的节点。但是,在交换和批准时,我都遇到了solidityErrors。问题是 web3 无法识别我的帐户,即使我签署了 tx 并将我的私钥传递给它。关于如何让 web3 识别我的钱包的任何想法?

这是我的批准功能代码。

def approve(self,token_name):

    my_token = token(token_name)
    contract = my_token.createContract()
    spender = uni_router_address
    max_amount = web3.toWei(2**64-1,'ether')
    nonce = web3.eth.getTransactionCount(account)

    tx = contract.functions.approve(spender,max_amount).buildTransaction({'nonce': nonce})
    signed_tx = web3.eth.account.signTransaction(tx, self.pkey)
    gas = signed_tx.estimateGas()
    print(gas)

    return

我正在估算 gas,所以我在发送之前知道要使用多少 gas。我知道我需要使用 sendRawTransaction 作为本地私钥。文档并不清楚如何与现有智能合约的本地私钥进行交互。

【问题讨论】:

    标签: python ethereum web3 web3py


    【解决方案1】:
    def approve(token, spender_address, wallet_address, private_key):
    
      spender = spender_address
      max_amount = web3.toWei(2**64-1,'ether')
      nonce = web3.eth.getTransactionCount(wallet_address)
    
      tx = token.functions.approve(spender, max_amount).buildTransaction({
          'from': wallet_address, 
          'nonce': nonce
          })
        
      signed_tx = web3.eth.account.signTransaction(tx, private_key)
      tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
    
      return web3.toHex(tx_hash)
    

    使用您的私钥签名后,您需要将原始交易发送出去以将其广播到区块链。

    【讨论】:

    猜你喜欢
    • 2021-09-07
    • 2021-05-15
    • 2022-11-04
    • 1970-01-01
    • 2022-08-13
    • 2021-04-15
    • 2021-02-18
    • 2022-07-21
    • 2022-12-23
    相关资源
    最近更新 更多