繁体   English   中英

如何批准用于在(Uniswap 路由器合约)上花费的代币

[英]How to approve a token for spending on (Uniswap router contract)

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

这是我的批准代码 function。

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。 文档并不清楚如何与现有智能合约的本地私钥进行交互。

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)

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

暂无
暂无

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

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