Skip to main content

Swapping

Owney supports single-chain and cross-chain swaps in both directions:

  • swapAndDeposit swaps a wallet asset into an agent-supported asset, then deposits it.
  • withdrawAndSwap withdraws an agent asset, then swaps it into the asset the user wants to receive.

The SDK measures what arrives and uses that amount for the next step. Supported source assets and deposit targets are available through getSwapTokens.

Discover assets and get a quote​

Use these methods to build an asset picker and preview a swap.

MethodPurpose
getSwapTokensGet preset source assets and supported deposit targets by chain.
getWalletBalancesGet swappable assets currently held by the connected wallet.
searchSwapTokensSearch additional source assets by name, symbol, or address.
getSwapQuotePreview the route and expected output.
checkSwapGasCheck whether the wallet has enough native gas for the quoted swap.

getSwapTokens​

getSwapTokens(): Promise<{ chains: SwapChainTokens[] }>

Each chain contains sources, an Owney preset of assets the user may pay with, and depositTargets, which Owney agents can receive. searchableSources indicates whether a broader source-asset catalogue is available. When greater than 0, applications can let users find other assets with searchSwapTokens().

getWalletBalances​

getWalletBalances(chainIds?: readonly number[]): Promise<{
tokens: Array<{
symbol: string;
address: `0x${string}`;
decimals: number;
chainId: number;
balance: string;
}>;
failedChainIds: number[];
}>

Returns non-zero wallet balances that can be used in a swap. A failed chain is reported in failedChainIds without discarding results from other chains.

searchSwapTokens​

searchSwapTokens(params: {
chainId?: number;
query?: string;
limit?: number;
direction?: "deposit" | "withdraw";
}): Promise<{ tokens: SwapTokenSearchResult[] }>

Provide a chainId with no query to list that chain's catalogue. Search results include the token address and chain; do not identify arbitrary tokens by symbol alone.

getSwapQuote​

getSwapQuote(params: SwapQuoteParams): Promise<SwapQuote>

Amounts are decimal strings in the token's smallest unit.

const quote = await owney.getSwapQuote({
from: { chainId: 8453, symbol: "ETH", amount: "10000000000000000" },
to: { chainId: 8453, symbol: "USDC" },
});

quote.dst.amount is the expected output. For deposits, use quote.dstAmountMin when checking the agent's minimum balance because it represents the lowest expected output.

checkSwapGas​

checkSwapGas(quote: SwapQuote): Promise<void>

Call this on a preview quote to warn the user before execution. swapAndDeposit and withdrawAndSwap also perform their required checks.

swapAndDeposit​

Swaps a wallet asset into an agent-supported asset and deposits the measured proceeds.

Requires wallet connection: Yes

swapAndDeposit(options: {
from: { chainId: number; symbol: string; amount: string };
to: { chainId: number; symbol: string };
agentId?: AgentId;
excludeAgentIds?: AgentId[];
slippage?: number;
onSwapProgress?: (stage: SwapStage) => void;
}): Promise<{
swap: { received: string; orderHash?: string; txHash?: string };
deposit: OwneyDepositResult | OwneyMultiDepositResult;
deposited: string;
operationId?: string;
}>

from.amount is always in the source token's smallest unit. If agentId is omitted, the deposit is split across eligible agents in the same way as deposit.

Advanced integrations can also use onBeforeDeposit, onDepositProgress, onBeforeDepositSubmission, assertCurrent, operationId, and batchId.

withdrawAndSwap​

Withdraws an agent asset, waits for it to arrive in the user's wallet, then swaps the measured amount into the requested asset.

Requires wallet connection: Yes

withdrawAndSwap(options: {
from: { chainId: number; symbol: string };
to: { chainId: number; symbol: string; decimals?: number };
amount?: string;
agentId?: AgentId;
slippage?: number;
onSwapProgress?: (stage: SwapStage) => void;
onOrderSubmitted?: (order: {
orderHash: string;
withdrawn: string;
expected: string;
}) => void;
arrivalTimeoutMs?: number;
}): Promise<{
withdraw: OwneyWithdrawResult | AgentWithdrawResult;
withdrawn: string;
swap: {
received: string;
spent?: string;
orderHash?: string;
txHash?: string;
};
operationId?: string;
}>

Unlike other fund methods, amount uses human units, for example "25" or "10.5". Omit it to withdraw the full balance. Advanced integrations can also use onWithdrawalProgress, assertCurrent, operationId, and batchId.

Progress stages​

onSwapProgress reports a SwapStage:

PhaseStages
Withdrawalwithdrawing, withdrawn, withdrawal-incomplete
Preparationquoting, wrapping, wrapped, approving, building, signing
Executionswapping, swapped, depositing
Recoveryswap-unknown, swap-partial, swap-not-started, refunding, refunded

Use these stages for progress UI. The method result or a recovery method determines the final outcome.

Resume an interrupted swap​

Call resumePendingSwaps() when your application starts. It continues swaps that were already submitted and does not require a connected wallet.

const results = await owney.resumePendingSwaps({
onStage: (orderHash, stage) => console.log(orderHash, stage),
});

After connecting the wallet, handle any remaining action:

for (const pending of owney.listPendingDeposits()) {
await owney.completePendingDeposit(pending.id);
}

const withdrawals = owney.listWithdrawSwaps();
  • completePendingDeposit(id) deposits funds from a swap that already completed. It does not swap them again.
  • recheckWithdrawalSwap(id) checks an incomplete withdrawal without withdrawing or swapping again.
  • continueWithdrawalSwap(id, callbacks?) continues a verified withdrawal into its swap.
  • dismissWithdrawSwap(orderHash) removes a settled result after it has been shown to the user.

The package also exports listPendingSwaps, listPendingDeposits, saveWithdrawSwap, updateWithdrawSwap, clearWithdrawSwap, listWithdrawSwaps, and subscribeWithdrawSwaps for integrations that manage their own recovery UI.

Error handling​

Swap errors describe where the funds are and whether the full operation can be retried.

OutcomeWhat to do
The quote or swap was not submittedCorrect the error and retry. Nothing moved.
A cross-chain order expired or was cancelledShow the reported order state while the funds settle or return.
SWAP_DEPOSIT_PENDINGThe target asset is in the user's wallet. Call completePendingDeposit; do not swap again.
WITHDRAW_ARRIVAL_TIMEOUT or WITHDRAW_SWAP_FAILEDThe withdrawal may already be in the user's wallet in the original asset. Check the returned details before continuing.

See Errors for individual error codes.