Meta-transaction Relayer
You may want to sponsor the gas for your user's transactions using a meta-transaction relayer.
Engine allows you to create meta-transaction relayer endpoints that you can use from your client-side application.
It supports the following standard meta-transaction formats:
In this guide, we're going to setup a meta-transaction relayer with Engine and use it to sponsor gas fees for users' client-side transactions.
Step 1: Create a meta-transaction relayer endpoint
First, we need to create a new meta-transaction relayer endpoint with Engine.
You can do this from the Explorer
tab on your Engine dashboard by navigating to the POST - /relayer/create
endpoint.
Here, you'll need to specify:
- The
chain
your relayer will function on. - The
backendWalletAddress
of the Engine backend wallet you want to use to sponsor funds for your user. - The
allowedContracts
that the endpoint can forward transactions to. - Optionally, the
name
of your relayer - Optionally, the
allowedForwarders
the relayer is allowed to forward transactions with
Once you create the endpoint, you'll get back a uuid
which is the unique ID of your relayer endpoint.
With this ID, you're meta-transaction relayer endpoint will be at POST - /relayer/{relayerId}
.
Step 2: Use your relayer endpoint to sponsor transactions
Now that we've setup our endpoint, we can use it to sponsor transactions using the @thirdweb-dev/sdk
package.
All we need to do is instantiate the SDK to be aware of our meta-transaction relayer endpoint, and it will attempt to send all transactions through the endpoint.
In the following snippet, we instantiate the SDK with the standard configuration, and additionally specify the gasless.engine.relayerUrl
parameter to enable meta-transaction relaying.
// Only instantiating the SDK with private key for demo purposes
const sdk = ThirdwebSDK.fromPrivateKey("<private-key>", "arbitrum-goerli", {
gasless: {
// Here we specify our engine relayer URL that we created earlier
engine: {
relayerUrl:
"http://0.0.0.0:3005/relayer/4cc2b996-27dd-4263-a9ab-0997905c5e29",
},
},
secretKey: "<thirdweb-api-secret-key>",
});
Now, when we send a transaction to a contract with this instance of the SDK, it will be forwarded through our meta-transaction relayer and sponsored by the Engine backend wallet instead of forcing the user to spend funds.
const contract = await sdk.getContract(
"0x7A0CE8524bea337f0beE853B68fAbDE145dAC0A0",
);
// This transaction will automatically go through the meta-transaction relayer!
const tx = await contract.erc20.mint(1);
Using this simple flow, you can use Engine as a meta-transaction relayer to sponsor funds for your users!
The last point to note - the SDK and Engine will automatically detect what type of meta-transaction method (EIP-2771, EIP-2612, etc.) is appropriate to use based on your contract, and will handle the details under the hood.