Skip to content

Commit

Permalink
add basics/program-derived-addresses/steel (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertohuertasm authored Jan 4, 2025
1 parent 2f244bf commit 16068f8
Show file tree
Hide file tree
Showing 18 changed files with 1,783 additions and 0 deletions.
2 changes: 2 additions & 0 deletions basics/program-derived-addresses/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/program-derived-addresses/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]
program-derived-addresses-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/program-derived-addresses/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Program Derived Addresses

This program demonstrates how to derive addresses. It will use a PDA to store a counter of visits and increment it.

## 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/program-derived-addresses/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "program-derived-addresses-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
2 changes: 2 additions & 0 deletions basics/program-derived-addresses/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Seed of the account PDA.
pub const SEED: &[u8] = b"program-derived-addresses";
23 changes: 23 additions & 0 deletions basics/program-derived-addresses/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use steel::*;

use crate::state::PageVisits;

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

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Create {
pub page_visits: PageVisits,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Increment {}

instruction!(ProgramDerivedAddressesInstruction, Create);
instruction!(ProgramDerivedAddressesInstruction, Increment);
16 changes: 16 additions & 0 deletions basics/program-derived-addresses/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub mod consts;
pub mod instruction;
pub mod sdk;
pub mod state;

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

use steel::*;

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

use crate::prelude::*;

pub fn create(signer: Pubkey, user: Pubkey, page_visits: PageVisits) -> Instruction {
let pda = page_visits_pda(&user);
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(user, false),
AccountMeta::new(pda.0, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: Create { page_visits }.to_bytes(),
}
}

pub fn increment(page_visits_pda: Pubkey) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![AccountMeta::new(page_visits_pda, false)],
data: Increment {}.to_bytes(),
}
}
34 changes: 34 additions & 0 deletions basics/program-derived-addresses/steel/api/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use steel::*;

use crate::consts::*;

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

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct PageVisits {
pub page_visits: [u8; 4], // u32
pub bump: [u8; 1],
}

impl PageVisits {
pub fn increment_visits(&mut self) {
let visits = u32::from_le_bytes(self.page_visits);
self.page_visits = (visits + 1).to_le_bytes();
}

pub fn page_visits(&self) -> u32 {
u32::from_le_bytes(self.page_visits)
}
}

account!(ProgramDerivedAddressesAccount, PageVisits);

/// Fetch PDA of the PageVisit account.
pub fn page_visits_pda(user: &Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[SEED, user.as_ref()], &crate::id())
}
28 changes: 28 additions & 0 deletions basics/program-derived-addresses/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "program-derived-addresses",
"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/program_derived_addresses_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",
"borsh": "^2.0.0",
"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 16068f8

Please sign in to comment.