---
# System prepended metadata

title: Designing the SwapRoute

---

# Designing the SwapRoute
> Jira [ticket](https://onramper.atlassian.net/jira/software/c/projects/ONRMP/boards/10?modal=detail&selectedIssue=ONRMP-349)

Assumptions for MVP:
* Network is Mainnet Ethereum
* DEX is Uniswap
* Final transaction must happen on the front end (user must sign)
* User must have the **best** price
* User does not have ETH in wallet for gas (we must adjust input amount)

Based on the assumptions above we must say that the user will always first exchange fiat for native currency and then from native to the target erc20. Below are the steps, broken down in detail:

## Step 1 : Get ETH

Is this step we rely on OnRamper's existing vendors to provbide the best price. OnRamper's backend will determine what the output amount will be in fiat.

## Step 2: Estimate output amount of ERC20

Knowing what amount of ETH we have, we can send this off to uniswap to determine a minimum output amount of ERC20 we may receive.

We will use these packages on the backend:
* [@uniswap/sdk-core](https://github.com/Uniswap/sdk-core)
* [@uniswap/smart-order-router](https://github.com/Uniswap/smart-order-router)
* [ethers.js](https://github.com/ethers-io/ethers.js/)

The *smart order router* does several things for us, namely:

* Split routes which execute trades across multiple pools at once
* More powerful algorithm that considers a larger data set for larger trades and better prices
* Gas cost awareness ensures every added step is net positive for your trade.

> Read more about the smart order router [here](https://uniswap.org/blog/auto-router)

For the following example, we make some more assumptions(these values can change):
* trade deadline = 200
* slippage = 1%

Now, since we know the amount of ETH the user may receive for their fiat, we can request the smart order router to give us the best trade route.

```typescript
import { AlphaRouter } from '@uniswap/smart-order-router';
import {  ethers } from 'ethers';
import { CurrencyAmount, Percent, Token, TradeType } from '@uniswap/sdk-core';
import JSBI from 'jsbi';

const getTransactionData = async (
  inputCurrency: string = 'ETH',
  outputCurrency: string
) => {

    const provider = new ethers.providers.JsonRpcProvider(
      process.env.JSON_RPC_PROVIDER,
      Number(process.env.CHAIN_ID)
    );

    const router = new AlphaRouter({
      chainId: Number(process.env.CHAIN_ID),
      provider: provider,
    });

    console.log(router);

    const WETH = new Token(
      Number(process.env.CHAIN_ID),
      '0xc778417E063141139Fce010982780140Aa0cD5Ab',
      18,
      'WETH',
      'Wrapped Ether'
    );

    const DAI = new Token(
      Number(process.env.CHAIN_ID),
      '0xc7ad46e0b8a400bb3c915120d284aafba8fc4735',
      18,
      'DAI',
      'Dai Stablecoin'
    );

    const wethAmount = CurrencyAmount.fromRawAmount(WETH, JSBI.BigInt(1));

    const route = await router.route(wethAmount, DAI, TradeType.EXACT_INPUT, {
      recipient: process.env.PUBLIC_KEY as string,
      slippageTolerance: new Percent(5, 100),
      deadline: 3000,
    });
    
return route;
}

```

For reference, here is the return type of `router.route()`:

```typescript
interface SwapRoute  {
  quote: CurrencyAmount;
  quoteGasAdjusted: CurrencyAmount;
  estimatedGasUsed: BigNumber;
  estimatedGasUsedQuoteToken: CurrencyAmount;
  estimatedGasUsedUSD: CurrencyAmount;
  gasPriceWei: BigNumber;
  trade: Trade<Currency, Currency, TradeType>;
  route: RouteWithValidQuote[];
  blockNumber: BigNumber;
  methodParameters?: MethodParameters;
};

```

## Step 3: Return trade info to front end

The next step is to send this information to the front end so that we may:
* display the estimated output amount
* possibly show the details of the trade route (eg. ETH -> DAI -> GRT)
* send transaction details to the user's wallet so that they may execute the trade.

