Skip to content

Commit 30d45c7

Browse files
add basics/realloc/steel (#241)
Co-authored-by: Ayush <[email protected]>
1 parent cd7eb23 commit 30d45c7

File tree

19 files changed

+427
-0
lines changed

19 files changed

+427
-0
lines changed

basics/realloc/steel/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
test-ledger

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

basics/realloc/steel/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Realloc
2+
3+
**Realloc** is a ...
4+
5+
## API
6+
- [`Consts`](api/src/consts.rs) – Program constants.
7+
- [`Error`](api/src/error.rs) – Custom program errors.
8+
- [`Event`](api/src/event.rs) – Custom program events.
9+
- [`Instruction`](api/src/instruction.rs) – Declared instructions.
10+
11+
## Instructions
12+
- [`Add`](program/src/add.rs) – Add ...
13+
- [`Initialize`](program/src/initialize.rs) – Initialize ...
14+
15+
## State
16+
- [`Counter`](api/src/state/counter.rs) – Counter ...
17+
18+
## Get started
19+
20+
Compile your program:
21+
```sh
22+
steel build
23+
```
24+
25+
Run unit and integration tests:
26+
```sh
27+
steel test
28+
```

basics/realloc/steel/api/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "realloc-api"
3+
description = "API for interacting with the Realloc program"
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
documentation.workspace = true
9+
repository.workspace = true
10+
readme.workspace = true
11+
keywords.workspace = true
12+
13+
[dependencies]
14+
bytemuck.workspace = true
15+
num_enum.workspace = true
16+
solana-program.workspace = true
17+
steel.workspace = true
18+
thiserror.workspace = true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// Seed of the address_info account PDA.
2+
pub const ADDRESS_INFO: &[u8] = b"address_info";
3+
4+
pub const MAX_STR_LEN: usize = 32;

basics/realloc/steel/api/src/error.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use steel::*;
2+
3+
#[derive(Debug, Error, Clone, Copy, PartialEq, Eq, IntoPrimitive)]
4+
#[repr(u32)]
5+
pub enum ReallocError {
6+
#[error("This is a dummy error")]
7+
Dummy = 0,
8+
}
9+
10+
error!(ReallocError);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use steel::*;
2+
3+
#[repr(u8)]
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5+
pub enum ReallocInstruction {
6+
Initialize = 0,
7+
Add = 1
8+
}
9+
10+
#[repr(C)]
11+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
12+
pub struct Initialize {}
13+
14+
#[repr(C)]
15+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
16+
pub struct Add {
17+
pub amount: [u8; 8]
18+
}
19+
20+
instruction!(ReallocInstruction, Initialize);
21+
instruction!(ReallocInstruction, Add);

basics/realloc/steel/api/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pub mod consts;
2+
pub mod error;
3+
pub mod instruction;
4+
pub mod sdk;
5+
pub mod state;
6+
pub mod utils;
7+
8+
pub mod prelude {
9+
pub use crate::consts::*;
10+
pub use crate::error::*;
11+
pub use crate::instruction::*;
12+
pub use crate::sdk::*;
13+
pub use crate::state::*;
14+
pub use crate::utils::*;
15+
}
16+
17+
use steel::*;
18+
19+
// TODO Set program id
20+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");

basics/realloc/steel/api/src/sdk.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn initialize(signer: Pubkey) -> Instruction {
6+
Instruction {
7+
program_id: crate::ID,
8+
accounts: vec![
9+
AccountMeta::new(signer, true),
10+
AccountMeta::new(counter_pda().0, false),
11+
AccountMeta::new_readonly(system_program::ID, false),
12+
],
13+
data: Initialize {}.to_bytes()
14+
}
15+
}
16+
17+
pub fn add(signer: Pubkey, amount: u64) -> Instruction {
18+
Instruction {
19+
program_id: crate::ID,
20+
accounts: vec![
21+
AccountMeta::new(signer, true),
22+
AccountMeta::new(counter_pda().0, false),
23+
],
24+
data: Add {
25+
amount: amount.to_le_bytes(),
26+
}
27+
.to_bytes(),
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::consts::*;
2+
use crate::utils::*;
3+
use steel::*;
4+
5+
use super::ReallocAccount;
6+
7+
#[repr(C)]
8+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
9+
pub struct AddressInfo {
10+
pub name: [u8; MAX_STR_LEN],
11+
pub house_number: u8,
12+
pub street: [u8; MAX_STR_LEN],
13+
pub city: [u8; MAX_STR_LEN],
14+
}
15+
16+
impl AddressInfo {
17+
pub fn new(name: &str, house_number: u8, street: &str, city: &str) -> Self {
18+
AddressInfo {
19+
name: str_to_bytes(name),
20+
house_number,
21+
street: str_to_bytes(street),
22+
city: str_to_bytes(city),
23+
}
24+
}
25+
}
26+
27+
account!(ReallocAccount, AddressInfo);

0 commit comments

Comments
 (0)