# executeSwap

## Description

`The executeSwap` function enables token swaps between two chains using a third-party API.&#x20;

It takes in the user's private key, source/destination chain, tokens, amount, receives address and slippage as input and returns the transaction receipt for the swap.

## Parameters

<table><thead><tr><th>Name</th><th width="122">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>privateKey</code></td><td>string</td><td>The private key of the user's wallet for the source chain</td></tr><tr><td><code>srcChain</code></td><td>string</td><td>The name of the source blockchain network (e.g., ETH, AVA, BSC, Polygon, Klaytn), and all specified networks are on the mainnet.</td></tr><tr><td><code>destChain</code></td><td>string</td><td>The name of the destination blockchain network (e.g., ETH, AVA, BSC, Polygon, Klaytn), and all specified networks are on the mainnet.</td></tr><tr><td><code>fromToken</code></td><td>string</td><td>The address of the token to be swapped on the source blockchain</td></tr><tr><td><code>toToken</code></td><td>string</td><td>The address of the token to receive on the destination blockchain</td></tr><tr><td><code>amount</code></td><td>string</td><td>The amount of the token to be swapped (in the smallest unit)</td></tr><tr><td><code>receiveAddress</code></td><td>string</td><td>The address to receive the swapped tokens on the destination chain</td></tr><tr><td><code>slippage</code></td><td>string</td><td>The acceptable slippage rate (in percentage)</td></tr></tbody></table>

## Response

| Property          | Type   | Description                                                 |
| ----------------- | ------ | ----------------------------------------------------------- |
| `Promise<Object>` | object | The receipt of the transaction after the token swap is done |

## Example Request and Response

### Prerequisites

Before making requests with NO.AI SDK, you must have it installed.

You can install NO.AI SDK using either **`npm`** or **`yarn`**. Use the following commands to install NO.AI SDK:

```jsx
npm install @nest25/xy-finance-lib
OR
yarn add @nest25/xy-finance-lib
```

### Request

Here is an example of how to make a `executeSwap` request using the NO.AI SDK:

```javascript
// import Nest SDK library
const { xySwap } = require('@nest25/xy-finance-lib');

// create xySwap instance
const swap = new xySwap();

async function main() {
  // execute swap
  const response = await swap.executeSwap(
    'your-private-key',
    'klaytn',
    'klaytn',
    '0xCF87f94fD8F6B6f0b479771F10dF672f99eADa63',
    '0x5c74070FDeA071359b86082bd9f9b3dEaafbe32b',
    '0.1',
    '0x9f658964c3658257545b13483e2c9a59b6faa98a',
    1,
  );

  console.log(`Contract hash is: ${response.hash}`);
}
// run main function
main();

```

### Response

```sh
Contract hash is: 0x08a2be0e4c0019c34f9705b07cb4f5991d7c8710964b0baca32fd378281ea1a8
```

## Use Cases

* **Cross-chain token swaps:** The `executeSwap` function can be used by decentralized exchanges to facilitate cross-chain token swaps, allowing users to trade their tokens across different blockchain networks.
* **Liquidity providers:** The function can also be used by liquidity providers to move their funds across different chains and earn higher yields.
