Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basics/close-account/steel #308

Closed
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
9 changes: 9 additions & 0 deletions basics/close-account/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[workspace]
members = ["api","program"]
resolver = "2"

[workspace.dependencies]
solana-program = "1.18.17"
steel = "2.0"
bytemuck = "1.14"
num_enum = "0.7"
13 changes: 13 additions & 0 deletions basics/close-account/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "close-account-steel-api"
version = "0.1.0"
edition = "2021"

[dependencies]
solana-program.workspace = true
steel.workspace = true
bytemuck.workspace = true
num_enum.workspace = true

[lib]
crate-type = ["cdylib", "lib"]
76 changes: 76 additions & 0 deletions basics/close-account/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use steel::*;

use crate::state::User;

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

instruction!(SteelInstruction, CreateUser);
/// Create User Instruction
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CreateUser {
name: [u8; 48],
}

impl CreateUser {
pub fn process(
program_id: &Pubkey,
accounts: &[AccountInfo<'_>],
data: &[u8],
) -> ProgramResult {
let args = Self::try_from_bytes(data)?;

let [user_info, payer, system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

user_info.is_writable()?.has_seeds(
&[User::SEED_PREFIX.as_bytes(), payer.key.as_ref()],
program_id,
)?;

// create the user account
create_account::<User>(
user_info,
system_program,
payer,
program_id,
&[User::SEED_PREFIX.as_bytes(), payer.key.as_ref()],
)?;

let user = user_info.as_account_mut::<User>(program_id)?;

*user = User { name: args.name };

Ok(())
}
}

instruction!(SteelInstruction, CloseUser);
/// Close User Instruction
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CloseUser {}

impl CloseUser {
pub fn process(program_id: &Pubkey, accounts: &[AccountInfo<'_>]) -> ProgramResult {
let [user_info, payer, _system_program] = accounts else {
return Err(ProgramError::NotEnoughAccountKeys);
};

// ensure it's the same account
user_info.is_writable()?.has_seeds(
&[User::SEED_PREFIX.as_bytes(), payer.key.as_ref()],
program_id,
)?;

// close the program account, transfer lamports to payer
user_info.close(payer)
}
}
11 changes: 11 additions & 0 deletions basics/close-account/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod instruction;
pub mod state;

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

use steel::*;

declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
20 changes: 20 additions & 0 deletions basics/close-account/steel/api/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use steel::*;

/// account discriminator
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)]
pub enum SteelAccount {
User = 0,
}

account!(SteelAccount, User);
/// User
#[repr(C, packed)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct User {
pub name: [u8; 48],
}

impl User {
pub const SEED_PREFIX: &'static str = "USER";
}
8 changes: 8 additions & 0 deletions basics/close-account/steel/cicd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# This script is for quick building & deploying of the program.
# It also serves as a reference for the commands used for building & deploying Solana programs.
# Run this bad boy with "bash cicd.sh" or "./cicd.sh"

cargo build-sbf --manifest-path=./program/Cargo.toml --bpf-out-dir=./program/target/so
solana program deploy ./program/target/so/program.so
22 changes: 22 additions & 0 deletions basics/close-account/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"scripts": {
"test": "pnpm ts-mocha -p ./tests/tsconfig.test.json -t 1000000 ./tests/close-account.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/program.so"
},
"dependencies": {
"@solana/web3.js": "^1.95.4"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.1",
"@types/mocha": "^10.0.9",
"borsh": "0.7.0",
"chai": "^4.3.4",
"mocha": "^10.8.2",
"solana-bankrun": "^0.4.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.6.3"
}
}
Loading