Token Transfer
Transfer ERC-20 tokens to a known address.
Example Flow
The following flow describes how to use the TokenTransfer
action to transfer an ERC-20 token to another address.
1
Import Required Libraries
import { ethers } from "ethers"import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk"const { TokenTransfer } = Modules
2
Create an ethers.Wallet EOA Instance
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)
3
Create & Initialize a FunWallet
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()
4
Create & Add TokenTransfer Module to a FunWallet
const tokenTransferModule = new TokenTransfer()await wallet.addModule(tokenTransferModule)
5
Deploy a FunWallet to a Blockchain
const deployWalletReceipt = await wallet.deploy()
6
Generate Token Transfer Transaction & Deploy to a Blockchain
const to = "0xB4C3826aFea3Bc437C49695983eAaCFF2Bf8E305" // Receiver of tokensconst amount = ethers.utils.parseEther(".01") // Amount of native gas tokens to transferconst tokenAddr = "0x9983f755bbd60d1886cbfe103c98c272aa0f03d6" // Address of ERC-20 token to tranferconst tokenTransferTx = await tokenTransferModule.createTransferTx(to, amount, { address: tokenAddr })const tokenTransferReceipt = await wallet.deployTx(tokenTransferTx)console.log(tokenTransferReceipt)
Full Example
token-transfer.js
// 1. Install necessary packages// npm install @fun-wallet/sdk ethers// 2. Add Importsimport { ethers } from "ethers"import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk"const { TokenTransfer } = Modules// 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" // 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()// 5. Create TokenTransfer Module and add it to our FunWalletconst tokenTransferModule = new TokenTransfer()await wallet.addModule(tokenTransferModule)// 6. Deploy FunWalletconst deployWalletReceipt = await wallet.deploy()// 7. Create Token Transfer Transaction & Deploy Onchainconst to = "0xB4C3826aFea3Bc437C49695983eAaCFF2Bf8E305" // Receiver of tokensconst amount = ethers.utils.parseEther(".01") // Amount of native gas tokens to transferconst tokenAddr = "0x9983f755bbd60d1886cbfe103c98c272aa0f03d6" // Address of ERC-20 token to tranferconst tokenTransferTx = await tokenTransferModule.createTransferTx(to, amount, { address: tokenAddr })const tokenTransferReceipt = await wallet.deployTx(tokenTransferTx)console.log(tokenTransferReceipt)
token-transfer-output.json
{userOpHash: '0xd7ea68043200a00db7bdf072842cb3669456525ab24db6032f910fe7aafd56cb',txid: '0xb98e9c5cd9451a5b7335723f223738eaa0c16b3a60f47e9d3592ff5faef54fb4',gasUsed: 173974,gasUSD: 0.09142246713}
To verify that this transaction executed successfully, view the transaction id on your testnet. The example transaction can be found here.
Mentioned Classes
FunWallet
Primary class in the FunWallet SDK that orchestrates the movement of assets & data
FunWalletConfig
FunWallet configuration class
TokenTransfer
Module that enables the transfer of tokens to known addresses