For the complete documentation index, see llms.txt. This page is also available as Markdown.

Quickstart

Get a quote and transaction with one swap call, then confirm and submit it.

Call arc.swap() once to get the quote and validated Direct transaction. This example uses your application's existing viem wallet and publicClient on Arc Testnet (5042002). For Mainnet configuration, see Client Configuration.

Install and initialize

npm install @masterpeach/arc-aggregator-sdk@1.0.0 viem@^2.33.3
import { formatUnits } from "viem";
import { ArcClient, ARC_TESTNET_CONFIG, arcTestnet } from "@masterpeach/arc-aggregator-sdk";

const arc = new ArcClient(ARC_TESTNET_CONFIG, publicClient);
const account = wallet.account ?? (await wallet.getAddresses())[0];
if (!account) throw new Error("Connect a wallet account first");
if (await wallet.getChainId() !== ARC_TESTNET_CONFIG.chainId) throw new Error("Switch to Arc Testnet");
const executor = typeof account === "string" ? account : account.address;

Get the quote and transaction

Use tokenIn, tokenOut, positive amount and outputDecimals from your order. Amounts are bigint values in input-token base units. Arc swaps use ERC-20 addresses; USDC's ERC-20 interface has 6 decimals.

const prepared = await arc.swap({
  from: tokenIn, target: tokenOut, amount, executor,
  slippageBps: 50, // 0.5%; use the user's chosen tolerance
  deadlineSecs: 120,
});
if (prepared.amountIn !== amount) throw new Error("Prepared input differs from the order");
console.log("Expected output:", formatUnits(prepared.expectAmountOut, outputDecimals));
console.log("Minimum received:", formatUnits(prepared.tx.amountOutMin, outputDecimals));

swap() makes one Aggregator request and returns both quote amounts and transaction data. It does not approve tokens, request a wallet signature or broadcast the swap. Display the returned expected output, minimum received, recipient and fees, then wait for the user to accept these terms. Logging them alone is not confirmation.

Confirm and submit

Submit the same accepted prepared result. Do not call swap() again in the confirmation handler. The Direct approval helper grants the exact input amount by default; it returns undefined when allowance is sufficient.

Refresh and retry

Discard the result and its confirmation if the tokens, amount, accounts, recipient, network, routing, fees or slippage change. If it expires during approval, call swap() again, display its new terms and obtain new acceptance. Never modify signed fields or silently raise slippage.

Cancel or ignore stale requests and disable duplicate submission while a trade is active. If submission times out, resolve the saved hash or wallet nonce before retrying. A successful receipt completes the swap.

See Direct Swaps for complete helpers, Delegated Swaps for prepare → signature → build, and Transaction Safety.

Last updated

Was this helpful?