Skip to content

Commit e013d66

Browse files
committed
Add basics/create-account/steel
1 parent 45e302a commit e013d66

File tree

18 files changed

+1663
-0
lines changed

18 files changed

+1663
-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"
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// Seed of the created account PDA.
2+
pub const CREATE_ACCOUNT: &[u8] = b"createaccount";
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub mod consts;
2+
pub mod error;
3+
pub mod instruction;
4+
pub mod sdk;
5+
pub mod state;
6+
7+
pub mod prelude {
8+
pub use crate::consts::*;
9+
pub use crate::error::*;
10+
pub use crate::instruction::*;
11+
pub use crate::sdk::*;
12+
pub use crate::state::*;
13+
}
14+
15+
use steel::*;
16+
17+
declare_id!("12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG");
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
6+
/// Creates an instruction to create an account
7+
pub fn initialize_account(signer: Pubkey) -> Instruction {
8+
Instruction {
9+
program_id: crate::ID,
10+
accounts: vec![
11+
AccountMeta::new(signer, true),
12+
AccountMeta::new(new_account_pda().unwrap().0, false),
13+
AccountMeta::new_readonly(system_program::ID, false),
14+
],
15+
data: InitializeAccount {}.to_bytes(),
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::consts::*;
2+
use steel::*;
3+
4+
/// Fetch PDA of the account.
5+
pub fn new_account_pda() -> Option<(Pubkey, u8)> {
6+
Pubkey::try_find_program_address(&[CREATE_ACCOUNT], &crate::id())
7+
}
8+
9+
/// This enum is used to get a discriminator
10+
/// for the new account.
11+
#[repr(u8)]
12+
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
13+
pub enum CreateAccountDiscriminator {
14+
NewAccount = 0,
15+
}
16+
17+
/// This empty struct represents the account type
18+
#[repr(C)]
19+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
20+
pub struct NewAccount {
21+
pub user_id: u8,
22+
}
23+
24+
account!(CreateAccountDiscriminator, NewAccount);

0 commit comments

Comments
 (0)