Sponsored Calls enable you to cover your users’ gas fees, providing a seamless and gasless experience for interacting with your dApp. This is made possible through Gelato’s multi-chain unified gas payment system, Gas Tank. Learn more about Gas Tank here.
Import CallWithERC2771Request for ERC2771 sponsored calls with GelatoRelay. Learn more about ERC2771 here.
Copy
Ask AI
import { GelatoRelay, CallWithERC2771Request } from "@gelatonetwork/relay-sdk-viem";
Once we have imported the GelatoRelay class, when using ERC2771 methods, we must initialize it with the appropriate trustedForwarder.
The possible configurations are:
Copy
Ask AI
contract: { relay1BalanceERC2771: "trustedForwarder for method sponsoredCallERC2771", relayERC2771: "trustedForwarder for method callWithSyncFeeERC2771", relay1BalanceConcurrentERC2771: "trustedForwarder for method concurrent sponsoredCallERC2771", relayConcurrentERC2771:"trustedForwarder for method concurrent callWithSyncFeeERC2771", relay1BalanceConcurrentERC2771zkSync: "trustedForwarder for method concurrent sponsoredCallERC2771 on zkSync", relay1BalanceERC2771zkSync: "trustedForwarder for method sponsoredCallERC2771 on zkSync", relayConcurrentERC2771zkSync: "trustedForwarder for method concurrent callWithSyncFeeERC2771 on zkSync", relayERC2771zkSync: "trustedForwarder for method callWithSyncFeeERC2771 on zkSync",}
We will need to go to the Supported Networks and check the network and the contract addresses to identify the trustedForwarder associated with our method.
In the example below, we are using the method sponsoredCallERC2771 on Sepolia, the trustedForwarder associated is 0xd8253782c45a12053594b9deB72d8e8aB2Fca54c. We will initialize GelatoRelay with the following config:
Copy
Ask AI
const relay = new GelatoRelay({ contract: { relay1BalanceERC2771:"0xd8253782c45a12053594b9deB72d8e8aB2Fca54c" }});
Import ERC2771Context into your target contract and initialize it in the constructor with the appropriate trusted forwarder based on your use case. checkout list of trusted forwarders here.
Copy
Ask AI
import { ERC2771Context} from "@gelatonetwork/relay-context/contracts/vendor/ERC2771Context.sol";contract TargetContract is ERC2771Context { // ERC2771Context: setting the immutable trustedForwarder variable constructor(address trustedForwarder) ERC2771Context(trustedForwarder) {} function example() external{ // your logic }}