Create FunWallet

Create a modular access control smart contract wallet

1

Add Required Libraries

import { ethers } from "ethers"
import { FunWallet, FunWalletConfig } from "@fun-wallet/sdk"
2

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 production
const 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 wallet
eoa = provider.getSigner()
3

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 testnet
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()

Full Example

,
create-funwallet.js
// 1. Install necessary
// npm install @fun-wallet/spackagesdk ethers
// 2. Add Imports
import { ethers } from "ethers"
import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk"
// 3. Create EOA Instance
// An internal Fun RPC for customer 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 production
const 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 FunWallet
const chainID = "43113"
const prefundAmt = 0.3 // ether
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)
// Gather data asynchronously from onchain resources to get expected wallet data
await 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