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
21 changes: 21 additions & 0 deletions basics/close-account/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]
close-account-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = "2.0"
thiserror = "1.0"
41 changes: 41 additions & 0 deletions basics/close-account/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# CloseAccount

**CloseAccount** is a example program show you how to close 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
- [`CreateUser`](program/src/create_user.rs) – Create user state account ...
- [`CloseUser`](program/src/close_user.rs) – Close user state account ...

## State
- [`UserState`](api/src/state/user_state.rs) – Counter ...

## 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/close-account/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "close-account-api"
description = "API for interacting with the CloseAccount 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
1 change: 1 addition & 0 deletions basics/close-account/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub const USER_SEED: &[u8] = b"USER";
21 changes: 21 additions & 0 deletions basics/close-account/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use steel::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
pub enum CloseAccountInstruction {
CreateUser = 0,
CloseUser = 1,
}

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

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CloseUser {}

instruction!(CloseAccountInstruction, CreateUser);
instruction!(CloseAccountInstruction, CloseUser);
16 changes: 16 additions & 0 deletions basics/close-account/steel/api/src/lib.rs
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");
29 changes: 29 additions & 0 deletions basics/close-account/steel/api/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use steel::*;

use crate::prelude::*;

pub fn create_user(user: Pubkey, name: &str) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(user, true),
AccountMeta::new(user_state_pda(user).0, false),
AccountMeta::new_readonly(system_program::ID, false),
],
data: CreateUser {
name: name.as_bytes().try_into().unwrap(),
}
.to_bytes(),
}
}

pub fn close_user(user: Pubkey) -> Instruction {
Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(user, true),
AccountMeta::new(user_state_pda(user).0, false),
],
data: CloseUser {}.to_bytes(),
}
}
13 changes: 13 additions & 0 deletions basics/close-account/steel/api/src/state/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod user_state;

pub use user_state::*;

use steel::*;

use crate::consts::*;

#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum CloseAccountAccount {
UserState = 0,
}
18 changes: 18 additions & 0 deletions basics/close-account/steel/api/src/state/user_state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use steel::*;

use super::{CloseAccountAccount, USER_SEED};

/// Fetch PDA of the counter account.
pub fn user_state_pda(user: Pubkey) -> (Pubkey, u8) {
Pubkey::find_program_address(&[USER_SEED, user.as_ref()], &crate::id())
}

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct UserState {
pub bump: u8, // 1 byte
pub user: Pubkey, // 32 bytes
pub name: [u8; 64],
}

account!(CloseAccountAccount, UserState);
29 changes: 29 additions & 0 deletions basics/close-account/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "steel",
"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 <[email protected]>",
"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