How to perform Gasless swap automation manually using ether.js?

Created by Sourajyoti Gupta, Modified on Wed, 22 Nov, 2023 at 5:50 PM by Sourajyoti Gupta

To perfrom the GasLess sawp we need to generate the require arguments for executeMetaTransaction function

The function is configured with the following arguments.


address userAddress: The address of the user who signed the  message data,

bytes memory functionSignature: encoded function data with required arguments. 

you can create functionSignature using ethers or web3 library:

const functionSignature = contract.interface.encodeFunctionData("transfer", toAddress, amount])


For below three arguments you need  to sign the EIP712 data and generate the R, S, and V 

using the user address.

const domain = {
      name: "Contract name",
      version: "1",
      chainId: chainId,
      verifyingContract: contract.address,
    };

    const types = {
      MetaTransaction: [
        { name: "nonce", type: "uint256" },
        { name: "from", type: "address" },
        { name: "functionSignature", type: "bytes" },
      ],
    };

 const signature = await (await ethers.getSigner(accounts[0].address))._signTypedData(domain, types, value);
    const r = signature.slice(0, 66);
    const s = "0x".concat(signature.slice(66, 130));
    let : any = "0x".concat(signature.slice(130, 132));
    v = ethers.BigNumber.from(v).toNumber();

bytes32 sigR,
bytes32 sigS,
uint8 sigV

contract.executeMetaTransaction(userAddress,functionSignature,R,S,V)

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article