# Smart Router

The Sovryn ecosystem provides various asset conversion options with distinct implementation details and fees. We have developed the Smart Router package to enable cost-effective conversions between specific asset pairs without the need for custom code for each source.

It serves as a wrapper for smart contracts, streamlining interactions across all supported asset sources. The Smart Router intelligently searches for the best conversion rate among the supported sources and presents it to the user, enhancing convenience and usability.&#x20;

## Installation

```bash
yarn add @sovryn/sdk @sovryn/contracts @sovryn/ethers-provider ethers @ethersproject/providers @ethersproject/abstract-provider
```

## Usage

```typescript
import { SmartRouter, DEFAULT_SWAP_ROUTES } from '@sovryn/sdk';
import { getProvider } from '@sovryn/ethers-provider';

// Get ethers provider
const provider = getProvider();

// Create a new instance of the smart router, passing the provider and the swap routes to use
const smartRouter = new SmartRouter(provider, DEFAULT_SWAP_ROUTES);

// Get token addresses we want to swap
const xusdToken = '0xb5999795BE0EbB5bAb23144AA5FD6A02D080299F';
const sovToken = '0xEFc78fc7d48b64958315949279Ba181c2114ABBd';
const amount = 100;

// Get the best swap route for a token pair
// result contains the best route and the amount of tokens we will get
// { route: SwapRoute, quote: BigNumber }
const result = await smartRouter.getBestQuote(xusdToken, sovToken, amount);

// Check if we need to approve xusd token to be able to swap
// approveTxData will return the transaction data to be signed by the user
// In most cases it will only have `to` and `data` fields
const approveTxData = await result.route.approve(xusdToken, sovToken, amount)
if (approveTxData) {
  // ask user to sign the transaction with approveTxData as data
}

// Check if we need to permit xusd token to be able to swap
// permitTxData will return the permit data to be signed by the user
const permitTxData = await result.route.permit(xusdToken, sovToken, amount)
let signedPermit;
if (permitTxData) {
  // ask user to sign the transaction with permitTxData as data
  signedPermit = await signPermit(permitTxData);
}

// Swap tokens
const txData = await result.route.swap(xusdToken, sovToken, amount, {
  permit: signedPermit,
});

// Sign the transaction with the user wallet of your choosing and send it to the network
const tx = await signAndSend(txData);
```

## Links

Source:

{% embed url="<https://github.com/DistributedCollective/sovryn-dapp/tree/main/packages/sdk>" %}

NPM Package:

<https://www.npmjs.com/package/@sovryn/sdk>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://build.sovryn.com/builder-portal/sovryn-sdk/smart-router.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
