-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
fix(suite): hide non-Everstake Solana staking accounts #17181
fix(suite): hide non-Everstake Solana staking accounts #17181
Conversation
WalkthroughThe changes involve updates to the Solana staking module and the associated reward-fetching functionality. The import statement for ✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 2
🧹 Nitpick comments (2)
suite-common/wallet-core/src/stake/stakeConstants.ts (1)
19-19
: Consider consolidating the validator public key constant.The same validator public key is duplicated in
stakingAccounts.ts
. Consider consolidating this into a single constant to avoid potential maintenance issues.Move the constant to a common location and import it in both files:
-export const EVERSTAKE_VALIDATOR = '9QU2QSxhb24FUX3Tu2FpczXjpK3VYrvRudywSZaM29mF'; +// Move to a common location (e.g., common/constants.ts) +export const EVERSTAKE_VALIDATOR_PUBKEYS = { + mainnet: '9QU2QSxhb24FUX3Tu2FpczXjpK3VYrvRudywSZaM29mF', + devnet: 'GkqYQysEGmuL6V2AJoNnWZUz2ZBGWhzQXsJiXm2CLKAN', +} as const; +export const EVERSTAKE_VALIDATOR = EVERSTAKE_VALIDATOR_PUBKEYS.mainnet;packages/blockchain-link/src/workers/solana/stakingAccounts.ts (1)
7-10
: Consider using an enum or object for voter public keys.Using array indices for different environments is error-prone and less maintainable.
-const EVERSTAKE_VOTER_PUBKEYS = [ - '9QU2QSxhb24FUX3Tu2FpczXjpK3VYrvRudywSZaM29mF', // mainnet - 'GkqYQysEGmuL6V2AJoNnWZUz2ZBGWhzQXsJiXm2CLKAN', // devnet -]; +const EVERSTAKE_VOTER_PUBKEYS = { + mainnet: '9QU2QSxhb24FUX3Tu2FpczXjpK3VYrvRudywSZaM29mF', + devnet: 'GkqYQysEGmuL6V2AJoNnWZUz2ZBGWhzQXsJiXm2CLKAN', +} as const;Then update the includes check:
- if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; + if (voterPubkey !== EVERSTAKE_VOTER_PUBKEYS[isTestnet ? 'devnet' : 'mainnet']) return;
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
packages/blockchain-link/src/workers/solana/index.ts
(1 hunks)packages/blockchain-link/src/workers/solana/stakingAccounts.ts
(1 hunks)suite-common/wallet-core/src/stake/stakeConstants.ts
(1 hunks)suite-common/wallet-core/src/stake/stakeThunks.ts
(2 hunks)
✅ Files skipped from review due to trivial changes (1)
- packages/blockchain-link/src/workers/solana/index.ts
🔇 Additional comments (1)
suite-common/wallet-core/src/stake/stakeThunks.ts (1)
131-135
: LGTM! The request configuration is properly implemented.The changes correctly:
- Include the validator parameter in the request body
- URL encode the parameter value
- Set the appropriate Content-Type header
if (state && 'fields' in state) { | ||
const { fields } = state; | ||
|
||
const voterPubkey = fields[1]?.delegation?.voterPubkey; | ||
if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; // filter out non-everstake accounts | ||
|
||
return { | ||
rentExemptReserve: fields[0]?.rentExemptReserve, | ||
stake: fields[1]?.delegation?.stake, | ||
status: stakeState, | ||
}; | ||
} |
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.
🛠️ Refactor suggestion
Add type safety for state fields access.
The code assumes the structure of fields
array without proper type checking, which could lead to runtime errors.
if (state && 'fields' in state) {
const { fields } = state;
+ const meta = fields[0];
+ const stake = fields[1];
+
+ if (!meta || !stake || !('delegation' in stake)) {
+ return;
+ }
- const voterPubkey = fields[1]?.delegation?.voterPubkey;
+ const voterPubkey = stake.delegation.voterPubkey;
if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return;
return {
- rentExemptReserve: fields[0]?.rentExemptReserve,
- stake: fields[1]?.delegation?.stake,
+ rentExemptReserve: meta.rentExemptReserve,
+ stake: stake.delegation.stake,
status: stakeState,
};
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (state && 'fields' in state) { | |
const { fields } = state; | |
const voterPubkey = fields[1]?.delegation?.voterPubkey; | |
if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; // filter out non-everstake accounts | |
return { | |
rentExemptReserve: fields[0]?.rentExemptReserve, | |
stake: fields[1]?.delegation?.stake, | |
status: stakeState, | |
}; | |
} | |
if (state && 'fields' in state) { | |
const { fields } = state; | |
const meta = fields[0]; | |
const stake = fields[1]; | |
if (!meta || !stake || !('delegation' in stake)) { | |
return; | |
} | |
const voterPubkey = stake.delegation.voterPubkey; | |
if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; // filter out non-everstake accounts | |
return { | |
rentExemptReserve: meta.rentExemptReserve, | |
stake: stake.delegation.stake, | |
status: stakeState, | |
}; | |
} |
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.
LGTM just needs rebase+squash
f824460
to
394ba64
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.
Actionable comments posted: 0
♻️ Duplicate comments (1)
packages/blockchain-link/src/workers/solana/stakingAccounts.ts (1)
39-40
: 🛠️ Refactor suggestionAdd type safety for state fields access.
The code assumes the structure of
fields
array without proper type checking, which could lead to runtime errors.if (state && 'fields' in state) { const { fields } = state; + const meta = fields[0]; + const stake = fields[1]; + + if (!meta || !stake || !('delegation' in stake)) { + return; + } - const voterPubkey = fields[1]?.delegation?.voterPubkey; - if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; // filter out non-everstake accounts + const voterPubkey = stake.delegation.voterPubkey; + if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; // filter out non-everstake accounts return { - rentExemptReserve: fields[0]?.rentExemptReserve, - stake: fields[1]?.delegation?.stake, + rentExemptReserve: meta.rentExemptReserve, + stake: stake.delegation.stake, status: stakeState, }; }
🧹 Nitpick comments (3)
packages/blockchain-link/src/workers/solana/stakingAccounts.ts (3)
14-15
: Add validation for the serverUrl parameter.It's important to validate the serverUrl parameter to prevent potential issues if it's empty or malformed.
export const getSolanaStakingData = async ( descriptor: string, isTestnet: boolean, epoch: number, serverUrl: string, ): Promise<SolanaStakingAccount[]> => { + if (!serverUrl) { + throw new Error('Server URL is required'); + } const network = isTestnet ? Network.Devnet : Network.Mainnet;
26-49
: Consider optimizing the map and filter operations.The current implementation maps all staking accounts and then filters out undefined values. Consider combining these operations for better performance.
- return stakingAccounts - .map(account => { + return stakingAccounts.reduce<SolanaStakingAccount[]>((validAccounts, account) => { const stakeAccount = account?.data; - if (!stakeAccount) return; + if (!stakeAccount) return validAccounts; const stakeState = stakeAccountState(stakeAccount, BigInt(epoch)); const { state } = account?.data ?? {}; - if (!isStake(state)) return; + if (!isStake(state)) return validAccounts; if (state && 'fields' in state) { const { fields } = state; + const meta = fields[0]; + const stake = fields[1]; + + if (!meta || !stake || !('delegation' in stake)) { + return validAccounts; + } - const voterPubkey = fields[1]?.delegation?.voterPubkey; - if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return; // filter out non-everstake accounts + const voterPubkey = stake.delegation.voterPubkey; + if (!EVERSTAKE_VOTER_PUBKEYS.includes(voterPubkey)) return validAccounts; // filter out non-everstake accounts - return { - rentExemptReserve: fields[0]?.rentExemptReserve, - stake: fields[1]?.delegation?.stake, + validAccounts.push({ + rentExemptReserve: meta.rentExemptReserve, + stake: stake.delegation.stake, status: stakeState, - }; + }); + return validAccounts; } - }) - .filter(account => account !== undefined); + return validAccounts; + }, []);
1-50
: Add documentation explaining the filtering of non-Everstake accounts.The code now filters out non-Everstake accounts, but there's no documentation explaining why this decision was made or providing context for users.
Consider adding a JSDoc comment to the function explaining the purpose of filtering:
+/** + * Retrieves Solana staking data for a given descriptor. + * Only returns staking accounts that are delegated to Everstake validators. + * This filtering is done to streamline the user experience by showing only staking accounts + * that are relevant to the Trezor Suite's support for Everstake staking services. + */ export const getSolanaStakingData = async ( descriptor: string, isTestnet: boolean, epoch: number, serverUrl: string, ): Promise<SolanaStakingAccount[]> => {
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/blockchain-link/src/workers/solana/index.ts
(2 hunks)packages/blockchain-link/src/workers/solana/stakingAccounts.ts
(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/blockchain-link/src/workers/solana/index.ts
🔇 Additional comments (3)
packages/blockchain-link/src/workers/solana/stakingAccounts.ts (3)
5-8
: Good addition of the Everstake voter public keys constant.The constant with comments clarifying which keys are for mainnet vs. devnet provides a clean way to filter staking accounts.
14-15
: Correctly implemented the serverUrl parameter as suggested.The function now accepts the serverUrl as a parameter instead of trying to derive it from a configuration file, which resolves the issue mentioned in the previous review.
18-18
: Parameter order verified for Solana client initialization.The serverUrl parameter is correctly passed to the Solana client constructor.
Description
Hid non-Everstake Solana staking accounts and rewards
Related Issue
Resolve #15229
Screenshots: