-
Notifications
You must be signed in to change notification settings - Fork 11
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
Conversation
WalkthroughThe 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
TipsChat with CodeRabbit Bot (
|
d57fd60
to
756dd39
Compare
There was a problem hiding this 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
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
andtokenBTwap
were being assigned. This change is reflected in the new hunk at lines 1647-1650 wheretokenATwap
andtokenBTwap
are now assigned the result ofstripTwapZeros
function. This change seems to be in line with the PR summary, which mentions the introduction ofstripTwapZeros
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 oftokenA
andtokenB
. 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 tonull
. 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. ThetoJSON
andtoEncodable
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 theConstant
class. ThetoJSON
andtoEncodable
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 thekind
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 theConstant
andScope
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 instancediscriminator
andkind
properties. ThetoJSON
andtoEncodable
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 theUpdateStakingRateChain
object. This function now returns an instance ofUpdateStakingRateChain
when the input object has a propertyUpdateStakingRateChain
. This change is consistent with the handling of other update modes in this function.365-367: The
fromJSON
function is updated to handle theUpdateStakingRateChain
object. This function now returns an instance ofUpdateStakingRateChain
when thekind
property of the input JSON object isUpdateStakingRateChain
. This change is consistent with the handling of other update modes in this function.384:
Thelayout
function is updated to includeUpdateStakingRateChain
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 interfaceAutodriftJSON
have been added. This class represents a new rebalance type. It includestoJSON()
andtoEncodable()
methods for serialization and deserialization. Thediscriminator
property is set to7
, 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 newAutodrift
rebalance type. If the decoded object contains anAutodrift
property, a newAutodrift
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 newAutodrift
rebalance type. If the JSON object contains anAutodrift
property, a newAutodrift
instance is returned. This change is consistent with the handling of other rebalance types in this function.264:
Thelayout()
function has been updated to include the newAutodrift
rebalance type in theborsh
layout. This change allows for efficient encoding and decoding of theAutodrift
type. Ensure that this new rebalance type is handled correctly in all parts of the codebase where thelayout()
function is used.packages/kamino-sdk/src/kamino-client/errors/custom.ts (6)
106-106: The
InvalidTwapId
error type has been replaced withInvalidScopeTWAPChain
. 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
, andDriftingOppositeDirection
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 theInvalidTwapId
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
, andDriftingOppositeDirection
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 withInvalidScopeTWAPChain
in thefromCode
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
, andDriftingOppositeDirection
have been added to thefromCode
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in theCollateralInfoFields
interface. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in theCollateralInfo
class. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in theCollateralInfo
class constructor. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in thelayout
function. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in thefromDecoded
function. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in thefromJSON
function. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in thetoJSON
function. Ensure that all references toscopePriceIdTwap
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 withscopeTwapPriceChain
andscopeStakingRateChain
properties in thefromJSON
function. Ensure that all references toscopePriceIdTwap
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.
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" | ||
|
There was a problem hiding this comment.
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.
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") | ||
} |
There was a problem hiding this comment.
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.
export function fromJSON( | ||
obj: types.RebalanceAutodriftStepJSON | ||
): types.RebalanceAutodriftStepKind { | ||
switch (obj.kind) { | ||
case "Uninitialized": { | ||
return new Uninitialized() | ||
} | ||
case "Autodrifting": { | ||
return new Autodrifting() | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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)
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}`); | |
} | |
} | |
} |
Summary by CodeRabbit
Autodrift
andUpdateStakingRateChain
functionalities to enhance the flexibility of rebalancing and updating staking rates.CollateralInfo
class with additional properties for better handling of TWAP price chains and staking rate chains.stripTwapZeros
to filter out zero values from TWAP price chains, improving data quality.Kamino
class and itsgetStrategiesShareData
method.These updates aim to improve the overall user experience by providing more accurate data and better error handling.