Uniswap

Swap tokens, provide liquidity & interact with Uniswaps's smart contact suite.

Overview

Currently, the FunWallet SDK supports the following Uniswap actions:

  • Token Swap



In the future, the FunWallet SDK will support:

  • Add liquidity to a liquidity pool

  • Remove liquidity from a liquidity pool



To learn more about Uniswap Module method headers, parameters & return values, visit the Uniswap Module API Reference.

It is highly recommended to test Uniswap on a fork of eth-mainnet. See how to test with forks here.



Example Flow

1

Import Requried Libraries

import { ethers } from "ethers";
import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk";
const { TokenSwapWithdrawal } = Modules;
2

Create an ethers.Wallet 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);
3

Create & Initialize a FunWallet

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

Create & Add TokenSwap Module to a FunWallet

const tokenSwapModule = new TokenSwap();
await wallet.addModule(tokenSwapModule);
5

Deploy a FunWallet to a Blockchain with the TokenSwap Module Attached

const deployWalletReceipt = await wallet.deploy();
6

Generate & Deploy the Transaction that Swaps a Token for Another

const tokenIn = new Token({ symbol: "eth", chainId: HARDHAT_FORK_CHAIN_ID })
const tokenOut = new Token({ address: DAI_ADDR, chainId: HARDHAT_FORK_CHAIN_ID })
// This particular transaction has a 5% slippage limit (specified by the parameters 5 & 100)
const swapTx = await swapModule.createSwapTx(tokenIn, tokenOut, AMOUNT, wallet.address, 5, 100)
const swapReceipt = await FunWallet.deployTx(swapTx);

Full example

,
uniswap-example.js
// Install necessary packages
// npm install @fun-wallet/sdk ethers
// Add Imports
import { ethers } from "ethers";
import { FunWallet, FunWalletConfig, Modules } from "@fun-wallet/sdk";
const { TokenSwapWithdrawal } = Modules;
// // 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);
// Create a FunWallet
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()
const tokenSwapModule = new TokenSwap();
await wallet.addModule(tokenSwapModule);
const deployWalletReceipt = await wallet.deploy();
const tokenIn = new Token({ symbol: "eth", chainId: HARDHAT_FORK_CHAIN_ID })
const tokenOut = new Token({ address: DAI_ADDR, chainId: HARDHAT_FORK_CHAIN_ID })
// This particular transaction has a 5% slippage limit (specified by the parameters 5 & 100)
const swapTx = await tokenSwapModule.createSwapTx(tokenIn, tokenOut, AMOUNT, wallet.address, 5, 100)
const swapReceipt = await wallet.deployTx(swapTx);
console.log(swapReceipt)
,
uniswap-example-output.json
{
userOpHash: '0xd7ea68043200a00db7bdf072842cb3669456525ab24db6032f910fe7aafd56cb',
txid: '0xb98e9c5cd9451a5b7335723f223738eaa0c16b3a60f47e9d3592ff5faef54fb4',
gasUsed: 173974,
gasUSD: 0.09142246713
}

Mentioned Classes