From b8939de85fe523669a3854cea6ad68657f57b0e7 Mon Sep 17 00:00:00 2001 From: Loris Leiva Date: Sat, 6 Jan 2024 20:36:44 +0000 Subject: [PATCH] Add constant seeds in Rust client (#144) Ports #118 into the next minor release --- .changeset/wise-fishes-bathe.md | 5 +++ src/renderers/rust/getRenderMapVisitor.ts | 4 +++ src/renderers/rust/templates/accountsPage.njk | 31 +++++++++++++++++++ .../src/generated/accounts/delegate_record.rs | 9 ++++++ .../generated/accounts/frequency_account.rs | 8 +++++ .../generated/accounts/master_edition_v1.rs | 9 ++++++ .../generated/accounts/master_edition_v2.rs | 11 +++++++ .../rust/src/generated/accounts/metadata.rs | 9 ++++++ 8 files changed, 86 insertions(+) create mode 100644 .changeset/wise-fishes-bathe.md diff --git a/.changeset/wise-fishes-bathe.md b/.changeset/wise-fishes-bathe.md new file mode 100644 index 000000000..f9049f633 --- /dev/null +++ b/.changeset/wise-fishes-bathe.md @@ -0,0 +1,5 @@ +--- +'@metaplex-foundation/kinobi': patch +--- + +Add constant seeds in Rust client diff --git a/src/renderers/rust/getRenderMapVisitor.ts b/src/renderers/rust/getRenderMapVisitor.ts index e3bc506ba..4b2f7caea 100644 --- a/src/renderers/rust/getRenderMapVisitor.ts +++ b/src/renderers/rust/getRenderMapVisitor.ts @@ -155,6 +155,9 @@ export function getRenderMapVisitor(options: GetRustRenderMapOptions = {}) { }); const hasVariableSeeds = pdaSeeds.filter(isNodeFilter('variablePdaSeedNode')).length > 0; + const constantSeeds = pdaSeeds.filter( + isNodeFilter('constantPdaSeedNode') + ); const { imports } = typeManifest; @@ -172,6 +175,7 @@ export function getRenderMapVisitor(options: GetRustRenderMapOptions = {}) { program, typeManifest, seeds, + constantSeeds, hasVariableSeeds, }) ); diff --git a/src/renderers/rust/templates/accountsPage.njk b/src/renderers/rust/templates/accountsPage.njk index 6ec3a889c..dbe5fe06e 100644 --- a/src/renderers/rust/templates/accountsPage.njk +++ b/src/renderers/rust/templates/accountsPage.njk @@ -17,6 +17,37 @@ impl {{ account.name | pascalCase }} { pub const LEN: usize = {{ account.size }}; {% endif %} + {% if constantSeeds.length > 0 %} + {% set index = 0 %} + /// Prefix values used to generate a PDA for this account. + /// + /// Values are positional and appear in the following order: + /// + {% for seed in seeds %} + {% if seed.kind === 'programIdPdaSeedNode' %} + /// {{ loop.index0 }}. `crate::{{ program.name | snakeCase | upper }}_ID` + {% elif seed.kind === 'constantPdaSeedNode' %} + /// {{ loop.index0 }}. `{{ account.name | pascalCase }}::PREFIX{{ '.' + index if constantSeeds.length > 1 }}` + {% set index = index + 1 %} + {% else %} + /// {{ loop.index0 }}. {{ seed.name | snakeCase }} (`{{ seed.typeManifest.type }}`) + {% endif %} + {% endfor %} + {% if constantSeeds.length > 1 %} + pub const PREFIX: ( + {% for seed in constantSeeds %} + &'static [u8], + {% endfor %} + ) = ( + {% for seed in constantSeeds %} + {{ seed.value.render }}.as_bytes(), + {% endfor %} + ); + {% elif constantSeeds.length === 1 %} + pub const PREFIX: &'static [u8] = {{ constantSeeds[0].value.render }}.as_bytes(); + {% endif %} + {% endif %} + {% if seeds.length > 0 %} pub fn create_pda( {% if hasVariableSeeds %} diff --git a/test/packages/rust/src/generated/accounts/delegate_record.rs b/test/packages/rust/src/generated/accounts/delegate_record.rs index 4134ff6f0..1edd7834f 100644 --- a/test/packages/rust/src/generated/accounts/delegate_record.rs +++ b/test/packages/rust/src/generated/accounts/delegate_record.rs @@ -21,6 +21,15 @@ pub struct DelegateRecord { impl DelegateRecord { pub const LEN: usize = 282; + /// Prefix values used to generate a PDA for this account. + /// + /// Values are positional and appear in the following order: + /// + /// 0. `DelegateRecord::PREFIX` + /// 1. `crate::MPL_TOKEN_METADATA_ID` + /// 2. role (`DelegateRole`) + pub const PREFIX: &'static [u8] = "delegate_record".as_bytes(); + pub fn create_pda( role: DelegateRole, bump: u8, diff --git a/test/packages/rust/src/generated/accounts/frequency_account.rs b/test/packages/rust/src/generated/accounts/frequency_account.rs index 9d38c62e3..20ec30eb9 100644 --- a/test/packages/rust/src/generated/accounts/frequency_account.rs +++ b/test/packages/rust/src/generated/accounts/frequency_account.rs @@ -22,6 +22,14 @@ pub struct FrequencyAccount { impl FrequencyAccount { pub const LEN: usize = 24; + /// Prefix values used to generate a PDA for this account. + /// + /// Values are positional and appear in the following order: + /// + /// 0. `FrequencyAccount::PREFIX` + /// 1. `crate::MPL_TOKEN_AUTH_RULES_ID` + pub const PREFIX: &'static [u8] = "frequency_pda".as_bytes(); + pub fn create_pda( bump: u8, ) -> Result { diff --git a/test/packages/rust/src/generated/accounts/master_edition_v1.rs b/test/packages/rust/src/generated/accounts/master_edition_v1.rs index f937fa00e..aa42df391 100644 --- a/test/packages/rust/src/generated/accounts/master_edition_v1.rs +++ b/test/packages/rust/src/generated/accounts/master_edition_v1.rs @@ -30,6 +30,15 @@ pub struct MasterEditionV1 { } impl MasterEditionV1 { + /// Prefix values used to generate a PDA for this account. + /// + /// Values are positional and appear in the following order: + /// + /// 0. `MasterEditionV1::PREFIX` + /// 1. `crate::MPL_TOKEN_METADATA_ID` + /// 2. delegate_role (`DelegateRole`) + pub const PREFIX: &'static [u8] = "metadata".as_bytes(); + pub fn create_pda( delegate_role: DelegateRole, bump: u8, diff --git a/test/packages/rust/src/generated/accounts/master_edition_v2.rs b/test/packages/rust/src/generated/accounts/master_edition_v2.rs index 885a731ad..3810fc255 100644 --- a/test/packages/rust/src/generated/accounts/master_edition_v2.rs +++ b/test/packages/rust/src/generated/accounts/master_edition_v2.rs @@ -21,6 +21,17 @@ pub struct MasterEditionV2 { impl MasterEditionV2 { pub const LEN: usize = 282; + /// Prefix values used to generate a PDA for this account. + /// + /// Values are positional and appear in the following order: + /// + /// 0. `MasterEditionV2::PREFIX.0` + /// 1. `crate::MPL_TOKEN_METADATA_ID` + /// 2. mint (`Pubkey`) + /// 3. `MasterEditionV2::PREFIX.1` + pub const PREFIX: (&'static [u8], &'static [u8]) = + ("metadata".as_bytes(), "edition".as_bytes()); + pub fn create_pda( mint: Pubkey, bump: u8, diff --git a/test/packages/rust/src/generated/accounts/metadata.rs b/test/packages/rust/src/generated/accounts/metadata.rs index f6636cd95..df4884dc5 100644 --- a/test/packages/rust/src/generated/accounts/metadata.rs +++ b/test/packages/rust/src/generated/accounts/metadata.rs @@ -50,6 +50,15 @@ pub struct Metadata { impl Metadata { pub const LEN: usize = 679; + /// Prefix values used to generate a PDA for this account. + /// + /// Values are positional and appear in the following order: + /// + /// 0. `Metadata::PREFIX` + /// 1. `crate::MPL_TOKEN_METADATA_ID` + /// 2. mint (`Pubkey`) + pub const PREFIX: &'static [u8] = "metadata".as_bytes(); + pub fn create_pda( mint: Pubkey, bump: u8,