# Swap

## Integrating Swap to website

Integrating the PiperX swap functionality to your website makes it easy for you to leverge the liquidity from PiperX without the needs of maintain your own pools. Some example applications including swapping staked $IP tokens to $IP tokens on the LSD website, allow users to buy NFT with stable coins (auto swap stable coins to $IP) on the NFT marketplace.&#x20;

### Steps to Integration

There are three steps in conducting a swap: 1) prepare input, 2) approve tokens, 3) conduct swap. You need to install PiperX sdk and import it to proceed.

```typescript
import { routingExactInput } from '@piperx/sdk/src/routing'
import { swap, routerTokenApproval } from '@piperx/sdk/src/core'
import { WIP_ADDRESS } from '@piperx/sdk/src/constant'
```

#### Prepare Input

You need the following inputs before you can conduct a swap

```typescript
token1_address: address, // if it is $IP native token, use WIP_ADDRESS
token2_address: address, // if it is $IP native token, use WIP_ADDRESS
amount1: bigint, // amount of token 1 you want to give
amount2Min: bigint, // minimal number of token 2 you want to receive
expire_time: bigint// expiration timestamp for a swap
signer: ethers.Signer. //
```

Note that, if you are swapping $IP token, you should pass in the PiperX wrapped $IP token address, because the $IP token is not a ERC-20 token. For example, if you are swapping from staked $IP token to $IP token, please use wrapped $IP address `WIP_ADDRESS`

#### Finding the best swap path

Depends on the swaps that you plan to do, we will use a universal router to find the best swap path, either through standard (V2) or concentrated (V3) path.

```typescript
let {bestRoute, max} = routingExactInput(token1_address, token2_address, amount1, signer)
```

#### Approve tokens

Then you need to approve the router to use your token before swap

```typescript
await routerTokenApproval(token1_address, amount1, bestRoute, signer)
```

#### Conduct swap

Depends on the swaps that you plan to do, we will call different functions from the PiperX V2 or V3 contract. One could use the follow universal swap API to deal with different swap cases.

```typescript
await swap(amount1, amount2Min, path, expire_time, signer)
```


---

# 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://docs.piperx.xyz/developer/sdks/swap.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.
