Crates.io | canonical-account-prefixes |
lib.rs | canonical-account-prefixes |
version | 0.1.0 |
source | src |
created_at | 2024-08-24 19:48:50.916304 |
updated_at | 2024-08-24 19:48:50.916304 |
description | A lib to make canonical constants in Solana and Anchor. |
homepage | https://github.com/kevinrodriguez-io/canonical-account-prefixes |
repository | https://github.com/kevinrodriguez-io/canonical-account-prefixes |
max_upload_size | |
id | 1350526 |
size | 8,105 |
Generates a const prefix for a given account name. This is useful for generating a prefix for a given account xname that can be used in a Solana Program.
Add the following to your Cargo.toml
:
[dependencies]
canonical_account_prefixes = "0.1.0"
If you're using anchor
, you can enable the anchor
feature:
[dependencies]
canonical_account_prefixes = { version = "0.1.0", features = ["anchor"] }
prefix!
macrouse canonical_account_prefixes::prefix;
prefix!(OTHER)
This generates the following const:
#[const] // If feature=anchor is enabled
pub const OTHER: [u8; 5] = [111, 116, 104, 101, 114];
Which is the equivalent of writing:
#[const] // If feature=anchor is enabled
pub const OTHER: [u8; 5] = *b"other";
That way you don't have to specify the string length.
prefix_account
attribute macrouse canonical_account_prefixes::prefix_account;
#[prefix_account]
pub struct Other {
pub data: [u8; 32],
}
Which is the equivalent of writing:
pub struct Other {
pub data: [u8; 32],
}
#[const] // If feature=anchor is enabled
pub const OTHER: [u8; 5] = *b"other";
For account names in UpperCamelCase
, the macro will do the following:
#[prefix_account]
pub struct UpperCamelCase {
pub data: [u8; 32],
}
Will generate:
pub struct UpperCamelCase {
pub data: [u8; 32],
}
#[const] // If feature=anchor is enabled
pub const UPPER_CAMEL_CASE: [u8; 16] = *b"upper_camel_case";
You can use this code under the MIT license. See LICENSE for more details.