Nhảy đến nội dung chính

Decentralized Exchanges (DEX)

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

dex

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.

info

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.

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"
}

tip

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.

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.

near call v2.ref-finance.near storage_deposit '' --accountId <account> --amount 0.1

Deposit funds

In order to swap tokens, one must first deposit tokens into the DEX. For this, you will need to 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 --depositYocto 1 --accountId <account>
danger

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 the get_deposits method:

const ammContract = "v2.ref-finance.near";
const depositBalances = Near.view(
ammContract,
"get_deposits",
{
account_id: "bob.near"
}
);
Example response

{
"token.v2.ref-finance.near": "0",
"wrap.near": "0"
}


Query Pools

DEXs work by having multiple pools of token pairs (e.g. NEAR-USDC) that users can deposit tokens into.

const ammContract = "v2.ref-finance.near";
const result = Near.view(
ammContract,
"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
},
{
pool_kind: 'SIMPLE_POOL',
token_account_ids: [
'c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2.factory.bridge.near',
'wrap.near'
],
amounts: [ '783621938569399817', '1100232280852443291118200599' ],
total_fee: 30,
shares_total_supply: '33923015415693335344747628',
amp: 0
}
]


Swap tokens

In order to swap a token for another, you need to have funds, and there needs to exist a pool that has both tokens on it.

const ammContract = "v2.ref-finance.near";
const result = Near.call(
ammContract,
"swap",
{
actions: [
{
pool_id: 79,
token_in: "token.v2.ref-finance.near",
token_out: "wrap.near",
amount_in: "100000000000000000",
min_amount_out: "1",
},
],
},
300000000000000,
1
);
Example response
"5019606679394603179450"

Additional Resources

  1. Claim Fungible Tokens from Lockup - the example how to claim locked tokens from the lockup.burrow.near contract.
  2. BSC Dex Collection - the example of how to build simple swap page for a DEX.
Was this page helpful?