Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
#basics
"basics/account-data/native/program",
"basics/account-data/anchor/programs/anchor-program-example",
"basics/account-data/steel/program",
"basics/checking-accounts/native/program",
"basics/checking-accounts/anchor/programs/anchor-program-example",
"basics/close-account/native/program",
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 = ""
repository = ""
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 = "2.0"
thiserror = "1.0"
43 changes: 43 additions & 0 deletions basics/account-data/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# AccountDataProgram

**AccountDataProgram** is a program that allows you to store and retrieve data from a Solana account.

## API

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

## Instructions

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

## State

- [`AddressInfoData`](api/src/state/address_info.rs) – Account data structure.

## How to?

Compile your program:

```sh
pnpm build
```

Run tests:

```sh
pnpm test
```

Run build and test

```sh
pnpm build-and-test
```

Deploy your program:

```sh
pnpm deploy
```
18 changes: 18 additions & 0 deletions basics/account-data/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "account-data-api"
description = "API for interacting with the AccountDataProgram 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
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 account PDA.
pub const ACCOUNT_DATA_SEED: &[u8] = b"account";
10 changes: 10 additions & 0 deletions basics/account-data/steel/api/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use steel::*;

#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
#[repr(u32)]
pub enum AccountDataProgramError {
#[error("This is a dummy error")]
Dummy = 0,
}

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

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum AccountDataInstruction {
Initialize = 0,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Initialize {
pub name: [u8; 64],
pub house_number: u8,
pub street: [u8; 64],
pub city: [u8; 64],
}

instruction!(AccountDataInstruction, Initialize);
18 changes: 18 additions & 0 deletions basics/account-data/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod consts;
pub mod error;
pub mod instruction;
pub mod sdk;
pub mod state;

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

use steel::*;

// TODO Set program id
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
27 changes: 27 additions & 0 deletions basics/account-data/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use steel::*;

use crate::prelude::*;

pub fn initialize(
signer: Pubkey,
name: &str,
house_number: u8,
city: &str,
street: &str,
) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(signer, true),
AccountMeta::new(address_info_pda(signer).0, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: Initialize {
name: name.as_bytes().try_into().unwrap(),
house_number: house_number,
city: city.as_bytes().try_into().unwrap(),
street: street.as_bytes().try_into().unwrap(),
}
.to_bytes(),
}
}
30 changes: 30 additions & 0 deletions basics/account-data/steel/api/src/state/address_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use steel::*;

use crate::consts::ACCOUNT_DATA_SEED;

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

/// Fetch PDA of the data account.
pub fn address_info_pda(authority: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[ACCOUNT_DATA_SEED, authority.as_ref()], &crate::id())
}

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct AddressInfoData {
// Max length 64 bytes
pub name: [u8; 64],
pub house_number: u8,
// Max length 64 bytes
pub street: [u8; 64],
// Max length 64 bytes
pub city: [u8; 64],
pub bump: u8,
}

account!(AddressInfoDescriminator, AddressInfoData);
3 changes: 3 additions & 0 deletions basics/account-data/steel/api/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod address_info;

pub use address_info::*;
29 changes: 29 additions & 0 deletions basics/account-data/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "account-data-program",
"version": "1.0.0",
"description": "",
"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/account_data_program.so"
},
"keywords": [],
"author": "Leo Pham <hongthaipro@gmail.com>",
"license": "ISC",
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.9",
"borsh": "^2.0.0",
"mocha": "^10.7.3",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3",
"chai": "^4.3.7",
"@types/chai": "^4.3.7"
}
}
Loading