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

Update IDL and twap computation #397

Merged
merged 2 commits into from
Oct 26, 2023
Merged

Conversation

alittlezz
Copy link
Contributor

@alittlezz alittlezz commented Oct 26, 2023

Summary by CodeRabbit

  • New Feature: Introduced Autodrift and UpdateStakingRateChain functionalities to enhance the flexibility of rebalancing and updating staking rates.
  • Improvement: Enhanced the CollateralInfo class with additional properties for better handling of TWAP price chains and staking rate chains.
  • Improvement: Implemented a new utility function stripTwapZeros to filter out zero values from TWAP price chains, improving data quality.
  • New Feature: Added new error types for better error handling and debugging.
  • Test: Added a new test case for validating the Kamino class and its getStrategiesShareData method.

These updates aim to improve the overall user experience by providing more accurate data and better error handling.

@coderabbitai
Copy link

coderabbitai bot commented Oct 26, 2023

Walkthrough

The changes introduce new classes and error types to handle autodrift rebalancing and staking rate sources. They also include updates to serialization and deserialization methods, and error handling. A utility function to filter out zero values from TWAP chains has been added, and test cases have been updated to validate the new functionality.

Changes

File(s) Summary
.../src/Kamino.ts, .../src/utils/utils.ts Introduced stripTwapZeros function to filter out zero values from TWAP chains. Updated Kamino.ts to use this function.
.../src/kamino-client/errors/custom.ts Added new error types related to staking rate chains and autodrift rebalancing.
.../src/kamino-client/types/CollateralInfo.ts, .../src/kamino-client/types/UpdateCollateralInfoMode.ts Updated CollateralInfo class to include new properties related to TWAP and staking rate chains. Added UpdateStakingRateChain class.
.../src/kamino-client/types/RebalanceAutodriftStep.ts, .../src/kamino-client/types/RebalanceType.ts Introduced new classes to handle autodrift rebalancing. Updated RebalanceType to include Autodrift.
.../src/kamino-client/types/StakingRateSource.ts Introduced new classes to handle different kinds of staking rate sources.
.../src/kamino-client/types/index.ts Added new modules and updated types to include new variants related to staking rate chains and autodrift rebalancing.
.../tests/stratRebalance.test.ts Updated test cases to validate new functionality.

"In the land of code, where logic is king, 🤴
Changes are made, new features they bring. 🎁
With each new line, the story unfolds, 📖
A tale of a rabbit, brave and bold. 🐇
Celebrate the changes, for they are here, 🎉
Bringing improvements, far and near. 🌍"


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

@alittlezz alittlezz force-pushed the vali/update-idl-and-scope-ids branch from d57fd60 to 756dd39 Compare October 26, 2023 16:07
@alittlezz alittlezz merged commit 8d79db4 into master Oct 26, 2023
3 checks passed
@alittlezz alittlezz deleted the vali/update-idl-and-scope-ids branch October 26, 2023 16:14
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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 0999e63 and 979f2b7.
Files selected for processing (9)
  • packages/kamino-sdk/src/Kamino.ts (3} hunks)
  • packages/kamino-sdk/src/kamino-client/errors/custom.ts (6} hunks)
  • packages/kamino-sdk/src/kamino-client/types/CollateralInfo.ts (9} hunks)
  • packages/kamino-sdk/src/kamino-client/types/RebalanceAutodriftStep.ts (1} hunks)
  • packages/kamino-sdk/src/kamino-client/types/RebalanceType.ts (4} hunks)
  • packages/kamino-sdk/src/kamino-client/types/StakingRateSource.ts (1} hunks)
  • packages/kamino-sdk/src/kamino-client/types/UpdateCollateralInfoMode.ts (4} hunks)
  • packages/kamino-sdk/src/kamino-client/types/index.ts (6} hunks)
  • packages/kamino-sdk/src/utils/utils.ts (2} hunks)
Files skipped from review due to trivial changes (1)
  • packages/kamino-sdk/src/kamino-client/types/index.ts
Additional comments: 31
packages/kamino-sdk/src/Kamino.ts (3)
  • 115-115: The stripTwapZeros function is imported but there is no context about what it does. Ensure that it is implemented correctly and that it indeed strips zero values from TWAP price chains as expected.

  • 1632-1637: The new hunk has removed the lines where tokenATwap and tokenBTwap were being assigned. This change is reflected in the new hunk at lines 1647-1650 where tokenATwap and tokenBTwap are now assigned the result of stripTwapZeros function. This change seems to be in line with the PR summary, which mentions the introduction of stripTwapZeros function to improve the accuracy of price fetching.

  • 1647-1650: The stripTwapZeros function is used to filter out zero values from the TWAP price chains of tokenA and tokenB. This is followed by fetching the price from the non-zero TWAP chains. If the TWAP chain is empty after stripping zeros, the price is set to null. This seems to be a logical and efficient way to handle zero values in the TWAP chains.

packages/kamino-sdk/src/kamino-client/types/StakingRateSource.ts (5)
  • 6-26: The Constant class and its methods seem to be well implemented. The toJSON and toEncodable methods return the correct JSON and encodable objects respectively. The static and instance properties are also correctly defined.

  • 29-49: The Scope class and its methods are also well implemented, similar to the Constant class. The toJSON and toEncodable methods return the correct JSON and encodable objects respectively. The static and instance properties are also correctly defined.

  • 52-66: The fromDecoded function correctly checks if the input object is of type "object" and throws an error if not. It then checks if the object contains either "Constant" or "Scope" and returns a new instance of the corresponding class. If neither is found, it throws an error. This function seems to be correctly implemented.

  • 68-79: The fromJSON function correctly switches on the kind property of the input object and returns a new instance of the corresponding class. This function seems to be correctly implemented.

  • 81-90: The layout function correctly creates a borsh schema for the Constant and Scope classes and replicates it if a property is provided. This function seems to be correctly implemented.

packages/kamino-sdk/src/utils/utils.ts (2)
  • 147-148: The numberToRebalanceType function has been updated to handle a new rebalance type, Autodrift. Ensure that all calls to this function throughout the codebase have been updated to handle this new type.

  • 232-236: The stripTwapZeros function is introduced to filter out zero values from TWAP price chains. This function improves the accuracy of price fetching by removing invalid data points. However, consider the implications of this change on the rest of the system. If there are parts of the system that rely on the length of the TWAP chain or its indices, this could potentially cause issues.

+ // Zero is not a valid TWAP component as that indicates the SOL price
+ export function stripTwapZeros(chain: number[]): number[] {
+   return chain.filter((component) => component > 0);
+ }
Committable suggestion (Beta)
// Zero is not a valid TWAP component as that indicates the SOL price
export function stripTwapZeros(chain: number[]): number[] {
  return chain.filter((component) => component > 0);
}
packages/kamino-sdk/src/kamino-client/types/UpdateCollateralInfoMode.ts (3)
  • 259-280: The UpdateStakingRateChain class is introduced with a static and instance discriminator and kind properties. The toJSON and toEncodable methods are implemented for JSON conversion and encoding respectively. This class seems to be a new type of update mode for collateral information. Ensure that this new update mode is handled appropriately in the rest of the codebase.

  • 321-323: The fromDecoded function is updated to handle the UpdateStakingRateChain object. This function now returns an instance of UpdateStakingRateChain when the input object has a property UpdateStakingRateChain. This change is consistent with the handling of other update modes in this function.

  • 365-367: The fromJSON function is updated to handle the UpdateStakingRateChain object. This function now returns an instance of UpdateStakingRateChain when the kind property of the input JSON object is UpdateStakingRateChain. This change is consistent with the handling of other update modes in this function.

384:
The layout function is updated to include UpdateStakingRateChain in the layout representation. This change is consistent with the handling of other update modes in this function.

packages/kamino-sdk/src/kamino-client/types/RebalanceType.ts (3)
  • 167-188: The Autodrift class and its corresponding interface AutodriftJSON have been added. This class represents a new rebalance type. It includes toJSON() and toEncodable() methods for serialization and deserialization. The discriminator property is set to 7, which should be unique among all rebalance types. Ensure that this new rebalance type is handled correctly in all parts of the codebase where rebalance types are used.

  • 217-219: The fromDecoded() function has been updated to handle the new Autodrift rebalance type. If the decoded object contains an Autodrift property, a new Autodrift instance is returned. This change is consistent with the handling of other rebalance types in this function.

  • 249-251: The fromJSON() function has been updated to handle the new Autodrift rebalance type. If the JSON object contains an Autodrift property, a new Autodrift instance is returned. This change is consistent with the handling of other rebalance types in this function.

264:
The layout() function has been updated to include the new Autodrift rebalance type in the borsh layout. This change allows for efficient encoding and decoding of the Autodrift type. Ensure that this new rebalance type is handled correctly in all parts of the codebase where the layout() function is used.

packages/kamino-sdk/src/kamino-client/errors/custom.ts (6)
  • 106-106: The InvalidTwapId error type has been replaced with InvalidScopeTWAPChain. Ensure that this change is reflected in all parts of the codebase where this error type is used.

  • 132-136: New error types InvalidScopeStakingRateChain, StakingRateNotValid, DecimalToU128ConversionFailed, DecimalNegativeSqrtRoot, and DriftingOppositeDirection have been added. Make sure these new error types are handled appropriately in the codebase.

  • 1294-1301: The InvalidScopeTWAPChain error class has been added, replacing the InvalidTwapId error class. Ensure that this change is reflected in all parts of the codebase where this error class is used.

  • 1595-1647: New error classes InvalidScopeStakingRateChain, StakingRateNotValid, DecimalToU128ConversionFailed, DecimalNegativeSqrtRoot, and DriftingOppositeDirection have been added. Make sure these new error classes are used and handled appropriately in the codebase.

  • 1861-1861: The InvalidTwapId error class has been replaced with InvalidScopeTWAPChain in the fromCode function. Ensure that this change is reflected in all parts of the codebase where this function is used.

  • 1912-1921: New error classes InvalidScopeStakingRateChain, StakingRateNotValid, DecimalToU128ConversionFailed, DecimalNegativeSqrtRoot, and DriftingOppositeDirection have been added to the fromCode function. Make sure these new error classes are used and handled appropriately in the codebase.

packages/kamino-sdk/src/kamino-client/types/CollateralInfo.ts (9)
  • 12-20: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 30-38: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the CollateralInfoFields interface. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 48-56: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the CollateralInfo class. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 65-73: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the CollateralInfo class constructor. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 85-94: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the layout function. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 108-116: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the fromDecoded function. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 128-136: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the fromJSON function. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 148-156: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the toJSON function. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

  • 168-176: The scopePriceIdTwap property has been replaced with scopeTwapPriceChain and scopeStakingRateChain properties in the fromJSON function. Ensure that all references to scopePriceIdTwap in the codebase have been updated to use these new properties. Also, verify that the new properties are being correctly populated and used in the application logic.

Comment on lines +1 to +5
import { PublicKey } from "@solana/web3.js" // eslint-disable-line @typescript-eslint/no-unused-vars
import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars
import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars
import * as borsh from "@project-serum/borsh"

Copy link

Choose a reason for hiding this comment

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

The import statements have eslint disable comments for unused vars. If these imports are not used in this file, consider removing them to clean up the code. If they are used in the code, remove the eslint disable comments.

Comment on lines +53 to +66
export function fromDecoded(obj: any): types.RebalanceAutodriftStepKind {
if (typeof obj !== "object") {
throw new Error("Invalid enum object")
}

if ("Uninitialized" in obj) {
return new Uninitialized()
}
if ("Autodrifting" in obj) {
return new Autodrifting()
}

throw new Error("Invalid enum object")
}
Copy link

Choose a reason for hiding this comment

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

The fromDecoded function uses a loose type any for its parameter. This can lead to potential type errors. Consider using a more specific type or interface if possible. Also, the function throws a generic error "Invalid enum object" in two different places. It would be more helpful to throw more specific error messages to help with debugging.

Comment on lines +68 to +79
export function fromJSON(
obj: types.RebalanceAutodriftStepJSON
): types.RebalanceAutodriftStepKind {
switch (obj.kind) {
case "Uninitialized": {
return new Uninitialized()
}
case "Autodrifting": {
return new Autodrifting()
}
}
}
Copy link

Choose a reason for hiding this comment

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

The fromJSON function does not handle the case where obj.kind is neither "Uninitialized" nor "Autodrifting". It would be better to throw an error in the default case of the switch statement to handle unexpected values.

68:
export function fromJSON(
69:
  obj: types.RebalanceAutodriftStepJSON
70:
): types.RebalanceAutodriftStepKind {
71:
  switch (obj.kind) {
72:
    case "Uninitialized": {
73:
      return new Uninitialized()
74:
    }
75:
    case "Autodrifting": {
76:
      return new Autodrifting()
77:
    }
+   default: {
+     throw new Error(`Invalid kind: ${obj.kind}`);
+   }
78:
  }
79:
}
Committable suggestion (Beta)
Suggested change
export function fromJSON(
obj: types.RebalanceAutodriftStepJSON
): types.RebalanceAutodriftStepKind {
switch (obj.kind) {
case "Uninitialized": {
return new Uninitialized()
}
case "Autodrifting": {
return new Autodrifting()
}
}
}
export function fromJSON(
obj: types.RebalanceAutodriftStepJSON
): types.RebalanceAutodriftStepKind {
switch (obj.kind) {
case "Uninitialized": {
return new Uninitialized()
}
case "Autodrifting": {
return new Autodrifting()
}
default: {
throw new Error(`Invalid kind: ${obj.kind}`);
}
}
}

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.

1 participant