# UniversalRouter

&#x20;This contract is KLAYswap router using V2 and V3 pools. This contract can aggregate trades across V2 and V3 pools to give users access highly-flexible and personalised transactions.&#x20;

## Code

Github Link: (Will be updated after official launch)<br>

## Address

* Cypress : [0xe0fbB27D0E7F3a397A67a9d4864D4f4DD7cF8cB9](https://scope.klaytn.com/account/0xe0fbB27D0E7F3a397A67a9d4864D4f4DD7cF8cB9?tabId=internalTx)

## Events, Read-Only Functions, and State-Changing Functions

{% tabs %}
{% tab title="Parameter Structs" %}

## Parameter Structs

**SwapParams**

```solidity
struct SwapParams {
    address to;
    address[] path;
    address[] pool;
    uint deadline;
}
```

**Parameters:**

<table><thead><tr><th width="142.66666666666663">Name</th><th width="107">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>to</code></td><td>address</td><td>Address to receive tokens after swap</td></tr><tr><td><code>path</code></td><td>address[]</td><td>Swap paths (token address list)</td></tr><tr><td><code>pool</code></td><td>address[]</td><td><p>Pool paths in swap (list of pool addresses) </p><p>When using v2 pool: <code>address(0)</code> </p><p>When using v3 pool: v3 pool address</p></td></tr><tr><td><code>deadline</code></td><td>uint256</td><td>deadline</td></tr></tbody></table>
{% endtab %}

{% tab title="Read-Only Functions" %}

## Read-Only Functions

**getAmountsOut**

```solidity
function getAmountsOut(
    uint256 amountIn, 
    address[] memory path, 
    address[] memory pool
) public view returns (
    uint256[] memory amounts
)
```

**Parameters:**

<table><thead><tr><th width="142.66666666666663">Name</th><th width="107">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountIn</code></td><td>uint256</td><td>The amount of first token required to be paid</td></tr><tr><td><code>path</code></td><td>address[]</td><td>The path of the swap (list of token addresses)</td></tr><tr><td><code>pool</code></td><td>address[]</td><td>The pool path of the swap (list of pool addresses)<br>When using v2 pool : <code>address(0)</code><br>When using v3 pool : <code>address of v3 pool</code></td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

**getAmountsIn**

```solidity
function getAmountsIn(
    uint256 amountOut, 
    address[] memory path, 
    address[] memory pool
) public view returns (
    uint256[] memory amounts
)
```

**Parameters:**

<table><thead><tr><th width="147.66666666666666">Name</th><th width="107">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountOut</code></td><td>uint256</td><td>The amount of the last token that would be received</td></tr><tr><td><code>path</code></td><td>address[]</td><td>The token path of the swap (list of token addresses)</td></tr><tr><td><code>pool</code></td><td>address[]</td><td>The pool path of the swap (list of pool addresses)<br>When using v2 pool : <code>address(0)</code><br>When using v3 pool : <code>address of v3 pool</code></td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>
{% endtab %}

{% tab title="State-Changing Functions" %}

## **State-Changing Functions**

**swapExactTokensForTokens**

```solidity
function swapExactTokensForTokens(
    uint256 amountIn, 
    uint256 amountOutMin, 
    struct SwapParams calldata p
) external returns (uint256[] memory amounts)
```

Swaps an exact amount of input tokens for as many output tokens as possible, along the route determined by the path.

**Parameters:**

<table><thead><tr><th width="142.66666666666663">Name</th><th width="142">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountIn</code></td><td>uint256</td><td>The amount of input tokens to paid</td></tr><tr><td><code>amountOutMin</code></td><td>uint256</td><td>The minimum amount of output tokens that must be received for the transaction not to revert</td></tr><tr><td><code>p</code></td><td><code>struct SwapParams</code> </td><td><code>struct SwapParams</code> for swap</td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

**swapTokensForExactTokens**

```solidity
function swapTokensForExactTokens(
    uint256 amountOut, 
    uint256 amountInMax, 
    struct SwapParams calldata p
) external returns (uint256[] memory amounts)
```

Receive an exact amount of output tokens for as few input tokens as possible, along the route determined by the path.

**Parameters:**

<table><thead><tr><th width="169.66666666666663">Name</th><th width="142">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountOut</code></td><td>uint256</td><td>The amount of output tokens to receive</td></tr><tr><td><code>amountInMax</code></td><td>uint256</td><td>The maximum amount of input tokens that can be required before the transaction reverts</td></tr><tr><td><code>p</code></td><td><code>struct SwapParams</code> </td><td><code>struct SwapParams</code> for swap</td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

**swapExactETHForTokens**

```solidity
function swapExactETHForTokens(
    uint256 amountOutMin, 
    struct SwapParams calldata p
) external payable returns (uint256[] memory amounts)
```

Swaps an exact amount of ETH for as many output tokens as possible, along the route determined by the path

**Parameters:**

<table><thead><tr><th width="173.66666666666663">Name</th><th width="142">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>msg.value</code></td><td>uint256</td><td>The amount of ETH to be paid</td></tr><tr><td><code>amountOutMin</code></td><td>uint256</td><td>The minimum amount of output tokens that must be received for the transaction not to revert</td></tr><tr><td><code>p</code></td><td><code>struct SwapParams</code> </td><td><code>struct SwapParams</code> for swap</td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

**swapTokensForExactETH**

```solidity
function swapTokensForExactETH(
    uint256 amountOut, 
    uint256 amountInMax, 
    struct SwapParams calldata p
) external returns (uint256[] memory amounts)
```

Receive an exact amount of ETH for as few input tokens as possible, along the route determined by the path.

**Parameters:**

<table><thead><tr><th width="173.66666666666663">Name</th><th width="142">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountOut</code></td><td>uint256</td><td>The amount of ETH to receive</td></tr><tr><td><code>amountInMax</code></td><td>uint256</td><td>The maximum amount of input tokens that can be required before the transaction reverts</td></tr><tr><td><code>p</code></td><td><code>struct SwapParams</code> </td><td><code>struct SwapParams</code> for swap</td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

**swapExactTokensForETH**

```solidity
function swapExactTokensForETH(
    uint256 amountIn, 
    uint256 amountOutMin, 
    struct SwapParams calldata p
) external returns (uint256[] memory amounts)
```

Swaps an exact amount of tokens for as much ETH as possible, along the route determined by the path.

**Parameters:**

<table><thead><tr><th width="173.66666666666663">Name</th><th width="142">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountIn</code></td><td>uint256</td><td>The amount of input tokens to be paid</td></tr><tr><td><code>amountOutMin</code></td><td>uint256</td><td>The minimum amount of output tokens that must be received for the transaction not to revert</td></tr><tr><td><code>p</code></td><td><code>struct SwapParams</code> </td><td><code>struct SwapParams</code> for swap</td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

**swapETHForExactTokens**

```solidity
function swapETHForExactTokens(
    uint256 amountOut,
    struct SwapParams calldata p
) external payable returns (uint256[] memory amounts)
```

Swaps an exact amount of tokens for as much ETH as possible, along the route determined by the path.

**Parameters:**

<table><thead><tr><th width="173.66666666666663">Name</th><th width="142">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amountOut</code></td><td>uint256</td><td>The amount of tokens to receive</td></tr><tr><td><code>msg.value</code> (amountInMax)</td><td>uint256</td><td>The maximum amount of ETH that can be required before the transaction revert</td></tr><tr><td><code>p</code></td><td><code>struct SwapParams</code> </td><td><code>struct SwapParams</code> for swap</td></tr></tbody></table>

**Return Values:**

<table><thead><tr><th width="129.66666666666666">Name</th><th width="115">Type</th><th>Description</th></tr></thead><tbody><tr><td><code>amounts</code></td><td>uint256[]</td><td>The input token amount and all subsequent output token amounts.</td></tr></tbody></table>

{% endtab %}
{% endtabs %}


---

# 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.klayswap.com/developers/contract/v3/universalrouter.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.
