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 testing
const rpc = "localhost:8545";
const provider = new ethers.providers.JsonRpcProvider(rpc);
// Note that the key here will be exposed and should never be used in production
const eoa = new ethers.Wallet(PRIV_KEY, provider);
3

Create & Initialize a FunWallet

const chainID = "31337" // Hardhat
const prefundAmt = 0.3 // amount of native gas token to prefund our FunWallet with
const API_KEY = "" // Get your API key from app.fun.xyz/api-key
const config = new FunWalletConfig(eoa, chainID, prefundAmt)
const wallet = new FunWallet(config, API_KEY)
// init will perform the pre-funding of the FunWallet
await 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 testing
const rpc = "localhost:8545";
const provider = new ethers.providers.JsonRpcProvider(rpc);
// Note that the key here will be exposed and should never be used in production
const eoa = new ethers.Wallet(PRIV_KEY, provider);
const chainID = "31337" // Hardhat
const prefundAmt = 0.3 // amount of native gas token to prefund our FunWallet with
const API_KEY = "" // Get your API key from app.fun.xyz/api-key
const config = new FunWalletConfig(eoa, chainID, prefundAmt)
const wallet = new FunWallet(config, API_KEY)
// init will perform the pre-funding of the FunWallet
await 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