Create FunWallet
Create a modular access control smart contract wallet
Add Required Libraries
import { ethers } from "ethers"import { FunWallet, FunWalletConfig } from "@fun-wallet/sdk"
Create an ethers.Wallet EOA Instance
This EOA will be used to fund the FunWallet
, make sure it is on the same chain as the desired FunWallet instance.
There are 2 ways of creating an ether.Wallet EOA instance:
With a known private key PRIV_KEY (only for testing):
const rpc = "https://avalanche-fuji.infura.io/v3/4a1a0a67f6874be6bb6947a62792dab7"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)
With an external EOA wallet provide (e.g. MetaMask)
const rpc = "https://avalanche-fuji.infura.io/v3/4a1a0a67f6874be6bb6947a62792dab7"const provider = new ethers.providers.JsonRpcProvider(rpc)// To create an EOA instance with an external EOA wallet (e.g MetaMask)const provider = new ethers.providers.Web3Provider(window.ethereum)await provider.send('eth_requestAccounts', []) // <- this promps user to connect external walleteoa = provider.getSigner()
Create & Initialize a FunWallet
A FunWallet is constructed with parameters from a FunWalletConfig object. We are required to call the init on a FunWallet after its construction to ensure that internal variables requiring external async data server calls are updated.
const chainID = "43113" // Fuji testnetconst 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()
Full Example
create-funwallet.js
// 1. Install necessary// npm install @fun-wallet/spackagesdk ethers// 2. Add Importsimport { ethers } from "ethers"import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk"// 3. Create EOA Instance// An internal Fun RPC for customer testingconst rpc = "https://avalanche-fuji.infura.io/v3/4a1a0a67f6874be6bb6947a62792dab7"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)// To create an EOA instance with an external wallet (e.g MetaMask) do this instead// const provider = new ethers.providers.Web3Provider(window.ethereum)// await provider.send('eth_requestAccounts', []) // <- this promps user to connect metamask// eoa = provider.getSigner()// 4. Create a FunWalletconst chainID = "43113"const prefundAmt = 0.3 // etherconst 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)// Gather data asynchronously from onchain resources to get expected wallet dataawait wallet.init()console.log("Public Address for Fun Wallet: ", wallet.address)
create-funwallet-output.log
Public Address for Fun Wallet: 0xA5165f8E16dF9FE1EA6866320fd2AbF78c429de8
The address displayed here is only the predicted address of the FunWallet. In order to see the activity of this FunWallet on-chain, such as by using a block explorer like Etherscan, we must first deploy the FunWallet to a blockchain.
Mentioned Classes
FunWallet
Primary class in the FunWallet SDK that orchestrates the movement of assets & data
FunWalletConfig
FunWallet configuration class