Skip to content

Commit 6c849d8

Browse files
committed
add codama macros
1 parent f503ccf commit 6c849d8

File tree

5 files changed

+237
-4
lines changed

5 files changed

+237
-4
lines changed

Cargo.lock

Lines changed: 211 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

program/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ no-entrypoint = []
1212

1313
[dependencies]
1414
bytemuck = { version = "1.23.1", features = ["derive"] }
15+
codama = "0.6.3"
1516
num-derive = "0.4"
1617
num-traits = "0.2"
1718
solana-account-info = "3.0.0"

program/src/error.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
//! Error types
22
3-
use {num_derive::FromPrimitive, solana_program_error::ProgramError, thiserror::Error};
3+
use {
4+
codama::CodamaErrors, num_derive::FromPrimitive, solana_program_error::ProgramError,
5+
thiserror::Error,
6+
};
47

58
/// Errors that may be returned by the program.
6-
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
9+
#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq, CodamaErrors)]
710
pub enum RecordError {
811
/// Incorrect authority provided on update or delete
912
#[error("Incorrect authority provided on update or delete")]

program/src/instruction.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22
33
use {
44
crate::id,
5+
codama::{codama, CodamaInstructions},
56
solana_instruction::{AccountMeta, Instruction},
67
solana_program_error::ProgramError,
78
solana_pubkey::Pubkey,
89
std::mem::size_of,
910
};
1011

1112
/// Instructions supported by the program
12-
#[derive(Clone, Debug, PartialEq)]
13+
#[derive(Clone, Debug, PartialEq, CodamaInstructions)]
1314
pub enum RecordInstruction<'a> {
1415
/// Create a new record
1516
///
1617
/// Accounts expected by this instruction:
1718
///
1819
/// 0. `[writable]` Record account, must be uninitialized
1920
/// 1. `[]` Record authority
21+
#[codama(account(name = "record_account", writable))]
22+
#[codama(account(name = "authority"))]
2023
Initialize,
2124

2225
/// Write to the provided record account
@@ -25,10 +28,14 @@ pub enum RecordInstruction<'a> {
2528
///
2629
/// 0. `[writable]` Record account, must be previously initialized
2730
/// 1. `[signer]` Current record authority
31+
#[codama(account(name = "record_account", writable))]
32+
#[codama(account(name = "authority", signer))]
2833
Write {
2934
/// Offset to start writing record, expressed as `u64`.
3035
offset: u64,
3136
/// Data to replace the existing record data
37+
#[codama(type = bytes)]
38+
#[codama(size_prefix = number(u32))]
3239
data: &'a [u8],
3340
},
3441

@@ -39,6 +46,9 @@ pub enum RecordInstruction<'a> {
3946
/// 0. `[writable]` Record account, must be previously initialized
4047
/// 1. `[signer]` Current record authority
4148
/// 2. `[]` New record authority
49+
#[codama(account(name = "record_account", writable))]
50+
#[codama(account(name = "authority", signer))]
51+
#[codama(account(name = "new_authority"))]
4252
SetAuthority,
4353

4454
/// Close the provided record account, draining lamports to recipient
@@ -49,6 +59,9 @@ pub enum RecordInstruction<'a> {
4959
/// 0. `[writable]` Record account, must be previously initialized
5060
/// 1. `[signer]` Record authority
5161
/// 2. `[]` Receiver of account lamports
62+
#[codama(account(name = "record_account", writable))]
63+
#[codama(account(name = "authority", signer))]
64+
#[codama(account(name = "receiver", writable))]
5265
CloseAccount,
5366

5467
/// Reallocate additional space in a record account
@@ -60,6 +73,8 @@ pub enum RecordInstruction<'a> {
6073
///
6174
/// 0. `[writable]` The record account to reallocate
6275
/// 1. `[signer]` The account's owner
76+
#[codama(account(name = "record_account", writable))]
77+
#[codama(account(name = "authority", signer))]
6378
Reallocate {
6479
/// The length of the data to hold in the record account excluding meta
6580
/// data

program/src/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
//! Program state
22
use {
33
bytemuck::{Pod, Zeroable},
4+
codama::{codama, CodamaAccount},
45
solana_program_pack::IsInitialized,
56
solana_pubkey::Pubkey,
67
};
78

89
/// Header type for recorded account data
910
#[repr(C)]
10-
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
11+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable, CodamaAccount)]
12+
#[codama(discriminator(field = "version"))]
1113
pub struct RecordData {
1214
/// Struct version, allows for upgrades to the program
15+
#[codama(default_value = 1)]
1316
pub version: u8,
1417

1518
/// The account allowed to update the data

0 commit comments

Comments
 (0)