Install Viem
Install Viem in your application if it is not already a dependency.
pnpm add viembun add viemnpm i viemyarn add viemPrerequisites
This example assumes your app already has:
- a Viem
publicClientconnected to the token’s chain - a Viem
walletClientconnected to the trader’s wallet - the Clank token address you want to trade
Quote and execute a buy
Use the copy button on this block and replace token with the Clank token
address. The example resolves its curve through the canonical factory before
quoting the buy. No Clank package is required.
import { parseAbi, parseEther, type Address } from "viem";
// Trade inputs — edit these values.
const tradeInputs = {
token: "0x..." as Address, // token you get after swap
amountIn: parseEther("0.0001"), // buy token with 0.0001 ETH
slippageBps: 100n, // 1%
} as const;
const CLANK_FACTORY = "0x798daAa0707C1e538bB5Acf0867Ac0e1A84cccF2" as const;
const [trader] = await walletClient.getAddresses();
const { token, amountIn, slippageBps } = tradeInputs;
const factoryAbi = parseAbi([
"function getLaunchedToken(address token) view returns ((address token,address curve,address deployer,address creatorFeeRecipient,address pairToken,uint256 graduationThreshold,uint24 poolFee,int24 tickSpacing,uint16 creatorTaxBps,bool buybackEnabled,uint8 phase,uint256 sweptQuote,uint256 sweptTokens,uint256 sweptAt,bool exists) launched)",
]);
const curveAbi = parseAbi([
"function state() view returns (uint8)",
"function quoteBuyFor(address recipient,uint256 grossQuoteIn) view returns (uint256 grossQuoteUsed,uint256 netQuoteIn,uint256 fee,uint256 tokensOut,uint256 refund)",
"function buy(uint256 quoteIn,uint256 minTokensOut,address recipient) payable returns (uint256 tokensOut)",
]);
const launch = await publicClient.readContract({
address: CLANK_FACTORY,
abi: factoryAbi,
functionName: "getLaunchedToken",
args: [token],
});
if (!launch.exists) throw new Error("Not a Clank token");
const curve = launch.curve;
const state = await publicClient.readContract({
address: curve,
abi: curveAbi,
functionName: "state",
});
if (state !== 0) throw new Error("Bonding curve is closed");
const [, , , tokensOut] = await publicClient.readContract({
address: curve,
abi: curveAbi,
functionName: "quoteBuyFor",
args: [trader, amountIn],
});
const minimumTokensOut = (tokensOut * (10_000n - slippageBps)) / 10_000n;
const { request } = await publicClient.simulateContract({
account: trader,
address: curve,
abi: curveAbi,
functionName: "buy",
args: [amountIn, minimumTokensOut, trader],
value: amountIn,
});
const hash = await walletClient.writeContract(request);
const receipt = await publicClient.waitForTransactionReceipt({ hash });getLaunchedToken returns the curve recorded for the token by the current
Clank factory on Robinhood Chain. quoteBuyFor then includes recipient-specific
launch tax in the quote. The example applies 1% slippage protection, simulates
the exact transaction, and only after a successful simulation opens the wallet
confirmation.
This direct call applies while the token is trading on its bonding curve. After graduation, route the trade through the token’s locked Uniswap V4 pool instead.