-
Notifications
You must be signed in to change notification settings - Fork 200
feat: customized cadence account loader #1666
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bfcc27
feat: customized cadence account loader bby
LukasDeco 01990a3
feat: method to read account cadence on custom cadence account loader
LukasDeco 870075a
feat: PR feedback on customized loader cleaup code and better naming
LukasDeco 3e40a43
fix: lint and prettify
LukasDeco b99f563
feat: more efficient rpc polling on custom polling intervals
LukasDeco e74ca7e
feat: custom cadence acct loader override load
LukasDeco 549e396
chore: prettify
LukasDeco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE } from '../constants/numericConstants'; | ||
| import { BulkAccountLoader } from './bulkAccountLoader'; | ||
| import { Commitment, Connection, PublicKey } from '@solana/web3.js'; | ||
| import { v4 as uuidv4 } from 'uuid'; | ||
|
|
||
| export class CustomizedCadenceBulkAccountLoader extends BulkAccountLoader { | ||
| private customIntervalId: NodeJS.Timeout | null; | ||
| private accountFrequencies: Map<string, number>; | ||
| private lastPollingTime: Map<string, number>; | ||
| private defaultPollingFrequency: number; | ||
|
|
||
| constructor( | ||
| connection: Connection, | ||
| commitment: Commitment, | ||
| defaultPollingFrequency: number | ||
| ) { | ||
| super(connection, commitment, defaultPollingFrequency); | ||
| this.customIntervalId = null; | ||
| this.accountFrequencies = new Map(); | ||
| this.lastPollingTime = new Map(); | ||
| this.defaultPollingFrequency = defaultPollingFrequency; | ||
| } | ||
|
|
||
| private getAccountsToLoad(): Array<{ | ||
| publicKey: PublicKey; | ||
| callbacks: Map<string, (buffer: Buffer, slot: number) => void>; | ||
| }> { | ||
| const currentTime = Date.now(); | ||
| const accountsToLoad: Array<{ | ||
| publicKey: PublicKey; | ||
| callbacks: Map<string, (buffer: Buffer, slot: number) => void>; | ||
| }> = []; | ||
|
|
||
| for (const [key, frequency] of this.accountFrequencies.entries()) { | ||
| const lastPollTime = this.lastPollingTime.get(key) || 0; | ||
| if (currentTime - lastPollTime >= frequency) { | ||
| const account = this.accountsToLoad.get(key); | ||
| if (account) { | ||
| accountsToLoad.push(account); | ||
| this.lastPollingTime.set(key, currentTime); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return accountsToLoad; | ||
| } | ||
|
|
||
| public async load(): Promise<void> { | ||
| return this.handleAccountLoading(); | ||
| } | ||
|
|
||
| private async handleAccountLoading(): Promise<void> { | ||
| const accounts = this.getAccountsToLoad(); | ||
|
|
||
| if (accounts.length > 0) { | ||
| const chunks = this.chunks( | ||
| this.chunks(accounts, GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE), | ||
| 10 | ||
| ); | ||
|
|
||
| await Promise.all( | ||
| chunks.map((chunk) => { | ||
| return this.loadChunk(chunk); | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public setCustomPollingFrequency( | ||
| publicKey: PublicKey, | ||
| newFrequency: number | ||
| ): void { | ||
| const key = publicKey.toBase58(); | ||
| this.accountFrequencies.set(key, newFrequency); | ||
| this.lastPollingTime.set(key, 0); // Reset last polling time to ensure immediate load | ||
| this.restartPollingIfNeeded(newFrequency); | ||
| } | ||
|
|
||
| private restartPollingIfNeeded(newFrequency: number): void { | ||
| const currentMinFrequency = Math.min( | ||
| ...Array.from(this.accountFrequencies.values()), | ||
| this.defaultPollingFrequency | ||
| ); | ||
|
|
||
| if (newFrequency < currentMinFrequency || !this.customIntervalId) { | ||
| this.stopPolling(); | ||
| this.startPolling(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds an account to be monitored by the bulk account loader | ||
| * @param publicKey The public key of the account to monitor | ||
| * @param callback Function to be called when account data is received | ||
| * @param customPollingFrequency Optional custom polling frequency in ms for this specific account. | ||
| * If not provided, will use the default polling frequency | ||
| * @returns A unique callback ID that can be used to remove this specific callback later | ||
| * | ||
| * The method will: | ||
| * 1. Create a new callback mapping for the account | ||
| * 2. Set up polling frequency tracking for the account | ||
| * 3. Reset last polling time to 0 to ensure immediate data fetch | ||
| * 4. Automatically restart polling if this account needs a faster frequency than existing accounts | ||
| */ | ||
| public async addAccount( | ||
| publicKey: PublicKey, | ||
| callback: (buffer: Buffer, slot: number) => void, | ||
| customPollingFrequency?: number | ||
| ): Promise<string> { | ||
| const callbackId = uuidv4(); | ||
| const callbacks = new Map<string, (buffer: Buffer, slot: number) => void>(); | ||
| callbacks.set(callbackId, callback); | ||
| const newAccountToLoad = { | ||
| publicKey, | ||
| callbacks, | ||
| }; | ||
| this.accountsToLoad.set(publicKey.toString(), newAccountToLoad); | ||
|
|
||
| const key = publicKey.toBase58(); | ||
| const frequency = customPollingFrequency || this.defaultPollingFrequency; | ||
| this.accountFrequencies.set(key, frequency); | ||
| this.lastPollingTime.set(key, 0); // Reset last polling time to ensure immediate load | ||
|
|
||
| this.restartPollingIfNeeded(frequency); | ||
|
|
||
| return callbackId; | ||
| } | ||
|
|
||
| public removeAccount(publicKey: PublicKey): void { | ||
| const key = publicKey.toBase58(); | ||
| this.accountFrequencies.delete(key); | ||
| this.lastPollingTime.delete(key); | ||
|
|
||
| if (this.accountsToLoad.size === 0) { | ||
| this.stopPolling(); | ||
| } else { | ||
| // Restart polling in case we removed the account with the smallest frequency | ||
| this.restartPollingIfNeeded(this.defaultPollingFrequency); | ||
| } | ||
| } | ||
|
|
||
| public getAccountCadence(publicKey: PublicKey): number | null { | ||
| const key = publicKey.toBase58(); | ||
| return this.accountFrequencies.get(key) || null; | ||
| } | ||
|
|
||
| public startPolling(): void { | ||
| if (this.customIntervalId) { | ||
| return; | ||
| } | ||
|
|
||
| const minFrequency = Math.min( | ||
| ...Array.from(this.accountFrequencies.values()), | ||
| this.defaultPollingFrequency | ||
| ); | ||
|
|
||
| this.customIntervalId = setInterval(() => { | ||
| this.handleAccountLoading().catch((error) => { | ||
| console.error('Error in account loading:', error); | ||
| }); | ||
| }, minFrequency); | ||
| } | ||
|
|
||
| public stopPolling(): void { | ||
| super.stopPolling(); | ||
|
|
||
| if (this.customIntervalId) { | ||
| clearInterval(this.customIntervalId); | ||
| this.customIntervalId = null; | ||
| } | ||
| this.lastPollingTime.clear(); | ||
| } | ||
|
|
||
| public clearAccountFrequencies(): void { | ||
| this.accountFrequencies.clear(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
is the idea you use this instead of a normal BulkAccountLoader? when does the default frequency polling get triggered?
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.
I wanted to replace the startPolling method to make sure things were not polled incorrectly(only using one interval) but maybe I can adjust that and maybe it seems odd the way it is...
Uh oh!
There was an error while loading. Please reload this page.
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.
Also, default frequency gets set when you add an account to this class and you don't specify a frequency, then it just uses the default(on line 96).