# Pure Swap Widget

This guide walks you through several easy steps to embed the swap widget in your website, app or extension.  The process should take only a few minutes — whether your site is already a decentralized application (dApp) or not. &#x20;

With the swap widget, your users can trade ERC20 tokens, using Fanbase Swap API, without leaving your site!  Wallet connectivity using Rainbowkit functionality is included.&#x20;

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FPqQc4msuvvS2OJe9asXK%2FPM.png?alt=media&#x26;token=6eb2add0-5780-4fba-9537-bab39db0f732" alt=""><figcaption></figcaption></figure>

Here are some cool things you can do with the Fanbase Pure Swap Widget:

* Build a Custom Website with Crypto Exchange Features
* Exchange or Trade Crypto Assets
* Convert to a Required Currency for NFT purchases such as WETH, ETH, USDT or USDC.&#x20;

## Installing the Pure Swap Widget SDK

```bash
npm install fanbase-pure-erc20-swap-widget
```

## Adding the Pure Swap Widget to your App - React.JS

```typescript
// App.tsx/jsx/ts/js (Or any page/component you want to plant widget)

import CryptoExchangeWidget from 'fanbase-pure-erc20-swap-widget';

function App() {
  return (
    <div className="App">
        <CryptoExchangeWidget appId="Your API Key" chainId="API key provided Chain ID" buyToken="Output token to display" />
    </div>
  );
}
export default App;
```

## Adding the Pure Swap Widget to your App - Next.JS

```javascript
// next.config.js

/** @type {import('next').NextConfig} */
module.exports = {
  reactStrictMode: false,
  transpilePackages: ["react-leaflet-cluster"],
  resolve: {
    extensions: ['.ts', '.tsx', '.js', '.jsx']
  },
  // In bleow configration, you should config fallback for unsupported Nodejs modules
  webpack(config, { isServer }) {
    if(!isServer) {
      config.resolve.fallback = { 
        net: false,
        tls: false,
        fs: false,
        zlib: false,
        stream: false,
        http: false,
        https: false,
        crypto: false,
        querystring: false
      };
    }
    
    return config
  }
}
```

```typescript
// index.tsx/jsx/ts/js (Or any page/component you want to plant widget)

import type { NextPage } from "next";
import styles from "../styles/Home.module.css";
import dynamic from "next/dynamic"

// Import widget with dynamic-import next tool disable ssr and safely use in next.js
const CryptoExchangeWidget = dynamic(() => import('fanbase-pure-erc20-swap-widget'), { ssr: false })

const Home: NextPage = () => {
    return (
        <div className={styles.container}>
            <main className={styles.main}>
                <CryptoExchangeWidget appId="Your API Key" chainId="API key provided Chain ID" buyToken="Output token to display" />
            </main>
        </div>
    );
};

export default Home;
```

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FbNmeq3IYDmLgQmyLPcwv%2Fimage.png?alt=media&#x26;token=642df916-09e7-4a68-8ecb-44d574e17773" alt=""><figcaption><p>Pure Swap Widget</p></figcaption></figure>

## Adding the Pure Swap Widget on the User Side

Press the wallet icon to connect your browser-injected wallet or wallet app using a QR code.

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FO81Gpo2dzF4EWBMMao2c%2Fimage.png?alt=media&#x26;token=3a3c7068-4201-467f-b9d4-71da0a095a00" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FJ8QfeD9Nz2H46PqF9dwZ%2Fimage.png?alt=media&#x26;token=10593878-64d9-4f24-8c55-25ce5e5d098d" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FhXoy15XfDd2C3CF2Q5i0%2Fimage.png?alt=media&#x26;token=88db11b4-bb27-443f-a70f-66ca40392436" alt="" width="494"><figcaption></figcaption></figure>

After your wallet is connected, press the SELECT button on the token input box. The token selection modal is now shown.

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FpR4PQSmjcMwQ7gAqT395%2Fimage.png?alt=media&#x26;token=764303a5-22d8-4eed-8f9d-ab434179f923" alt="" width="545"><figcaption><p>Token Selection Modal</p></figcaption></figure>

Now, you can fetch quotes and market information for the selected tokens. Before confirming a swap, you can check current token prices by fetching quotes.

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FAmfxVxlkkcBQAB3ykqXb%2Fimage.png?alt=media&#x26;token=f6a781ee-90dc-4f6e-8dc0-f928e74fd419" alt="" width="468"><figcaption><p>Fetching Quote</p></figcaption></figure>

You can also create your own liquidity pool or add liquidity before or after the swap action using the 'add liquidity' button.

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FFQUcfT29XCp0rYdmKI2P%2Fimage.png?alt=media&#x26;token=2bcb028e-ca9e-409d-8825-2fe0a5d3f33c" alt="" width="477"><figcaption></figcaption></figure>

Slippage tolerance is the maximum price difference a trader is willing to accept in order to execute their trade. This tolerance should be considered when trading so that traders can accurately weigh their rewards and risks. Generally, the higher the slippage tolerance, the greater the risk of losses.\
\
For the Fanbase Pure Swap Widget, default slippage tolerance is set to 0.5%. But, the widget UI allows you to set slippage tolerance anywhere from 0.1% to 5%.\
\
You can adjust slippage tolerance settings using the gear icon, beside the wallet connect button.

<figure><img src="https://1972835299-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fjekgdg0lFIHEmnCb2DJX%2Fuploads%2FARArDsCTqjPDhg5uYHZY%2Fimage.png?alt=media&#x26;token=7277b842-ac40-47e0-bf1a-955a0780f830" alt="" width="494"><figcaption></figcaption></figure>
