Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basics/favorites/steel #309

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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/favorites/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.0"
bytemuck = "1.14"
num_enum = "0.7"
13 changes: 13 additions & 0 deletions basics/favorites/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "favorites-steel-api"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]

[dependencies]
solana-program.workspace = true
steel.workspace = true
bytemuck.workspace = true
num_enum.workspace = true
1 change: 1 addition & 0 deletions basics/favorites/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const FAVORITES_SEED: &[u8] = b"favorites";
63 changes: 63 additions & 0 deletions basics/favorites/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::{consts::*, state::*};
use solana_program::msg;
use std::ffi::CStr;
use steel::*;

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

instruction!(FavoritesInstruction, SetFavorites);
/// Init Instruction
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct SetFavorites {
favorites: Favorites,
}

impl SetFavorites {
pub fn process(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
let favorites_data = bytemuck::try_from_bytes::<Favorites>(data)
.or(Err(ProgramError::InvalidInstructionData))?;

let [favorites_info, user, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

favorites_info.is_writable()?;
favorites_info.has_seeds(&[FAVORITES_SEED, user.key.as_ref()], &crate::ID)?;

// if we have not created our favourites account, let us create it
if favorites_info.lamports() == 0 {
create_account::<Favorites>(
favorites_info,
system_program,
user,
&crate::ID,
&[FAVORITES_SEED, user.key.as_ref()],
)?;
}

let favorites = favorites_info.as_account_mut::<Favorites>(&crate::ID)?;

*favorites = *favorites_data;

let favorite_number = favorites.number;

msg!("Favorite number: {}", favorite_number);
msg!(
"Favorite color: {:?}",
CStr::from_bytes_until_nul(&favorites.color).unwrap()
);
for i in 0..favorites.hobbies.len() {
msg!(
"Favorite hobby {i}: {:?}",
CStr::from_bytes_until_nul(&favorites.hobbies[i]).unwrap()
);
}

Ok(())
}
}
13 changes: 13 additions & 0 deletions basics/favorites/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;

use steel::*;

declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");

pub mod prelude {
pub use crate::consts::*;
pub use crate::instruction::*;
pub use crate::state::*;
}
16 changes: 16 additions & 0 deletions basics/favorites/steel/api/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use steel::*;

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

account!(SteelAccount, Favorites);
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct Favorites {
pub number: u64,
pub color: [u8; 48],
pub hobbies: [[u8; 48]; 5],
}
8 changes: 8 additions & 0 deletions basics/favorites/steel/cicd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# This script is for quick building & deploying of the program.
# It also serves as a reference for the commands used for building & deploying Solana programs.
# Run this bad boy with "bash cicd.sh" or "./cicd.sh"

cargo build-sbf --manifest-path=./program/Cargo.toml --bpf-out-dir=./program/target/so
solana program deploy ./program/target/so/program.so
23 changes: 23 additions & 0 deletions basics/favorites/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"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.so"
},
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.1",
"@types/mocha": "^10.0.9",
"@types/node": "^22.8.5",
"borsh": "0.7.0",
"chai": "^4.3.4",
"mocha": "^10.8.2",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5"
}
}
Loading