-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basics/create-account/steel (#265)
- Loading branch information
1 parent
cb4b969
commit 3c74f3b
Showing
17 changed files
with
1,614 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
target | ||
test-ledger | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.