I want to transfer erc20 token from one account to another using meta transaction with help of erc2771.
I am using openzeppelin/contracts verison 5.0.1
I have two contracts
- MyToken - it inherit ERC2771Context
- Forwarder - it inherit ERC2771Forwarder
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/metatx/ERC2771Forwarder.sol";
import "hardhat/console.sol";
contract Forwarder is ERC2771Forwarder {
constructor() ERC2771Forwarder("Forwarder") {}
receive() payable external {}
}
below is js script:
let iface = new ethers.Interface(ABI);
var data = iface.encodeFunctionData("transferToken", [ owner.address, bob.address, ethers.parseUnits("100", 18) ])
let request = {
from: bob.address,
to: MyTokenAddress,
value: 0,
gasPrice: web3.utils.toHex(20 * 1e9),
gas: web3.utils.toHex(20 * 1e9),
nonce: web3.utils.toHex(web3.eth.getTransactionCount(gasPayer.address)),
deadline: 1708583535,
data: data,
}
const signature = await web3.eth.accounts.signTransaction(request, "private_key")
request.signature = signature.rawTransaction;
request.from = gasPayer.address
forwarderContract.connect(gasPayer).execute(request)
Here Bob sign transaction and gasPayer send transaction.
In above code I am getting signatureInvalid error.
what I am doing worng here?
is there any tutorial for erc2771 (want to deploy in private chain, can not use gelato, gsn or defender relayer)