Skip to content

Commit 61d82af

Browse files
committed
add spl token minter steel example
1 parent 3a36468 commit 61d82af

File tree

14 files changed

+440
-0
lines changed

14 files changed

+440
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
test-ledger
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
respository = ""
12+
readme = "./README.md"
13+
keywords = ["solana"]
14+
15+
[workspace.dependencies]
16+
steel-api = { path = "./api", version = "0.1.0" }
17+
bytemuck = "1.14"
18+
num_enum = "0.7"
19+
solana-program = "1.18"
20+
steel = { version = "1.3", features = ["spl"] }
21+
thiserror = "1.0"
22+
spl-token = "^4"
23+
mpl-token-metadata = { version = "4.1.2" }
24+
# mpl-token-metadata = "=2.0.0-beta.1" # mpl-token-metadata = { version = "1.11", features = ["no-entrypoint"] }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Steel
2+
3+
**Steel** 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+
- [`Hello`](program/src/hello.rs) – Hello ...
13+
14+
## State
15+
- [`User`](api/src/state/user.rs) – User ...
16+
17+
## Tests
18+
19+
To run the test suit, use the Solana toolchain:
20+
```
21+
cargo test-sbf
22+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "steel-api"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
bytemuck.workspace = true
8+
num_enum.workspace = true
9+
solana-program.workspace = true
10+
steel.workspace = true
11+
thiserror.workspace = true
12+
spl-token.workspace = true
13+
mpl-token-metadata.workspace = true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// The seed of the mint account PDA.
2+
pub const MINT: &[u8] = b"mint";
3+
4+
/// Noise for deriving the mint pda
5+
pub const MINT_NOISE: [u8; 16] = [
6+
89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41,
7+
];
8+
9+
/// The seed of the metadata account PDA.
10+
pub const METADATA: &[u8] = b"metadata";
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 SteelError {
6+
#[error("This is a dummy error")]
7+
Dummy = 0,
8+
}
9+
10+
error!(SteelError);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::str;
2+
use steel::*;
3+
4+
#[repr(u8)]
5+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
6+
pub enum SteelInstruction {
7+
Create = 0,
8+
Mint = 1,
9+
}
10+
11+
#[repr(C)]
12+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
13+
pub struct Create {
14+
pub token_name: [u8; 32],
15+
pub token_symbol: [u8; 8],
16+
pub token_uri: [u8; 64],
17+
}
18+
19+
#[repr(C)]
20+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
21+
pub struct Mint {
22+
pub amount: [u8; 8],
23+
}
24+
25+
instruction!(SteelInstruction, Mint);
26+
instruction!(SteelInstruction, Create);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub mod consts;
2+
pub mod error;
3+
pub mod instruction;
4+
pub mod sdk;
5+
6+
pub mod prelude {
7+
pub use crate::consts::*;
8+
pub use crate::error::*;
9+
pub use crate::instruction::*;
10+
pub use crate::sdk::*;
11+
}
12+
13+
use steel::*;
14+
15+
// TODO Set program id
16+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn create(
6+
payer: Pubkey,
7+
mint_authority: Pubkey,
8+
token_name: String,
9+
token_symbol: String,
10+
token_uri: String,
11+
) -> Instruction {
12+
let token_name_bytes: [u8; 32] = token_name
13+
.as_bytes()
14+
.try_into()
15+
.expect("String wrong length, expected 32 bytes");
16+
let token_symbol_bytes: [u8; 8] = token_symbol
17+
.as_bytes()
18+
.try_into()
19+
.expect("String wrong length, expected 32 bytes");
20+
let token_uri_bytes: [u8; 64] = token_uri
21+
.as_bytes()
22+
.try_into()
23+
.expect("String wrong length, expected 32 bytes");
24+
25+
let mint_pda = Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &crate::ID);
26+
let metadata_pda = Pubkey::find_program_address(
27+
&[
28+
METADATA,
29+
mpl_token_metadata::ID.as_ref(),
30+
mint_pda.0.as_ref(),
31+
],
32+
&mpl_token_metadata::ID,
33+
);
34+
35+
Instruction {
36+
program_id: crate::ID,
37+
accounts: vec![
38+
AccountMeta::new(payer, true),
39+
AccountMeta::new(mint_pda.0, false),
40+
AccountMeta::new(mint_authority, false),
41+
AccountMeta::new(metadata_pda.0, false),
42+
AccountMeta::new_readonly(system_program::ID, false),
43+
AccountMeta::new_readonly(spl_token::ID, false),
44+
AccountMeta::new_readonly(mpl_token_metadata::ID, false),
45+
AccountMeta::new_readonly(sysvar::rent::ID, false),
46+
],
47+
data: Create {
48+
token_name: token_name_bytes,
49+
token_symbol: token_symbol_bytes,
50+
token_uri: token_uri_bytes,
51+
}
52+
.to_bytes(),
53+
}
54+
}
55+
pub fn mint(
56+
signer: Pubkey,
57+
mint: Pubkey,
58+
to: Pubkey,
59+
authority: Pubkey,
60+
amount: u64,
61+
) -> Instruction {
62+
Instruction {
63+
program_id: crate::ID,
64+
accounts: vec![
65+
AccountMeta::new(signer, true),
66+
AccountMeta::new(mint, false),
67+
AccountMeta::new(to, false),
68+
AccountMeta::new(authority, false),
69+
AccountMeta::new_readonly(spl_token::ID, false),
70+
],
71+
data: Mint {
72+
amount: amount.to_le_bytes(),
73+
}
74+
.to_bytes(),
75+
}
76+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "steel-program"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
crate-type = ["cdylib", "lib"]
8+
9+
[dependencies]
10+
steel-api.workspace = true
11+
solana-program.workspace = true
12+
steel.workspace = true
13+
spl-token.workspace = true
14+
mpl-token-metadata.workspace = true
15+
16+
17+
[dev-dependencies]
18+
bs64 = "0.1.2"
19+
rand = "0.8.5"
20+
solana-program-test = "1.18"
21+
solana-sdk = "1.18"
22+
tokio = { version = "1.35", features = ["full"] }

0 commit comments

Comments
 (0)