Gelato Network
Search…
⌃K
1⃣

relayWithSyncFee

SDK Method 1️⃣
WARNING: A new package release is pending for relay-context npm package. If you are using relayWithSyncFee you will have to upgrade to the new version, 2.0.0.
After reading this page:
  • You'll know how to use the relayWithSyncFee SDK method, using the syncFee payment method.
  • You'll see some code which will help you send a relay request within minutes.

Overview

relayWithSyncFeehandles the syncFee payment method.

SDK method: relayWithSyncFee

const relayWithSyncFee = async (
request: CallWithSyncFeeRequest,
options?: RelayRequestOptions
): Promise<RelayResponse>

Arguments:

  • request: this is the request body used to send a request.
  • options?: relayRequestOptions is an optional object.

Return Object: RelayResponse

type RelayResponse = {
taskId: string;
};

Optional Parameters

Sending a Request

Request Body

const request = {
chainId: BigNumberish;
target: string;
data: BytesLike;
isRelayContext?: boolean;
feeToken: string;
};
  • chainId: the chain ID of the chain where the target 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 required target 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 the feeCollector address, the feeToken address, and the uint256 fee to the calldata.
    • If set to false, Gelato Relay will only append the feeCollector address to the calldata.
    • As this is an optional parameter with a default value of true, explicit passing is only required if you are using GelatoRelayFeeCollector from our relay-context-contracts.
  • feeToken: the address of the token that is to be used for payment.

Example Code

Please see relay-docs-examples for more in-depth code explainers.
For your testing, Gelato has deployed some simple contracts:
  • Counter.sol: deployed at address 0xEEeBe2F778AA186e88dCf2FEb8f8231565769C27 on these networks.
  • MyDummyWallet.sol: deployed at address 0xA045eb75e78f4988d42c3cd201365bDD5D76D406 on these networks.

1. Deploy a GelatoRelayContext compatible contract

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
8
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
9
import {
10
SafeERC20
11
} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
12
13
// Inheriting GelatoRelayContext gives access to:
14
// 1. onlyGelatoRelay modifier
15
// 2. payment methods, i.e. _transferRelayFee
16
// 3. _getFeeCollector(), _getFeeToken(), _getFee()
17
contract MyDummyWallet is GelatoRelayContext {
18
event LogSendToFriend(address indexed to, uint256 amount);
19
20
// `sendToFriend` is the target function to call
21
// this function uses this contract's mock ERC-20 balance to send
22
// an _amount of tokens to the _to address.
23
function sendToFriend(
24
address _token,
25
address _to,
26
uint256 _amount
27
) external onlyGelatoRelay {
28
// Payment to Gelato
29
// NOTE: be very careful here!
30
// if you do not use the onlyGelatoRelay modifier,
31
// anyone could encode themselves as the fee collector
32
// in the low-level data and drain tokens from this contract.
33
_transferRelayFee();
34
35
// transfer of ERC-20 tokens
36
SafeERC20.safeTransfer(IERC20(_token), _to, _amount);
37
38
// emitting an event for testing purposes
39
emit LogSendToFriend(_to, _amount);
40
}
41
}
42

2. Import GelatoRelaySDK into your front-end .js project

1
import { GelatoRelaySDK } from "@gelatonetwork/relay-sdk";

3. Send the payload to Gelato

1
// target contract address
2
const myDummyWallet = "0xA045eb75e78f4988d42c3cd201365bDD5D76D406";
3
4
// using a human-readable ABI for generating the payload
5
const abi = ["function sendToFriend(address _token,
6
address _to,
7
uint256 _amount");
8
9
// sendToFriend arguments
10
const feeToken = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
11
const vitalik = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
12
const amountToSend = ethers.utils.parseUnits("0.05");
13
14
// connect to the blockchain via a front-end provider
15
const provider = new ethers.providers.Web3Provider(window.ethereum);
16
17
// generate the target payload
18
const signer = provider.getSigner();
19
const contract = new ethers.Contract(myDummyWallet, abi, signer);
20
const { data } =
21
await contract.populateTransaction.sendToFriend(feeToken, vitalik, amountToSend);
22
23
// populate the relay SDK request body
24
const request = {
25
chainId: provider.network.chainId;
26
target: myDummyWallet;
27
data: data;
28
relayContext: true,
29
feeToken: feeToken;
30
};
31
32
// send relayRequest to Gelato Relay API
33
const relayResponse =
34
await GelatoRelaySDK.relayWithSyncFee(request);
Lines 24-30 build the request which is sent to the Gelato Relay API using the relayWithSyncFee SDK method.