Skip to content

Commit 40a8031

Browse files
[chore]: update relay implementation
1 parent 560ee04 commit 40a8031

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

smart-contracts/advanced/relay.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,74 @@ Transactions can be paid for in two primary ways: off-chain payments and on-chai
4141

4242
- **callWithSyncFee**: This method is similar to callWithSyncFeeERC2771 but without the need for ERC-2771’s off-chain signature verification. The user’s gas fee is calculated and paid directly from the target smart contract during the transaction execution. This approach is useful for applications where users are expected to pay for their own gas without requiring meta-transaction features.
4343

44+
### Implementation
45+
46+
We will require three simple steps to implement Gelato Relay. Here, we are going to showcase the three steps required to implement the method `sponsoredCallERC2771`, which is the most used one.
47+
48+
#### Step 1: Inherit Context Contract
49+
50+
Depending on the method, you must inherit different contracts as they will provide other methods. In this case, we will have to inherit the `ERC2771Context`. The `ERC2771Context` provide us with the methods `_msgSender()` and `_msgData()` that will allow us to recover the original user sending the transaction.
51+
52+
```solidity
53+
import {
54+
ERC2771Context
55+
} from "@gelatonetwork/relay-context/contracts/vendor/ERC2771Context.sol";
56+
57+
contract CounterERC2771 is ERC2771Context {
58+
59+
// ERC2771Context: setting the immutable trustedForwarder variable
60+
constructor(address trustedForwarder) ERC2771Context(trustedForwarder) {}
61+
62+
function incrementContext() external {
63+
64+
// Incrementing the counter mapped to the _msgSender!
65+
contextCounter[_msgSender()]++;
66+
67+
// Emitting an event for testing purposes
68+
emit IncrementContextCounter(_msgSender());
69+
}
70+
}
71+
```
72+
73+
#### Step 2: Import the relay SDK
74+
75+
In your frontend/backend, you would need to import and instantiate the relay class.
76+
77+
```
78+
import { GelatoRelay, SponsoredCallERC2771Request } from "@gelatonetwork/relay-sdk";
79+
const relay = new GelatoRelay(API_KEY);
80+
```
81+
82+
#### Step 3: Send the payload to Gelato
83+
84+
This is an example using Gelato's CounterERC2771.sol, which is deployed on these networks.
85+
86+
```
87+
// Set up on-chain variables, such as target address
88+
const counter = "0x00172f67db60E5fA346e599cdE675f0ca213b47b";
89+
const abi = ["function incrementContext()"];
90+
const provider = new ethers.BrowserProvider(window.ethereum);
91+
const signer = provider.getSigner();
92+
const user = signer.getAddress();
93+
94+
// Generate the target payload
95+
const contract = new ethers.Contract(counter, abi, signer);
96+
const { data } = await contract.incrementContext.populateTransaction();
97+
98+
// Populate a relay request
99+
const request: CallWithERC2771Request = {
100+
chainId: (await provider.getNetwork()).chainId,
101+
target: counter;
102+
data: data;
103+
user: user;
104+
};
105+
106+
// Without a specific API key, the relay request will fail!
107+
// Go to https://relay.gelato.network to get a testnet API key with 1Balance.
108+
// Send a relay request using Gelato Relay!
109+
const relayResponse = await relay.sponsoredCallERC2771(request, provider, apiKey);
110+
```
111+
44112
#### Further Gelato resources
45113

46114
- [Gelato Relay Docs](https://docs.gelato.network/web3-services/relay)

0 commit comments

Comments
 (0)