4⃣
callWithSyncFee
Permissionless transactions with on-chain payments
After reading this page:
- You'll see some code which will help you send a relay request within minutes.
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.
const callWithSyncFee = async (
request: CallWithSyncFeeRequest,
options?: RelayRequestOptions
): Promise<RelayResponse>
- options?:
relayRequestOptions
is an optional object.
type RelayResponse = {
taskId: string;
};
const request = {
chainId: BigNumberish;
target: string;
data: BytesLike;
isRelayContext?: boolean;
feeToken: string;
};
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.isRelayContext
: an optional boolean (default:true
) denoting what data you would prefer appended to the end of the calldata.- If set to
true
, Gelato Relay will append thefeeCollector
address, thefeeToken
address, and the uint256fee
to the calldata. - If set to
false
, Gelato Relay will only append thefeeCollector
address to the calldata. - As this is an optional parameter with a default value of
true
, explicit passing is only required if you are usingGelatoRelayFeeCollector
from our relay-context-contracts.
feeToken
: the address of the token that is to be used for payment.
1
// SPDX-License-Identifier: MIT
2
pragma solidity 0.8.17;
3
4
import {
5
GelatoRelayContext
6
} from "@gelatonetwork/relay-context/contracts/GelatoRelayContext.sol";
7
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
8
9
// Inheriting GelatoRelayContext gives access to:
10
// 1. _getFeeCollector(): returns the address of Gelato's feeCollector
11
// 2. _getFeeToken(): returns the address of the fee token
12
// 3. _getFee(): returns the fee to pay
13
// 4. _transferRelayFee(): transfers the required fee to Gelato's feeCollector.abi
14
// 5. _transferRelayFeeCapped(uint256 maxFee): transfers the fee to Gelato
15
// only if fee < maxFee
16
// 6. _getMsgData(): returns the original msg.data without appended information
17
// 7. onlyGelatoRelay modifier: allows only Gelato Relay's smart contract
18
// to call the function
19
contract CounterRelayContext is GelatoRelayContext {
20
using Address for address payable;
21
22
uint256 public counter;
23
24
event IncrementCounter(uint256 newCounterValue);
25
26
// `increment` is the target function to call.
27
// This function increments a counter variable by 1
28
// IMPORTANT: with `callWithSyncFee` you need to implement
29
// your own smart contract security measures, as this
30
// function can be called by any third party and not only by
31
// Gelato Relay. If not done properly, funds kept in this
32
// smart contract can be stolen.
33
function increment() external onlyGelatoRelay {
34
// Remember to autheticate your call since you are not using ERC-2771
35
// _yourAuthenticationLogic()
36
37
// Payment to Gelato
38
// NOTE: be very careful here!
39
// if you do not use the onlyGelatoRelay modifier,
40
// anyone could encode themselves as the fee collector
41
// in the low-level data and drain tokens from this contract.
42
_transferRelayFee();
43
44
counter++;
45
46
emit IncrementCounter(counter);
47
}
48
}
1
import { GelatoRelay, CallWithSyncFeeRequest } from "@gelatonetwork/relay-sdk";
2
const relay = new GelatoRelay();
1
// set up target address and function signature abi
2
const counter = "<your counter contract address>";
3
const abi = ["function increment()"];
4
5
// generate payload using front-end provider such as MetaMask
6
const provider = new ethers.providers.Web3Provider(window.ethereum);
7
const signer = provider.getSigner();
8
9
const contract = new ethers.Contract(counterAddress, abi, signer);
10
const { data } = await contract.populateTransaction.increment();
11
12
// address of the token to pay fees
13
const feeToken = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
14
15
// populate the relay SDK request body
16
const request: CallWithSyncFeeRequest = {
17
chainId: provider.network.chainId,
18
target: counter,
19
data: data,
20
feeToken: feeToken,
21
isRelayContext: true,
22
};
23
24
// send relayRequest to Gelato Relay API
25
const relayResponse = await relay.callWithSyncFee(request);
Last modified 28d ago