Skip to content

Commit

Permalink
Add constant seeds in Rust client (#144)
Browse files Browse the repository at this point in the history
Ports #118 into the next minor release
  • Loading branch information
lorisleiva authored Jan 6, 2024
1 parent cdac690 commit b8939de
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-fishes-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@metaplex-foundation/kinobi': patch
---

Add constant seeds in Rust client
4 changes: 4 additions & 0 deletions src/renderers/rust/getRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -172,6 +175,7 @@ export function getRenderMapVisitor(options: GetRustRenderMapOptions = {}) {
program,
typeManifest,
seeds,
constantSeeds,
hasVariableSeeds,
})
);
Expand Down
31 changes: 31 additions & 0 deletions src/renderers/rust/templates/accountsPage.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
9 changes: 9 additions & 0 deletions test/packages/rust/src/generated/accounts/delegate_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<solana_program::pubkey::Pubkey, solana_program::pubkey::PubkeyError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions test/packages/rust/src/generated/accounts/master_edition_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions test/packages/rust/src/generated/accounts/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b8939de

Please sign in to comment.