-
Notifications
You must be signed in to change notification settings - Fork 90
feat: stateless js add pda helpers #1971
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
c41346f
chore: add address merkle tree pubkey print to light program test output
ananas-block 8ccb9de
feat: statelessjs add PackedAccounts v1 and v2
ananas-block 217564e
get address tree pubkey
ananas-block 66e578b
fix feedback
ananas-block c094fdd
fix: ts sdk anchor test
ananas-block c53986d
revert: anchor dev dep bump
ananas-block cd3f226
build anchor sdk test program for ci
ananas-block 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 |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
|
|
||
| # Rust | ||
| **/target/ | ||
| !sdk-tests/sdk-anchor-test/target/ | ||
|
|
||
| **/dist/ | ||
|
|
||
|
|
||
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
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
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
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,253 @@ | ||
| import { AccountMeta, PublicKey, SystemProgram } from '@solana/web3.js'; | ||
| import { defaultStaticAccountsStruct } from '../constants'; | ||
| import { LightSystemProgram } from '../programs'; | ||
|
|
||
| export class PackedAccounts { | ||
| private preAccounts: AccountMeta[] = []; | ||
| private systemAccounts: AccountMeta[] = []; | ||
| private nextIndex: number = 0; | ||
| private map: Map<PublicKey, [number, AccountMeta]> = new Map(); | ||
|
|
||
| static newWithSystemAccounts( | ||
| config: SystemAccountMetaConfig, | ||
| ): PackedAccounts { | ||
| const instance = new PackedAccounts(); | ||
| instance.addSystemAccounts(config); | ||
| return instance; | ||
| } | ||
|
|
||
| static newWithSystemAccountsV2( | ||
| config: SystemAccountMetaConfig, | ||
| ): PackedAccounts { | ||
| const instance = new PackedAccounts(); | ||
| instance.addSystemAccountsV2(config); | ||
| return instance; | ||
| } | ||
|
|
||
| addPreAccountsSigner(pubkey: PublicKey): void { | ||
| this.preAccounts.push({ pubkey, isSigner: true, isWritable: false }); | ||
| } | ||
|
|
||
| addPreAccountsSignerMut(pubkey: PublicKey): void { | ||
| this.preAccounts.push({ pubkey, isSigner: true, isWritable: true }); | ||
| } | ||
|
|
||
| addPreAccountsMeta(accountMeta: AccountMeta): void { | ||
| this.preAccounts.push(accountMeta); | ||
| } | ||
|
|
||
| addSystemAccounts(config: SystemAccountMetaConfig): void { | ||
| this.systemAccounts.push(...getLightSystemAccountMetas(config)); | ||
| } | ||
|
|
||
| addSystemAccountsV2(config: SystemAccountMetaConfig): void { | ||
| this.systemAccounts.push(...getLightSystemAccountMetasV2(config)); | ||
| } | ||
|
|
||
| insertOrGet(pubkey: PublicKey): number { | ||
| return this.insertOrGetConfig(pubkey, false, true); | ||
| } | ||
|
|
||
| insertOrGetReadOnly(pubkey: PublicKey): number { | ||
| return this.insertOrGetConfig(pubkey, false, false); | ||
| } | ||
|
|
||
| insertOrGetConfig( | ||
| pubkey: PublicKey, | ||
| isSigner: boolean, | ||
| isWritable: boolean, | ||
| ): number { | ||
| const entry = this.map.get(pubkey); | ||
| if (entry) { | ||
| return entry[0]; | ||
| } | ||
| const index = this.nextIndex++; | ||
| const meta: AccountMeta = { pubkey, isSigner, isWritable }; | ||
| this.map.set(pubkey, [index, meta]); | ||
| return index; | ||
| } | ||
|
|
||
| private hashSetAccountsToMetas(): AccountMeta[] { | ||
| const entries = Array.from(this.map.entries()); | ||
| entries.sort((a, b) => a[1][0] - b[1][0]); | ||
| return entries.map(([, [, meta]]) => meta); | ||
| } | ||
|
|
||
| private getOffsets(): [number, number] { | ||
| const systemStart = this.preAccounts.length; | ||
| const packedStart = systemStart + this.systemAccounts.length; | ||
| return [systemStart, packedStart]; | ||
| } | ||
|
|
||
| toAccountMetas(): { | ||
| remainingAccounts: AccountMeta[]; | ||
| systemStart: number; | ||
| packedStart: number; | ||
| } { | ||
| const packed = this.hashSetAccountsToMetas(); | ||
| const [systemStart, packedStart] = this.getOffsets(); | ||
| return { | ||
| remainingAccounts: [ | ||
| ...this.preAccounts, | ||
| ...this.systemAccounts, | ||
| ...packed, | ||
| ], | ||
| systemStart, | ||
| packedStart, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export class SystemAccountMetaConfig { | ||
| selfProgram: PublicKey; | ||
| cpiContext?: PublicKey; | ||
| solCompressionRecipient?: PublicKey; | ||
| solPoolPda?: PublicKey; | ||
|
|
||
| private constructor( | ||
| selfProgram: PublicKey, | ||
| cpiContext?: PublicKey, | ||
| solCompressionRecipient?: PublicKey, | ||
| solPoolPda?: PublicKey, | ||
| ) { | ||
| this.selfProgram = selfProgram; | ||
| this.cpiContext = cpiContext; | ||
| this.solCompressionRecipient = solCompressionRecipient; | ||
| this.solPoolPda = solPoolPda; | ||
| } | ||
|
|
||
| static new(selfProgram: PublicKey): SystemAccountMetaConfig { | ||
| return new SystemAccountMetaConfig(selfProgram); | ||
| } | ||
|
|
||
| static newWithCpiContext( | ||
| selfProgram: PublicKey, | ||
| cpiContext: PublicKey, | ||
| ): SystemAccountMetaConfig { | ||
| return new SystemAccountMetaConfig(selfProgram, cpiContext); | ||
| } | ||
| } | ||
|
|
||
| export function getLightSystemAccountMetas( | ||
| config: SystemAccountMetaConfig, | ||
| ): AccountMeta[] { | ||
| let signerSeed = new TextEncoder().encode('cpi_authority'); | ||
| const cpiSigner = PublicKey.findProgramAddressSync( | ||
| [signerSeed], | ||
| config.selfProgram, | ||
| )[0]; | ||
| const defaults = defaultStaticAccountsStruct(); | ||
| const metas: AccountMeta[] = [ | ||
| { | ||
| pubkey: LightSystemProgram.programId, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { pubkey: cpiSigner, isSigner: false, isWritable: false }, | ||
| { | ||
| pubkey: defaults.registeredProgramPda, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { pubkey: defaults.noopProgram, isSigner: false, isWritable: false }, | ||
| { | ||
| pubkey: defaults.accountCompressionAuthority, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { | ||
| pubkey: defaults.accountCompressionProgram, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { pubkey: config.selfProgram, isSigner: false, isWritable: false }, | ||
| ]; | ||
| if (config.solPoolPda) { | ||
| metas.push({ | ||
| pubkey: config.solPoolPda, | ||
| isSigner: false, | ||
| isWritable: true, | ||
| }); | ||
| } | ||
| if (config.solCompressionRecipient) { | ||
| metas.push({ | ||
| pubkey: config.solCompressionRecipient, | ||
| isSigner: false, | ||
| isWritable: true, | ||
| }); | ||
| } | ||
| metas.push({ | ||
| pubkey: SystemProgram.programId, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }); | ||
| if (config.cpiContext) { | ||
| metas.push({ | ||
| pubkey: config.cpiContext, | ||
| isSigner: false, | ||
| isWritable: true, | ||
| }); | ||
| } | ||
| return metas; | ||
| } | ||
|
|
||
| export function getLightSystemAccountMetasV2( | ||
| config: SystemAccountMetaConfig, | ||
| ): AccountMeta[] { | ||
| let signerSeed = new TextEncoder().encode('cpi_authority'); | ||
| const cpiSigner = PublicKey.findProgramAddressSync( | ||
| [signerSeed], | ||
| config.selfProgram, | ||
| )[0]; | ||
| const defaults = defaultStaticAccountsStruct(); | ||
| const metas: AccountMeta[] = [ | ||
| { | ||
| pubkey: LightSystemProgram.programId, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { pubkey: cpiSigner, isSigner: false, isWritable: false }, | ||
| { | ||
| pubkey: defaults.registeredProgramPda, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { | ||
| pubkey: defaults.accountCompressionAuthority, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { | ||
| pubkey: defaults.accountCompressionProgram, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| { | ||
| pubkey: SystemProgram.programId, | ||
| isSigner: false, | ||
| isWritable: false, | ||
| }, | ||
| ]; | ||
| if (config.solPoolPda) { | ||
| metas.push({ | ||
| pubkey: config.solPoolPda, | ||
| isSigner: false, | ||
| isWritable: true, | ||
| }); | ||
| } | ||
| if (config.solCompressionRecipient) { | ||
| metas.push({ | ||
| pubkey: config.solCompressionRecipient, | ||
| isSigner: false, | ||
| isWritable: true, | ||
| }); | ||
| } | ||
| if (config.cpiContext) { | ||
| metas.push({ | ||
| pubkey: config.cpiContext, | ||
| isSigner: false, | ||
| isWritable: true, | ||
| }); | ||
| } | ||
| return metas; | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.