npm package: @keeper-wallet/swap-client
Description: Get the best asset swap prices from [Keeper Wallet](https://keeper-wallet.app/) swap services aggregator.
Latest version: 0.3.0

# @keeper-wallet/swap-client

Get the best asset swap prices from [Keeper Wallet](https://keeper-wallet.app/) swap services aggregator.

## Installation

Install using npm:

```sh
npm install @keeper-wallet/swap-client
```

or yarn:

```sh
yarn add @keeper-wallet/swap-client
```

## Usage

First, import `SwapClient` (to setup connection) and `SwapClientErrorCode` (to
understand what happened in case of an error):

```javascript
import { SwapClient, SwapClientErrorCode } from '@keeper-wallet/swap-client';
```

Create an instance of `SwapClient` class:

```javascript
const swapClient = new SwapClient();
```

_Note: If you want to receive data for multiple asset pairs simultaneously, you'll need separate instances for each pair._

Subscribe to receive data about possible swaps:

```javascript
// Call unsubscribe when you no longer interested in receiving swaps.
const unsubscribe = swapClient.subscribe({
  // called in case we can't connect to the service
  onError: () => {
    console.error('Could not connect to the swap service');
  },
  // called with updated data about possible swaps
  onData: (
    // vendor name as a string like 'keeper', 'swopfi' or 'puzzle'
    vendor,
    // see below
    response,
  ) => {
    // type equals 'error' if vendor can't perform swap with given parameters
    if (response.type === 'error') {
      if (response.code === SwapClientErrorCode.INVALID_ASSET_PAIR) {
        console.error(`${vendor} cannot perform swap with given asset pair`);
      } else if (response.code === SwapClientErrorCode.UNAVAILABLE) {
        console.error(`${vendor} service is not available at the moment`);
      } else if (response.code === SwapClientErrorCode.UNEXPECTED) {
        console.error(`Something really bad happened with ${vendor}`);
      }
    } else if (response.type === 'data') {
      // we've got data to perform swap!!!

      // priceImpact is a number ranging from 0 to 1, which represents a ratio
      // by which asset price will change after swap
      console.log(response.priceImpact);

      // amountCoins is a string (because JavaScript number can't fit 64-bit
      // integer) showing how much coins we'll receive
      console.log(response.amountCoins);

      // minimumReceivedCoins is also a string (for the same reason)
      // showing minimum amount of coins which user would received according to
      // passed slippage tolerance
      console.log(response.minimumReceivedCoins);

      // contains swapParams for which we got the response
      console.log(response.swapParams);

      // tx contains prepared fields of invoke script transaction, which you can
      // sign and broadcast to actually perform it
      console.log(response.tx);
    }
  },
});
```

Finally, set parameters for the swap you would like to perform:

```javascript
swapClient.setSwapParams({
  // amount (in coins) which you're going to swap
  amountCoins: '100000000', // 100 USDN (it has 6 decimal places)
  // asset you'd like to swap (USDN in this case)
  fromAssetId: 'DG2xFkPdDwKUoBkzGAhQtLpSGzfXLiCYPEzeKH2Ad24p',
  // if price goes down by more than this ratio, swap will not be performed
  slippageTolerance: 0.1,
  // asset which you would like to receive
  toAssetId: 'WAVES',
});
```

_Note: to change parameters (e.g. to change assets and/or amount) just call it again. The last call determines parameters with which you want to swap._

And once you don't want to receive swaps anymore, call `unsubscribe` returned from `subscribe` method above:

```javascript
unsubscribe();
```

And if there are no subscribers left, connection will be closed after 3 seconds.