-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(account-data): add steel account data example
- Loading branch information
1 parent
3a36468
commit 4e4d1cc
Showing
17 changed files
with
1,770 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,2 @@ | ||
target | ||
test-ledger |
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 = "" | ||
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" |
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,25 @@ | ||
# AccountData | ||
|
||
Creates an account with data. | ||
|
||
## Building | ||
|
||
```sh | ||
cargo build-sbf | ||
|
||
``` | ||
## Tests | ||
|
||
This project includes both: | ||
- Rust tests: [`program/tests`](/program/tests) directory. | ||
- Node.js tests using [Bankrun](https://kevinheavey.github.io/solana-bankrun/): [`tests`](/tests) directory. | ||
|
||
```sh | ||
# rust tests | ||
cargo test-sbf | ||
|
||
# node tests | ||
pnpm build-and-test # this will also build the program | ||
#or | ||
pnpm test # if you have already built the program | ||
``` |
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,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 |
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,2 @@ | ||
/// Seed of the address info account PDA. | ||
pub const ADDRESS_INFO_SEED: &[u8] = b"address_info"; |
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::*; | ||
|
||
use crate::state::AddressInfoData; | ||
|
||
#[repr(u8)] | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] | ||
pub enum AddressInfoInstruction { | ||
CreateAddressInfo = 0, | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy, Debug, Pod, Zeroable)] | ||
pub struct CreateAddressInfo { | ||
pub data: AddressInfoData, | ||
} | ||
|
||
instruction!(AddressInfoInstruction, CreateAddressInfo); |
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,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::*; | ||
|
||
// TODO Set program id | ||
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35"); |
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 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(), | ||
} | ||
} |
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,50 @@ | ||
use steel::*; | ||
|
||
use crate::consts::ADDRESS_INFO_SEED; | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)] | ||
pub struct AddressInfoData { | ||
pub name: [u8; 64], | ||
pub house_number: [u8; 8], | ||
pub street: [u8; 64], | ||
pub city: [u8; 64], | ||
} | ||
|
||
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 | ||
} | ||
|
||
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), | ||
} | ||
} | ||
} | ||
|
||
#[repr(u8)] | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] | ||
pub enum AddressInfoAccount { | ||
AddressInfo = 0, | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] | ||
pub struct AddressInfo { | ||
pub data: AddressInfoData, | ||
} | ||
|
||
account!(AddressInfoAccount, AddressInfo); | ||
|
||
/// Fetch PDA of the address info account. | ||
pub fn account_pda() -> (Pubkey, u8) { | ||
Pubkey::find_program_address(&[ADDRESS_INFO_SEED], &crate::id()) | ||
} |
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,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" | ||
} | ||
} |
Oops, something went wrong.