Skip to content

Commit

Permalink
add basics/processing-instructions/steel (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertohuertasm authored Jan 2, 2025
1 parent d535d2c commit da2163d
Show file tree
Hide file tree
Showing 15 changed files with 1,679 additions and 0 deletions.
2 changes: 2 additions & 0 deletions basics/processing-instructions/steel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
test-ledger
21 changes: 21 additions & 0 deletions basics/processing-instructions/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[workspace]
resolver = "2"
members = ["api", "program"]

[workspace.package]
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
homepage = ""
documentation = ""
respository = ""
readme = "./README.md"
keywords = ["solana"]

[workspace.dependencies]
processing-instructions-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = "2.1"
thiserror = "1.0"
25 changes: 25 additions & 0 deletions basics/processing-instructions/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ProcessingInstructions

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).

## Building

```sh
cargo build-sbf

```
## Tests

This project includes both:
- Rust tests: [`program/tests`](/program/tests) directory.
- Node.js tests using [Bankrun](https://kevinheavey.github.io/solana-bankrun/): [`tests`](/tests) directory.

```sh
# rust tests
cargo test-sbf

# node tests
pnpm build-and-test # this will also build the program
#or
pnpm test # if you have already built the program
```
11 changes: 11 additions & 0 deletions basics/processing-instructions/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "processing-instructions-api"
version = "0.1.0"
edition = "2021"

[dependencies]
bytemuck.workspace = true
num_enum.workspace = true
solana-program.workspace = true
steel.workspace = true
thiserror.workspace = true
55 changes: 55 additions & 0 deletions basics/processing-instructions/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum ProcessingInstructionsInstruction {
GoToThePark = 0,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)]
pub struct GoToTheParkData {
pub name: [u8; 64],
pub height: [u8; 8],
}

impl_to_bytes!(GoToTheParkData);

fn string_to_bytes(s: &str) -> [u8; 64] {
let mut bytes = [0; 64];
let s_bytes = s.as_bytes();
let len = s_bytes.len().min(64);
bytes[..len].copy_from_slice(&s_bytes[..len]);
bytes
}

impl GoToTheParkData {
pub fn new(name: String, height: u64) -> Self {
Self {
name: string_to_bytes(&name),
height: height.to_le_bytes(),
}
}

pub fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError> {
bytemuck::try_from_bytes(data).or(Err(ProgramError::InvalidInstructionData))
}

pub fn name(&self) -> String {
String::from_utf8_lossy(&self.name)
.trim_end_matches(char::from(0))
.to_string()
}

pub fn height(&self) -> u64 {
u64::from_le_bytes(self.height)
}
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct GoToThePark {
pub data: GoToTheParkData,
}

instruction!(ProcessingInstructionsInstruction, GoToThePark);
12 changes: 12 additions & 0 deletions basics/processing-instructions/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub mod instruction;
pub mod sdk;

pub mod prelude {
pub use crate::instruction::*;
pub use crate::sdk::*;
}

use steel::*;

// TODO Set program id
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
11 changes: 11 additions & 0 deletions basics/processing-instructions/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use steel::*;

use crate::prelude::*;

pub fn go_to_the_park(signer: Pubkey, data: GoToTheParkData) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![AccountMeta::new(signer, true)],
data: GoToThePark { data }.to_bytes(),
}
}
27 changes: 27 additions & 0 deletions basics/processing-instructions/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "processing-instructions",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
"deploy": "solana program deploy ./program/target/so/processing_instructions_program.so"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/chai": "^4.3.7",
"@types/mocha": "10.0.9",
"@types/node": "^22.7.4",
"chai": "^4.3.7",
"mocha": "10.7.3",
"solana-bankrun": "0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "5.6.3"
}
}
Loading

0 comments on commit da2163d

Please sign in to comment.