# Swaps

## 🔄 Implementing a Swap on Zealous Swap

When building a smart contract that performs swaps through Zealous Swap, it’s essential to ensure your contract is using up-to-date pricing information to avoid slippage or price manipulation. This is especially important in an on-chain environment where transactions can be front-run.

***

### 🚀 Using the Router

The Zealous Swap Router provides convenient functions to swap between tokens, including KAS and other tokens. You’ll use different functions depending on whether you are providing the input or output amount.

Before initiating a swap, your contract must:

* Own the tokens being sold.
* Approve the router to spend those tokens.
* Define safety limits like minimum received or maximum paid.

***

### 📈 Quoting a Swap with `getAmountsOut`

Before executing a swap, it's recommended to **preview the expected output** using the `getAmountsOut` function. Zealous Swap enhances this quoting logic by supporting discounted swap rates for eligible users.

Use the following function to get a quote:

```solidity
function getAmountsOut(
    uint amountIn,
    address[] memory path,
    bool isDiscountEligible
) external view returns (uint[] memory amounts);
```

To determine if the user is eligible for a discount, query the `ZealousSwapDiscountManager` contract:

```solidity
bool isEligible = ZealousSwapDiscountManager.isDiscountEligible(user);
```

Then, use that flag when calling `getAmountsOut`:

```solidity
uint[] memory amounts = ZealousSwapRouter.getAmountsOut(
    amountIn,
    path,
    isEligible
);

uint amountOut = amounts[amounts.length - 1];
```

> ⚠️ **Note:** This is only a quote — actual output may vary due to price movement or slippage at the time of execution. Use this result to set `amountOutMin` safely.

This allows your contract (or frontend) to estimate how much of the output token it will receive, taking into account any discounts that may apply.

***

### 🧪 Example: Swapping 50 USDT for KAS

#### 1. Transfer USDT to Your Contract

Your smart contract needs to have custody of the tokens before performing a swap.

```solidity
uint amountIn = 50 * 10 ** USDT.decimals();
require(USDT.transferFrom(msg.sender, address(this), amountIn), "Transfer failed");
```

#### 2. Approve the Router to Spend USDT

Once the contract holds the tokens, you must approve the router.

<pre class="language-solidity"><code class="lang-solidity"><strong>require(USDT.approve(address(ZealousSwapRouter), amountIn), "Approval failed");
</strong></code></pre>

#### 3. Define the Swap Path and Execute the Swap

Now, perform the swap using the appropriate function. In this example, we are selling an exact amount of USDT for as much KAS as possible.

```solidity
address[] memory path = new address[](2);
path[0] = address(USDT);
path[1] = ZealousSwapRouter.WKAS(); // Wrapped KAS token address

ZealousSwapRouter.swapExactTokensForKAS(
    amountIn,
    amountOutMin,
    path,
    msg.sender,
    block.timestamp
);
```

***

### 🛡️ Safety Tips

* **Always use a trusted price source** to set `amountOutMin` or `amountInMax` in your swaps. This protects against front-running and price manipulation.
* For best protection, use external oracles or calculate expected output from off-chain price sources and pass those as parameters to the swap.

Even in highly liquid pools, adding price validation logic increases security and reduces the chance of slippage-related losses.

***

### ✅ Summary

To safely implement a token swap in your smart contract using Zealous Swap:

1. Transfer tokens to your contract.
2. Approve the Zealous Swap Router to spend those tokens.
3. Use the router's swap functions with safety parameters.

This ensures your contracts interact securely and reliably with the Zealous Swap AMM.
