Ref Exchange
The Exchange contract is the main contract facilitating the trading. All pools are managed inside one contract, allowing for atomic trades between them.
There are next main interactions:
Create a new pool with set of tokens
Currently there is only equal weight constant product of 2 tokens, called
simple_pool
.More pool types will be added over time via governance.
Call to register user's account with the exchange.
Deposit funds into the contract -- as a user you first need to deposit the funds into the contracts. There is a list of globally whitelisted tokens, or user can also separately register for tokens if they are not whitelisted (this is done to avoid malicious token contracts filling user's space).
Deposited funds can be put as liquidity into one of the pools.
Depoisted funds can be used to swap, including multihop swap between multiple pools.
Withdraw funds back to the user's account from the contract.
Constructor
Create new pool with given owner_id
account that will have priviliaged access (like upgrades and whitelists) and exchange_fee
in bps and referral_fee
in bps that will be charged from pools on top of their LP pools.
Register or deposit more storage for user's account
When interacting with the contract first time, user must call storage_deposit
with attached $NEAR to register the account. Later, user can keep adding more storage to have more tokens in the portfolio. This $NEAR can be retrieved back later, if user withdraws all it's deposits and can unregister.
Notes:
registration_only
if passed, only attached $NEAR to cover minimum balance for account creation. The rest gets refunded.stores the amount of $NEAR passed at deposit to cover future deposits usage.
Register tokens for given user
There is global whitelist, but users can register tokens they want to use separately. This makes sure that malicious token contracts can't spam user's storage.
Unregister token for given user
Deposit
To deposit funds, user should send the token to the contract via callback.
Notes:
make sure that
exchange contract
account has storage deposit intoken
. User code can call thetoken.storage_deposit(Some(<exchange contract>))
covering exactly 125 bytes of storage.msg
must be empty.
This will call ft_on_transfer
callback which will record the deposits.
If token is not in the global whitelist or user haven't registered this token for themself - this will fail.
Withdraw
To withdraw funds, the request is made per token with required amount.
Notes:
if
unregister
istrue
andamount
is full amount on the account of the given user, can automatically callunregister_tokens
ontoken_id
. Allowing to free up some of the $NEAR locked for storage for this token.
Add simple pool
To create "Simple pool" (equal weighted constant product), need to specify list of tokens (up to 10). Must attach enough $NEAR to cover storage for the new pool (~300 bytes).
Add liquidity
Add given amounts from deposited into given pool. Amounts map to the tokens in the given pool.
The amounts must be deposited before calling this contract first. Fails if there is not enough tokens.
Remove liquidity
Remove liquidity from given pool. Pass how many shares to withdraw and min_amounts
of tokens of given pool that should be received (to prevent front running).
Swap
Swap some token via set of swap actions to the outcome tokens. Swap action represents some amount
of token_in
being exchanged to token_out
.
Notes:
referral_id
is optional argument that allows frontends to get paid for facilitating transactions. If not provided, thereferral_fee
doesn't get paid out and goes to LPs.
Get return
To compute how much exchanging over a single pool would return:
Number of pools
Get pools
Get limit
number of pools with ids starting from given index.
Shares in the pool
Get number of shares that given user has in the pool.
Total number of shares in the pool
Get total number of shares in the pool.
Get users deposits
Get deposits of the user in the contract across all tokens.
Get global token whitelist
Get user token whitelist
Returns tokens that are specifically allowed for given user (doesn't include global whitelist).
Get owner
Last updated