> For the complete documentation index, see [llms.txt](https://docs.safle.com/safleid/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.safle.com/safleid/safle-wallet/safle-swaps.md).

# Safle Swaps

Safle enables decentralised swaps via the in house built swaps SDK. The SDK exposes set of functionalities required to execute a swap. Currently [1inch ](https://1inch.io/)DEX is integrated on safle swaps.

The SDK has a modular architecture where a dedicated controller for each DEX can be plugged in with just a few lines of code

### Step by step description

#### 1. Installation, import & initialisation

```jsx
npm install @getsafle/safle-swaps-v2

const Swap = require('@getsafle/safle-swaps-v2');
const swap = new Swap({ dex, rpcURL,chain });
```

* `dex` - The name of DEX using which you want to swap tokens.
* `rpcURL` - The rpc url to connect to a blockchain node.
* `chain` - The chain name of the blockchain. Supported for now - `ethereum`

#### 2. User Flow

1. User selects chain, wallet, source token, destination token and source token amount
   1. Get Supported Tokens: This method returns list of tokens supported on the dex to be populated.

      <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const supportedToken = await swap.getSupportedTokens();
      </code></pre>
   2. Get Exchange Rates: This method returns exchange rates for a token pair

      <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">await swap.getExchangeRates({ toContractAddress, toContractDecimal, fromContractAddress, fromContractDecimal, fromQuantity, slippageTolerance });
      </code></pre>
2. Exchange rates, estimated slippage and estimated gas fees is displayed to user
   1. Get Estimated Gas: This method returns estimated gas for the swap transaction

      <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">await swap.getEstimatedGas({ toContractAddress, toContractDecimal, fromContractAddress, fromContractDecimal, fromQuantity, slippageTolerance })
      </code></pre>
3. User confirms exchange rates
   1. Get Raw Transaction: This method returns the raw transaction

      <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">await swap.getRawTransaction({ walletAddress, toContractAddress, toContractDecimal, fromContractAddress, fromContractDecimal, fromQuantity, toQuantity, slippageTolerance })
      </code></pre>
4. User continues to sign transaction. Enters vault pin to proceed
   1. Application calls vault method to sign raw transaction and enters pin

      <pre class="language-javascript" data-overflow="wrap" data-full-width="true"><code class="lang-javascript">const signedTransaction = await vault.signTransaction(rawTx, pin, chain);
      </code></pre>

      Returns the signed transaction string

      * `chain` - The chain for which the transaction is to be signed.
      * `rawTx` - The raw transaction object to be signed
      * `pin` - The pin to access the vault's private functions.
5. Signed transaction broadcasted by client application

   ![](https://prod-files-secure.s3.us-west-2.amazonaws.com/11344230-d69e-4a21-96e6-a71b74945f9f/b8081dbe-b22b-4ea5-8e64-61e335089078/swaps.svg)

   <figure><img src="/files/93PrnPcoq5Uymyd00AJu" alt=""><figcaption></figcaption></figure>
