Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 81fc224

Browse files
committed
More instructions
1 parent 82e7db7 commit 81fc224

33 files changed

+1396
-63
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ repository = "https://github.com/febo/token"
1616

1717
[workspace.dependencies]
1818
bytemuck = { version="1.18.0", features=["derive"] }
19-
pinocchio = { git = "https://github.com/febo/pinocchio.git", branch = "febo/floating" }
20-
pinocchio-pubkey = { git = "https://github.com/febo/pinocchio.git", branch = "febo/floating" }
19+
pinocchio = { git = "https://github.com/febo/pinocchio.git", branch = "tweaks" }
20+
pinocchio-pubkey = { git = "https://github.com/febo/pinocchio.git", branch = "tweaks" }
21+
#pinocchio = { version = "^0", path = "/Users/febo/Developer/febo/pinocchio/sdk/pinocchio" }
22+
#pinocchio-pubkey = { version = "^0", path = "/Users/febo/Developer/febo/pinocchio/sdk/pubkey" }

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ This repository contains a **proof-of-concept** of a reimplementation of the SPL
2020

2121
| Instruction | Completed | CU (`p-token`) | CU (`spl-token`) |
2222
|----------------------------|-----------|----------------|------------------|
23-
| `InitializeMint` || 396 | 2967 |
24-
| `InitializeAccount` || 444 | 4527 |
25-
| `InitializeMultisig` | | | |
23+
| `InitializeMint` || 370 | 2967 |
24+
| `InitializeAccount` || 406 | 4527 |
25+
| `InitializeMultisig` | | 431 | 2973 |
2626
| `Transfer` || 161 | 4645 |
27-
| `Approve` | | | |
28-
| `Revoke` | | | |
29-
| `SetAuthority` | | | |
27+
| `Approve` | | 123 | 2904 |
28+
| `Revoke` | | 88 | 2677 |
29+
| `SetAuthority` | | 137 | 3167 |
3030
| `MintTo` || 160 | 4538 |
31-
| `Burn` | | | |
32-
| `CloseAccount` | | | |
33-
| `FreezeAccount` | | | |
34-
| `ThawAccount` | | | |
31+
| `Burn` | | 158 | 4753 |
32+
| `CloseAccount` | | 162 | 2916 |
33+
| `FreezeAccount` | | 128 | 4265 |
34+
| `ThawAccount` | | 129 | 4267 |
3535
| `TransferChecked` | | | |
3636
| `ApproveChecked` | | | |
3737
| `MintToChecked` | | | |

interface/src/instruction.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Instruction types
22
3-
use pinocchio::{program_error::ProgramError, pubkey::Pubkey};
3+
use pinocchio::pubkey::Pubkey;
44

5-
use crate::{error::TokenError, state::PodCOption};
5+
use crate::state::PodCOption;
66

77
/// Instructions supported by the token program.
88
#[repr(C)]
@@ -506,13 +506,13 @@ impl AuthorityType {
506506
}
507507
}
508508

509-
pub fn from(index: u8) -> Result<Self, ProgramError> {
509+
pub fn from(index: u8) -> Self {
510510
match index {
511-
0 => Ok(AuthorityType::MintTokens),
512-
1 => Ok(AuthorityType::FreezeAccount),
513-
2 => Ok(AuthorityType::AccountOwner),
514-
3 => Ok(AuthorityType::CloseAccount),
515-
_ => Err(TokenError::InvalidInstruction.into()),
511+
0 => AuthorityType::MintTokens,
512+
1 => AuthorityType::FreezeAccount,
513+
2 => AuthorityType::AccountOwner,
514+
3 => AuthorityType::CloseAccount,
515+
_ => panic!("invalid authority type: {index}"),
516516
}
517517
}
518518
}

interface/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ pub mod error;
22
pub mod instruction;
33
pub mod native_mint;
44
pub mod state;
5+
6+
pub mod program {
7+
pinocchio_pubkey::declare_id!("11111111111111111111111111111111");
8+
}

interface/src/state/account.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub struct Account {
3737
}
3838

3939
impl Account {
40+
/// Size of the `Account` account.
41+
pub const LEN: usize = core::mem::size_of::<Self>();
42+
4043
#[inline]
4144
pub fn is_initialized(&self) -> bool {
4245
self.state != AccountState::Uninitialized as u8

interface/src/state/mint.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ pub struct Mint {
2525
/// Optional authority to freeze token accounts.
2626
pub freeze_authority: PodCOption<Pubkey>,
2727
}
28+
29+
impl Mint {
30+
/// Size of the `Mint` account.
31+
pub const LEN: usize = core::mem::size_of::<Self>();
32+
}

interface/src/state/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bytemuck::{Pod, Zeroable};
44

55
pub mod account;
66
pub mod mint;
7-
pub mod multisignature;
7+
pub mod multisig;
88

99
#[repr(C)]
1010
#[derive(Clone, Copy, Debug, Default, PartialEq)]
@@ -40,6 +40,13 @@ impl<T: Default + PartialEq + Pod + Sized> PodCOption<T> {
4040

4141
pub const SOME: [u8; 4] = [1, 0, 0, 0];
4242

43+
pub fn some(value: T) -> Self {
44+
Self {
45+
tag: [1, 0, 0, 0],
46+
value,
47+
}
48+
}
49+
4350
/// Returns `true` if the option is a `None` value.
4451
#[inline]
4552
pub fn is_none(&self) -> bool {

interface/src/state/multisignature.rs renamed to interface/src/state/multisig.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ pub struct Multisig {
2424

2525
impl Multisig {
2626
pub const LEN: usize = core::mem::size_of::<Multisig>();
27+
28+
/// Utility function that checks index is between [`MIN_SIGNERS`] and [`MAX_SIGNERS`].
29+
pub fn is_valid_signer_index(index: usize) -> bool {
30+
(MIN_SIGNERS..=MAX_SIGNERS).contains(&index)
31+
}
2732
}

program/src/entrypoint.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ use pinocchio::{
44
};
55

66
use crate::processor::{
7+
approve::process_approve,
8+
burn::process_burn,
9+
close_account::process_close_account,
10+
freeze_account::process_freeze_account,
711
initialize_account::process_initialize_account,
812
initialize_mint::{process_initialize_mint, InitializeMint},
13+
initialize_multisig::process_initialize_multisig,
914
mint_to::process_mint_to,
15+
revoke::process_revoke,
16+
set_authority::{process_set_authority, SetAuthority},
17+
thaw_account::process_thaw_account,
1018
transfer::process_transfer,
1119
};
1220

@@ -34,6 +42,15 @@ pub fn process_instruction(
3442

3543
process_initialize_account(program_id, accounts, None, true)
3644
}
45+
// 2 - InitializeMultisig
46+
Some((&2, data)) => {
47+
#[cfg(feature = "logging")]
48+
pinocchio::msg!("Instruction: InitializeMultisig");
49+
50+
let m = data.first().ok_or(ProgramError::InvalidInstructionData)?;
51+
52+
process_initialize_multisig(accounts, *m, true)
53+
}
3754
// 3 - Transfer
3855
Some((&3, data)) => {
3956
#[cfg(feature = "logging")]
@@ -46,6 +63,38 @@ pub fn process_instruction(
4663

4764
process_transfer(program_id, accounts, amount, None)
4865
}
66+
// 4 - Approve
67+
Some((&4, data)) => {
68+
#[cfg(feature = "logging")]
69+
pinocchio::msg!("Instruction: Approve");
70+
71+
let amount = u64::from_le_bytes(
72+
data.try_into()
73+
.map_err(|_error| ProgramError::InvalidInstructionData)?,
74+
);
75+
76+
process_approve(program_id, accounts, amount, None)
77+
}
78+
// 5 - Revoke
79+
Some((&5, _)) => {
80+
#[cfg(feature = "logging")]
81+
pinocchio::msg!("Instruction: Revoke");
82+
83+
process_revoke(program_id, accounts)
84+
}
85+
// 6 - SetAuthority
86+
Some((&6, data)) => {
87+
#[cfg(feature = "logging")]
88+
pinocchio::msg!("Instruction: SetAuthority");
89+
90+
let instruction = SetAuthority::try_from_bytes(data)?;
91+
process_set_authority(
92+
program_id,
93+
accounts,
94+
instruction.authority_type,
95+
instruction.new_authority,
96+
)
97+
}
4998
// 7 - InitializeMint
5099
Some((&7, data)) => {
51100
#[cfg(feature = "logging")]
@@ -58,6 +107,39 @@ pub fn process_instruction(
58107

59108
process_mint_to(program_id, accounts, amount, None)
60109
}
110+
// 8 - Burn
111+
Some((&8, data)) => {
112+
#[cfg(feature = "logging")]
113+
pinocchio::msg!("Instruction: Burn");
114+
115+
let amount = u64::from_le_bytes(
116+
data.try_into()
117+
.map_err(|_error| ProgramError::InvalidInstructionData)?,
118+
);
119+
120+
process_burn(program_id, accounts, amount, None)
121+
}
122+
// 9 - CloseAccount
123+
Some((&9, _)) => {
124+
#[cfg(feature = "logging")]
125+
pinocchio::msg!("Instruction: CloseAccount");
126+
127+
process_close_account(program_id, accounts)
128+
}
129+
// 10 - FreezeAccount
130+
Some((&10, _)) => {
131+
#[cfg(feature = "logging")]
132+
pinocchio::msg!("Instruction: FreezeAccount");
133+
134+
process_freeze_account(program_id, accounts)
135+
}
136+
// 10 - ThawAccount
137+
Some((&11, _)) => {
138+
#[cfg(feature = "logging")]
139+
pinocchio::msg!("Instruction: ThawAccount");
140+
141+
process_thaw_account(program_id, accounts)
142+
}
61143
_ => Err(ProgramError::InvalidInstructionData),
62144
}
63145
}

0 commit comments

Comments
 (0)