Skip to content

Commit 4e4d1cc

Browse files
feat(account-data): add steel account data example
1 parent 3a36468 commit 4e4d1cc

File tree

17 files changed

+1770
-0
lines changed

17 files changed

+1770
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
test-ledger
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"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# AccountData
2+
3+
Creates an account with data.
4+
5+
## Building
6+
7+
```sh
8+
cargo build-sbf
9+
10+
```
11+
## Tests
12+
13+
This project includes both:
14+
- Rust tests: [`program/tests`](/program/tests) directory.
15+
- Node.js tests using [Bankrun](https://kevinheavey.github.io/solana-bankrun/): [`tests`](/tests) directory.
16+
17+
```sh
18+
# rust tests
19+
cargo test-sbf
20+
21+
# node tests
22+
pnpm build-and-test # this will also build the program
23+
#or
24+
pnpm test # if you have already built the program
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use steel::*;
2+
3+
use crate::state::AddressInfoData;
4+
5+
#[repr(u8)]
6+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
7+
pub enum AddressInfoInstruction {
8+
CreateAddressInfo = 0,
9+
}
10+
11+
#[repr(C)]
12+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
13+
pub struct CreateAddressInfo {
14+
pub data: AddressInfoData,
15+
}
16+
17+
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+
// TODO Set program id
16+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn create_address_info(signer: Pubkey, data: AddressInfoData) -> Instruction {
6+
Instruction {
7+
program_id: crate::ID,
8+
accounts: vec![
9+
AccountMeta::new(signer, true),
10+
AccountMeta::new(account_pda().0, false),
11+
AccountMeta::new_readonly(system_program::ID, false),
12+
],
13+
data: CreateAddressInfo { data }.to_bytes(),
14+
}
15+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use steel::*;
2+
3+
use crate::consts::ADDRESS_INFO_SEED;
4+
5+
#[repr(C)]
6+
#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)]
7+
pub struct AddressInfoData {
8+
pub name: [u8; 64],
9+
pub house_number: [u8; 8],
10+
pub street: [u8; 64],
11+
pub city: [u8; 64],
12+
}
13+
14+
fn string_to_bytes(s: &str) -> [u8; 64] {
15+
let mut bytes = [0; 64];
16+
let s_bytes = s.as_bytes();
17+
let len = s_bytes.len().min(64);
18+
bytes[..len].copy_from_slice(&s_bytes[..len]);
19+
bytes
20+
}
21+
22+
impl AddressInfoData {
23+
pub fn new(name: String, house_number: u64, street: String, city: String) -> Self {
24+
Self {
25+
name: string_to_bytes(&name),
26+
house_number: house_number.to_le_bytes(),
27+
street: string_to_bytes(&street),
28+
city: string_to_bytes(&city),
29+
}
30+
}
31+
}
32+
33+
#[repr(u8)]
34+
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
35+
pub enum AddressInfoAccount {
36+
AddressInfo = 0,
37+
}
38+
39+
#[repr(C)]
40+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
41+
pub struct AddressInfo {
42+
pub data: AddressInfoData,
43+
}
44+
45+
account!(AddressInfoAccount, AddressInfo);
46+
47+
/// Fetch PDA of the address info account.
48+
pub fn account_pda() -> (Pubkey, u8) {
49+
Pubkey::find_program_address(&[ADDRESS_INFO_SEED], &crate::id())
50+
}
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)