Quick Start
Below is a simple example in order to get us started.
We get Gelato Relay SDK to call a HelloWorld smart contract on our behalf. Note that in this example there is no dependency on RPC providers, as we simply build all transaction data, its required sponsorSignature and send it to Gelato Relay API.
sponsorSignature is required by sponsor in order for Gelato to securely credit payments, but in this example we do not enforce any payment to be made as it is a testnet. In this way, interacting with a blockchain is simplified to sending a POST request to a web server.
1
import { Wallet, utils } from "ethers";
2
import { GelatoRelaySDK } from "@gelatonetwork/gelato-relay-sdk";
3
4
const forwardRequestExample = async () => {
5
// Goerli
6
const chainId = 5;
7
// HELLO WORLD smart contract on Goerli
8
const target = "0x8580995EB790a3002A55d249e92A8B6e5d0b384a";
9
const NATIVE_TOKEN = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
10
// Create Mock wallet
11
const wallet = Wallet.createRandom();
12
const sponsor = await wallet.getAddress();
13
14
console.log(`Mock PK: ${await wallet._signingKey().privateKey}`);
15
console.log(`Mock wallet address: ${sponsor}`);
16
// abi encode for HelloWorld.sayHiVanilla(address _feeToken)
17
const data = `0x4b327067000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeaeeeeeeeeeeeeeeeee`;
18
// Async Gas Tank payment model (won't be enforced on testnets, hence no need to deposit into Gelato's Gas Tank smart contract)
19
const paymentType = 1;
20
// Maximum fee that sponsor is willing to pay worth of NATIVE_TOKEN
21
const maxFee = "1000000000000000000";
22
// Gas limit
23
const gas = "200000";
24
// We do not enforce smart contract nonces to simplify the example.
25
// In reality, this decision depends whether or not target address already implements
26
// replay protection. (More info in the docs)
27
const sponsorNonce = 0;
28
const enforceSponsorNonce = false;
29
// Only relevant when enforceSponsorNonce=true
30
const enforceSponsorNonceOrdering = false;
31
32
// Build ForwardRequest object
33
const forwardRequest = GelatoRelaySDK.forwardRequest(
34
chainId,
35
target,
36
data,
37
NATIVE_TOKEN,
38
paymentType,
39
maxFee,
40
gas,
41
sponsorNonce,
42
enforceSponsorNonce,
43
sponsor
44
);
45
46
// Get EIP-712 hash (aka digest) of forwardRequest
47
const digest = GelatoRelaySDK.getForwardRequestDigestToSign(forwardRequest);
48
49
// Sign digest using Mock private key
50
const sponsorSignature: utils.BytesLike = utils.joinSignature(
51
await wallet._signingKey().signDigest(digest)
52
);
53
54
// Send forwardRequest and its sponsorSignature to Gelato Relay API
55
await GelatoRelaySDK.sendForwardRequest(forwardRequest, sponsorSignature);
56
57
console.log("ForwardRequest submitted!");
58
};
59
60
forwardRequestExample();
Copied!
Copy link