Skip to main content
A Decentralized Exchange (DEX) is an application that allows users to trade tokens (native and fungible tokens) through smart contracts. DEX Architecture DEXs work by having pools of token pairs (e.g., NEAR-USDC) that users can deposit tokens into. The ratio of tokens in the pool determines the exchange rate for a swap. Swapping means adding tokens to one side of the pool while removing tokens from the other side.
This documentation refers to Ref Finance, a community-built DEX on NEAR. Check their official docs for more information.

Query Token Exchange Rate

You can query the exchange rate of a token by calling the Ref Finance indexer:
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();

console.log(tokenPriceValue);
// {
//   "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 can list tokens for sale in the DEX. To protect users, the DEX contract maintains a list of whitelisted tokens:
near view v2.ref-finance.near get_whitelisted_tokens
Example response:
[
  'wrap.near',
  'usdt.tether-token.near',
  'token.v2.ref-finance.near',
  'token.paras.near',
  'meta-pool.near',
  ...
]

Register in the DEX

Before using the DEX, you must register your account by paying for storage:
near call v2.ref-finance.near storage_deposit '' --accountId <account> --amount 0.1

Deposit Funds

To swap tokens, you must first deposit tokens into the DEX. Transfer the FT you want to swap to the DEX contract:
near call token.v2.ref-finance.near ft_transfer_call '{
  "receiver_id": "v2.ref-finance.near",
  "amount": "1000000000000",
  "msg": ""
}' --gas 300000000000000 --deposit 0.000000000000000000000001 --accountId <account>
Do NOT transfer NEAR tokens to Ref Finance. Instead, call near_deposit in the wrap.near contract, attaching the amount of NEAR you want to swap. This will mint wrap.near for you, which you can then transfer to Ref Finance.

Get Deposit Balances

Query your deposit balances by calling get_deposits:
near view v2.ref-finance.near get_deposits '{"account_id": "bob.near"}'

Query Pools

DEXs work by having multiple pools of token pairs. You can query available pools:
near view v2.ref-finance.near get_pools '{"from_index": 0, "limit": 1000}'
Example response:
[
  {
    pool_kind: 'SIMPLE_POOL',
    token_account_ids: ['token.skyward.near', 'wrap.near'],
    amounts: ['51865812079751349630100', '6254162663147994789053210138'],
    total_fee: 30,
    shares_total_supply: '1305338644973934698612124055',
    amp: 0,
  },
  ...
]

Swap Tokens

To swap tokens, you need to have funds deposited and there must exist a pool with both tokens:
near call v2.ref-finance.near swap '{
  "actions": [{
    "pool_id": 79,
    "token_in": "token.v2.ref-finance.near",
    "amount_in": "100000000000000000",
    "token_out": "wrap.near",
    "min_amount_out": "1"
  }]
}' --gas 300000000000000 --deposit 0.000000000000000000000001 --accountId bob.near

Additional Resources

Build docs developers (and LLMs) love