Swapping
Owney supports single-chain and cross-chain swaps in both directions:
swapAndDepositswaps a wallet asset into an agent-supported asset, then deposits it.withdrawAndSwapwithdraws 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.
| Method | Purpose |
|---|---|
getSwapTokens | Get preset source assets and supported deposit targets by chain. |
getWalletBalances | Get swappable assets currently held by the connected wallet. |
searchSwapTokens | Search additional source assets by name, symbol, or address. |
getSwapQuote | Preview the route and expected output. |
checkSwapGas | Check 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
- Signature
- Example
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;
}>
const result = await owney.swapAndDeposit({
from: {
chainId: 1,
symbol: "USDT",
amount: "100000000", // 100 USDT with 6 decimals
},
to: { chainId: 8453, symbol: "USDC" },
onSwapProgress: (stage) => console.log(stage),
});
console.log(result.deposited);
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
- Signature
- Example
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;
}>
const result = await owney.withdrawAndSwap({
from: { chainId: 8453, symbol: "USDC" },
to: { chainId: 1, symbol: "ETH", decimals: 18 },
amount: "25",
onSwapProgress: (stage) => console.log(stage),
});
console.log(result.swap.received);
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:
| Phase | Stages |
|---|---|
| Withdrawal | withdrawing, withdrawn, withdrawal-incomplete |
| Preparation | quoting, wrapping, wrapped, approving, building, signing |
| Execution | swapping, swapped, depositing |
| Recovery | swap-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.
| Outcome | What to do |
|---|---|
| The quote or swap was not submitted | Correct the error and retry. Nothing moved. |
| A cross-chain order expired or was cancelled | Show the reported order state while the funds settle or return. |
SWAP_DEPOSIT_PENDING | The target asset is in the user's wallet. Call completePendingDeposit; do not swap again. |
WITHDRAW_ARRIVAL_TIMEOUT or WITHDRAW_SWAP_FAILED | The 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.