Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions basics/counter/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]
members = ["api", "program"]
resolver = "2"

[workspace.dependencies]
solana-program = "1.18.17"
steel = "2.1"
bytemuck = "1.4"
num_enum = "0.7"
3 changes: 3 additions & 0 deletions basics/counter/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Counter: Solana Steel

This example program is written in Solana using Steel toolsuite.
13 changes: 13 additions & 0 deletions basics/counter/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "counter-solana-steel-api"
version = "0.1.0"
edition = "2021"

[dependencies]
solana-program = "=1.18.17"
steel = "2.1"
bytemuck = "1.4"
num_enum = "0.7"

[lib]
crate-type = ["cdylib", "lib"]
1 change: 1 addition & 0 deletions basics/counter/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const COUNTER_SEEDS: &[u8] = b"counter"; // counter seeds
72 changes: 72 additions & 0 deletions basics/counter/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::{consts::*, state::*};
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum CounterInstruction {
Initialize = 0,
Increment = 1,
}

instruction!(CounterInstruction, Initialize);
// Initialize
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Initialize {}

impl Initialize {
pub fn process(accounts: &[AccountInfo<'_>]) -> ProgramResult {
let [counter_account, payer, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

payer.is_writable()?.is_signer()?; // make use payer is writable and signer
system_program.is_program(&system_program::ID)?; // system program check
counter_account
.is_writable()? // check the account is writable
.has_seeds(&[b"counter"], &crate::ID)?; // check the address is derived from the right seeds

// create the counter account
create_account::<Counter>(
counter_account, // account to be created
system_program, // system program
payer, // payer
&crate::ID, // program id
&[COUNTER_SEEDS], // seeds
)?;

let counter = counter_account.as_account::<Counter>(&crate::ID)?;
let count = counter.count;

solana_program::msg!("Counter initialized! Count is {}", count);

Ok(())
}
}

instruction!(CounterInstruction, Increment);
// Increment
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Increment {}

impl Increment {
pub fn process(accounts: &[AccountInfo<'_>]) -> ProgramResult {
let [counter_account] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

counter_account
.is_writable()? // check the account is writable
.has_seeds(&[COUNTER_SEEDS], &crate::ID)?; // check the address is derived from the right seeds

let counter = counter_account.as_account_mut::<Counter>(&crate::ID)?;
counter.count += 1;

let count = counter.count;

solana_program::msg!("Counter state incremented to {:?}", count);

Ok(())
}
}
13 changes: 13 additions & 0 deletions basics/counter/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub mod consts;
pub mod instruction;
pub mod state;

pub mod prelude {
pub use crate::consts::*;
pub use crate::instruction::*;
pub use crate::state::*;
}

use steel::*;

declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
15 changes: 15 additions & 0 deletions basics/counter/steel/api/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use steel::*;

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

account!(CounterAccount, Counter);
// Counter
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Counter {
pub count: u64,
}
29 changes: 29 additions & 0 deletions basics/counter/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "counter-solana-steel",
"version": "0.1.0",
"description": "Counter program written using Steel framework",
"main": "index.js",
"author": "The Wuh",
"license": "Apache-2.0",
"private": false,
"scripts": {
"test": "pnpm ts-mocha -p ./tests/tsconfig.test.json -t 1000000 ./tests/counter.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/program.so"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.1",
"@types/mocha": "^10.0.9",
"@types/node": "^22.8.5",
"chai": "^4.3.4",
"mocha": "^10.8.2",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5"
},
"dependencies": {
"@solana/web3.js": "^1.95.4"
}
}
Loading