Initiating a Swap
This guide shows how to build and send a swap with the Mento SDK v3. The SDK returns transaction parameters (including an approval transaction if needed); you submit them with any viem wallet client.
Create the client and get a quote
Read-only operations (quotes, routes) only need a public client. For building swap transactions you still use Mento.create; the SDK returns transaction parameters that you send with your own wallet:
import { Mento, ChainId, deadlineFromMinutes } from '@mento-protocol/mento-sdk'
import { parseUnits } from 'viem'
const mento = await Mento.create(ChainId.CELO)
const USDm = '0x765DE816845861e75A25fCA122bb6898B8B1282a'
const CELO = '0x471EcE3750Da237f93B8E339c536989b8978a438'
const amountIn = parseUnits('100', 18)
const expectedOut = await mento.quotes.getAmountOut(USDm, CELO, amountIn)Build swap transaction (approval + swap)
buildSwapTransaction returns both an approval (if the token needs an allowance) and the swap transaction. All public APIs use object-based parameters:
const recipientAddress = '0x...' // address that receives the output tokens
const ownerAddress = '0x...' // address that holds tokenIn and signs the tx
const { approval, swap } = await mento.swap.buildSwapTransaction(
USDm,
CELO,
amountIn,
recipientAddress,
ownerAddress,
{
slippageTolerance: 0.5, // 0.5%
deadline: deadlineFromMinutes(5),
}
)slippageTolerance — percentage (e.g.
0.5= 0.5%). The SDK caps this (e.g. at 50%) to reduce sandwich risk.deadline — use
deadlineFromMinutes(minutes)or pass a Unix timestamp.
Send with a viem wallet client
Execute the approval first (if present), then the swap:
The SDK returns transaction request objects; you are responsible for sending them and handling receipts (e.g. waiting for confirmation).
Summary
Quote —
mento.quotes.getAmountOut(tokenIn, tokenOut, amountIn)(orgetAmountInfor exact amount out).Build —
mento.swap.buildSwapTransaction(tokenIn, tokenOut, amountIn, recipient, owner, { slippageTolerance, deadline }).Send — If
approvalis set, send it first; then sendswap.paramswith your viem wallet client.
Runnable examples: mento-sdk-examples.
Last updated