Skip to content

Commit

Permalink
add basics/account-data/steel (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCipherCoder authored Jan 2, 2025
1 parent 2c4bc25 commit 865b716
Show file tree
Hide file tree
Showing 20 changed files with 1,802 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ no-log-ix-name = []
idl-build = ["anchor-lang/idl-build"]

[dependencies]
anchor-lang = "0.30.0"
anchor-lang = "0.30.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const ANCHOR_DESCRIMINATOR_SIZE: usize = 8;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::state::AddressInfo;
use crate::{constants::ANCHOR_DESCRIMINATOR_SIZE, state::AddressInfo};
use anchor_lang::prelude::*;

#[derive(Accounts)]
Expand All @@ -9,7 +9,7 @@ pub struct CreateAddressInfo<'info> {
#[account(
init,
payer = payer,
space = 8 + AddressInfo::INIT_SPACE,
space = ANCHOR_DESCRIMINATOR_SIZE + AddressInfo::INIT_SPACE,
)]
address_info: Account<'info, AddressInfo>,
system_program: Program<'info, System>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use anchor_lang::prelude::*;
use instructions::*;

pub mod constants;
pub mod instructions;
pub mod state;

Expand Down
21 changes: 21 additions & 0 deletions basics/account-data/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]
account-data-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = "1.3"
thiserror = "1.0"
25 changes: 25 additions & 0 deletions basics/account-data/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Account Data Program Example in Steel Framewrok

Creates an account with data.

## Build

```sh

cargo build-sbf

```

## Tests

Run the tests using following command:

```sh

# Node tests
pnpm build-and-test # This will build and test the program

#or
pnpm test # If you have already built the program test the program

```
11 changes: 11 additions & 0 deletions basics/account-data/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "account-data-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/account-data/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Seed of the address_info account PDA.
pub const ADDRESS_INFO_SEED: &[u8] = b"address_info";
19 changes: 19 additions & 0 deletions basics/account-data/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::state::AddressInfoData;
use steel::*;

/// Instruction types for the address info program
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum AddressInfoInstruction {
CreateAddressInfo = 0,
}

/// Instruction data for creating address info
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CreateAddressInfo {
pub data: AddressInfoData,
}

// Link instruction type with its data structure
instruction!(AddressInfoInstruction, CreateAddressInfo);
16 changes: 16 additions & 0 deletions basics/account-data/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub mod consts;
pub mod instruction;
pub mod sdk;
pub mod state;

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

use steel::*;

// Set your Program ID
declare_id!("Dw6Yq7TZSHdaqB2nKjsxuDrdp5xYCuZaVKFZb5vp5Y4Y");
14 changes: 14 additions & 0 deletions basics/account-data/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::prelude::*;
use steel::*;

pub fn create_address_info(signer: Pubkey, data: AddressInfoData) -> 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: CreateAddressInfo { data }.to_bytes(),
}
}
55 changes: 55 additions & 0 deletions basics/account-data/steel/api/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::consts::ADDRESS_INFO_SEED;
use steel::*;

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)]
pub struct AddressInfoData {
/// Name of the address owner (max 64 bytes)
pub name: [u8; 64],
/// House number as bytes
pub house_number: [u8; 8],
/// Street name (max 64 bytes)
pub street: [u8; 64],
/// City name (max 64 bytes)
pub city: [u8; 64],
}

impl AddressInfoData {
pub fn new(name: String, house_number: u64, street: String, city: String) -> Self {
Self {
name: string_to_bytes(&name),
house_number: house_number.to_le_bytes(),
street: string_to_bytes(&street),
city: string_to_bytes(&city),
}
}
}

fn string_to_bytes(s: &str) -> [u8; 64] {
let mut bytes = [0; 64];
let s_bytes = s.as_bytes();
let len = s_bytes.len().min(64);
bytes[..len].copy_from_slice(&s_bytes[..len]);
bytes
}

/// Account type discriminator
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum AddressInfoAccount {
AddressInfo = 0,
}

/// Account data structure
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct AddressInfo {
pub data: AddressInfoData,
}

// Link account discriminator with account data structure
account!(AddressInfoAccount, AddressInfo);

pub fn account_pda() -> (Pubkey, u8) {
Pubkey::find_program_address(&[ADDRESS_INFO_SEED], &crate::id())
}
28 changes: 28 additions & 0 deletions basics/account-data/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "account-data-program",
"version": "1.0.0",
"description": "",
"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/account_data_program.so"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/chai": "^4.3.7",
"@types/mocha": "10.0.9",
"@types/node": "^22.7.4",
"borsh": "^2.0.0",
"chai": "^4.3.7",
"mocha": "10.7.3",
"solana-bankrun": "0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "5.6.3"
}
}
Loading

0 comments on commit 865b716

Please sign in to comment.