Example Flow
1
Import Required Libraries
import { ethers } from "ethers";import { FunWallet, FunWalletConfig, Paymasters } from "@fun-wallet/sdk";const { PaymasterSponsorInterface } = Paymasters
2
Create an ethers.Wallet EOA Instance
// Hardhat port for local for testingconst rpc = "localhost:8545";const provider = new ethers.providers.JsonRpcProvider(rpc);// Note that the key here will be exposed and should never be used in productionconst eoa = new ethers.Wallet(PRIV_KEY, provider);
3
Create & Initialize a FunWallet
const chainID = "31337" // Hardhatconst prefundAmt = 0.3 // amount of native gas token to prefund our FunWallet withconst API_KEY = "" // Get your API key from app.fun.xyz/api-keyconst config = new FunWalletConfig(eoa, chainID, prefundAmt)const wallet = new FunWallet(config, API_KEY)// init will perform the pre-funding of the FunWalletawait wallet.init()
4
Create & Initialize PaymasterSponsorInterface Istance
const paymasterInterface = new PaymasterSponsorInterface(eoa)await paymasterInterface.init()
5
Deposit & Lock USDC & eth on Paymaster
await paymasterInterface.addTokenDepositTo(wallet.address, USDCETHAMT)await paymasterInterface.addEthDepositForSponsor(value, eoa.address)await paymasterInterface.lockTokenDeposit()await paymasterInterface.setWhitelistMode(true)
Update Paymaster on FunWallet
const usdcPaymaster = new USDCPaymaster(paymasterInterface.paymasterAddress)wallet.updatePaymaster(usdcPaymaster)
Full Example
pay-gas-in-usdc.js
import { ethers } from "ethers";import { FunWallet, FunWalletConfig, Paymasters } from "@fun-wallet/sdk";const { PaymasterSponsorInterface } = Paymasters// Hardhat port for local for testingconst rpc = "localhost:8545";const provider = new ethers.providers.JsonRpcProvider(rpc);// Note that the key here will be exposed and should never be used in productionconst eoa = new ethers.Wallet(PRIV_KEY, provider);const chainID = "31337" // Hardhatconst prefundAmt = 0.3 // amount of native gas token to prefund our FunWallet withconst API_KEY = "" // Get your API key from app.fun.xyz/api-keyconst config = new FunWalletConfig(eoa, chainID, prefundAmt)const wallet = new FunWallet(config, API_KEY)// init will perform the pre-funding of the FunWalletawait wallet.init()const paymasterInterface = new PaymasterSponsorInterface(eoa)await paymasterInterface.init()await paymasterInterface.addTokenDepositTo(wallet.address, USDCETHAMT)await paymasterInterface.addEthDepositForSponsor(value, eoa.address)await paymasterInterface.lockTokenDeposit()await paymasterInterface.setWhitelistMode(true)const usdcPaymaster = new USDCPaymaster(paymasterInterface.paymasterAddress)wallet.updatePaymaster(usdcPaymaster)
Mentioned Classes
FunWallet
Primary class in the FunWallet SDK that orchestrates the movement of assets & data
FunWalletConfig
FunWallet configuration class
USDCPaymaster
A class for storing a user's paymaster data
PaymasterSponsorInterface
The primary class for controlling a user's Paymaster sponsor