> For the complete documentation index, see [llms.txt](https://docs.eldorito.club/dkit-by-eldorito/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.eldorito.club/dkit-by-eldorito/swap.md).

# Swap

## **Step 1: Create a `QuoteParams` Object**

The `QuoteParams` object defines the details of a swap between supported blockchains. It includes all the necessary information to find the best routing options.

Here’s the interface structure for `QuoteParams`:

```typescript
interface QuoteParams {
    affiliateBasisPoints?: string;
    buyAsset: string;
    recipientAddress?: string;
    sellAmount: string;
    sellAsset: string;
    senderAddress?: string;
    slippage: string;
}
```

{% hint style="info" %}
The `buyAsset` & `sellAsset` must be of the format `'chain.ticker'` For example, `BTC.BTC`.
{% endhint %}

{% hint style="info" %}
The recipientAddress **must** be a valid address for the `buyAsset` blockchain. Similarly the senderAddress must be a valid address for the `sellAsset`.
{% endhint %}

## Step 2. Call getQuote from doritoKit API package <a href="#step-2.-call-getquote-from-swapkitapi-package" id="step-2.-call-getquote-from-swapkitapi-package"></a>

After creating the `quoteParams` object, you can pass it to the `getQuote` function of the `dKitApi` class.

```typescript
// or directly from @doritokit/api
import { SwapKitApi } from '@doritokit/sdk'

const quoteParams = {
    sellAsset: 'BTC.BTC',
    sellAmount: '1',
    buyAsset: 'ETH.ETH',
    senderAddress: '...', // A valid Ethereum address
    recipientAddress: '...', // A valid Bitcoin address
    slippage: '3',
};

const { routes } = await SwapKitApi.getQuote(quoteParams);
```

## Step 3: Choose fee option multiplier, route & execute swap <a href="#step-3-choose-fee-option-multiplier-route-and-execute-swap" id="step-3-choose-fee-option-multiplier-route-and-execute-swap"></a>

```typescript
import { FeeOption } from '@doritokit/sdk';

const bestRoute = routes.find(({ optimal }) => optimal)

const txHash = await dkClient.swap({
    route: bestRoute,
    recipient: '...',
    feeOptionKey: FeeOption.Fast
    // FeeOption multiplies current base fee by:
    // Average => 1.2 
    // Fast => 1.5
    // Fastest => 2
});

// Returns explorer url like etherscan, viewblock, etc.
const explorerUrl = skClient.getExplorerTxUrl(inputChain, txHash)
```

The `skClient` used above assumes a wallet has been connected as described in Set up the SDK.

{% hint style="warning" %}
Executing ERC20 Swaps with tokens on EVM chains need approval spending. Check if asset has been approved with built in methods.
{% endhint %}

```typescript
import { AmountWithBaseDenom, AssetEntity } from '@doritokit/sdk'

const isApproved = skClient.isAssetApprovedForContract(
  asset, // AssetEntity
  contractAddress: selectedRoute.contract
  amount, // AmountWithBaseDenom => amount to check that's possible to spent, default MaxInt256
)

const approveTx = skClient.approveAssetForContract(
  asset, // AssetEntity
  contractAddress: selectedRoute.contract
  amount, // AmountWithBaseDenom => amount approved to spent, default MaxInt256
)
```
