Testing on a Local Fork
Testing via Fun's local fork reproduces Ethereum's mainnet environment on a developer's local machine & provides an EOA prefunded with an unlimited amount of ETH so developers can test without worrying about how to procure gas resources.
Setting up a local fork environment requires two local services to be deployed:
A bundler service deployed on http://127.0.0.1:3000.
A hardhat fork of Ethereum mainnet deployed on http://127.0.0.1:8545.
The details of the fork's prefunded EOA are given below:
Public Key | 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 | ||
Private Key | 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 |
Additionally, to test on a local fork, the Environment Config's chain variable must be set to "ethereum-localfork".
The chainID of a hardhat localfork is 31337.
Setup Bundler & Local Fork Services
Clone the Bundler Repository
git clone https://github.com/TheFunGroup/bundler.git
Change into "bundler" Directory
cd bundler
Install Packages
yarn && yarn preprocess
Change into "localfork" Directory
cd localfork
Install Packages
yarn install
Start Hardhat Local Fork Service
npx hardhat node --fork "https://eth-mainnet.g.alchemy.com/v2/lcA7Kyqv42J1Qh-wLm__DdqSCJBtZyd1"
In Separate Terminal, Change Permissions to Run the Bundler Service
chmod +x setup.sh
Start Bundler Service, Fund Accounts & Deploy Needed Contracts
./setup.sh
The bundler service is up and running if running on http://localhost:3000/rpc is displayed after executing ./setup.sh.
Full Example
The following example flow showcases how to create a new FunWallet on a local fork of Ethereum's mainnet & perform a swap between ETH & USDC:
swap.js
const { FunWallet, configureEnvironment } = require("fun-wallet")const { Eoa } = require("fun-wallet/auth")const { fundWallet } = require("fun-wallet/utils")const PRIVATE_KEY = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"const API_KEY = "MYny3w7xJh6PRlRgkJ9604sHouY2MTke6lCPpSHq"const swap = async () => {await configureEnvironment({apiKey: API_KEY,chain: "ethereum-localfork"})const auth = new Eoa({ privateKey: PRIVATE_KEY })const uniqueId = await auth.getUniqueId()const wallet = new FunWallet({ uniqueId })await fundWallet(auth, wallet, 1)const receipt = await wallet.swap(auth, {in: "eth",amount: .001,out: "dai",});}swap()