Skip to content

Commit 3c74f3b

Browse files
authored
add basics/create-account/steel (#265)
1 parent cb4b969 commit 3c74f3b

File tree

17 files changed

+1614
-0
lines changed

17 files changed

+1614
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
test-ledger
3+
node_modules
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+
repository = ""
12+
readme = "./README.md"
13+
keywords = ["solana"]
14+
15+
[workspace.dependencies]
16+
create-account-api = { path = "./api", version = "0.1.0" }
17+
bytemuck = "1.14"
18+
num_enum = "0.7"
19+
solana-program = "1.18"
20+
steel = "2.0"
21+
thiserror = "1.0"

basics/create-account/steel/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Steel: Create account
2+
3+
This "create-account" program is written using **Steel**, a framework for writing onchain programs.
4+
5+
## API
6+
- [`Error`](api/src/error.rs) - Custom defined errors.
7+
- [`Consts`](api/src/consts.rs) – Program constants.
8+
- [`Instruction`](api/src/instruction.rs) – Declared instructions.
9+
10+
## Instructions
11+
- [`Initialize`](program/src/initialize.rs) – Initialize the account creation.
12+
13+
## State
14+
- [`New Account`](api/src/state.rs) – Link account and the struct that stores unique user ID.
15+
16+
## Get started
17+
18+
Compile your program:
19+
```sh
20+
pnpm build
21+
```
22+
23+
Run unit and integration tests:
24+
```sh
25+
pnpm test
26+
```
27+
28+
Do both together:
29+
```sh
30+
pnpm build-and-test
31+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "create-account-api"
3+
description = "API for interacting with the create account program"
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
documentation.workspace = true
9+
repository.workspace = true
10+
readme.workspace = true
11+
keywords.workspace = true
12+
13+
[dependencies]
14+
bytemuck.workspace = true
15+
num_enum.workspace = true
16+
solana-program.workspace = true
17+
steel.workspace = true
18+
thiserror.workspace = true
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use steel::*;
2+
3+
/// Declare custom error enum
4+
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
5+
#[repr(u32)]
6+
pub enum CreateAccountError {
7+
/// Discriminator for error is set to '0'
8+
#[error("There was an error while creating your account")]
9+
AccountCreation = 0,
10+
}
11+
12+
error!(CreateAccountError);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use steel::*;
2+
3+
/// Declare the Instructions enum for create account
4+
#[repr(u8)]
5+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
6+
pub enum CreateAccountInstruction {
7+
/// Initialize account discriminator set to '0'
8+
InitializeAccount = 0,
9+
}
10+
11+
/// Empty initialize account struct since
12+
/// no data input is needed
13+
#[repr(C)]
14+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
15+
pub struct InitializeAccount {}
16+
17+
// Link Instructions enum to variant
18+
instruction!(CreateAccountInstruction, InitializeAccount);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub mod error;
2+
pub mod instruction;
3+
pub mod sdk;
4+
pub mod state;
5+
6+
pub mod prelude {
7+
pub use crate::error::*;
8+
pub use crate::instruction::*;
9+
pub use crate::sdk::*;
10+
pub use crate::state::*;
11+
}
12+
13+
use steel::*;
14+
15+
declare_id!("12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn initialize_account(signer: Pubkey, new_account_key: Pubkey) -> Instruction {
6+
Instruction {
7+
program_id: crate::ID,
8+
accounts: vec![
9+
AccountMeta::new(signer, true),
10+
AccountMeta::new(new_account_key, false),
11+
AccountMeta::new_readonly(system_program::ID, false),
12+
],
13+
data: InitializeAccount {}.to_bytes(),
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use steel::*;
2+
3+
/// This enum is used to get a discriminator
4+
/// for the new account.
5+
#[repr(u8)]
6+
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
7+
pub enum CreateAccountDiscriminator {
8+
NewAccount = 0,
9+
}
10+
11+
/// This empty struct represents the system account
12+
/// It contains no data and is used to create a new account
13+
#[repr(C)]
14+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
15+
pub struct NewAccount {}
16+
17+
account!(CreateAccountDiscriminator, NewAccount);

basics/create-account/steel/cicd.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Buld and deploy this program with ease using a single command
4+
# Run this script with "bash cicd.sh" or "./cicd.sh"
5+
# Note: Try running "chmod +x cicd.sh" if you face any issues.
6+
7+
# Check if cargo is installed
8+
if ! command -v cargo &> /dev/null
9+
then
10+
echo "Cargo could not be found. Please install Rust."
11+
exit 1
12+
fi
13+
14+
# Check if solana CLI is installed
15+
if ! command -v solana &> /dev/null
16+
then
17+
echo "Solana CLI could not be found. Please install Solana."
18+
exit 1
19+
fi
20+
21+
22+
# Build
23+
cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so
24+
25+
# Deploy
26+
solana program deploy ./program/target/so/create_account_program.so

0 commit comments

Comments
 (0)