# Jetton prices API (https://cd1652e2.ton-docs.pages.dev/llms/api/price/content.md)



Each swap operation, whether between a native asset (Gram) and Jetton or between two distinct Jettons, has its price, a fixed exchange rate of one asset to another. This price is calculated by the internal DEX math algorithm, called [AMM](https://www.coinbase.com/learn/advanced-trading/what-is-an-automated-market-maker-amm). Some services require information about previous swaps on the blockchain to use it in their internal business logic or to simply show statistics to users.

## Off-chain API [#off-chain-api]

The most common use case for the price API is to fetch Jetton info on the web2 backend and use aggregated data inside the service. There are [several](https://www.coingecko.com/en/api/ton) [historical](https://dyor.io/tonapi) Jetton price providers in TON.

There is no established solution for real-time jetton swap market data; however, one can explore [this websocket API](https://docs.coingecko.com/websocket).

## On-chain API [#on-chain-api]

Currently, it is not possible to retrieve **historical** jetton prices on-chain - since TON contracts [are limited by storage](https://cd1652e2.ton-docs.pages.dev/llms/from-ethereum/content.md), it is quite hard to implement such an API fully on-chain. However, it is possible to retrieve **current** prices via the Request-Response pattern on some DEXes; refer to the specific service documentation to learn more about it.

<Callout>
  Since the TON [execution model is asynchronous](https://cd1652e2.ton-docs.pages.dev/llms/from-ethereum/content.md), the jetton price might change between the moment of the response from the price provider and the moment the contract processes the response. Consider this factor in smart contract logic.
</Callout>

For example, on a [popular DEX](https://dedust.io), it is possible to retrieve pool information on-chain using an internal message with the following [TL-B](https://cd1652e2.ton-docs.pages.dev/llms/foundations/tlb/overview/content.md) schema:

```tlb
provide_pool_state#6e24728d query_id:uint64 include_assets:Bool = InMsgBody;

take_pool_state#bddd4954 query_id:uint64 reserve0:Coins reserve1:Coins total_supply:Coins
                         assets:(Maybe ^[ asset0:Asset asset1:Asset ]) = InMsgBody;

# See:
# https://hub.dedust.io/contracts/v2/reference/core/pool#operation-provide_pool_stat
```

Here is a smart contract snippet in [Tolk](https://cd1652e2.ton-docs.pages.dev/llms/tolk/overview/content.md) illustrating how one can send the `provide_pool_state` message:

```tolk title="Tolk"
struct (0x6e24728d) ProvideDeDustPool {
    queryId: uint64
    doIncludeAssets: bool
}

fun main() {
    val requestDeDustPoolInfoMsg = createMessage({
        body: ProvideDeDustPool { queryId: 1, doIncludeAssets: true },
        bounce: true,
        dest: dedustPoolAddress,
        value: 0,
    });

    requestDeDustPoolInfoMsg.send(SEND_MODE_CARRY_ALL_REMAINING_MESSAGE_VALUE);
}
```
