Skip to content

Commit 865b716

Browse files
add basics/account-data/steel (#198)
1 parent 2c4bc25 commit 865b716

File tree

20 files changed

+1802
-3
lines changed

20 files changed

+1802
-3
lines changed

basics/account-data/anchor/programs/anchor-program-example/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ no-log-ix-name = []
1717
idl-build = ["anchor-lang/idl-build"]
1818

1919
[dependencies]
20-
anchor-lang = "0.30.0"
20+
anchor-lang = "0.30.1"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub const ANCHOR_DESCRIMINATOR_SIZE: usize = 8;

basics/account-data/anchor/programs/anchor-program-example/src/instructions/create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::state::AddressInfo;
1+
use crate::{constants::ANCHOR_DESCRIMINATOR_SIZE, state::AddressInfo};
22
use anchor_lang::prelude::*;
33

44
#[derive(Accounts)]
@@ -9,7 +9,7 @@ pub struct CreateAddressInfo<'info> {
99
#[account(
1010
init,
1111
payer = payer,
12-
space = 8 + AddressInfo::INIT_SPACE,
12+
space = ANCHOR_DESCRIMINATOR_SIZE + AddressInfo::INIT_SPACE,
1313
)]
1414
address_info: Account<'info, AddressInfo>,
1515
system_program: Program<'info, System>,

basics/account-data/anchor/programs/anchor-program-example/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use anchor_lang::prelude::*;
33
use instructions::*;
44

5+
pub mod constants;
56
pub mod instructions;
67
pub mod state;
78

basics/account-data/steel/Cargo.toml

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+
respository = ""
12+
readme = "./README.md"
13+
keywords = ["solana"]
14+
15+
[workspace.dependencies]
16+
account-data-api = { path = "./api", version = "0.1.0" }
17+
bytemuck = "1.14"
18+
num_enum = "0.7"
19+
solana-program = "1.18"
20+
steel = "1.3"
21+
thiserror = "1.0"

basics/account-data/steel/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Account Data Program Example in Steel Framewrok
2+
3+
Creates an account with data.
4+
5+
## Build
6+
7+
```sh
8+
9+
cargo build-sbf
10+
11+
```
12+
13+
## Tests
14+
15+
Run the tests using following command:
16+
17+
```sh
18+
19+
# Node tests
20+
pnpm build-and-test # This will build and test the program
21+
22+
#or
23+
pnpm test # If you have already built the program test the program
24+
25+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "account-data-api"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
bytemuck.workspace = true
8+
num_enum.workspace = true
9+
solana-program.workspace = true
10+
steel.workspace = true
11+
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 address_info account PDA.
2+
pub const ADDRESS_INFO_SEED: &[u8] = b"address_info";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use crate::state::AddressInfoData;
2+
use steel::*;
3+
4+
/// Instruction types for the address info program
5+
#[repr(u8)]
6+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
7+
pub enum AddressInfoInstruction {
8+
CreateAddressInfo = 0,
9+
}
10+
11+
/// Instruction data for creating address info
12+
#[repr(C)]
13+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
14+
pub struct CreateAddressInfo {
15+
pub data: AddressInfoData,
16+
}
17+
18+
// Link instruction type with its data structure
19+
instruction!(AddressInfoInstruction, CreateAddressInfo);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub mod consts;
2+
pub mod instruction;
3+
pub mod sdk;
4+
pub mod state;
5+
6+
pub mod prelude {
7+
pub use crate::consts::*;
8+
pub use crate::instruction::*;
9+
pub use crate::sdk::*;
10+
pub use crate::state::*;
11+
}
12+
13+
use steel::*;
14+
15+
// Set your Program ID
16+
declare_id!("Dw6Yq7TZSHdaqB2nKjsxuDrdp5xYCuZaVKFZb5vp5Y4Y");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::prelude::*;
2+
use steel::*;
3+
4+
pub fn create_address_info(signer: Pubkey, data: AddressInfoData) -> Instruction {
5+
Instruction {
6+
program_id: crate::ID,
7+
accounts: vec![
8+
AccountMeta::new(signer, true),
9+
AccountMeta::new(account_pda().0, false),
10+
AccountMeta::new_readonly(system_program::ID, false),
11+
],
12+
data: CreateAddressInfo { data }.to_bytes(),
13+
}
14+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::consts::ADDRESS_INFO_SEED;
2+
use steel::*;
3+
4+
#[repr(C)]
5+
#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)]
6+
pub struct AddressInfoData {
7+
/// Name of the address owner (max 64 bytes)
8+
pub name: [u8; 64],
9+
/// House number as bytes
10+
pub house_number: [u8; 8],
11+
/// Street name (max 64 bytes)
12+
pub street: [u8; 64],
13+
/// City name (max 64 bytes)
14+
pub city: [u8; 64],
15+
}
16+
17+
impl AddressInfoData {
18+
pub fn new(name: String, house_number: u64, street: String, city: String) -> Self {
19+
Self {
20+
name: string_to_bytes(&name),
21+
house_number: house_number.to_le_bytes(),
22+
street: string_to_bytes(&street),
23+
city: string_to_bytes(&city),
24+
}
25+
}
26+
}
27+
28+
fn string_to_bytes(s: &str) -> [u8; 64] {
29+
let mut bytes = [0; 64];
30+
let s_bytes = s.as_bytes();
31+
let len = s_bytes.len().min(64);
32+
bytes[..len].copy_from_slice(&s_bytes[..len]);
33+
bytes
34+
}
35+
36+
/// Account type discriminator
37+
#[repr(u8)]
38+
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
39+
pub enum AddressInfoAccount {
40+
AddressInfo = 0,
41+
}
42+
43+
/// Account data structure
44+
#[repr(C)]
45+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
46+
pub struct AddressInfo {
47+
pub data: AddressInfoData,
48+
}
49+
50+
// Link account discriminator with account data structure
51+
account!(AddressInfoAccount, AddressInfo);
52+
53+
pub fn account_pda() -> (Pubkey, u8) {
54+
Pubkey::find_program_address(&[ADDRESS_INFO_SEED], &crate::id())
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "account-data-program",
3+
"version": "1.0.0",
4+
"description": "",
5+
"scripts": {
6+
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/*.test.ts",
7+
"build-and-test": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./tests/fixtures && pnpm test",
8+
"build": "cargo build-sbf --manifest-path=./program/Cargo.toml --sbf-out-dir=./program/target/so",
9+
"deploy": "solana program deploy ./program/target/so/account_data_program.so"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC",
14+
"dependencies": {
15+
"@solana/web3.js": "^1.95.4"
16+
},
17+
"devDependencies": {
18+
"@types/chai": "^4.3.7",
19+
"@types/mocha": "10.0.9",
20+
"@types/node": "^22.7.4",
21+
"borsh": "^2.0.0",
22+
"chai": "^4.3.7",
23+
"mocha": "10.7.3",
24+
"solana-bankrun": "0.4.0",
25+
"ts-mocha": "^10.0.0",
26+
"typescript": "5.6.3"
27+
}
28+
}

0 commit comments

Comments
 (0)