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.