MeteoraAg / dlmm-sdk

An SDK for building applications on top of Dynamic CLMM
68 stars 57 forks source link

fix: swap quote on edge case #105

Closed codewithgun closed 3 months ago

codewithgun commented 3 months ago

In the normal scenario, integrator will only cache N bin arrays to the right and left of the active ID based on the bin step compute unit usage, and the volatility of the pair, instead of caching all bin arrays.

Case 1. The quote in amount more than liquidity of client side cached bin arrays, but all bin arrays of the pool on chain are able to fulfill the in amount. The swap quote will throw the below error even with isPartialFill true. The reason is that the bitmap still able to find the next bin array for swap, but client side doesn't cache enough bin array. https://github.com/MeteoraAg/dlmm-sdk/blob/20fdbc0e46a806da2b16bcab6d63cd321ba35136/ts-client/src/dlmm/helpers/binArray.ts#L322 One may try to cache more bin arrays to avoid the error, but if the swap quote consumes more than 3 bin arrays, it's quite high likely that the actual swap will out of compute unit which will still fail in the end.

Case 2. The pool consists of one type of token. In this case, token X. When attempt to quote for Y token, the swap quote will throw the below error. https://github.com/MeteoraAg/dlmm-sdk/blob/20fdbc0e46a806da2b16bcab6d63cd321ba35136/ts-client/src/dlmm/index.ts#L3369 The reason is that there's still available bin array with liquidity for quote (which is the active bin array). However, the active bin is the last bin with liquidity and contain no token Y. Therefore, it couldn't locate any bin for quoting and throw Invalid start bin. It shall throw a more meaningful error message.