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
2 changes: 2 additions & 0 deletions basics/create-account/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/create-account/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]
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"
22 changes: 22 additions & 0 deletions basics/create-account/steel/README.md
Original file line number Diff line number Diff line change
@@ -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
```
11 changes: 11 additions & 0 deletions basics/create-account/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions basics/create-account/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Seed of the counter account PDA.
pub const COUNTER: &[u8] = b"counter";
10 changes: 10 additions & 0 deletions basics/create-account/steel/api/src/error.rs
Original file line number Diff line number Diff line change
@@ -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);
14 changes: 14 additions & 0 deletions basics/create-account/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -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);

16 changes: 16 additions & 0 deletions basics/create-account/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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");
16 changes: 16 additions & 0 deletions basics/create-account/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -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(),
}
}

19 changes: 19 additions & 0 deletions basics/create-account/steel/program/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use steel::*


20 changes: 20 additions & 0 deletions basics/create-account/steel/program/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
46 changes: 46 additions & 0 deletions basics/create-account/steel/program/tests/test.rs
Original file line number Diff line number Diff line change
@@ -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);
}