Skip to content

Commit

Permalink
[wip]: Refactor program structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando Esteban Barril Otero committed Jan 8, 2025
1 parent b1758be commit 937cdb1
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 414 deletions.
35 changes: 23 additions & 12 deletions program/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult,
program_error::PrintProgramError, pubkey::Pubkey,
#[cfg(feature = "logging")]
use pinocchio::msg;
use pinocchio::{
account_info::AccountInfo, entrypoint, program_error::ProgramError, pubkey::Pubkey,
ProgramResult,
};

use crate::{error::CounterError, processor};
use crate::{instruction::ProgramMetadataInstruction, processor::write::write};

entrypoint!(process_instruction);
fn process_instruction<'a>(
program_id: &'a Pubkey,
accounts: &'a [AccountInfo<'a>],

fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
if let Err(error) = processor::process_instruction(program_id, accounts, instruction_data) {
// catch the error so we can print it
error.print::<CounterError>();
return Err(error);
let (instruction, _data) = instruction_data
.split_first()
.ok_or(ProgramError::InvalidInstructionData)?;

match ProgramMetadataInstruction::try_from(instruction)? {
// 0 - Write
ProgramMetadataInstruction::Write => {
#[cfg(feature = "logging")]
msg!("Instruction: Write");

write(accounts)
}
_ => Err(ProgramError::InvalidInstructionData),
}
Ok(())
}
59 changes: 5 additions & 54 deletions program/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,13 @@
use num_derive::FromPrimitive;
use solana_program::{
decode_error::DecodeError,
msg,
program_error::{PrintProgramError, ProgramError},
};
use thiserror::Error;
use pinocchio::program_error::ProgramError;

#[derive(Error, Clone, Debug, Eq, PartialEq, FromPrimitive)]
pub enum CounterError {
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ProgramMetadataError {
/// 0 - Error deserializing an account
#[error("Error deserializing an account")]
DeserializationError,
/// 1 - Error serializing an account
#[error("Error serializing an account")]
SerializationError,
/// 2 - Invalid program owner
#[error("Invalid program owner. This likely mean the provided account does not exist")]
InvalidProgramOwner,
/// 3 - Invalid PDA derivation
#[error("Invalid PDA derivation")]
InvalidPda,
/// 4 - Expected empty account
#[error("Expected empty account")]
ExpectedEmptyAccount,
/// 5 - Expected non empty account
#[error("Expected non empty account")]
ExpectedNonEmptyAccount,
/// 6 - Expected signer account
#[error("Expected signer account")]
ExpectedSignerAccount,
/// 7 - Expected writable account
#[error("Expected writable account")]
ExpectedWritableAccount,
/// 8 - Account mismatch
#[error("Account mismatch")]
AccountMismatch,
/// 9 - Invalid account key
#[error("Invalid account key")]
InvalidAccountKey,
/// 10 - Numerical overflow
#[error("Numerical overflow")]
NumericalOverflow,
}

impl PrintProgramError for CounterError {
fn print<E>(&self) {
msg!(&self.to_string());
}
}

impl From<CounterError> for ProgramError {
fn from(e: CounterError) -> Self {
impl From<ProgramMetadataError> for ProgramError {
fn from(e: ProgramMetadataError) -> Self {
ProgramError::Custom(e as u32)
}
}

impl<T> DecodeError<T> for CounterError {
fn type_of() -> &'static str {
"Mpl Project Name Error"
}
}
61 changes: 45 additions & 16 deletions program/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
use borsh::{BorshDeserialize, BorshSerialize};
use shank::{ShankContext, ShankInstruction};
use pinocchio::program_error::ProgramError;

#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, ShankContext, ShankInstruction)]
#[derive(Clone, Debug)]
#[rustfmt::skip]
pub enum CounterInstruction {
/// Creates the counter account derived from the provided authority.
#[account(0, writable, name="counter", desc = "The program derived address of the counter account to create (seeds: ['counter', authority])")]
#[account(1, signer, name="authority", desc = "The authority of the counter")]
#[account(2, writable, signer, name="payer", desc = "The account paying for the storage fees")]
#[account(3, name="system_program", desc = "The system program")]
Create,
// Write (Includes resize)
// InitializeCanonical (Write before on same account)
// InitializeThirdParty (Write before on same account)
// Update (Write before on external Buffer account)
// Set authority (for canonical only)
// Close
pub enum ProgramMetadataInstruction {
/// Writes data to a pre-funded (buffer) account.
///
/// This instruction allocated and assign the account to the
/// program if needed.
///
/// 0. `[w]` The account to write to.
/// 1. `[b]` The data to write.
Write,

/// Initializes a canonical metadata account.
///
/// A canonical metadata account is an account initialized
/// by the program upgrade authority.
InitializeCanonical,

/// Initializes a "third-porty" metadata account.
InitializeThirdParty,

/// Updates the data of a metadata account from an buffer account.
Update,

/// Sets the authority of a metadata account.
SetAuthority,

/// Closes a metadata account.
Close,
}

impl TryFrom<&u8> for ProgramMetadataInstruction {
type Error = ProgramError;

fn try_from(value: &u8) -> Result<Self, Self::Error> {
match *value {
0 => Ok(ProgramMetadataInstruction::Write),
1 => Ok(ProgramMetadataInstruction::InitializeCanonical),
2 => Ok(ProgramMetadataInstruction::InitializeThirdParty),
3 => Ok(ProgramMetadataInstruction::Update),
4 => Ok(ProgramMetadataInstruction::SetAuthority),
5 => Ok(ProgramMetadataInstruction::Close),
_ => Err(ProgramError::InvalidInstructionData),
}
}
}
6 changes: 1 addition & 5 deletions program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
pub mod assertions;
pub mod entrypoint;
pub mod error;
pub mod instruction;
pub mod processor;
pub mod state;
pub mod utils;

pub use solana_program;

solana_program::declare_id!("4FX3oHhpAkJcb2tFFrq9JBY8gc4RhCRM5g75VG9QHnj1");
pinocchio_pubkey::declare_id!("4FX3oHhpAkJcb2tFFrq9JBY8gc4RhCRM5g75VG9QHnj1");
53 changes: 0 additions & 53 deletions program/src/processor/create.rs

This file was deleted.

21 changes: 1 addition & 20 deletions program/src/processor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1 @@
use crate::instruction::CounterInstruction;
use borsh::BorshDeserialize;
use create::create;
use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult, msg, pubkey::Pubkey};

pub mod create;

pub fn process_instruction<'a>(
_program_id: &Pubkey,
accounts: &'a [AccountInfo<'a>],
instruction_data: &[u8],
) -> ProgramResult {
let instruction: CounterInstruction = CounterInstruction::try_from_slice(instruction_data)?;
match instruction {
CounterInstruction::Create => {
msg!("Instruction: Create");
create(accounts)
}
}
}
pub mod write;
5 changes: 5 additions & 0 deletions program/src/processor/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use pinocchio::{account_info::AccountInfo, ProgramResult};

pub fn write<'a>(_accounts: &'a [AccountInfo]) -> ProgramResult {
Ok(())
}
Loading

0 comments on commit 937cdb1

Please sign in to comment.