Skip to content

Commit 45531de

Browse files
committed
add pda mint authority steel example
1 parent dfaa6d9 commit 45531de

File tree

16 files changed

+503
-0
lines changed

16 files changed

+503
-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+
mpl-token-metadata = { version = "4.1.2" }
23+
spl-token = "^4"
24+
const-crypto = "0.1.0"
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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
14+
const-crypto.workspace = true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use const_crypto::ed25519;
2+
use solana_program::pubkey::Pubkey;
3+
4+
/// Program id for const pda derivations
5+
const PROGRAM_ID: [u8; 32] = unsafe { *(&crate::id() as *const Pubkey as *const [u8; 32]) };
6+
7+
/// The seed of the mint authority account PDA.
8+
pub const MINT_AUTHORITY: &[u8] = b"mint_authority";
9+
10+
/// The seed of the mint account PDA.
11+
pub const MINT: &[u8] = b"mint";
12+
13+
/// Noise for deriving the mint pda
14+
pub const MINT_NOISE: [u8; 16] = [
15+
89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41,
16+
];
17+
18+
/// The seed of the metadata account PDA.
19+
pub const METADATA: &[u8] = b"metadata";
20+
21+
/// The bump of the mint authority account, for cpis.
22+
pub const MINT_AUTHORITY_BUMP: u8 =
23+
ed25519::derive_program_address(&[MINT_AUTHORITY], &PROGRAM_ID).1;
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use steel::*;
2+
3+
#[repr(u8)]
4+
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5+
pub enum SteelInstruction {
6+
Create = 0,
7+
Mint = 1,
8+
}
9+
10+
#[repr(C)]
11+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
12+
pub struct Create {
13+
pub token_name: [u8; 32],
14+
pub token_symbol: [u8; 8],
15+
pub token_uri: [u8; 64],
16+
pub mint_authority_bump: u8,
17+
pub mint_bump: u8,
18+
}
19+
20+
#[repr(C)]
21+
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
22+
pub struct Mint {
23+
pub amount: [u8; 8],
24+
}
25+
26+
instruction!(SteelInstruction, Create);
27+
instruction!(SteelInstruction, Mint);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub mod consts;
2+
pub mod error;
3+
pub mod instruction;
4+
pub mod sdk;
5+
pub mod state;
6+
7+
pub mod prelude {
8+
pub use crate::consts::*;
9+
pub use crate::error::*;
10+
pub use crate::instruction::*;
11+
pub use crate::sdk::*;
12+
pub use crate::state::*;
13+
}
14+
15+
use steel::*;
16+
17+
// TODO Set program id
18+
declare_id!("z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35");
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use steel::*;
2+
3+
use crate::prelude::*;
4+
5+
pub fn create(
6+
payer: Pubkey,
7+
token_name: String,
8+
token_symbol: String,
9+
token_uri: String,
10+
) -> Instruction {
11+
let token_name_bytes: [u8; 32] = token_name
12+
.as_bytes()
13+
.try_into()
14+
.expect("String wrong length, expected 32 bytes");
15+
let token_symbol_bytes: [u8; 8] = token_symbol
16+
.as_bytes()
17+
.try_into()
18+
.expect("String wrong length, expected 32 bytes");
19+
let token_uri_bytes: [u8; 64] = token_uri
20+
.as_bytes()
21+
.try_into()
22+
.expect("String wrong length, expected 32 bytes");
23+
24+
let mint_pda = Pubkey::find_program_address(&[MINT, MINT_NOISE.as_slice()], &crate::ID);
25+
let metadata_pda = Pubkey::find_program_address(
26+
&[
27+
METADATA,
28+
mpl_token_metadata::ID.as_ref(),
29+
mint_pda.0.as_ref(),
30+
],
31+
&mpl_token_metadata::ID,
32+
);
33+
let mint_authority_pda = mint_authority_pda();
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_pda.0, 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+
mint_authority_bump: mint_authority_pda.1,
52+
mint_bump: mint_pda.1,
53+
}
54+
.to_bytes(),
55+
}
56+
}
57+
pub fn mint(
58+
signer: Pubkey,
59+
mint: Pubkey,
60+
to: Pubkey,
61+
authority: Pubkey,
62+
amount: u64,
63+
) -> Instruction {
64+
Instruction {
65+
program_id: crate::ID,
66+
accounts: vec![
67+
AccountMeta::new(signer, true),
68+
AccountMeta::new(mint, false),
69+
AccountMeta::new(to, false),
70+
AccountMeta::new(authority, false),
71+
AccountMeta::new_readonly(spl_token::ID, false),
72+
],
73+
data: Mint {
74+
amount: amount.to_le_bytes(),
75+
}
76+
.to_bytes(),
77+
}
78+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use steel::*;
2+
3+
use super::SteelAccount;
4+
5+
#[repr(C)]
6+
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
7+
pub struct MintAuthorityPda {
8+
bump: u8,
9+
}
10+
11+
account!(SteelAccount, MintAuthorityPda);

0 commit comments

Comments
 (0)