Uniswap – TokenSwap

Interact with Uniswap's swap capabilities.

Methods

createSwapTx

Creates a Transaction object that swaps one token for another.

createSwapTx(tokenInData, tokenOutData, amountIn)

Parameters

tokenIn
Token
Required

Object representing the source token in a swap.

tokenIn properties
symbol
string

Token symbol, such as weth, dai, or uni.

address
string

Token address.

chainId
string
Required

A valid chainId – look online for more.

tokenOut
Token
Required

Object representing the destination token in a swap.

tokenOut properties
symbol
string

Token symbol, such as weth, dai, or uni.

address
string

Token address.

chainId
string
Required

A valid chainId – look online for more.

amountIn
float
Required

Amount of source token to swap.

returnAddr
string

Address to send destination token to after swap.

slippage
int

Slippage regardless of order of magnitude.

percentDec
int

Slippage order of magnitude. For example a slippage of 5% would have slippage = 5 and percentDec = 10.

Returns

swapTx
Transaction
Required

A transaction object that swaps one token for another.

Example Usage

const tokenIn = new Token({ symbol: "eth", chainId: HARDHAT_FORK_CHAIN_ID });
const tokenOut = new Token({
address: DAI_ADDR,
chainId: HARDHAT_FORK_CHAIN_ID,
});
const amountIn = 60;
const returnAddr = "0x...";
const slippage = 5;
const percentDec = 100; // 5 / 100
const swapTx = await tokenSwapModule.createWithdrawTx(
tokenInData,
tokenOutData,
amountIn,
returnAddr,
slippage,
percentDec
);

Main-Net Token Address Table

Token NameAddress
DAI0x6B175474E89094C44Da98b954EedeAC495271d0F
USDC0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48