CetusProtocol / cetus-clmm-sui-sdk

The clmm sdk on Sui.
Apache License 2.0
28 stars 20 forks source link

some bugs in calculateRates #3

Closed Godxia closed 3 months ago

Godxia commented 1 year ago

Hello,

I seem to have encountered a bug while testing the official cetus-clmm-sui-sdk repository.

I made a modification to the file tests/swap.test.ts. I first changed the settings to Mainnet and then added a random address with sui tokens in mainnet_config.ts.

Subsequently, I altered the test("calculateRates") in the swap.test.ts file to the following code:

javascript Copy code test('calculateRates', async () => { const a2b = false; const byAmountIn = true; const amount = new BN('20000000');

const poolObjectId = "0x014abe87a6669bec41edcaa95aab35763466acb26a46d551325b07808f0c59c1"; const currentPool = await buildTestPool(sdk, poolObjectId);

const tickdatas = await sdk.Pool.fetchTicksByRpc(currentPool.ticks_handle); const res = await sdk.Swap.calculateRates({ decimalsA: 6, decimalsB: 8, a2b, byAmountIn, amount, swapTicks: tickdatas, currentPool, });

console.log('calculateRates', { estimatedAmountIn: res.estimatedAmountIn.toString(), estimatedAmountOut: res.estimatedAmountOut.toString(), estimatedEndSqrtprice: res.estimatedEndSqrtPrice.toString(), estimatedFeeAmount: res.estimatedFeeAmount.toString(), isExceed: res.isExceed, a2b, byAmountIn, }) }) The result I got is:

yaml Copy code console.log calculateRates { estimatedAmountIn: '20000000', estimatedAmountOut: '1349086', estimatedEndSqrtprice: '309482603896108133615', estimatedFeeAmount: '200001', isExceed: false, a2b: false, byAmountIn: true } I then made similar changes to test('preswap') as follows:

javascript Copy code test('preswap', async () => { const a2b = false; const pool = await sdk.Pool.getPool('0x014abe87a6669bec41edcaa95aab35763466acb26a46d551325b07808f0c59c1'); const byAmountIn = true; const amount = '20000000';

const res: any = await sdk.Swap.preswap({ pool: pool, current_sqrt_price: pool.current_sqrt_price, coinTypeA: pool.coinTypeA, coinTypeB: pool.coinTypeB, decimalsA: 6, decimalsB: 8, a2b, by_amount_in: byAmountIn, amount, });

console.log('preswap###res###', res); }) The result I got is:

yaml Copy code console.log preswap###res### { poolAddress: '0x014abe87a6669bec41edcaa95aab35763466acb26a46d551325b07808f0c59c1', currentSqrtPrice: '309440836972128401341', estimatedAmountIn: '20000000', estimatedAmountOut: '70342', estimatedEndSqrtPrice: '309536786273261356364', estimatedFeeAmount: '200000', isExceed: false, amount: '20000000', aToB: false, byAmountIn: true } I noticed a considerable difference in the results for estimatedAmountOut, which leads me to believe that there could be some errors in the SDK's calculations. Could you please help check this? Thank you.

Neoplayer commented 11 months ago

Did you manage to figure out what the problem is?

devlb1024 commented 3 months ago

Thanks for the feedback. Fixed in v5.1.2