Gelato Network
Search…
⌃K
3⃣

relayWithSponsoredCall

SDK Method 3️⃣
After reading this page:
  • You'll know how to use the relayWithSponsoredCall SDK method. This uses the 1Balance payment method, allowing you to sponsor some/all of your user's gas costs.
  • You'll see some code which will help you send a relay request within minutes.

Overview

relayWithSponsoredCall utilises authentication via a sponsor API key to sponsor gasless transactions for your users securely. The payment method is Gelato 1Balance.

SDK method: relayWithSponsoredCall

const relayWithSponsoredCall = async (
request: SponsoredCallRequest,
sponsorApiKey: string,
options?: RelayRequestOptions
): Promise<RelayResponse>

Arguments:

  • request: this is the request body used to send a request.
  • sponsorApiKey : an API key used to authenticate your sponsorship.
  • options?: RelayRequestOptions is an optional request object

Return Object: RelayResponse

type RelayResponse = {
taskId: string;
};

Optional Parameters

Sending a Request

Request Body

const request = {
chainId: BigNumberish;
target: string;
data: BytesLike;
};
  • 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.

Example Code

Please see relay-docs-examples for more in-depth code explainers.
Since relayWithSponsoredCall assumes you have your own security logic, i.e. replay and re-entrancy protection, built in, you can go ahead and generate the payload for your function call and populate a request object.

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

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

2. Generate a payload for your target contract

This is an example using Gelato's CounterERC2771.sol which is deployed on Goerli and Polygon.
// set up target address and function signature abi
const counter = "0xEEeBe2F778AA186e88dCf2FEb8f8231565769C27";
const abi = ["function increment()"];
// generate payload using front-end provider such as MetaMask
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(counterAddress, abi, signer);
const { data } = await contract.populateTransaction.increment();

3. Send the payload to Gelato

1
// Populate a relay request
2
const request = {
3
chainId: provider.network.chainId,
4
target: counter,
5
data: data,
6
};
7
8
// Without a specific API key, the relay request will fail!
9
// Reach out to us on Discord to discuss getting an API key.
10
// Send the relay request using GelatoRelaySDK!
11
const relayResponse =
12
await GelatoRelaySDK.relayWithSponsoredCall(
13
request,
14
sponsorApiKey
15
);