4 个稳定版本

1.3.0 2024年4月16日
1.2.0 2023年12月14日
1.1.0 2023年12月14日
1.0.0 2023年12月11日

#162#codegen

Download history 68/week @ 2024-04-17 57/week @ 2024-04-24 53/week @ 2024-05-01 46/week @ 2024-05-08 82/week @ 2024-05-15 100/week @ 2024-05-22 108/week @ 2024-05-29 80/week @ 2024-06-05 58/week @ 2024-06-12 36/week @ 2024-06-19 64/week @ 2024-06-26 99/week @ 2024-07-03 59/week @ 2024-07-10 76/week @ 2024-07-17 151/week @ 2024-07-24 83/week @ 2024-07-31

每月388 次下载

MIT/Apache

18KB
306

sanctum-macros

https://github.com/Ellipsis-Labs/ellipsis-macros 启发

编译时 Pubkey 常量生成

solana_program 应该是使用这些宏的 crate 中声明的依赖。

declare_program_keys!

// first arg = program ID
// second arg = list of static PDA names and their seeds.
//
// Rules from solana runtime:
// - Each seed can have a max length of 32 bytes.
// - Each PDA can have max 16 seeds.
sanctum_macros::declare_program_keys!(
    "9BoN4yBYwH63LFM9fDamaHK62YjM56hWYZqok7MnAakJ",
    [
        ("state", b"state"),
        ("empty-kebab", b""),
        ("multiseed", b"two", b"seeds"),
    ]
);

展开为

pub const ID_STR: &str = "9BoN4yBYwH63LFM9fDamaHK62YjM56hWYZqok7MnAakJ";
pub const ID_BYTES: [u8; 32] = [121, 161, 186, 2, 16, 170, 248, 125, 201, 230, 113, 160, 74, 35, 69, 149, 10, 116, 97, 215, 244, 204, 210, 189, 7, 112, 233, 119, 14, 109, 226, 43];
pub const ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(ID_BYTES);

// let (STATE_ID, STATE_BUMP) = Pubkey::find_program_address(&[STATE_SEED], &ID);
pub const STATE_SEED: &[u8] = b"state";
pub const STATE_BUMP: u8 = 255;
pub const STATE_ID_BYTES: [u8; 32] = [182, 221, 112, 246, 145, 207, 204, 110, 1, 1, 34, 100, 242, 173, 44, 12, 6, 58, 98, 95, 54, 209, 117, 196, 110, 161, 65, 215, 10, 127, 217, 120];
pub const STATE_ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(STATE_ID_BYTES);
pub const STATE_ID_STR: &str = "DJq3bbgiJq34LKrH37UEb7rXDaWTUGjnieLZamkRvu5R";

// let (EMPTY_KEBAB_ID, EMPTY_KEBAB_BUMP) = Pubkey::find_program_address(&[EMPTY_KEBAB_SEED], &ID);
pub const EMPTY_KEBAB_SEED: &[u8] = b"";
pub const EMPTY_KEBAB_BUMP: u8 = 255;
pub const EMPTY_KEBAB_ID_BYTES: [u8; 32] = [149, 184, 104, 22, 114, 239, 248, 126, 73, 171, 206, 5, 196, 95, 255, 54, 180, 176, 70, 241, 246, 15, 193, 242, 103, 208, 21, 144, 97, 138, 236, 108];
pub const EMPTY_KEBAB_ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(EMPTY_KEBAB_ID_BYTES);
pub const EMPTY_KEBAB_ID_STR: &str = "B5SqYyds9eLeX5mK4uycKGgZHft1URCbTzU6LoWhCV63";

// let (MULTISEED_ID, MULTISEED_BUMP) = Pubkey::find_program_address(&[MULTISEED_SEED_0, MULTISEED_SEED_1], &ID);
pub const MULTISEED_SEED_0: &[u8] = b"two";
pub const MULTISEED_SEED_1: &[u8] = b"seeds";
pub const MULTISEED_BUMP: u8 = 255;
pub const MULTISEED_ID_BYTES: [u8; 32] = [98, 108, 4, 246, 99, 56, 99, 58, 187, 75, 184, 142, 246, 2, 131, 19, 58, 122, 3, 133, 156, 253, 176, 142, 61, 20, 125, 96, 17, 29, 112, 113];
pub const MULTISEED_ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(MULTISEED_ID_BYTES);
pub const MULTISEED_ID_STR: &str = "7dCVGBbemkex9cQNMp17kupcL41jueGXwjSrc2NWf6Ek";

create_with_seed!

使用 Pubkey::create_with_seed 创建一个 Pubkey

// args: (base, seed, owner)
sanctum_macros::create_with_seed!(
    "9BoN4yBYwH63LFM9fDamaHK62YjM56hWYZqok7MnAakJ",
    "seed",
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
);

展开为

// let ID = Pubkey::create_with_seed(&BASE_ID, SEED, OWNER_ID).unwrap();
pub const ID_BYTES: [u8; 32] = [213, 14, 91, 195, 42, 234, 158, 119, 156, 244, 205, 121, 213, 202, 90, 242, 52, 33, 180, 126, 177, 142, 7, 59, 192, 104, 238, 67, 90, 112, 177, 48];
pub const ID_STR: &str = "FLgYDs1daqCvXAmz5vqij39zFdSraks4S6xuKvf2NJsZ";
pub const ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(ID_BYTES);

pub const BASE_ID_BYTES: [u8; 32] = [121, 161, 186, 2, 16, 170, 248, 125, 201, 230, 113, 160, 74, 35, 69, 149, 10, 116, 97, 215, 244, 204, 210, 189, 7, 112, 233, 119, 14, 109, 226, 43];
pub const BASE_ID_STR: &str = "9BoN4yBYwH63LFM9fDamaHK62YjM56hWYZqok7MnAakJ";
pub const BASE_ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(BASE_ID_BYTES);

pub const SEED: &str = "seed";

pub const OWNER_ID_BYTES: [u8; 32] = [6, 221, 246, 225, 215, 101, 161, 147, 217, 203, 225, 70, 206, 235, 121, 172, 28, 180, 133, 237, 95, 91, 55, 145, 58, 140, 245, 133, 126, 255, 0, 169];
pub const OWNER_ID_STR: &str = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
pub const OWNER_ID: solana_program::pubkey::Pubkey = solana_program::pubkey::Pubkey::new_from_array(OWNER_ID_BYTES);

当与 system_instruction::create_account_with_seed() 一起使用时,将在 ID 处创建一个新的由 OWNER_ID 程序拥有的账户。 BASE_ID 必须是签署者。

依赖关系

~16–25MB
~420K SLoC