Game plan
-
Single DEX swaps for REF and Jumbo
- Study REF's router
- Study the provider object returned by the wallet adapter.
- Study Jupiter's SDK. Follow its naming conventions.
- Write SDK: wrapper on a graphQL API
-
/quote
: Get a ranked list of possible routes -
/swap
: Get serialized TX to swap
-
-
Split routes (v2): Use
NearWalletSelector.signAndSendTransactions
. The wallet can sign multiple TXs directed to different contracts using https://github.com/ref-finance/ref-ui/blob/19d5bee1c40e0b98686965e19a590a7f3ea27ac0/src/utils/sender-wallet.ts#L163. Eg. for adding liquidity in REF, you first callft_transfer_call()
for each token, then call theadd_liquidity
function on REF. -
Multi leg swaps and Orderbook based swaps- todo
-
Aurora based swaps- Copy code from REF's UI. This is an add-on for the aggregator.
-
TXs
- Token storage deposit: https://explorer.mainnet.near.org/transactions/AVdCHhFM46mYdAUes4EqbXEMfDo8sYfMqC6oCKF5qspT
- Transfer token to Aurora: https://explorer.mainnet.near.org/transactions/84kGefNaj3ZKZeFGJi5pW7NBjHAfPMtLvHXVdDP47xNN
- Opaque messages sent to Aurora: https://explorer.mainnet.near.org/transactions/5oFCN1aLrCCGJXLi2PEAWB7xwJ1ZnUiGrQGMTSvap2E9, https://explorer.mainnet.near.org/transactions/8TC2XcdUsKNqSnsWQfkkx5WUkmkmXLx6KM9MoGkvzwdA, https://explorer.mainnet.near.org/transactions/5KnMNnaswMULq2b4MiKrveUJDecaNKgZ4LtoeFgA37Jw
DEX aggregator test setup
-
Single DEX swap
- Best route tests are not immediate priority. We can setup local pools and compare results.
- No tests for returned TX structure.
-
Multi leg swaps
-
Deploy an instance of test-token for every token needed by pools.
-
Deploy pools for every token pair
-
Test cases:
- Ref- single and double swap
- Combination of Ref and Jumbo
Stableswap has the same swap interface as regular pools, so we do not need separate tests. Mainnet stableswap pool ID is 1910.
-
Have separate unit tests to find best route
-
Execution plan
- Call
ft_transfer_call
on the input token withv1.comet.near
as the destination. - The args are
{
"receiver_id": "v1.comet.near",
"amount" : "100000",
"msg": {
"referral_id": "vault.comet.near",
"dexes": [
{
"dex": "v2.ref-finance.near",
"token_in": "wrap.near",
// Do not decode
"actions": [{
"pool_id": 0,
"token_in": "wrap.near",
"token_out": "dai.near",
"amount_in": "100000",
"min_amount_out": "5000",
}],
},
{
"dex": "spot.spin-fi.testnet",
"token_in": "dai.near",
// do not decode
"actions": [{
"market_id": 1,
"drop": ["23"],
"place": [
{
"price": "2000000000000000000000000",
"quantity": "1000000000000000000000000",
"ttl": 604800,
"market_order": true,
"order_type": "Bid",
"client_order_id": 1
}
]
}]
}
]
}
}
-
v1.comet.near
will get this message throughft_on_transfer
. -
For each dex
- Read the
dex
andtoken_in
. - Call
token_in::ft_transfer_call
, withdex
as thereciever_id
. The message format should be
{ "force": 0, "actions": {}, // decoded actions array "referral_id": "" // the passed reciever ID }
- Read the
-
Keep a whitelist of allowed DEXes to prevent fund loss.
-
Spin needs 3 calls
- Deposit input token with
ft_transfer_call
- Place market orders with
place_bid
orplace_ask
. Market orders are executed immediately. Multi-market swaps can be atomically performed usingbatch_ops
. - Withdraw tokens with
withdraw
function
- Deposit input token with
Gas research
-
1 TGas = 10^12 gas
-
3 pool swap on REF: 47 TGas
- Tx to reciept conversion: 2 Tgas
- Transfer wNEAR to Ref: 10 TGas
- Ref swap: 22 TGas
- Transfer output token to user: 5 TGas
- Withdraw callback: 3 Tgas
- Token transfer callback: 2 Tgas
-
Max gas per call: 300 TGas. This should be sufficient for 3 REF-like swaps.
-
swap() based method: takes 139 TGas.
-
Instant swap method: 121 TGas
- 100 TGas if ft_balance_of and callback is removed
- Instant swap without outbound transfer: 84
wNEAR -> USDT issue
-
REF UI returns wrap.near -> DAI (6b175474e89094c44da98b954eedeac495271d0f.factory.bridge.near) -> USDT
-
SDK returns wrap.near -> USDC (a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.factory.bridge.near) -> USDT
It's using uni v2 algorithm on the stable pool 1910.