> For the complete documentation index, see [llms.txt](https://docs.ston.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ston.fi/developer-section/dex/smart-contracts/v2.md).

# DEX v2 contracts

STON.fi v2 smart contract API - advanced contracts with vault architecture and gas optimization

For SDK transaction-building examples, see [SDK integration](/developer-section/dex/sdk.md).

## Overview

The section contains separate documents for each smart contract used in AMM:

* [Pool](/developer-section/dex/smart-contracts/v2/pool.md)
* [Router](/developer-section/dex/smart-contracts/v2/router.md)
* [LpAccount](/developer-section/dex/smart-contracts/v2/lpaccount.md)
* [LpWallet](/developer-section/dex/smart-contracts/v2/lpwallet.md)
* [Vault](/developer-section/dex/smart-contracts/v2/vault.md)

## Example schemes

Example schemes can be found here:

* [Swap examples](/developer-section/dex/smart-contracts/v2/examples/swap.md)
* [Lp provide examples](/developer-section/dex/smart-contracts/v2/examples/lp-provide.md)
* [Vault examples](/developer-section/dex/smart-contracts/v2/examples/vault.md)

## Op code list

A table with DEX v2 op codes:

* [Op Codes](/developer-section/dex/smart-contracts/v2/op-codes.md)

## DEX v2 features

### Swap

* custom payload & nested operations after swaps
* chain multiple swaps on the same `Router`
* chain multiple swaps on different v2 `Routers`
* custom refund address and payload on swap failure
* deadline for tx completion

### Liquidity

* custom payload after liquidity provision
* improved initial liquidity locking management
* now always mints a maximum possible amount of lp tokens to user even if provision ratio is different from current one in `Pool`
* single side liquidity provision
* deadline for tx completion

### Referral

* referral fees are stored in `Vault` contract
* custom referral fee value in each swap (maximum 1%)

> **Note**: Protocol fees in DEX v2 are collected in ASK jetton.

#### Referral Fees

DEX v2 accumulates referral fees in `Vault` contracts identified by the router, referrer, and router token wallet. The referrer withdraws accrued fees from each Vault. See the [DEX fee model](/developer-section/dex/fees.md#dex-v2-vault-payouts).

You can inspect vault balances and accrual history using the Stats & Vaults REST API:

* `GET /v1/wallets/{addr_str}/fee_vaults`: lists all known vaults per referrer
* `GET /v1/stats/fee_accruals`: shows all operations that led to fee accrual for the referrer, filterable by period
* `GET /v1/stats/fee_withdrawals`: lists all withdrawals from the referrer's vaults, filterable by period
* `GET /v1/stats/fees`: returns aggregated referral fee metrics (for example, total accrued USD value) by time frame

All endpoints are documented in the [Swagger UI](https://api.ston.fi/swagger-ui/#/).

### pTON v2

* now uses a custom op code for ton transfers
* ton transfer to user is non-bouncable
* improved gas management
* can chain ton transfers between 2 pTON wallets (to chain pTON swaps on v2 `Routers`)

### Other

* `LpAccount` and `Vault` are deleted if they have 0 tokens on balance to avoid paying storage cost
* error handling for missing pools and invalid payloads
* refactored codebase with shared libraries in the masterchain
* fixed various excesses issues
* fixed some `Pools` having broken `get_jetton_data`

### Removed

#### Pool

* off-chain `get_expected_outputs`
* off-chain `get_expected_tokens`
* off-chain `get_expected_liquidity`
* on-chain `getter_expected_outputs`
* on-chain `getter_expected_tokens`
* on-chain `getter_expected_liquidity`
* user-called `collect_fees`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.ston.fi/developer-section/dex/smart-contracts/v2.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
