From 29a71b7dca95da61057e7236995f6699c8c53481 Mon Sep 17 00:00:00 2001 From: Eric Alaribe Date: Tue, 22 Oct 2024 12:39:14 +0100 Subject: [PATCH] task: add project scaffold for create-account --- basics/create-account/steel/.gitignore | 2 + basics/create-account/steel/Cargo.toml | 21 +++++++++ basics/create-account/steel/README.md | 22 +++++++++ basics/create-account/steel/api/Cargo.toml | 11 +++++ basics/create-account/steel/api/src/consts.rs | 2 + basics/create-account/steel/api/src/error.rs | 10 ++++ .../steel/api/src/instruction.rs | 14 ++++++ basics/create-account/steel/api/src/lib.rs | 16 +++++++ basics/create-account/steel/api/src/sdk.rs | 16 +++++++ .../create-account/steel/program/Cargo.toml | 19 ++++++++ .../program/src/create_system_account.rs | 3 ++ .../create-account/steel/program/src/lib.rs | 20 ++++++++ .../steel/program/tests/test.rs | 46 +++++++++++++++++++ 13 files changed, 202 insertions(+) create mode 100644 basics/create-account/steel/.gitignore create mode 100644 basics/create-account/steel/Cargo.toml create mode 100644 basics/create-account/steel/README.md create mode 100644 basics/create-account/steel/api/Cargo.toml create mode 100644 basics/create-account/steel/api/src/consts.rs create mode 100644 basics/create-account/steel/api/src/error.rs create mode 100644 basics/create-account/steel/api/src/instruction.rs create mode 100644 basics/create-account/steel/api/src/lib.rs create mode 100644 basics/create-account/steel/api/src/sdk.rs create mode 100644 basics/create-account/steel/program/Cargo.toml create mode 100644 basics/create-account/steel/program/src/create_system_account.rs create mode 100644 basics/create-account/steel/program/src/lib.rs create mode 100644 basics/create-account/steel/program/tests/test.rs diff --git a/basics/create-account/steel/.gitignore b/basics/create-account/steel/.gitignore new file mode 100644 index 000000000..052739dbc --- /dev/null +++ b/basics/create-account/steel/.gitignore @@ -0,0 +1,2 @@ +target +test-ledger diff --git a/basics/create-account/steel/Cargo.toml b/basics/create-account/steel/Cargo.toml new file mode 100644 index 000000000..203056488 --- /dev/null +++ b/basics/create-account/steel/Cargo.toml @@ -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] +steel-api = { path = "./api", version = "0.1.0" } +bytemuck = "1.14" +num_enum = "0.7" +solana-program = "1.18" +steel = "1.3" +thiserror = "1.0" diff --git a/basics/create-account/steel/README.md b/basics/create-account/steel/README.md new file mode 100644 index 000000000..4f4fe2a54 --- /dev/null +++ b/basics/create-account/steel/README.md @@ -0,0 +1,22 @@ +# Steel + +**Steel** is a ... + +## API +- [`Consts`](api/src/consts.rs) – Program constants. +- [`Error`](api/src/error.rs) – Custom program errors. +- [`Event`](api/src/event.rs) – Custom program events. +- [`Instruction`](api/src/instruction.rs) – Declared instructions. + +## Instructions +- [`Hello`](program/src/hello.rs) – Hello ... + +## State +- [`User`](api/src/state/user.rs) – User ... + +## Tests + +To run the test suit, use the Solana toolchain: +``` +cargo test-sbf +``` diff --git a/basics/create-account/steel/api/Cargo.toml b/basics/create-account/steel/api/Cargo.toml new file mode 100644 index 000000000..34b563782 --- /dev/null +++ b/basics/create-account/steel/api/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "steel-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 diff --git a/basics/create-account/steel/api/src/consts.rs b/basics/create-account/steel/api/src/consts.rs new file mode 100644 index 000000000..af20c1a11 --- /dev/null +++ b/basics/create-account/steel/api/src/consts.rs @@ -0,0 +1,2 @@ +/// Seed of the counter account PDA. +pub const COUNTER: &[u8] = b"counter"; diff --git a/basics/create-account/steel/api/src/error.rs b/basics/create-account/steel/api/src/error.rs new file mode 100644 index 000000000..96e84da4d --- /dev/null +++ b/basics/create-account/steel/api/src/error.rs @@ -0,0 +1,10 @@ +use steel::*; + +#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)] +#[repr(u32)] +pub enum SteelError { + #[error("This is a dummy error")] + Dummy = 0, +} + +error!(SteelError); diff --git a/basics/create-account/steel/api/src/instruction.rs b/basics/create-account/steel/api/src/instruction.rs new file mode 100644 index 000000000..1e3cbccd5 --- /dev/null +++ b/basics/create-account/steel/api/src/instruction.rs @@ -0,0 +1,14 @@ +use steel::*; + +#[repr(u8)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] +pub enum SteelInstruction { + CreateSystemAccount = 0, +} + +#[repr(C)] +#[derive(Clone, Copy, Debug, Pod, Zeroable)] +pub struct CreateSystemAccount {} + +instruction!(SteelInstruction, CreateSystemAccount); + diff --git a/basics/create-account/steel/api/src/lib.rs b/basics/create-account/steel/api/src/lib.rs new file mode 100644 index 000000000..7a5660031 --- /dev/null +++ b/basics/create-account/steel/api/src/lib.rs @@ -0,0 +1,16 @@ +pub mod consts; +pub mod error; +pub mod instruction; +pub mod sdk; + +pub mod prelude { + pub use crate::consts::*; + pub use crate::error::*; + pub use crate::instruction::*; + pub use crate::sdk::*; +} + +use steel::*; + +// TODO Set program id +declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35"); diff --git a/basics/create-account/steel/api/src/sdk.rs b/basics/create-account/steel/api/src/sdk.rs new file mode 100644 index 000000000..72f0cdb65 --- /dev/null +++ b/basics/create-account/steel/api/src/sdk.rs @@ -0,0 +1,16 @@ +use steel::*; + +use crate::prelude::*; + +pub fn create_system_account(signer: Pubkey) -> Instruction { + Instruction{ + program_id: crate::ID, + accounts: vec![ + AccountMeta::new(signer, true), + AccountMeta::new(account_pda().0, false), + AccountMeta::new_readonly(system_program::ID, false), + ], + data: CreateSystemAccount { data }.to_bytes(), + } +} + diff --git a/basics/create-account/steel/program/Cargo.toml b/basics/create-account/steel/program/Cargo.toml new file mode 100644 index 000000000..96364fed9 --- /dev/null +++ b/basics/create-account/steel/program/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "steel-program" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib", "lib"] + +[dependencies] +steel-api.workspace = true +solana-program.workspace = true +steel.workspace = true + +[dev-dependencies] +bs64 = "0.1.2" +rand = "0.8.5" +solana-program-test = "1.18" +solana-sdk = "1.18" +tokio = { version = "1.35", features = ["full"] } diff --git a/basics/create-account/steel/program/src/create_system_account.rs b/basics/create-account/steel/program/src/create_system_account.rs new file mode 100644 index 000000000..b5c13664e --- /dev/null +++ b/basics/create-account/steel/program/src/create_system_account.rs @@ -0,0 +1,3 @@ +use steel::* + + diff --git a/basics/create-account/steel/program/src/lib.rs b/basics/create-account/steel/program/src/lib.rs new file mode 100644 index 000000000..c85e8f7cd --- /dev/null +++ b/basics/create-account/steel/program/src/lib.rs @@ -0,0 +1,20 @@ +mod create_system_account; + +use steel_api::prelude::*; +use steel::*; + +pub fn process_instruction( + program_id: &Pubkey, + accounts: &[AccountInfo], + data: &[u8], +) -> ProgramResult { + let (ix, data) = parse_instruction(&steel_api::ID, program_id, data)?; + + match ix { + SteelInstruction::CreateSystemAccount => process_initialize(accounts, data)?, + } + + Ok(()) +} + +entrypoint!(process_instruction); diff --git a/basics/create-account/steel/program/tests/test.rs b/basics/create-account/steel/program/tests/test.rs new file mode 100644 index 000000000..c5bb50e1b --- /dev/null +++ b/basics/create-account/steel/program/tests/test.rs @@ -0,0 +1,46 @@ +use steel_api::prelude::*; +use solana_program::hash::Hash; +use solana_program_test::{processor, BanksClient, ProgramTest}; +use solana_sdk::{signature::Keypair, signer::Signer, transaction::Transaction}; +use steel::*; + +async fn setup() -> (BanksClient, Keypair, Hash) { + let mut program_test = ProgramTest::new( + "steel", + steel_api::ID, + processor!(steel_program::process_instruction), + ); + program_test.prefer_bpf(true); + program_test.start().await +} + +#[tokio::test] +async fn run_test() { + // Setup test + let (mut banks, payer, blockhash) = setup().await; + + // Submit initialize transaction. + let ix = initialize(payer.pubkey()); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); + let res = banks.process_transaction(tx).await; + assert!(res.is_ok()); + + // Verify counter was initialized. + let counter_address = counter_pda().0; + let counter_account = banks.get_account(counter_address).await.unwrap().unwrap(); + let counter = Counter::try_from_bytes(&counter_account.data).unwrap(); + assert_eq!(counter_account.owner, steel_api::ID); + assert_eq!(counter.value, 0); + + // Submit add transaction. + let ix = add(payer.pubkey(), 42); + let tx = Transaction::new_signed_with_payer(&[ix], Some(&payer.pubkey()), &[&payer], blockhash); + let res = banks.process_transaction(tx).await; + assert!(res.is_ok()); + + // Verify counter was incremented. + let counter_account = banks.get_account(counter_address).await.unwrap().unwrap(); + let counter = Counter::try_from_bytes(&counter_account.data).unwrap(); + assert_eq!(counter.value, 42); +} +