Skip to content

Commit a0b17f0

Browse files
committed
processing-instructions/steel - add api
1 parent fb340b3 commit a0b17f0

File tree

7 files changed

+96
-43
lines changed

7 files changed

+96
-43
lines changed
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
[workspace]
2-
members = ["program"]
2+
members = ["api", "program"]
33
resolver = "2"
4+
5+
[workspace.dependencies]
6+
solana-program = "1.18.17"
7+
steel = "2.1"
8+
bytemuck = "1.4"
9+
num_enum = "0.7"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "processing-instructions-steel-api"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
solana-program = "1.18.17"
8+
steel = "2.1"
9+
bytemuck = "1.4"
10+
num_enum = "0.7"
11+
12+
[lib]
13+
crate-type = ["cdylib", "lib"]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use solana_program::msg;
2+
use std::ffi::CStr;
3+
use steel::*;
4+
5+
#[repr(u8)]
6+
#[derive(Clone, Copy, Debug, Zeroable, TryFromPrimitive, IntoPrimitive)]
7+
pub enum ParkInstruction {
8+
Park = 0,
9+
}
10+
11+
instruction!(ParkInstruction, Park);
12+
#[repr(C, packed)]
13+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
14+
pub struct Park {
15+
pub name: [u8; 32],
16+
pub height: u32,
17+
}
18+
19+
impl Park {
20+
pub fn process_instruction_data(instruction_data: &[u8]) -> ProgramResult {
21+
// Steel uses bytemuck under the hood to process instruction data
22+
// bytemuck::try_from_bytes::<Park>(instruction_data)
23+
//
24+
let instruction_data_object = Park::try_from_bytes(instruction_data)?;
25+
26+
msg!(
27+
"Welcome to the park, {:?}!",
28+
CStr::from_bytes_until_nul(&instruction_data_object.name).unwrap()
29+
);
30+
31+
if instruction_data_object.height > 5 {
32+
msg!("You are tall enough to ride this ride. Congratulations.");
33+
} else {
34+
msg!("You are NOT tall enough to ride this ride. Sorry mate.");
35+
};
36+
37+
Ok(())
38+
}
39+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod instruction;
2+
3+
use steel::*;
4+
5+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
6+
7+
pub mod prelude {
8+
pub use crate::instruction::*;
9+
}

basics/processing-instructions/steel/program/Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
solana-program = "1.18.17"
8-
steel = "2.1"
9-
bytemuck = "1.4"
10-
num_enum = "0.7"
7+
steel.workspace = true
8+
processing-instructions-steel-api = {path = "../api"}
119

1210
[lib]
1311
crate-type = ["cdylib", "lib"]
Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
1-
use std::ffi::CStr;
2-
3-
use solana_program::msg;
1+
use processing_instructions_steel_api::prelude::*;
42
use steel::*;
53

6-
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
7-
84
entrypoint!(process_instruction);
95

106
fn process_instruction(
11-
_program_id: &Pubkey,
7+
program_id: &Pubkey,
128
_accounts: &[AccountInfo],
139
instruction_data: &[u8],
1410
) -> ProgramResult {
15-
// Steel uses bytemuck under the hood to process instruction data
16-
// bytemuck::try_from_bytes::<Park>(instruction_data)
11+
// get the first bytes from the instruction data as the instruction discriminator
1712
//
18-
let instruction_data_object = Park::try_from_bytes(instruction_data)?;
19-
20-
msg!(
21-
"Welcome to the park, {:?}!",
22-
CStr::from_bytes_until_nul(&instruction_data_object.name).unwrap()
23-
);
13+
let (ix, data) = parse_instruction(
14+
&processing_instructions_steel_api::ID,
15+
program_id,
16+
instruction_data,
17+
)?;
2418

25-
if instruction_data_object.height > 5 {
26-
msg!("You are tall enough to ride this ride. Congratulations.");
27-
} else {
28-
msg!("You are NOT tall enough to ride this ride. Sorry mate.");
29-
};
30-
31-
Ok(())
32-
}
33-
34-
instruction!(ParkInstruction, Park);
35-
#[repr(C, packed)]
36-
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
37-
pub struct Park {
38-
pub name: [u8; 32],
39-
pub height: u32,
40-
}
41-
42-
#[repr(u8)]
43-
#[derive(Clone, Copy, Debug, Zeroable)]
44-
pub enum ParkInstruction {
45-
Park = 0,
19+
// match the discriminator
20+
//
21+
match ix {
22+
// process the rest of the data
23+
//
24+
ParkInstruction::Park => Park::process_instruction_data(data),
25+
}
4626
}

basics/processing-instructions/steel/tests/test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as borsh from 'borsh';
55
import { start } from 'solana-bankrun';
66

77
describe('custom-instruction-data', async () => {
8-
const PROGRAM_ID = PublicKey.unique();
8+
const PROGRAM_ID = new PublicKey('z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35');
99
const context = await start([{ name: 'processing_instructions_steel_program', programId: PROGRAM_ID }], []);
1010
const client = context.banksClient;
1111
const payer = context.payer;
@@ -50,6 +50,14 @@ describe('custom-instruction-data', async () => {
5050
],
5151
]);
5252

53+
enum ParkInstruction {
54+
Park = 0,
55+
}
56+
57+
const addInstructionDiscriminator = (discriminator: ParkInstruction, instructionData: InstructionData) => {
58+
return Buffer.concat([Buffer.from([discriminator]), instructionData.toBuffer()]);
59+
};
60+
5361
test('Go to the park!', async () => {
5462
const blockhash = context.lastBlockhash;
5563

@@ -66,12 +74,12 @@ describe('custom-instruction-data', async () => {
6674
const ix1 = new TransactionInstruction({
6775
keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
6876
programId: PROGRAM_ID,
69-
data: jimmy.toBuffer(),
77+
data: addInstructionDiscriminator(ParkInstruction.Park, jimmy),
7078
});
7179

7280
const ix2 = new TransactionInstruction({
7381
...ix1,
74-
data: mary.toBuffer(),
82+
data: addInstructionDiscriminator(ParkInstruction.Park, mary),
7583
});
7684

7785
const tx = new Transaction();

0 commit comments

Comments
 (0)