Skip to content

Commit

Permalink
add basics/create-account/steel (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitish-bot authored Jan 2, 2025
1 parent cb4b969 commit 3c74f3b
Show file tree
Hide file tree
Showing 17 changed files with 1,614 additions and 0 deletions.
3 changes: 3 additions & 0 deletions basics/create-account/steel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
test-ledger
node_modules
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 = ""
repository = ""
readme = "./README.md"
keywords = ["solana"]

[workspace.dependencies]
create-account-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = "2.0"
thiserror = "1.0"
31 changes: 31 additions & 0 deletions basics/create-account/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Steel: Create account

This "create-account" program is written using **Steel**, a framework for writing onchain programs.

## API
- [`Error`](api/src/error.rs) - Custom defined errors.
- [`Consts`](api/src/consts.rs) – Program constants.
- [`Instruction`](api/src/instruction.rs) – Declared instructions.

## Instructions
- [`Initialize`](program/src/initialize.rs) – Initialize the account creation.

## State
- [`New Account`](api/src/state.rs) – Link account and the struct that stores unique user ID.

## Get started

Compile your program:
```sh
pnpm build
```

Run unit and integration tests:
```sh
pnpm test
```

Do both together:
```sh
pnpm build-and-test
```
18 changes: 18 additions & 0 deletions basics/create-account/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "create-account-api"
description = "API for interacting with the create account program"
version.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
documentation.workspace = true
repository.workspace = true
readme.workspace = true
keywords.workspace = true

[dependencies]
bytemuck.workspace = true
num_enum.workspace = true
solana-program.workspace = true
steel.workspace = true
thiserror.workspace = true
12 changes: 12 additions & 0 deletions basics/create-account/steel/api/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use steel::*;

/// Declare custom error enum
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum CreateAccountError {
/// Discriminator for error is set to '0'
#[error("There was an error while creating your account")]
AccountCreation = 0,
}

error!(CreateAccountError);
18 changes: 18 additions & 0 deletions basics/create-account/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use steel::*;

/// Declare the Instructions enum for create account
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum CreateAccountInstruction {
/// Initialize account discriminator set to '0'
InitializeAccount = 0,
}

/// Empty initialize account struct since
/// no data input is needed
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct InitializeAccount {}

// Link Instructions enum to variant
instruction!(CreateAccountInstruction, InitializeAccount);
15 changes: 15 additions & 0 deletions basics/create-account/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub mod error;
pub mod instruction;
pub mod sdk;
pub mod state;

pub mod prelude {
pub use crate::error::*;
pub use crate::instruction::*;
pub use crate::sdk::*;
pub use crate::state::*;
}

use steel::*;

declare_id!("12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG");
15 changes: 15 additions & 0 deletions basics/create-account/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use steel::*;

use crate::prelude::*;

pub fn initialize_account(signer: Pubkey, new_account_key: Pubkey) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(new_account_key, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: InitializeAccount {}.to_bytes(),
}
}
17 changes: 17 additions & 0 deletions basics/create-account/steel/api/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use steel::*;

/// This enum is used to get a discriminator
/// for the new account.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum CreateAccountDiscriminator {
NewAccount = 0,
}

/// This empty struct represents the system account
/// It contains no data and is used to create a new account
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct NewAccount {}

account!(CreateAccountDiscriminator, NewAccount);
26 changes: 26 additions & 0 deletions basics/create-account/steel/cicd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

# Buld and deploy this program with ease using a single command
# Run this script with "bash cicd.sh" or "./cicd.sh"
# Note: Try running "chmod +x cicd.sh" if you face any issues.

# Check if cargo is installed
if ! command -v cargo &> /dev/null
then
echo "Cargo could not be found. Please install Rust."
exit 1
fi

# Check if solana CLI is installed
if ! command -v solana &> /dev/null
then
echo "Solana CLI could not be found. Please install Solana."
exit 1
fi


# Build
cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so

# Deploy
solana program deploy ./program/target/so/create_account_program.so
29 changes: 29 additions & 0 deletions basics/create-account/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "create-account-program",
"version": "1.0.0",
"type": "module",
"description": "Create an account using the steel framework for Solana",
"main": "index.js",
"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/create_account_program.so"
},
"keywords": ["solana"],
"author": "",
"license": "MIT",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/chai": "^4.3.20",
"@types/mocha": "^10.0.9",
"@types/node": "^22.8.1",
"chai": "^4.5.0",
"mocha": "^10.7.3",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3"
}
}
Loading

0 comments on commit 3c74f3b

Please sign in to comment.