π±Swap
This guide demonstrates how to fetch token data, quote, and execute token swaps using the Holdstation SDK.
Installation
npm install @holdstation/worldchain-sdk @holdstation/worldchain-ethers-v5 ethers
Setup
import { Client, Multicall3, Quoter, SwapHelper } from "@holdstation/worldchain-ethers-v5";
import { config, inmemoryTokenStorage, TokenProvider } from "@holdstation/worldchain-sdk";
import { ethers } from "ethers";
const RPC_URL = "<https://worldchain-mainnet.g.alchemy.com/public>";
const provider = new ethers.providers.StaticJsonRpcProvider(RPC_URL, {
chainId: 480,
name: "worldchain",
});
const client = new Client(provider);
config.client = client;
config.multicall3 = new Multicall3(provider);
const tokenProvider = new TokenProvider();
const quoter = new Quoter(client);
const swapHelper = new SwapHelper(client, {
tokenStorage: inmemoryTokenStorage,
});
Overall Flow
Call
tokenProvider.details(...)
to fetch token metadata if needed.(Optional) Call
quoter.simple()
orquoter.smart()
to preview routes.Call
swapHelper.quote(params)
withSwapParams["quoteInput"]
to get a live quote for swapping.Call
swapHelper.swap(...)
withSwapParams["input"]
using the result from quote.(Optional) Ensure
feeReceiver
is whitelisted on the contract if you are charging a fee.
Token Metadata
Get Token Info
const tokenInfo = await tokenProvider.details("0xTokenAddress");
console.log("Token Info:", tokenInfo);
Get Multiple Token Details
const tokens = await tokenProvider.details(
"0xToken1", "0xToken2", "0xToken3"
);
console.log("Token Details:", tokens);
Quoter
Simple Quote
const WETH = "0x4200000000000000000000000000000000000006";
const USDCe = "0x79A02482A880bCE3F13e09Da970dC34db4CD24d1";
const quote = await quoter.simple(WETH, USDCe);
console.log("Best route:", quote.best);
Smart Quote (Slippage + Deadline)
const quote = await quoter.smart(WETH, {
slippage: 3,
deadline: 10,
});
console.log("Smart quote:", quote.quote);
π Estimate Swap
π Parameters β SwapParams["quoteInput"]
SwapParams["quoteInput"]
tokenIn
string
β Yes
Input token address
tokenOut
string
β Yes
Output token address
amountIn
string
β Yes
Input amount (human-readable format)
slippage
string
β Yes
Slippage percentage (e.g., "0.3"
for 0.3%)
fee
string
β Yes
Fee percentage (e.g., "0.2"
for 0.2%)
π Usage Example β Estimate Quote
const params = {
tokenIn: "0xTokenIn",
tokenOut: "0xTokenOut",
amountIn: "2",
slippage: "0.3",
fee: "0.2",
};
const estimate = await swapHelper.quote(params);
console.log("Quote estimate:", estimate);
Execute Swap
π Parameters β SwapParams["input"]
SwapParams["input"]
tokenIn
string
β Yes
Input token address
tokenOut
string
β Yes
Output token address
amountIn
string
β Yes
Input token amount
tx
object
β Yes
Transaction data returned from quote()
fee
string
β Yes
Fee percentage
feeAmountOut
string
β Optional
Fee amount to take from output token
feeReceiver
string
β Yes
Address to receive fee (use AddressZero
if none)
π Usage Example β Execute Swap
const quoteResponse = await swapHelper.quote(params);
const swapParams = {
tokenIn: "0xTokenIn",
tokenOut: "0xTokenOut",
amountIn: "2",
tx: {
data: quoteResponse.data,
to: quoteResponse.to,
value: quoteResponse.value,
},
feeAmountOut: quoteResponse.addons?.feeAmountOut,
fee: "0.2",
feeReceiver: ethers.constants.AddressZero,
};
const result = await swapHelper.swap(swapParams);
console.log("Swap result:", result);
π Full Example on GitHub
Check the full swap flow example:
Last updated