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

Direct Swaps

Get a Direct quote and transaction with one swap call, then submit the accepted result.

The same account supplies input tokens and submits the swap. Call arc.swap(params) once to receive the quote and a validated PreparedDirectSwap with status: "TRANSACTION_READY".

executor is required; payer and recipient default to executor. The payer must equal executor; recipient may differ. Set recipient, routing, fees, slippage and deadline in this one request.

Get and submit a Direct result

These complete helpers use Arc Testnet and viem. directSwap() only gets terms. Display prepared.expectAmountOut, prepared.tx.amountOutMin, recipient and fees, and let the user accept them before calling submitDirectSwap() with that same result. Bind the confirmation to the order and discard it if any input changes.

import { type Address, type PublicClient, type WalletClient } from "viem";
import { ArcClient, ARC_TESTNET_CONFIG, arcTestnet, type PreparedDirectSwap, type PreparedDelegatedSwap } from "@masterpeach/arc-aggregator-sdk";

/** Get the quote and transaction together; no approval, signature or submission. */
export async function directSwap(
  publicClient: PublicClient, wallet: WalletClient,
  from: Address, target: Address, amount: bigint,
) {
  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;
  const arc = new ArcClient(ARC_TESTNET_CONFIG, publicClient);
  const prepared = await arc.swap({ from, target, amount, executor, slippageBps: 50, deadlineSecs: 120 });
  if (prepared.amountIn !== amount) throw new Error("Prepared input differs from the order");
  return prepared;
}

/** Call after the user accepts this prepared result; reuse it without another swap request. */
export async function submitDirectSwap(
  publicClient: PublicClient, wallet: WalletClient, prepared: PreparedDirectSwap,
) {
  const account = wallet.account ?? (await wallet.getAddresses())[0];
  if (!account) throw new Error("Connect a wallet account first");
  const executor = typeof account === "string" ? account : account.address;
  if (executor.toLowerCase() !== prepared.executor.toLowerCase()) throw new Error("Wallet account changed");
  if (await wallet.getChainId() !== ARC_TESTNET_CONFIG.chainId) throw new Error("Switch to Arc Testnet");
  const arc = new ArcClient(ARC_TESTNET_CONFIG, publicClient);
  const approval = await arc.buildDirectApprovalRequest(prepared, executor);
  if (approval) {
    const hash = await wallet.sendTransaction({ ...approval.tx, account, chain: arcTestnet });
    const receipt = await publicClient.waitForTransactionReceipt({ hash });
    if (receipt.status !== "success") throw new Error(`Approval reverted: ${hash}`);
  }
  if (prepared.execution.plan.deadline <= BigInt(Math.floor(Date.now() / 1_000))) {
    throw new Error("Swap expired; get and confirm a new result");
  }
  await publicClient.call({ ...prepared.tx, account: executor });
  const hash = await wallet.sendTransaction({ ...prepared.tx, account, chain: arcTestnet });
  console.log("Transaction hash:", hash); // Save before waiting; resolve uncertain outcomes before retrying.
  const receipt = await publicClient.waitForTransactionReceipt({ hash });
  if (receipt.status !== "success") throw new Error(`Swap reverted: ${hash}`);
  return { hash, receipt };
}

The helper preserves bound LocalAccounts and browser wallet addresses. It rechecks the wallet account and chain before approval. buildDirectApprovalRequest() uses the exact input amount by default; { approval: "max" } requests unlimited allowance and should be disclosed. It returns undefined when allowance is sufficient. Some tokens require zeroing a nonzero allowance before approving a new value; the application must handle that token behavior.

Result and execution

swap() returns quote amounts, roles, tokens, IDs, approvalSpender, validated tx, decoded execution, constraints and raw API data. tx.amountOutMin is the signed net minimum, and execution.plan.deadline is the signed expiry. Preserve the entire result unchanged.

Simulation uses the accepted transaction and current RPC state after approval. Only a successful receipt completes the trade. An expired plan requires a new result and new confirmation, not modified calldata or a higher tolerance. Resolve an uncertain submission before retrying.

Last updated

Was this helpful?