Skip to content

Commit

Permalink
Add constant seeds (#118)
Browse files Browse the repository at this point in the history
* Add constant seeds

* Add changeset

* Run on version branch
  • Loading branch information
febo authored Dec 29, 2023
1 parent e4f7bc1 commit 7b2f489
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-falcons-beg.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: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Main

on:
push:
branches: [ main ]
branches: [ main, v0.16 ]
pull_request:
branches: [ main ]
branches: [ main, v0.16 ]

jobs:
lint_fix:
Expand Down
4 changes: 4 additions & 0 deletions src/renderers/rust/GetRustRenderMapVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ export class GetRustRenderMapVisitor extends BaseThrowVisitor<RenderMap> {
});
const hasVariableSeeds =
account.seeds.filter((seed) => seed.kind === 'variable').length > 0;
const constantSeeds = account.seeds.filter(
(seed) => seed.kind === 'constant'
).length;

const { imports } = typeManifest;

Expand All @@ -183,6 +186,7 @@ export class GetRustRenderMapVisitor extends BaseThrowVisitor<RenderMap> {
typeManifest,
seeds,
hasVariableSeeds,
constantSeeds,
})
);
}
Expand Down
41 changes: 41 additions & 0 deletions src/renderers/rust/templates/accountsPage.njk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,47 @@ impl {{ account.name | pascalCase }} {
{% if account.size != null %}
pub const LEN: usize = {{ account.size }};
{% endif %}
{% if constantSeeds > 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 === 'programId' %}
/// {{ loop.index0 }}. `crate::{{ program.name | snakeCase | upper }}_ID`
{% elif seed.kind === 'constant' %}
/// {{ loop.index0 }}. `{{ account.name | pascalCase }}::PREFIX{{ '.' + index if constantSeeds > 1 }}`
{% set index = index + 1 %}
{% else %}
/// {{ loop.index0 }}. {{ seed.name | snakeCase }} (`{{ seed.typeManifest.type }}`)
{% endif %}
{% endfor %}
{% if constantSeeds > 1 %}
pub const PREFIX: (
{% for seed in seeds %}
{% if seed.kind === 'constant' %}
&'static [u8],
{% endif %}
{% endfor %}
) = (
{% for seed in seeds %}
{% if seed.kind === 'constant' %}
{{ seed.value.render }}.as_bytes(),
{% endif %}
{% endfor %}
);
{% endif %}
{% if constantSeeds === 1 %}
pub const PREFIX: &'static [u8] =
{% for seed in seeds %}
{% if seed.kind === 'constant' %}
{{ seed.value.render }}.as_bytes()
{% endif %}
{% endfor %}
;
{% endif %}
{% endif %}

{% if seeds.length > 0 %}
pub fn create_pda(
Expand Down
8 changes: 8 additions & 0 deletions test/packages/rust/src/generated/accounts/delegate_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use borsh::BorshSerialize;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct EditionMarker {
pub key: TmKey,
#[cfg_attr(feature = "serde", serde_with::serde_as(as = "serde_with::Bytes"))]
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
pub ledger: [u8; 200],
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ 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,
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
10 changes: 10 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 @@ -20,6 +20,16 @@ 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,
Expand Down
8 changes: 8 additions & 0 deletions test/packages/rust/src/generated/accounts/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ 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,
Expand Down
2 changes: 1 addition & 1 deletion test/packages/rust/src/generated/types/hidden_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ pub struct HiddenSettings {
/// Shared URI
pub uri: String,
/// Hash of the hidden settings file
#[cfg_attr(feature = "serde", serde_with::serde_as(as = "serde_with::Bytes"))]
#[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
pub hash: [u8; 64],
}

0 comments on commit 7b2f489

Please sign in to comment.