Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get the raydium pool with the smallest tick spacing #393

Merged

Conversation

silviutroscot
Copy link
Contributor

@silviutroscot silviutroscot commented Oct 18, 2023

Summary by CodeRabbit

  • New Feature: Enhanced the getPool functionality in the Kamino SDK. The update now provides a more efficient way to find and select the optimal pool from the available options. If no matching pools are found, a default value is returned, ensuring a smoother user experience. This change aims to improve the reliability and performance of pool selection, contributing to a more seamless interaction with the platform.

@coderabbitai
Copy link

coderabbitai bot commented Oct 18, 2023

Walkthrough

The changes introduced in the Kamino class primarily focus on the getPool method. The method now initializes an empty array and populates it with matching pools from raydiumPools. If no matches are found, it returns a default PublicKey. If matches are found, it selects the pool with the smallest tickSpacing and returns its PublicKey.

Changes

File Summary
.../kamino-sdk/src/Kamino.ts The getPool method now initializes an empty array and populates it with matching pools from raydiumPools. If no matches are found, it returns a default PublicKey. If matches are found, it selects the pool with the smallest tickSpacing and returns its PublicKey.

🐇

In the land of code, where the shadows lie,

A rabbit hops, its gaze held high.

Changes made, a pool to find,

In the realm of keys, where data's bind.

Tick and tock, the spacing small,

The rabbit's code, stands tall.

🐇


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.
  • Additionally, enter @coderabbitai pause to halt the bot's ongoing reviews as any issue or review comment. To
    resume reviews, enter @coderabbitai resume as any issue or review comment.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 2

Commits Files that changed from the base of the PR and between 5991c54 and 85988a8.
Files selected for processing (1)
  • packages/kamino-sdk/src/Kamino.ts (1 hunks)
Additional comments (Suppressed): 1
packages/kamino-sdk/src/Kamino.ts (1)
  • 845-847: The code now handles the case where no matching pools are found by returning a default PublicKey. This is a good addition for error handling.

Comment on lines +838 to +844
let pools: Pool[] = [];
let raydiumPools = await this.getRaydiumPoolsForTokens(poolTokenA, poolTokenB);
raydiumPools.forEach((element) => {
if (new Decimal(element.ammConfig.tradeFeeRate).div(FullBPS).div(FullPercentage).equals(feeBPS.div(FullBPS))) {
pools.push(element);
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new code introduces a filtering mechanism to select pools based on the tradeFeeRate from raydiumPools. This is a good approach to filter out the pools that don't meet the criteria. However, the filtering can be done more efficiently using the filter method instead of forEach. This would make the code more readable and maintainable.

- raydiumPools.forEach((element) => {
-   if (new Decimal(element.ammConfig.tradeFeeRate).div(FullBPS).div(FullPercentage).equals(feeBPS.div(FullBPS))) {
-     pools.push(element);
-   }
- });
+ pools = raydiumPools.filter((element) => 
+   new Decimal(element.ammConfig.tradeFeeRate).div(FullBPS).div(FullPercentage).equals(feeBPS.div(FullBPS))
+ );

Comment on lines +848 to 856
let pool = PublicKey.default;
let tickSpacing = Number.MAX_VALUE;
pools.forEach((element) => {
if (element.ammConfig.tickSpacing < tickSpacing) {
pool = new PublicKey(element.id);
tickSpacing = element.ammConfig.tickSpacing;
}
});
return pool;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new code selects the pool with the smallest tickSpacing value. This is a good approach to optimize the pool selection process. However, the same can be achieved more efficiently using the reduce method instead of forEach. This would make the code more readable and maintainable.

- let pool = PublicKey.default;
- let tickSpacing = Number.MAX_VALUE;
- pools.forEach((element) => {
-   if (element.ammConfig.tickSpacing < tickSpacing) {
-     pool = new PublicKey(element.id);
-     tickSpacing = element.ammConfig.tickSpacing;
-   }
- });
+ let pool = pools.reduce((prev, curr) => 
+   curr.ammConfig.tickSpacing < (prev.ammConfig.tickSpacing || Number.MAX_VALUE) ? curr : prev, 
+   { ammConfig: { tickSpacing: Number.MAX_VALUE } }
+ );
+ pool = new PublicKey(pool.id);

Overall, the changes in the new hunk are a good improvement over the old hunk. The new code is more robust and handles edge cases better. However, the use of filter and reduce methods can make the code more efficient and maintainable.

@silviutroscot silviutroscot merged commit fffc82e into master Oct 18, 2023
3 checks passed
@silviutroscot silviutroscot deleted the silviu/return-the-rayium-pool-with-smaller-tick-spacing branch October 18, 2023 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants