Skip to content

Commit 81d5424

Browse files
authored
add basics/favorites/steel (#192)
1 parent ecf7486 commit 81d5424

File tree

17 files changed

+529
-0
lines changed

17 files changed

+529
-0
lines changed

basics/favorites/steel/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
test-ledger

basics/favorites/steel/Cargo.toml

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+
steel-api = { path = "./api", version = "0.1.0" }
17+
bytemuck = "1.14"
18+
num_enum = "0.7"
19+
solana-program = "1.18"
20+
steel = "1.3"
21+
thiserror = "1.0"

basics/favorites/steel/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Steel
2+
3+
**Steel** is a ...
4+
5+
## API
6+
- [`Consts`](api/src/consts.rs) – Program constants.
7+
- [`Error`](api/src/error.rs) – Custom program errors.
8+
- [`Event`](api/src/event.rs) – Custom program events.
9+
- [`Instruction`](api/src/instruction.rs) – Declared instructions.
10+
11+
## Instructions
12+
- [`Hello`](program/src/hello.rs) – Hello ...
13+
14+
## State
15+
- [`User`](api/src/state/user.rs) – User ...
16+
17+
## Tests
18+
19+
To run the test suit, use the Solana toolchain:
20+
```
21+
cargo test-sbf
22+
```

basics/favorites/steel/api/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "steel-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Seed of the favorites account PDA.
2+
pub const FAVORITES: &[u8] = b"favorites";
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use steel::*;
2+
3+
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
4+
#[repr(u32)]
5+
pub enum SteelError {
6+
#[error("This is a dummy error")]
7+
Dummy = 0,
8+
}
9+
10+
error!(SteelError);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use steel::*;
2+
3+
#[repr(u8)]
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5+
pub enum SteelInstruction {
6+
SetFavorites = 0,
7+
}
8+
9+
#[repr(C)]
10+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
11+
pub struct SetFavorites {
12+
pub number: [u8; 8],
13+
14+
pub color: [u8; 32],
15+
16+
pub hobbies: [[u8; 32]; 3],
17+
}
18+
19+
instruction!(SteelInstruction, SetFavorites);

basics/favorites/steel/api/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pub mod consts;
2+
pub mod error;
3+
pub mod instruction;
4+
pub mod sdk;
5+
pub mod state;
6+
pub mod utils;
7+
8+
pub mod prelude {
9+
pub use crate::consts::*;
10+
pub use crate::error::*;
11+
pub use crate::instruction::*;
12+
pub use crate::sdk::*;
13+
pub use crate::state::*;
14+
pub use crate::utils::*;
15+
}
16+
17+
use steel::*;
18+
19+
// TODO Set program id
20+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");

basics/favorites/steel/api/src/sdk.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn set_favorites(signer: Pubkey, number: u64, color: &str, hobbies: Vec<&str>) -> Instruction {
6+
let color_bytes: [u8; 32] = string_to_bytes32_padded(color).unwrap();
7+
8+
let hobbies_bytes = strings_to_bytes32_array_padded(hobbies).unwrap();
9+
10+
Instruction {
11+
program_id: crate::ID,
12+
accounts: vec![
13+
AccountMeta::new(signer, true),
14+
AccountMeta::new(favorites_pda().0, false),
15+
AccountMeta::new_readonly(system_program::ID, false),
16+
],
17+
data: SetFavorites {
18+
number: number.to_le_bytes(),
19+
color: color_bytes,
20+
hobbies: hobbies_bytes,
21+
}
22+
.to_bytes(),
23+
}
24+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use steel::*;
2+
3+
use super::SteelAccount;
4+
5+
#[repr(C)]
6+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
7+
pub struct Favorites {
8+
pub number: u64,
9+
10+
pub color: [u8; 32],
11+
12+
pub hobbies: [[u8; 32]; 3],
13+
}
14+
15+
account!(SteelAccount, Favorites);

0 commit comments

Comments
 (0)