Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ readme = "./README.md"
crate-type = ["rlib"]

[dependencies]
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
pinocchio-pubkey = { version = "0.2", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
pinocchio = "0.8"
pinocchio-pubkey = "0.2"

[dev-dependencies]
strum = "0.27"
Expand Down
71 changes: 70 additions & 1 deletion interface/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Error types

use pinocchio::program_error::ProgramError;
use pinocchio::program_error::{ProgramError, ToStr};

/// Errors that may be returned by the Token program.
#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -59,3 +59,72 @@ impl From<TokenError> for ProgramError {
ProgramError::Custom(e as u32)
}
}

impl ToStr for TokenError {
fn to_str<E>(&self) -> &'static str
where
E: 'static + ToStr + TryFrom<u32>,
{
match self {
TokenError::NotRentExempt => "Error: Lamport balance below rent-exempt threshold",
TokenError::InsufficientFunds => "Error: insufficient funds",
TokenError::InvalidMint => "Error: Invalid Mint",
TokenError::MintMismatch => "Error: Account not associated with this Mint",
TokenError::OwnerMismatch => "Error: owner does not match",
TokenError::FixedSupply => "Error: the total supply of this token is fixed",
TokenError::AlreadyInUse => "Error: account or token already in use",
TokenError::InvalidNumberOfProvidedSigners => {
"Error: Invalid number of provided signers"
}
TokenError::InvalidNumberOfRequiredSigners => {
"Error: Invalid number of required signers"
}
TokenError::UninitializedState => "Error: State is uninitialized",
TokenError::NativeNotSupported => "Error: Instruction does not support native tokens",
TokenError::NonNativeHasBalance => {
"Error: Non-native account can only be closed if its balance is zero"
}
TokenError::InvalidInstruction => "Error: Invalid instruction",
TokenError::InvalidState => "Error: Invalid account state for operation",
TokenError::Overflow => "Error: Operation overflowed",
TokenError::AuthorityTypeNotSupported => {
"Error: Account does not support specified authority type"
}
TokenError::MintCannotFreeze => "Error: This token mint cannot freeze accounts",
TokenError::AccountFrozen => "Error: Account is frozen",
TokenError::MintDecimalsMismatch => "Error: decimals different from the Mint decimals",
TokenError::NonNativeNotSupported => {
"Error: Instruction does not support non-native tokens"
}
}
}
}

impl TryFrom<u32> for TokenError {
type Error = ProgramError;
fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(TokenError::NotRentExempt),
1 => Ok(TokenError::InsufficientFunds),
2 => Ok(TokenError::InvalidMint),
3 => Ok(TokenError::MintMismatch),
4 => Ok(TokenError::OwnerMismatch),
5 => Ok(TokenError::FixedSupply),
6 => Ok(TokenError::AlreadyInUse),
7 => Ok(TokenError::InvalidNumberOfProvidedSigners),
8 => Ok(TokenError::InvalidNumberOfRequiredSigners),
9 => Ok(TokenError::UninitializedState),
10 => Ok(TokenError::NativeNotSupported),
11 => Ok(TokenError::NonNativeHasBalance),
12 => Ok(TokenError::InvalidInstruction),
13 => Ok(TokenError::InvalidState),
14 => Ok(TokenError::Overflow),
15 => Ok(TokenError::AuthorityTypeNotSupported),
16 => Ok(TokenError::MintCannotFreeze),
17 => Ok(TokenError::AccountFrozen),
18 => Ok(TokenError::MintDecimalsMismatch),
19 => Ok(TokenError::NonNativeNotSupported),
_ => Err(ProgramError::InvalidArgument),
}
}
}
4 changes: 2 additions & 2 deletions p-token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ crate-type = ["cdylib"]
logging = []

[dependencies]
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
pinocchio-log = { version = "0.3", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
pinocchio = "0.8"
pinocchio-log = { version = "0.4", default-features = false }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabling default features to avoid bringing the log macro dependencies – the macro is not used.

spl-token-interface = { version = "^0", path = "../interface" }

[dev-dependencies]
Expand Down
24 changes: 18 additions & 6 deletions p-token/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use {
crate::processor::*,
pinocchio::{
account_info::AccountInfo, default_panic_handler, no_allocator, program_entrypoint,
program_error::ProgramError, pubkey::Pubkey, ProgramResult,
account_info::AccountInfo,
default_panic_handler, no_allocator, program_entrypoint,
program_error::{ProgramError, ToStr},
pubkey::Pubkey,
ProgramResult,
},
spl_token_interface::error::TokenError,
};

program_entrypoint!(process_instruction);
Expand All @@ -12,6 +16,12 @@ no_allocator!();
// Use the default panic handler.
default_panic_handler!();

/// Log an error.
#[cold]
fn log_error(error: &ProgramError) {
pinocchio::log::sol_log(error.to_str::<TokenError>());
}

/// Process an instruction.
///
/// In the first stage, the entrypoint checks the discriminator of the
Expand All @@ -29,15 +39,17 @@ pub fn process_instruction(
return Err(ProgramError::InvalidInstructionData);
};

if *discriminator == 255 {
let result = if *discriminator == 255 {
// 255 - Batch
#[cfg(feature = "logging")]
pinocchio::msg!("Instruction: Batch");

return process_batch(accounts, remaining);
}
process_batch(accounts, remaining)
} else {
inner_process_instruction(accounts, instruction_data)
};

inner_process_instruction(accounts, instruction_data)
result.inspect_err(log_error)
}

/// Process a "regular" instruction.
Expand Down
8 changes: 4 additions & 4 deletions p-token/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ const MAX_FORMATTED_DIGITS: usize = u8::MAX as usize + 2;
/// Checks that the account is owned by the expected program.
#[inline(always)]
fn check_account_owner(account_info: &AccountInfo) -> ProgramResult {
if &TOKEN_PROGRAM_ID != account_info.owner() {
Err(ProgramError::IncorrectProgramId)
} else {
if account_info.is_owned_by(&TOKEN_PROGRAM_ID) {
Ok(())
} else {
Err(ProgramError::IncorrectProgramId)
}
}

Expand All @@ -98,7 +98,7 @@ fn validate_owner(
}

if owner_account_info.data_len() == Multisig::LEN
&& owner_account_info.owner() == &TOKEN_PROGRAM_ID
&& owner_account_info.is_owned_by(&TOKEN_PROGRAM_ID)
{
// SAFETY: the caller guarantees that there are no mutable borrows of
// `owner_account_info` account data and the `load` validates that the
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"fixtures:run": "zx ./scripts/rust/fixtures.mjs run",
"interface:format": "zx ./scripts/rust/format.mjs interface",
"interface:lint": "zx ./scripts/rust/lint.mjs interface"

},
"devDependencies": {
"@codama/renderers-js": "^1.2.7",
Expand Down