|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +title: Gelato Smart Wallet |
| 4 | +description: Learn how to use Gelato Smart Wallet to enable gasless transactions on Flow EVM. |
| 5 | +keywords: |
| 6 | + - tutorials |
| 7 | + - guides |
| 8 | + - flow |
| 9 | + - evm |
| 10 | + - smart contracts |
| 11 | + - blockchain |
| 12 | + - gas efficiency |
| 13 | + - gas free |
| 14 | + - gasless |
| 15 | + - EIP-7702 |
| 16 | + - gelato |
| 17 | + - smart wallet |
| 18 | +--- |
| 19 | + |
| 20 | +import Tabs from '@theme/Tabs'; |
| 21 | +import TabItem from '@theme/TabItem'; |
| 22 | + |
| 23 | +# Gelato Smart Wallet Integration Guide |
| 24 | + |
| 25 | +You can use Gelato Smart Wallet to enable gasless transactions on Flow EVM. It supports features like: EIP-7702 or ERC-4337 depending on the wallet you use. |
| 26 | + |
| 27 | +## Prerequisites of using Gelato Smart Wallet |
| 28 | + |
| 29 | +You need to setup all the followings in the Gelato App to create a Gelato Sponsor API Key. |
| 30 | + |
| 31 | +### Step 1. Create Your Gelato Account |
| 32 | + |
| 33 | +Sign up on the [Gelato App] to establish an account. This account is the foundation for setting up relay tasks and managing gas sponsorships. |
| 34 | + |
| 35 | +### Step 2. Create a Relay App |
| 36 | + |
| 37 | +Within your Gelato account, create a new Relay App with the Flow EVM network. |
| 38 | +For Testnet, you can first allow `Any contract` to call your relay app. |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +:::note |
| 43 | + |
| 44 | +You can add more smart contract information and its associated chain information later in this App. |
| 45 | +When set to a specific contract instead of `Any contract`, the API keys will only allow gasless transactions for calls to the designated methods within the ABI of the contract. |
| 46 | + |
| 47 | +::: |
| 48 | + |
| 49 | +### Step 3. Create/Obtain a Sponsor API Key |
| 50 | + |
| 51 | +After creating the relay app, navigate to its dashboard to locate your Sponsor API Key. |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +This key links your Gelato setup with 1Balance for gas sponsorship. |
| 56 | + |
| 57 | +### Step 4. Deposit Funds into 1Balance |
| 58 | + |
| 59 | +To use Gelato sponsoring transactions, you need to deposit funds into 1Balance according to your target environment: |
| 60 | + |
| 61 | +- Mainnets: Deposit USDC. |
| 62 | +- Testnets: Deposit Sepolia ETH. |
| 63 | + |
| 64 | +Here we use Sepolia ETH as the gas fee for Testnet‘s gasless transactions. |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +Here are some third party Sepolia ETH faucets you can use: |
| 69 | + |
| 70 | +- [Google Cloud Sepolia Faucet] |
| 71 | +- [Alchemy Sepolia Faucet] |
| 72 | +- [Chainlink Sepolia Faucet] |
| 73 | +- [Metamask Sepolia Faucet] |
| 74 | + |
| 75 | +## Send Gasless Transactions for your users |
| 76 | + |
| 77 | +After you have created a Sponsor API Key and deposited funds into 1Balance, you can use gasless transactions features for your users. |
| 78 | +With the Gelato Smart Wallet SDK, developers can easily set up sponsored transactions for their applications in just a few simple steps, enabling seamless onboarding and interaction without requiring users to hold native tokens. |
| 79 | + |
| 80 | +:::note |
| 81 | + |
| 82 | +You can find the examples in the [Gelato Smart Wallet SDK] repository. |
| 83 | + |
| 84 | +::: |
| 85 | + |
| 86 | +### Step 1. Install all relevant dependencies |
| 87 | + |
| 88 | +<Tabs> |
| 89 | + <TabItem value="pnpm" label="pnpm" default> |
| 90 | + ```bash |
| 91 | + pnpm add @gelatodigital/smartwallet viem |
| 92 | + ``` |
| 93 | + </TabItem> |
| 94 | + <TabItem value="bun" label="bun"> |
| 95 | + ```bash |
| 96 | + bun add @gelatodigital/smartwallet viem |
| 97 | + ``` |
| 98 | + </TabItem> |
| 99 | + <TabItem value="yarn" label="yarn"> |
| 100 | + ```bash |
| 101 | + yarn add @gelatodigital/smartwallet viem |
| 102 | + ``` |
| 103 | + </TabItem> |
| 104 | + <TabItem value="npm" label="npm"> |
| 105 | + ```bash |
| 106 | + npm install @gelatodigital/smartwallet viem |
| 107 | + ``` |
| 108 | + </TabItem> |
| 109 | +</Tabs> |
| 110 | + |
| 111 | + |
| 112 | +### Step 2. Setup Smart Wallet Account |
| 113 | + |
| 114 | +Import required dependencies: |
| 115 | + |
| 116 | +```ts |
| 117 | +import { createGelatoSmartWalletClient, sponsored } from "@gelatonetwork/smartwallet"; |
| 118 | +import { gelato } from "@gelatonetwork/smartwallet/accounts"; |
| 119 | +import { createWalletClient, createPublicClient, http, type Hex } from "viem"; |
| 120 | +import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
| 121 | +import { flowTestnet } from "viem/chains"; |
| 122 | + |
| 123 | +// Create a public client for the Flow Testnet |
| 124 | +const publicClient = createPublicClient({ |
| 125 | + chain: flowTestnet, |
| 126 | + transport: http(), |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +You can set up a Smart Account as per your needs. |
| 131 | +Once you create the `gelato` account, the Gelato Smart Account address will be the same as your EOA, enabling EIP-7702 features. |
| 132 | + |
| 133 | +```ts |
| 134 | +// Prepare a normal EOA account |
| 135 | +const privateKey = (process.env.PRIVATE_KEY ?? generatePrivateKey()) as Hex; |
| 136 | +const owner = privateKeyToAccount(privateKey); |
| 137 | + |
| 138 | +// Wrap the EOA account into a Gelato Smart account |
| 139 | +const account = await gelato({ |
| 140 | + owner, |
| 141 | + client: publicClient, |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +Then you need to create a standard viem wallet client with the Gelato Smart Account. |
| 146 | + |
| 147 | +```ts |
| 148 | +const client = createWalletClient({ |
| 149 | + account, |
| 150 | + chain: flowTestnet, |
| 151 | + transport: http() |
| 152 | +}); |
| 153 | +``` |
| 154 | + |
| 155 | +Once you have a standard viem wallet client, you can wrap it into a Gelato Smart Wallet client with the sponsor API key. |
| 156 | + |
| 157 | +```ts |
| 158 | +const sponsorApiKey = process.env.SPONSOR_API_KEY; |
| 159 | +if (!sponsorApiKey) { |
| 160 | + throw new Error("SPONSOR_API_KEY is not set"); |
| 161 | +} |
| 162 | + |
| 163 | +const swc = await createGelatoSmartWalletClient(client, { |
| 164 | + apiKey: sponsorApiKey |
| 165 | +}); |
| 166 | +``` |
| 167 | + |
| 168 | +### Step 3. Estimate or Send a Gasless Transaction |
| 169 | + |
| 170 | +Now you can estimate or send a gasless transaction with the Gelato Smart Wallet client. |
| 171 | + |
| 172 | +To estimate the fee and gas for a gasless transaction, you can use the `estimate` method. |
| 173 | + |
| 174 | +```ts |
| 175 | +const response = await swc.estimate({ |
| 176 | + payment: sponsored(sponsorApiKey), |
| 177 | + calls: [ |
| 178 | + { |
| 179 | + to: "0xa8851f5f279eD47a292f09CA2b6D40736a51788E", |
| 180 | + data: "0xd09de08a", |
| 181 | + value: 0n |
| 182 | + } |
| 183 | + ] |
| 184 | +}); |
| 185 | + |
| 186 | +console.log(`Estimated fee: ${formatEther(BigInt(response.fee.amount))} FLOW`); |
| 187 | +console.log(`Estimated gas: ${JSON.stringify(response.gas)} GAS`); |
| 188 | +``` |
| 189 | + |
| 190 | +To send a gasless transaction, you can use the `execute` method. |
| 191 | + |
| 192 | +```ts |
| 193 | +const results = await smartWalletClient.execute({ |
| 194 | + payment : sponsored(sponsorApiKey), |
| 195 | + calls: [ |
| 196 | + { |
| 197 | + to: "0xa8851f5f279eD47a292f09CA2b6D40736a51788E", |
| 198 | + data: "0xd09de08a", |
| 199 | + value: 0n |
| 200 | + } |
| 201 | + ] |
| 202 | +}); |
| 203 | + |
| 204 | +console.log("userOp hash:", results?.id); |
| 205 | +const txHash = await results?.wait(); |
| 206 | +console.log("transaction hash",txHash); |
| 207 | +``` |
| 208 | + |
| 209 | +Or you can use `prepare` + `send` to send a gasless transaction. |
| 210 | + |
| 211 | +```ts |
| 212 | +const preparedCalls = await swc.prepare({ |
| 213 | + payment: sponsored(sponsorApiKey), |
| 214 | + calls: [ |
| 215 | + { |
| 216 | + to: "0xa8851f5f279eD47a292f09CA2b6D40736a51788E", |
| 217 | + data: "0xd09de08a", |
| 218 | + value: 0n |
| 219 | + } |
| 220 | + ] |
| 221 | +}); |
| 222 | + |
| 223 | +// Other actions can be performed here |
| 224 | + |
| 225 | +const results = await swc.send({ |
| 226 | + preparedCalls |
| 227 | +}); |
| 228 | + |
| 229 | +// You can listen for events from the Gelato Smart Wallet client |
| 230 | +results.on("submitted", (status: GelatoTaskStatus) => { |
| 231 | + console.log(`Transaction submitted: ${status.transactionHash}`); |
| 232 | +}); |
| 233 | +results.on("success", async (status: GelatoTaskStatus) => { |
| 234 | + console.log(`Transaction successful: ${status.transactionHash}`); |
| 235 | +}); |
| 236 | +results.on("error", (error: Error) => { |
| 237 | + console.error(`Transaction failed: ${error.message}`); |
| 238 | +}); |
| 239 | +``` |
| 240 | + |
| 241 | +--- |
| 242 | + |
| 243 | +## Conclusion |
| 244 | + |
| 245 | +So far, you’ve learned how to send sponsored EIP-7702 transactions using a Gelato Smart Wallet on Flow EVM. |
| 246 | + |
| 247 | +[Gelato App]: https://app.gelato.network/ |
| 248 | +[Google Cloud Sepolia Faucet]: https://cloud.google.com/application/web3/faucet/ethereum/sepolia |
| 249 | +[Alchemy Sepolia Faucet]: https://www.alchemy.com/faucets/ethereum-sepolia |
| 250 | +[Chainlink Sepolia Faucet]: https://faucets.chain.link/sepolia |
| 251 | +[Metamask Sepolia Faucet]: https://docs.metamask.io/developer-tools/faucet/ |
| 252 | +[Gelato Smart Wallet SDK]: https://github.com/gelatodigital/smartwallet/tree/master/examples |
0 commit comments