Skip to content

Commit da2163d

Browse files
add basics/processing-instructions/steel (#166)
1 parent d535d2c commit da2163d

File tree

15 files changed

+1679
-0
lines changed

15 files changed

+1679
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
test-ledger
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[workspace]
2+
resolver = "2"
3+
members = ["api", "program"]
4+
5+
[workspace.package]
6+
version = "0.1.0"
7+
edition = "2021"
8+
license = "Apache-2.0"
9+
homepage = ""
10+
documentation = ""
11+
respository = ""
12+
readme = "./README.md"
13+
keywords = ["solana"]
14+
15+
[workspace.dependencies]
16+
processing-instructions-api = { path = "./api", version = "0.1.0" }
17+
bytemuck = "1.14"
18+
num_enum = "0.7"
19+
solana-program = "1.18"
20+
steel = "2.1"
21+
thiserror = "1.0"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# ProcessingInstructions
2+
3+
See the [Processing instructions's README](../README.md) for more information. In our case, we cannot use Borsh for serialization, as we're constrained by the `Steel` framework dependency on PODs (Plain Old Data).
4+
5+
## Building
6+
7+
```sh
8+
cargo build-sbf
9+
10+
```
11+
## Tests
12+
13+
This project includes both:
14+
- Rust tests: [`program/tests`](/program/tests) directory.
15+
- Node.js tests using [Bankrun](https://kevinheavey.github.io/solana-bankrun/): [`tests`](/tests) directory.
16+
17+
```sh
18+
# rust tests
19+
cargo test-sbf
20+
21+
# node tests
22+
pnpm build-and-test # this will also build the program
23+
#or
24+
pnpm test # if you have already built the program
25+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "processing-instructions-api"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
bytemuck.workspace = true
8+
num_enum.workspace = true
9+
solana-program.workspace = true
10+
steel.workspace = true
11+
thiserror.workspace = true
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use steel::*;
2+
3+
#[repr(u8)]
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5+
pub enum ProcessingInstructionsInstruction {
6+
GoToThePark = 0,
7+
}
8+
9+
#[repr(C)]
10+
#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)]
11+
pub struct GoToTheParkData {
12+
pub name: [u8; 64],
13+
pub height: [u8; 8],
14+
}
15+
16+
impl_to_bytes!(GoToTheParkData);
17+
18+
fn string_to_bytes(s: &str) -> [u8; 64] {
19+
let mut bytes = [0; 64];
20+
let s_bytes = s.as_bytes();
21+
let len = s_bytes.len().min(64);
22+
bytes[..len].copy_from_slice(&s_bytes[..len]);
23+
bytes
24+
}
25+
26+
impl GoToTheParkData {
27+
pub fn new(name: String, height: u64) -> Self {
28+
Self {
29+
name: string_to_bytes(&name),
30+
height: height.to_le_bytes(),
31+
}
32+
}
33+
34+
pub fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError> {
35+
bytemuck::try_from_bytes(data).or(Err(ProgramError::InvalidInstructionData))
36+
}
37+
38+
pub fn name(&self) -> String {
39+
String::from_utf8_lossy(&self.name)
40+
.trim_end_matches(char::from(0))
41+
.to_string()
42+
}
43+
44+
pub fn height(&self) -> u64 {
45+
u64::from_le_bytes(self.height)
46+
}
47+
}
48+
49+
#[repr(C)]
50+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
51+
pub struct GoToThePark {
52+
pub data: GoToTheParkData,
53+
}
54+
55+
instruction!(ProcessingInstructionsInstruction, GoToThePark);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pub mod instruction;
2+
pub mod sdk;
3+
4+
pub mod prelude {
5+
pub use crate::instruction::*;
6+
pub use crate::sdk::*;
7+
}
8+
9+
use steel::*;
10+
11+
// TODO Set program id
12+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn go_to_the_park(signer: Pubkey, data: GoToTheParkData) -> Instruction {
6+
Instruction {
7+
program_id: crate::ID,
8+
accounts: vec![AccountMeta::new(signer, true)],
9+
data: GoToThePark { data }.to_bytes(),
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "processing-instructions",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
7+
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
8+
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
9+
"deploy": "solana program deploy ./program/target/so/processing_instructions_program.so"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@solana/web3.js": "^1.95.4"
16+
},
17+
"devDependencies": {
18+
"@types/chai": "^4.3.7",
19+
"@types/mocha": "10.0.9",
20+
"@types/node": "^22.7.4",
21+
"chai": "^4.3.7",
22+
"mocha": "10.7.3",
23+
"solana-bankrun": "0.4.0",
24+
"ts-mocha": "^10.0.0",
25+
"typescript": "5.6.3"
26+
}
27+
}

0 commit comments

Comments
 (0)