Skip to content

Commit

Permalink
Use nitrate entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
febo committed May 3, 2024
1 parent 15eb674 commit cc0e7ea
Show file tree
Hide file tree
Showing 27 changed files with 365 additions and 296 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions configs/scripts/program/clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ OUTPUT="./programs/.bin"
cd $(dirname $(dirname $(dirname ${SCRIPT_DIR})))

rm -rf $OUTPUT
rm -rf ./target

if [ -z ${PROGRAMS+x} ]; then
PROGRAMS="$(cat .github/.env | grep "PROGRAMS" | cut -d '=' -f 2)"
Expand Down
1 change: 1 addition & 0 deletions programs/asset/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ borsh = "^0.10"
bytemuck = "1.14"
shank = "0.3.0"
nifty-asset-types = { path = "../types" }
nitrate = "0.1.0"
num-derive = "^0.3"
num-traits = "^0.2"
solana-security-txt = "1.1.1"
Expand Down
8 changes: 4 additions & 4 deletions programs/asset/program/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use nitrate::{entrypoint, program::AccountInfo};
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
program_error::{PrintProgramError, ProgramError},
pubkey::Pubkey,
};

use crate::{error::AssetError, processor};

entrypoint!(process_instruction);
entrypoint!(process_instruction, 7);

fn process_instruction<'a>(
program_id: &'a Pubkey,
accounts: &'a [AccountInfo<'a>],
accounts: &'a [AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
if let Err(error) = processor::process_instruction(program_id, accounts, instruction_data) {
Expand Down
5 changes: 3 additions & 2 deletions programs/asset/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use nifty_asset_types::{
extensions::ExtensionType,
state::{DelegateRole, Standard},
};
use shank::{ShankContext, ShankInstruction};
use nitrate::Accounts;
use shank::ShankInstruction;

#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, ShankContext, ShankInstruction)]
#[derive(BorshDeserialize, BorshSerialize, Clone, Debug, ShankInstruction, Accounts)]
#[rustfmt::skip]
pub enum Instruction {
/// Closes an uninitialized asset (buffer) account.
Expand Down
49 changes: 24 additions & 25 deletions programs/asset/program/src/processor/allocate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use nifty_asset_types::{
podded::ZeroCopy,
state::{Asset, Discriminator},
};
use nitrate::program::system;
use solana_program::{
entrypoint::ProgramResult, msg, program::invoke, program_error::ProgramError,
program_memory::sol_memcpy, pubkey::Pubkey, rent::Rent, system_instruction, system_program,
sysvar::Sysvar,
entrypoint::ProgramResult, msg, program_error::ProgramError, program_memory::sol_memcpy,
pubkey::Pubkey, rent::Rent, system_program, sysvar::Sysvar,
};

use crate::{
error::AssetError,
instruction::{
accounts::{AllocateAccounts, Context},
accounts::{Allocate, Context},
AllocateInput,
},
processor::resize,
Expand All @@ -29,13 +29,13 @@ use crate::{
#[inline(always)]
pub fn process_allocate(
program_id: &Pubkey,
ctx: Context<AllocateAccounts>,
ctx: Context<Allocate>,
args: AllocateInput,
) -> ProgramResult {
// account validation

require!(
ctx.accounts.asset.is_signer,
ctx.accounts.asset.is_signer(),
ProgramError::MissingRequiredSignature,
"asset"
);
Expand All @@ -46,7 +46,7 @@ pub fn process_allocate(
(false, Asset::LEN)
} else {
require!(
ctx.accounts.asset.owner == program_id,
ctx.accounts.asset.owner() == program_id,
ProgramError::IllegalOwner,
"asset"
);
Expand All @@ -57,7 +57,7 @@ pub fn process_allocate(
"asset"
);

let data = (*ctx.accounts.asset.data).borrow();
let data = ctx.accounts.asset.try_borrow_data()?;

// make sure that the asset is not already initialized
require!(
Expand Down Expand Up @@ -117,7 +117,7 @@ pub fn process_allocate(
length
);
} else {
let asset_data = (*ctx.accounts.asset.data).borrow();
let asset_data = ctx.accounts.asset.try_borrow_data()?;
let (extension, offset) =
Asset::last_extension(&asset_data).ok_or(AssetError::ExtensionNotFound)?;

Expand All @@ -127,7 +127,7 @@ pub fn process_allocate(
drop(asset_data);

// validate the extension data
let asset_data = &mut (*ctx.accounts.asset.data).borrow_mut();
let asset_data = &mut ctx.accounts.asset.try_borrow_mut_data()?;
on_create(
extension_type,
&mut asset_data[offset..offset + length],
Expand All @@ -148,7 +148,7 @@ pub fn process_allocate(
}

#[inline(always)]
fn create_account(ctx: &Context<AllocateAccounts>, space: usize) -> ProgramResult {
fn create_account(ctx: &Context<Allocate>, space: usize) -> ProgramResult {
let payer = {
require!(
ctx.accounts.payer.is_some(),
Expand All @@ -160,7 +160,7 @@ fn create_account(ctx: &Context<AllocateAccounts>, space: usize) -> ProgramResul
};

require!(
payer.is_signer,
payer.is_signer(),
ProgramError::MissingRequiredSignature,
"payer"
);
Expand All @@ -176,26 +176,25 @@ fn create_account(ctx: &Context<AllocateAccounts>, space: usize) -> ProgramResul
};

require!(
system_program.key == &system_program::ID,
system_program.key() == &system_program::ID,
ProgramError::IncorrectProgramId,
"system_program"
);

invoke(
&system_instruction::create_account(
payer.key,
ctx.accounts.asset.key,
Rent::get()?.minimum_balance(space),
space as u64,
&crate::ID,
),
&[payer.clone(), ctx.accounts.asset.clone()],
)
system::create_account(
payer,
ctx.accounts.asset,
Rent::get()?.minimum_balance(space),
space as u64,
&crate::ID,
);

Ok(())
}

#[inline(always)]
fn save_extension_data(
ctx: &Context<AllocateAccounts>,
ctx: &Context<Allocate>,
args: &crate::instruction::ExtensionInput,
exists: bool,
offset: usize,
Expand Down Expand Up @@ -235,7 +234,7 @@ fn save_extension_data(
)?;
}

let asset_data = &mut (*ctx.accounts.asset.data).borrow_mut();
let asset_data = &mut ctx.accounts.asset.try_borrow_mut_data()?;
let extension = Extension::load_mut(&mut asset_data[offset..offset + Extension::LEN]);

extension.set_extension_type(args.extension_type);
Expand Down
16 changes: 8 additions & 8 deletions programs/asset/program/src/processor/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use solana_program::{entrypoint::ProgramResult, program_error::ProgramError, pub
use crate::{
error::AssetError,
instruction::{
accounts::{ApproveAccounts, Context},
accounts::{Approve, Context},
DelegateInput,
},
require,
Expand All @@ -22,18 +22,18 @@ use crate::{
/// 2. `[]` delegate
pub fn process_approve(
program_id: &Pubkey,
ctx: Context<ApproveAccounts>,
ctx: Context<Approve>,
args: DelegateInput,
) -> ProgramResult {
// account validation

require!(
ctx.accounts.asset.owner == program_id,
ctx.accounts.asset.owner() == program_id,
ProgramError::IllegalOwner,
"asset"
);

let mut data = (*ctx.accounts.asset.data).borrow_mut();
let mut data = ctx.accounts.asset.try_borrow_mut_data()?;

require!(
data.len() >= Asset::LEN && data[0] == Discriminator::Asset.into(),
Expand All @@ -44,13 +44,13 @@ pub fn process_approve(
let asset = Asset::load_mut(&mut data);

require!(
asset.owner == *ctx.accounts.owner.key,
asset.owner == *ctx.accounts.owner.key(),
AssetError::InvalidAssetOwner,
"owner"
);

require!(
ctx.accounts.owner.is_signer,
ctx.accounts.owner.is_signer(),
ProgramError::MissingRequiredSignature,
"owner"
);
Expand All @@ -65,14 +65,14 @@ pub fn process_approve(
// only need to enable the roles; otherwise we are setting a new delegate
// and replacing the existing one (if any)
if let Some(delegate) = asset.delegate.value_mut() {
if *delegate.address == *ctx.accounts.delegate.key {
if *delegate.address == *ctx.accounts.delegate.key() {
delegate.roles |= roles;
return Ok(());
}
}

let delegate = Delegate {
address: NullablePubkey::new(*ctx.accounts.delegate.key),
address: NullablePubkey::new(*ctx.accounts.delegate.key()),
roles,
};
asset.delegate = delegate.into();
Expand Down
16 changes: 8 additions & 8 deletions programs/asset/program/src/processor/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::ops::Deref;
use crate::{
err,
error::AssetError,
instruction::accounts::{BurnAccounts, Context},
instruction::accounts::{Burn, Context},
require,
utils::{assert_delegate, close_program_account},
};
Expand All @@ -22,15 +22,15 @@ use crate::{
/// 1. `[writable, signer]` signer
/// 2. `[writable, optional]` recipient
/// 3. `[writable, optional]` group
pub fn process_burn(program_id: &Pubkey, ctx: Context<BurnAccounts>) -> ProgramResult {
pub fn process_burn(program_id: &Pubkey, ctx: Context<Burn>) -> ProgramResult {
require!(
ctx.accounts.asset.owner == program_id,
ctx.accounts.asset.owner() == program_id,
ProgramError::IllegalOwner,
"asset"
);

require!(
ctx.accounts.signer.is_signer,
ctx.accounts.signer.is_signer(),
ProgramError::MissingRequiredSignature,
"missing required signer"
);
Expand All @@ -48,13 +48,13 @@ pub fn process_burn(program_id: &Pubkey, ctx: Context<BurnAccounts>) -> ProgramR
let asset = Asset::load(asset);

// Validate the signer is the owner or the burn delegate.
let is_allowed = asset.owner == *ctx.accounts.signer.key
let is_allowed = asset.owner == *ctx.accounts.signer.key()
|| assert_delegate(
&[
asset.delegate.value(),
Extension::get::<Manager>(extensions).map(|s| s.delegate),
],
ctx.accounts.signer.key,
ctx.accounts.signer.key(),
DelegateRole::Burn,
)
.is_ok();
Expand All @@ -76,12 +76,12 @@ pub fn process_burn(program_id: &Pubkey, ctx: Context<BurnAccounts>) -> ProgramR
})?;

require!(
group.deref() == group_asset.key,
group.deref() == group_asset.key(),
ProgramError::InvalidArgument,
"group mismatch"
);

let mut group_data = group_asset.data.borrow_mut();
let mut group_data = group_asset.try_borrow_mut_data()?;

// sanity check: this should not happen since the asset is referencing the group
let grouping = if let Some(grouping) = Asset::get_mut::<GroupingMut>(&mut group_data) {
Expand Down
10 changes: 5 additions & 5 deletions programs/asset/program/src/processor/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use solana_program::{entrypoint::ProgramResult, program_error::ProgramError, pub

use crate::{
error::AssetError,
instruction::accounts::{CloseAccounts, Context},
instruction::accounts::{Close, Context},
require,
utils::close_program_account,
};
Expand All @@ -14,22 +14,22 @@ use crate::{
///
/// 0. `[writable, signer]` buffer
/// 1. `[writable]` recipient
pub fn process_close(program_id: &Pubkey, ctx: Context<CloseAccounts>) -> ProgramResult {
pub fn process_close(program_id: &Pubkey, ctx: Context<Close>) -> ProgramResult {
// account validation

require!(
ctx.accounts.buffer.is_signer,
ctx.accounts.buffer.is_signer(),
ProgramError::MissingRequiredSignature,
"buffer"
);

require!(
ctx.accounts.buffer.owner == program_id,
ctx.accounts.buffer.owner() == program_id,
ProgramError::IllegalOwner,
"buffer"
);

let data = (*ctx.accounts.buffer.data).borrow();
let data = ctx.accounts.buffer.try_borrow_data()?;

if !data.is_empty() {
// make sure that the asset is uninitialized
Expand Down
Loading

0 comments on commit cc0e7ea

Please sign in to comment.