3⃣
sponsoredCall
Permissionless sponsored transactions
After reading this page:
- You'll know how to use the
SponsoredCall
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.
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.
sponsoredCall
method utilises authentication via a sponsor API key to sponsor gasless transactions for your users securely. The payment method is Gelato 1Balance.const sponsoredCall = async (
request: SponsoredCallRequest,
sponsorApiKey: string,
options?: RelayRequestOptions
): Promise<RelayResponse>
sponsorApiKey
: an API key used to authenticate your sponsorship.options?
:RelayRequestOptions
is an optional request object
type RelayResponse = {
taskId: string;
};
const request = {
chainId: BigNumberish;
target: string;
data: BytesLike;
};
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.
Since
sponsoredCall
assumes you have your own security logic built in (i.e. replay and re-entrancy protection), you can go ahead and generate the payload for your function call and populate a request object.1
import { GelatoRelay, SponsoredCallRequest } from "@gelatonetwork/relay-sdk";
2
const relay = new GelatoRelay();
pragma solidity 0.8.17;
contract SimpleCounter {
uint256 public counter;
event IncrementCounter(uint256 newCounterValue, address msgSender);
// `increment` is the target function to call.
// This function increments a counter variable by 1
// IMPORTANT: with `sponsoredCall` you need to implement
// your own smart contract security measures, as this
// function can be called by any third party and not only by
// Gelato Relay. If not done properly, funds kept in this
// smart contract can be stolen.
function increment() external {
counter++;
emit IncrementCounter(counter, msg.sender);
}
}
// set up target address and function signature abi
const counter = "0x763D37aB388C5cdd2Fb0849d6275802F959fbF30";
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();
1
// Populate a relay request
2
const request: SponsoredCallRequest = {
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
// Go to https://relay.gelato.network to get a testnet API key with 1Balance.
10
// Send the relay request using Gelato Relay!
11
const relayResponse = await relay.sponsoredCall(request, apiKey);
Last modified 28d ago