2️⃣callWithSyncFeeERC2771
Transactions with on-chain payments and ERC2771 authentication support
If you are using @gelatonetwork/relay-sdk
v3 or contracts from the package @gelatonetwork/relay-context
v2 please follow this migration guide to migrate to the new versions.
After reading this page:
You'll know how to use the
callWithSyncFeeERC2771
SDK method, using the syncFee payment method.You'll see some code which will help you send a relay request within minutes.
You'll learn how to pay for transactions using the provided values for
fee
,feeToken
andfeeCollector
.
Please proceed to our Security Considerations page and read it thoroughly before advancing with your implementation. It is crucial to understand all potential security risks and measures to mitigate them.
Overview
callWithSyncFeeERC2771
method uses the syncFee payment method with ERC-2771 support.
Paying for Transactions
When using callWithSyncFeeERC2771
relay method the target contract assumes responsibility for transferring the fee to Gelato's fee collector during transaction execution. For this, the target contract needs to know:
fee
: the transfer amountfeeToken
: the token to be transferredfeeCollector
: the destination address for the fee
Fortunately, Gelato provides some useful tools within the Relay Context Contracts:
By inheriting the GelatoRelayContextERC2771 contract in your target contract, you have the ability to transfer the fee through one of two straightforward methods:
_transferRelayFee()
or_transferRelayFeeCapped(uint256 maxFee)
. In either case, the inherited contract takes care of decoding thefee
,feeToken
, andfeeCollector
behind the scenes. The Gelato Relay backend simplifies the process by automatically calculating the fee for you, using Gelato's Fee Oracle to perform the calculations in the background.Alternatively, you may choose to inherit the GelatoRelayFeeCollectorERC2771 contract. With this approach, Gelato only decodes the
feeCollector
. You must provide thefee
andfeeToken
on-chain, either by hardcoding them (which is not recommended) or embedding them within the payload to be executed. The suggested way to handle this is to calculate the fee with Gelato's Fee Oracle.
Setting maxFee
for Your Transaction
maxFee
for Your TransactionSetting a maximum fee, or maxFee
, for your transactions is strongly advised. This practice enables you to ensure that transaction costs remain below a specific limit. The method _transferRelayFeeCapped(uint256 maxFee)
in the GelatoRelayContextERC2771 contract provides a convenient way to set the maxFee
easily.
If you are utilizing the GelatoRelayFeeCollectorERC2771 contract, the recommended way to pass the maxFee
is by calculating the fee with Gelato's Fee Oracle, which is accessible in the relay-sdk. The getEstimatedFee()
method is provided to facilitate this calculation.
SDK method: callWithSyncFeeERC2771
Arguments:
request
: this is the request body used to send a request.options?:
relayRequestOptions
is an optional object.
Return Object: RelayResponse
taskId
: your unique relay task ID which can be used for tracking your request.
Optional Parameters
See Optional Parameters.
Sending a Request
As of today, we support two distinct ways of sending callWithSyncFeeERC2771
requests:
Sequentially: This approach ensures that each request is ordered and validated against the
nonce
stored on-chain. You have two options in this method:Fetch the current
nonce
value from the smart contract yourself and include it with your request.Allow the relay-sdk to fetch the
nonce
value for you when handling your relay request.
Concurrently: This method enables you to send multiple transactions simultaneously. Replay protection is achieved using a hash-based
salt
mechanism. Again, you have two options:Provide your own
salt
value.Allow the relay-sdk to generate a unique
salt
value for you when processing your relay request.
By default callWithSyncFeeERC2771
requests are using the sequential method.
Concurrent ERC2771 support has been introduced in the relay-sdk version 5.1.0
. Please make sure that your package is up-to-date to start using it.
Request Body
Common Parameters
chainId
: the chain ID of the chain where thetarget
smart contract is deployed.target
: the address of the target smart contract.data
: encoded payload data (usually a function selector plus the required arguments) used to call the requiredtarget
address.user
: the address of the user's EOA.userDeadline
: optional, the amount of time in seconds that a user is willing for the relay call to be active in the relay backend before it is dismissed.This way the user knows that if the transaction is not sent within a certain timeframe, it will expire. Without this, an adversary could pick up the transaction in the mempool and send it later. This could transfer money, or change state at a point in time which would be highly undesirable to the user.
feeToken
: the address of the token that is to be used for payment. Please visit SyncFee Payment Tokens for the full list of supported payment tokens per network.isRelayContext
: an optional boolean (default:true
) denoting what data you would prefer appended to the end of the calldata.If set to
true
(default), Gelato Relay will append thefeeCollector
address, thefeeToken
address, and the uint256fee
to the calldata. In this case your target contract should inherit from the GelatoRelayContextERC2771 contract.If set to
false
, Gelato Relay will only append thefeeCollector
address to the calldata. In this case your target contract should inherit from theGelatoRelayFeeCollectorERC2771
contract.
Parameters For Sequential Requests:
isConcurrent
: false (default), optional, represents that the users' requests are validated based on a nonce, which enforces them to be processed sequentially.userNonce
: optional, this nonce, akin to Ethereum nonces, is stored in a local mapping on the relay contracts. It serves to enforce the nonce ordering of relay calls if the user requires sequential processing. If this parameter is omitted, the relay-sdk will automatically query the current value on-chain.
Parameters For Concurrent Requests:
isConcurrent
: true, indicates that the users' requests are validated based on a unique salt, allowing them to be processed concurrently. Replay protection is still ensured by permitting each salt value to be used only once.userSalt
: optional, this is a bytes32 hash that is used for replay protection. If the salt is not provided then relay-sdk would generate a unique value based on a random seed and a timestamp.
Example Code GelatoRelayContextERC2771
1. Deploy a GelatoRelayContextERC2771 compatible contract
2. Import GelatoRelaySDK into your front-end .js project
3. Send the payload to Gelato
Last updated