Canisters <-> DEXes

For a couple of weeks, I’ve been collecting stats from the 4 popular IC dexes using Blast and the IC Chrome Inspector addon. These tools made discovering how things work easier and finding interfaces pretty fast. The result was icpcoins.com however, its backend is not a canister just yet.

I suppose the next IC DeFi step is to have canisters communicate with DEXes. Things like arbitrage, stats aggregation, algorithmic trading, etc. For that to happen these canisters need to periodically fetch and record stats. It will be best if only one query for each is needed and it returns the most important stats for these smart contracts to function. Currently, none of the DEXes return that info in one call.

Price
Prices are relatively easy to get even tho there are at least three different token standards and each dex has a different system and interface.
Price - token1/token2 (Not converted into USD)

Volume
Volume 24h is relatively easy to get, but it’s not very useful.
A better stat would be total volume - volume bought and volume sold. From that, a canister periodically recording the values can derive volume 24h or split it into chunks with various periods.

For all intends and purposes let’s assume every token has a pool with ICP and its USD value is calculated based on ICP/USD price.

ICDex - simple and good, you get both values.

ICPSwap - returns total volume in USD and the USD/ICP price is lagging. Not sure if the whole volume was converted with the last ICP price or if each new transaction gets converted to USD and then added. With the lagging ICP price, this can’t be converted back to ICP volume. Since ICPSwap has liquidity ranges, which make things more complex, there may be another way to get the volume, but I currently don’t know how to do it and it will probably require processing big amounts of data (big for a canister).

InfinitySwap - Has its own function which returns volume split into periods during a time range. It seems to be made for frontends. Just getting the total volume buy/sell would be best for canisters.

Sonic - Will require you to call off-chain graph API to get it or you can also obtain it on-chain by digesting the whole multi-canister ledger - pretty tedious.

Depth
It’s good to know what is the current market depth. The bids and asks should be grouped by prices (0%, -10% +10% -20% +20%, and so on). Something like what ICDex returns with its level100 function, but a bit easier to process - less granular.


This will work for both order book dexes, standard AMM, and AMMs with liquidity ranges.

One can easily calculate the depth of a standard AMM by using the two reserve values, but it’s probably best for them to just provide the depth so once they upgrade to something different, nothing will break in the canisters using them.

Thoughts on IC async DeFi - In Ethereum you have a single state. To rebalance DEXes a script may analyze the whole blockchain and create a long multi-step transaction that takes a flash loan, swaps in one dex, swaps in another, and then returns the loan and takes a profit - all in one block. Such transactions are something miners will be doing first and probably an outsider can’t make. A miner can probably check the incoming transactions, find these and replace the profit address with theirs.

In IC things are different because there isn’t a single state and canisters communicate asynchronously. So it will be more like scripts(arbitrage bots/ algo trading) available to everyone to run and will work in a way similar to how off-chain scripts communicate with CEXes or multiple blockchains.

To conclude, having one function which returns {price, volume, depth} directly given by the pool canister where transactions happen will be awesome and as fast as things can get. If the 4 DEXes work together to provide these, hopefully, standardized, then the next level of DeFi apps on IC will be a lot easier to make.

11 Likes

@memecake Any thoughts on this? Did you add an on-chain volume report in your API?

Can someone tag the other DEXes. I am currently making the on-chain aggregator and it’s a pain to do with undocumented APIs coming from closed-source canisters (mostly talking about ICPSwap).

If we create a standard for this, we will have the amazing feature - wallets knowing the value of their assets. Also: DeFi trading contracts. Stats sites.

2 Likes

@ICPSwap @simpson @Maxfinity @witter

Thanks @infu for bringing this up again. Besides the reasons you mentioned, it is also important for our DeFi ecosystem that our Dexes are listed on Coinmarketcap. Therefore, we need standardized interfaces.

3 Likes

Hello, @infu @domwoe

We have updated several API interfaces.

Please take a look and let us know which one is suitable for you!

## 1、Query pools

### URL:
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/pairs  
  
#### Request Method : GET

#### Request Params
    Empty

#### Response

| field  | type  | description|
|---|---|---|
| ticker_id  | string  |swap pool ticker|
| base  | string  |base token symbol|
| target  | string  |target token symbol|
| pool_id  |  string |swap pool canister id (contract id)|



## 2、Query single pool totalVolumeUSD

### URL:
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/totalVolumeUSD?poolId={pool_id}&limit={limit_days}

example: 
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/totalVolumeUSD?poolId=2obmq-pqaaa-aaaag-qcesa-cai&limit=30
  
#### Request Method : GET

#### Request Params

| field  | type  | description|
|---|---|---|
| poolId  | string  | swap pool canister id (contract id) |
| limit  | int  | the last few days |

#### Response

| field  | type  | description|
|---|---|---|
| day  | int  | day id |
| token0Id  | string  | token0 canister id(contract id) |
| token1Id  | string  | token1 canister id(contract id) |
| token0Symbol  |  string | token0 symbo |
| token1Symbol  |  string | token1 symbo |
| volumeUSD  |  double |swap pool daily trade volume|
| totalVolumeUSD  |  double | swap pool total trade volume |
| volumeUSDChange  |  double | swap pool daily trade volume change amount|
|volumeUSDWeek| double | swap pool weekly trade volume |
|volumeUSDWeekChange |double| swap pool weekly trade volume change amount |
|txCount|int| swap pool daily trade quantity |


## 3、Query single pool TVL

### URL:
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/pool_tvl?poolId={pool_id}&limit={limit_days}

example: 
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/pool_tvl?poolId=2obmq-pqaaa-aaaag-qcesa-cai&limit=30
  
#### Request Method : GET

#### Request Params

| field  | type  | description|
|---|---|---|
| poolId  | string  | swap pool canister id (contract id) |
| limit  | int  | the last few days |

#### Response

| field  | type  | description|
|---|---|---|
| day  | int  | day id |
| timestamp  | long  | summary time |
| tvlUSD  | double  | swap pool TVL |

## 4、Query token TVL

### URL:
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/token_tvl?tokenId={token_id}&limit={limit_days}

example: 
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/token_tvl?tokenId=fkhzl-saaaa-aaaan-qd26a-cai&limit=30
  
#### Request Method : GET

#### Request Params

| field  | type  | description|
|---|---|---|
| tokenId  | string  | token canister id (contract id) |
| limit  | int  | the last few days |

#### Response

| field  | type  | description|
|---|---|---|
| day  | int  | day id |
| timestamp  | long  | summary time |
| tvlUSD  | double  | token TVL on ICPSwap |


## 5、Query pools TVL

### URL:
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/pools_tvl?limit={limit_days}

example: 
https://uvevg-iyaaa-aaaak-ac27q-cai.raw.ic0.app/pools_tvl?limit=1
  
#### Request Method : GET

#### Request Params

| field  | type  | description|
|---|---|---|
| limit  | int  | the last few days |

#### Response

| field  | type  | description|
|---|---|---|
| poolTVLList  | [PoolTVL] | TVL data for multiple pools |

Item -- PoolTVL:

| field  | type  | description|
|---|---|---|
| poolId  | string  | swap pool canister id (contract id) |
| tvlInfoList  | [TVLInfo]  | a set of TVL data for a single pool |

Item -- TVLInfo:

| field  | type  | description|
|---|---|---|
| day  | int  | day id |
| timestamp  | long  | summary time |
| tvlUSD  | double  | pool TVL |

And @infu

If your questions are resolved, please feel free to email “contact@icpswap.org”, and we will send you the API interface PDF or md document, which may provide a better viewing experience for you.

If this doesn’t fully address your inquiries, please feel free to update us, and I will provide feedback to the team and get back to you. Thank you.

5 Likes

I need the market depth.
It is useful for trading bots, and liquidity score calculation.
It looks like your app is using getTicks and getTickInfos to draw these.
image
The API returns


Which, after trying out a few things, I was able to turn into this chart:

It looks similar, but the numbers are strange, and it has offset.
After numbers and offset get figured out, (if I know your quoting algorithm, I can calculate the data needed for this chart)

I’ve already done that for ICDex, pretty easy with their API. Sonic is easy too, because there’s no range liquidity. ICPSwap seems to be the hardest. Some help with this will be appreciated.

Another problem is - Currently your apps are only using that API and you may change it to serve your app better. But that will break the canisters using it.

That’s why it will be best to add another function providing market depth, a bit simplified if possible (made to fit one query) The current getTickInfos for SNS-1 requires 3 queries.

Here is what Coinmarketcap requires when it comes to market depth(order book):
https://api.bitget.com/data/v1/market/depth?symbol=BTC_USDT&type=step1

Market depth of a trading pair. One array containing a list of ask prices and another array containing bid prices. Query for level 2 order book with full depth available as minimum requirement.

3 Likes

Understood, I’ve already provided feedback to the team. The Devs need some time to organize the corresponding code and will provide it to you. Thank you.

2 Likes

The NNS wallet cannot be used directly on DEX, which is a bit painful.

Our Devs have organized and open-sourced these codes. Please take a look to see if they can address the issues you mentioned.

Details: GitHub - ICPSwap-Labs/ticks-chart

5 Likes

Thanks, looks like this will be enough.

3 Likes

Cool! That‘d be great!

2 Likes