Skip to main content

Executing Swaps

After you create and inspect a quote with swapper.swap(...), the usual next step is swap.execute(...).

execute() is the high-level execution path used by the SDK. It is state-aware, so it resumes from the current swap state, performs the next on-chain or off-chain action, and then waits for automatic settlement when the protocol supports it. Generally the execute() function follows this syntax:

import {isRefundableSwap, isClaimableSwap} from "@atomiqlabs/sdk";

const result = await swap.execute(/* source signer or wallet */, /* optional callbacks */, /* optional options */);

if (result === false) {
// In case of Smart chain → BTC/Lightning swaps:
// - the swap has failed and the user can refund with swap.refund(...)
if(isRefundableSwap(swap)) await swap.refund(/* source chain signer or wallet - always a smart chain signer */);

// In case of BTC/Lightning → Smart chain swaps:
// - the swap wasn't settled automatically, call swap.claim(...) to settle the swap manually with the destination chain signer.
if(isClaimableSwap(swap)) await swap.claim(/* destination chain signer or wallet - always a smart chain signer */);
}

The high-level flow is shared across swap types, but the execute(...) signature is not identical everywhere, in general the swaps:

  • take a signer/wallet on the source chain
  • return a boolean indicating whether the execution was successful or not (in this case the user should either refund() on the source chain or claim() on the destination chain manually).
tip

If you need low-level control instead of execute() or want to get raw transactions and PSBTs that you sign and send manually, see the respective swap pages.

Swap Types

An overview of execute() function arguments for various swap types

info

Swaps from Bitcoin on-chain (L1) and Lightning (L2) to Solana use legacy swap protocols and are described below in the Legacy Swaps section.

Swap classes: ToBTCSwap and ToBTCLNSwap

Same flow is shared for both, Smart chain → BTC & Smart chain → Lightning swaps.

Typical usage of the execute() function:

// Returns whether the swap was successfully executed by the LP, if not the user can refund
const swapSuccess = await swap.execute(srcSigner, callbacks?, options?);
// Refund on failure, this returns the funds back to the signer's wallet
if (!swapSuccess) await swap.refund(srcSigner);

Type of the signer is dependent on the source network:

Callbacks

NameTypeDescription
onSourceTransactionSent(sourceTxId: string) => voidFired when the source smart-chain commit transaction is sent.
onSourceTransactionConfirmed(sourceTxId: string) => voidFired when the source smart-chain transaction is confirmed.
onSwapSettled(destinationTxId: string) => voidFired when the BTC or Lightning payment is sent.

Options

NameTypeDescription
abortSignalAbortSignalCancels execution or waiting.
paymentCheckIntervalSecondsnumberPolling interval used while waiting for the LP to process the payment.
maxWaitTillSwapProcessedSecondsnumberMaximum time to wait for the LP to process the swap. Defaults to 120. If the LP doesn't process the swap an Error is thrown, you can then get the swap expiration time with swap.getExpiry() after which you can refund unilaterally.
info

For more details about the swap, swap states, going through swap steps manually or getting the transactions to sign and send outside the SDK refer to the respective Smart Chain → Bitcoin swap.

Legacy Swaps

These swaps use the legacy protocol in the Bitcoin/Lightning → Solana direction, and require a destination Solana wallet to sign and send transactions.

Swap classes: FromBTCSwap

Typical usage of the execute() function:

// Returns whether the swap was successfully executed automatically, if not the user can claim manually
const automaticallySettled = await swap.execute(dstSigner, bitcoinWallet?, callbacks?, options?);
// Claim on failure, this settles the funds to the destination chain signer
if (!automaticallySettled) await swap.claim(dstSigner);

Type of the destination dstSigner signer: SolanaSigner, or native @coral-xyz/anchor Wallet

Type of the source bitcoin wallet can be:

  • A class extending the abstract IBitcoinWallet, e.g. the built-in SingleAddressBitcoinWallet
  • A simple object implementing MinimalBitcoinWalletInterfaceWithSigner type:
    const bitcoinWallet: MinimalBitcoinWalletInterfaceWithSigner = {
    address: "bc1p..", // Wallet address
    publicKey: "03030cbd...", // Wallet public key
    signPsbt: (psbtToSign: {psbt: Transaction, psbtHex: string, psbtBase64: string}, signInputs: number[]) => {
    ... // Sign the inputs specified by `signInputs` of the provided PSBT here.
    return signedPsbt; // Can be in base64 or hex serialized format, or `@scure/bitcoin-js` Transaction object.
    }
    }

Callbacks

NameTypeDescription
onDestinationCommitSent(destinationCommitTxId: string) => voidFired when the destination-chain commit/open-address transaction is sent.
onSourceTransactionSent(sourceTxId: string) => voidFired when the BTC funding transaction is sent.
onSourceTransactionConfirmationStatus(sourceTxId?: string, confirmations?: number, targetConfirations?: number, etaMs?: number) => voidProgress callback while waiting for the BTC transaction to confirm.
onSourceTransactionConfirmed(sourceTxId: string) => voidFired once the BTC transaction is confirmed.
onSwapSettled(destinationTxId: string) => voidFired when the destination-chain settlement is completed automatically.

Options

NameTypeDescription
feeRatenumberBitcoin fee rate to use when the SDK sends the BTC transaction.
abortSignalAbortSignalCancels execution or waiting.
btcTxCheckIntervalSecondsnumberPolling interval while waiting for the BTC transaction to confirm.
maxWaitTillAutomaticSettlementSecondsnumberMaximum time to wait for automatic destination settlement. Defaults to 60.
info

For more details about the swap, swap states, going through swap steps manually or getting the transactions to sign and send outside the SDK refer to the respective Bitcoin → Smart Chain swap.

Summary of Failure Modes and Fallbacks

When execute() returns false and does not fully finish the swap automatically:

Swap familyFailure ModeManual fallback
Smart chain → BTC L1 / LightningLP didn't process the swapswap.refund(sourceSigner)
BTC L1 → Starknet / EVMSwap wasn't settled automaticallyswap.claim(destinationSigner)
Lightning → Starknet / EVMSwap wasn't settled automaticallyswap.claim(destinationSigner)

Legacy Swaps

Swap familyFailure ModeManual fallback
BTC L1 → Solana (legacy)Swap wasn't settled automaticallyswap.claim(destinationSigner)
Lightning → Solana (legacy)None-

Next Steps

Detailed Swap Type Descriptions

Explain each swap type in-depth, go through manually processing the swap & explain various swap states.

Swap Types →


Utilities

SDK-exposed utilities allow you to parse addresses (including LNURL links), fetch token balances, list supported tokens and swap types.

Utilities →


Swap Management

Retrieve & query past swaps, handle refundable and claimable swaps.

Swap Management →