Create FunWallet
Create the most powerful smart wallet in web3.
A FunWallet can execute any transaction interpretable by an EVM blockchain. You can swap one token for another, transfer assets to a Twitter handle, mint an NFT and much more.
Each FunWallet is created at a deterministic address based on it's Auth Type & index. This means that every auth-index pair will result in a unique FunWallet & address. This allows users to have multiple FunWallets with the same Auth Type by varying the index. Checkout how to create a new FunWallet for an owner that already has created a wallet in the past here.
Unlike an EOA, FunWallets must be independently deployed. This can be done in one of two ways:
Manually by calling FunWallet.create if no FunWallet exists with that auth-index pair.
Automatically by executing any FunWallet transaction (ie. transfer, swap) if no wallet exists with that auth-index pair.
Manual FunWallet creation
The following flow outlines how to manually create a FunWallet.
The wallet must have enough funds to be instantiated. This can be done either by transferring native gas token to the FunWallet address (you can get this via FunWallet.getAddress()) or by specifying a GaslesSponsor.
Running the code below won't create the contract because it already exists. You can change the index or private key if you wish to create a new wallet.
create.js
const { FunWallet, configureEnvironment } = require("fun-wallet")const { Eoa } = require("fun-wallet/auth")const { fundWallet } = require("fun-wallet/utils")const PRIVATE_KEY = "0x98e9cfb323863bc4bfc094482703f3d4ac0cd407e3af2351c00dde1a6732756a"const API_KEY = "MYny3w7xJh6PRlRgkJ9604sHouY2MTke6lCPpSHq"const create = async () => {await configureEnvironment({apiKey: API_KEY})const auth = new Eoa({ privateKey: PRIVATE_KEY })const uniqueId = await auth.getUniqueId()const index = Math.ceil(Math.random() * 100_000_000)const wallet = new FunWallet({ uniqueId, index })await fundWallet(auth, wallet, .01)// auth: an auth object (see Auth API)const receipt = await wallet.create(auth)}create()
Automatic FunWallet creation
The following flow outlines how to automatically create a FunWallet without calling FunWallet.create() by executing a transaction (in this case a transfer of tokens) from the FunWallet (which automatically creates & deploys the wallet).
The wallet must have enough funds to be instantiated. This can be done either by transferring native gas token to the FunWallet address (you can get this via FunWallet.getAddress()) or by specifying a GaslesSponsor.
Running the code below won't create the contract because it already exists. You can change the index or private key if you wish to create a new wallet.
transfer.js
const { FunWallet, configureEnvironment } = require("fun-wallet")const { Eoa } = require("fun-wallet/auth")const { fundWallet } = require("fun-wallet/utils")// Replace these with your ownconst PRIVATE_KEY = "0x98e9cfb323863bc4bfc094482703f3d4ac0cd407e3af2351c00dde1a6732756a"const API_KEY = "MYny3w7xJh6PRlRgkJ9604sHouY2MTke6lCPpSHq"const TO_ADDR = "0x07Ac5A221e5b3263ad0E04aBa6076B795A91aef9"const create = async () => {await configureEnvironment({apiKey: API_KEY})const auth = new Eoa({ privateKey: PRIVATE_KEY })// Get FunWallet associated with EOAconst uniqueId = await auth.getUniqueId()const index = Math.ceil(Math.random() * 100_000_000)const wallet = new FunWallet({ uniqueId, index })await fundWallet(auth, wallet, .01)// If the wallet doesn't exist, calling any function (ie. transfer)// will automatically create the wallet.const receipt = await wallet.transfer(auth, {to: TO_ADDR,amount: 10,token: "USDC"})}create()
Create new FunWallet with existing owner
The following example shows how to create another wallet using the index 5343 from the private key.
The wallet must have enough funds to be instantiated. This can be done either by transferring native gas token to the FunWallet address (you can get this via FunWallet.getAddress()) or by specifying a GaslesSponsor.
Running the code below won't create the contract because it already exists. You can change the index or private key if you wish to create a new wallet.
MultipleWallet.js
const { FunWallet, configureEnvironment } = require("fun-wallet")const { Eoa } = require("fun-wallet/auth")const PRIVATE_KEY = "0x98e9cfb323863bc4bfc094482703f3d4ac0cd407e3af2351c00dde1a6732756a"const API_KEY = "MYny3w7xJh6PRlRgkJ9604sHouY2MTke6lCPpSHq"const create = async () => {await configureEnvironment({apiKey: API_KEY})const auth = new Eoa({ privateKey: PRIVATE_KEY })const uniqueId = await auth.getUniqueId()// Specify index of walletconst wallet = new FunWallet({ uniqueId, index: 5343 }) // Specify index of wallet}create()
See more info about FunWallets.