Skip to content

Commit

Permalink
add create token steel example
Browse files Browse the repository at this point in the history
add steel and bankrun tests
  • Loading branch information
Perelyn-sama committed Oct 31, 2024
1 parent dfaa6d9 commit ae67036
Show file tree
Hide file tree
Showing 21 changed files with 2,515 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tokens/create-token/steel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
test-ledger
23 changes: 23 additions & 0 deletions tokens/create-token/steel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[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]
create-token-api = { path = "./api", version = "0.1.0" }
bytemuck = "1.14"
num_enum = "0.7"
solana-program = "1.18"
steel = { version = "1.3", features = ["spl"] }
thiserror = "1.0"
spl-token = "^4"
mpl-token-metadata = { version = "4.1.2" }
22 changes: 22 additions & 0 deletions tokens/create-token/steel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Steel

**Steel** is a ...

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

## Instructions
- [`Hello`](program/src/hello.rs) – Hello ...

## State
- [`User`](api/src/state/user.rs) – User ...

## Tests

To run the test suit, use the Solana toolchain:
```
cargo test-sbf
```
13 changes: 13 additions & 0 deletions tokens/create-token/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "create-token-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
spl-token.workspace = true
mpl-token-metadata.workspace = true
10 changes: 10 additions & 0 deletions tokens/create-token/steel/api/src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// The seed of the mint account PDA.
pub const MINT: &[u8] = b"mint";

/// Noise for deriving the mint pda
pub const MINT_NOISE: [u8; 16] = [
89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41,
];

/// The seed of the metadata account PDA.
pub const METADATA: &[u8] = b"metadata";
10 changes: 10 additions & 0 deletions tokens/create-token/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 SteelError {
#[error("This is a dummy error")]
Dummy = 0,
}

error!(SteelError);
19 changes: 19 additions & 0 deletions tokens/create-token/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::str;
use steel::*;

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

#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct Create {
pub token_name: [u8; 32],
pub token_symbol: [u8; 8],
pub token_uri: [u8; 64],
pub decimals: u8,
}

instruction!(SteelInstruction, Create);
18 changes: 18 additions & 0 deletions tokens/create-token/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 utils;

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

use steel::*;

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

use crate::prelude::*;

pub fn create(
payer: Pubkey,
mint: Pubkey,
token_name: [u8; 32],
token_symbol: [u8; 8],
token_uri: [u8; 64],
decimals: u8,
) -> Instruction {
let metadata_pda = Pubkey::find_program_address(
&[METADATA, mpl_token_metadata::ID.as_ref(), mint.as_ref()],
&mpl_token_metadata::ID,
);

Instruction {
program_id: crate::ID,
accounts: vec![
AccountMeta::new(payer, true),
AccountMeta::new(mint, true),
AccountMeta::new(metadata_pda.0, false),
AccountMeta::new_readonly(system_program::ID, false),
AccountMeta::new_readonly(spl_token::ID, false),
AccountMeta::new_readonly(mpl_token_metadata::ID, false),
AccountMeta::new_readonly(sysvar::rent::ID, false),
],
data: Create {
token_name,
token_symbol,
token_uri,
decimals,
}
.to_bytes(),
}
}
6 changes: 6 additions & 0 deletions tokens/create-token/steel/api/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn str_to_bytes<const N: usize>(str: &str) -> [u8; N] {
let mut str_bytes = [0u8; N];
let copy_len = str.len().min(N);
str_bytes[..copy_len].copy_from_slice(&str.as_bytes()[..copy_len]);
str_bytes
}
8 changes: 8 additions & 0 deletions tokens/create-token/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
solana program deploy ./program/target/deploy/program.so
28 changes: 28 additions & 0 deletions tokens/create-token/steel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"scripts": {
"test": "pnpm ts-mocha -p ./tsconfig.json -t 1000000 ./tests/bankrun.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/create_token_program.so",
"postinstall": "zx prepare.mjs"
},
"dependencies": {
"@metaplex-foundation/mpl-token-metadata": "^2.5.2",
"@solana/spl-token": "^0.3.7",
"@solana/web3.js": "^1.73.0",
"borsh": "^0.7.0",
"buffer": "^6.0.3",
"fs": "^0.0.1-security"
},
"devDependencies": {
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.1",
"@types/mocha": "^9.1.1",
"chai": "^4.3.4",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5",
"solana-bankrun": "^0.4.0",
"zx": "^8.1.4"
}
}
Loading

0 comments on commit ae67036

Please sign in to comment.