1⃣
callWithSyncFee
SDK Method 1️⃣
WARNING: 22nd December 2022
callWithSyncFee
is only available with the latestrelay-context
2.1.0 npm package version and only on supported v2 networks. Please read the migration guide and reach out on our Discord if you have any questions. After reading this page:
- You'll see some code which will help you send a relay request within minutes.
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.
For your testing, Gelato has deployed some simple contracts:
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
1
import { GelatoRelay, CallWithSyncFeeRequest } from "@gelatonetwork/relay-sdk";
2
const relay = new GelatoRelay();
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: CallWithSyncFeeRequest = {
25
chainId: provider.network.chainId,
26
target: myDummyWallet,
27
data: data,
28
feeToken: feeToken,
29
isRelayContext: true,
30
};
31
32
// send relayRequest to Gelato Relay API
33
const relayResponse = await relay.callWithSyncFee(request);
Lines 24-30 build the
request
which is sent to the Gelato Relay API using the callWithSyncFee
SDK method.Last modified 1mo ago