Decentralized Exchanges (DEX)
A Decentralized Exchange (DEX) is an application that allows users to trade tokens (native & fungible tokens) through smart contracts.

In brief, DEXs work by having pools of token pairs (e.g. NEAR-USDC) that users can deposit tokens into.
The ratio of the tokens in the pool determines the exchange rate for a swap. Indeed, swapping is adding tokens to one side of the pool while removing tokens from the other side of the pool.
This docs refer to Ref Finance, a community built DEX in NEAR.
Please check their docs for more information.
Query Token Exchange Rate
One can query the exchange rate of a token pair by calling the get-token-price method on the DEX contract.
- ⚛️ Component
- 🌐 WebApp
const tokenContract = "token.v2.ref-finance.near";
const tokenPriceResult = fetch(
`https://indexer.ref.finance/get-token-price?token_id=${tokenContract}`
).body;
const tokenPriceValue = JSON.parse(tokenPriceResult);
Example response
{
"token_contract_id": "token.v2.ref-finance.near",
"price": "0.08153090"
}
Ref Finance has a method to get all token prices at once.
const tokenContract = "token.v2.ref-finance.near";
const tokenPriceResult = await fetch(
`https://indexer.ref.finance/get-token-price?token_id=${tokenContract}`
);
const tokenPriceValue = await tokenPriceResult.json();
The Wallet object comes from our quickstart template
Example response
{
"token_contract_id": "token.v2.ref-finance.near",
"price": "0.08153090"
}
Ref Finance has a method to get all token prices at once.
Query Whitelisted Tokens
Anyone list tokens for sale in the DEX. This is why, in order to protect users, the DEX contract has a list of whitelisted tokens that can be traded.
- 🖥️ CLI
near view v2.ref-finance.near get_whitelisted_tokens
Examples Response
'wrap.near',
'usdt.tether-token.near',
'berryclub.ek.near',
'farm.berryclub.ek.near',
'token.v2.ref-finance.near',
'token.paras.near',
'marmaj.tkn.near',
'meta-pool.near',
...
Register in the DEX
In order to use the contract, make sure to register your account in the DEX by paying for the storage you will use in order to keep track of your balances.
- 🖥️ CLI
near call v2.ref-finance.near storage_deposit '' --accountId <account> --amount 0.1