Skip to content

Commit 5364129

Browse files
fix: steel learning example (#365)
* docs(anchor-program-example): correct all occurrences of 'DESCRIMINATOR' to 'DISCRIMINATOR' * feat(example): overhaul code to improve correctness * refactor: improve design structure, add tests & enhance CI/CD * add checks for mint and metadata * added extra checks for token and nft mint and metadata --------- Co-authored-by: Perelyn <[email protected]>
1 parent 16068f8 commit 5364129

File tree

24 files changed

+9093
-114
lines changed

24 files changed

+9093
-114
lines changed

Cargo.lock

Lines changed: 4014 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
pub const ANCHOR_DESCRIMINATOR_SIZE: usize = 8;
1+
pub const ANCHOR_DISCRIMINATOR_SIZE: usize = 8;

basics/account-data/anchor/programs/anchor-program-example/src/instructions/create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{constants::ANCHOR_DESCRIMINATOR_SIZE, state::AddressInfo};
1+
use crate::{constants::ANCHOR_DISCRIMINATOR_SIZE, state::AddressInfo};
22
use anchor_lang::prelude::*;
33

44
#[derive(Accounts)]
@@ -9,7 +9,7 @@ pub struct CreateAddressInfo<'info> {
99
#[account(
1010
init,
1111
payer = payer,
12-
space = ANCHOR_DESCRIMINATOR_SIZE + AddressInfo::INIT_SPACE,
12+
space = ANCHOR_DISCRIMINATOR_SIZE + AddressInfo::INIT_SPACE,
1313
)]
1414
address_info: Account<'info, AddressInfo>,
1515
system_program: Program<'info, System>,

tokens/create-token/steel/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

tokens/create-token/steel/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ edition = "2021"
88
license = "Apache-2.0"
99
homepage = ""
1010
documentation = ""
11-
respository = ""
11+
repository = ""
1212
readme = "./README.md"
1313
keywords = ["solana"]
1414

1515
[workspace.dependencies]
1616
steel-api = { path = "./api", version = "0.1.0" }
1717
bytemuck = "1.14"
1818
num_enum = "0.7"
19-
solana-program = "1.18"
20-
steel = "1.3"
19+
solana-program = "2.1"
20+
steel = "3.0"
2121
thiserror = "1.0"
22+
mpl-token-metadata = { version = "5.1.0", features = ["no-entrypoint"] }
23+
spl-token = { version = "7.0.0", features = ["no-entrypoint"] }
24+

tokens/create-token/steel/README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
# Steel
22

33
**Steel** is a ...
4-
4+
55
## API
6+
67
- [`Consts`](api/src/consts.rs) – Program constants.
78
- [`Error`](api/src/error.rs) – Custom program errors.
89
- [`Event`](api/src/event.rs) – Custom program events.
910
- [`Instruction`](api/src/instruction.rs) – Declared instructions.
1011

1112
## Instructions
12-
- [`Hello`](program/src/hello.rs) – Hello ...
13+
14+
- [`Create-Token`](program/src/create_token.rs) – Create Token ...
1315

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

17-
## Tests
18+
- [`Token`](api/src/state/token.rs) – Token ...
19+
20+
## Get started
1821

19-
To run the test suit, use the Solana toolchain:
22+
Compile your program:
23+
24+
```sh
25+
steel build
2026
```
21-
cargo test-sbf
27+
28+
Run unit and integration tests:
29+
30+
```sh
31+
steel test
2232
```
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
[package]
22
name = "steel-api"
3-
version = "0.1.0"
4-
edition = "2021"
3+
description = "API for interacting with the Steel program"
4+
version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
documentation.workspace = true
9+
repository.workspace = true
10+
readme.workspace = true
11+
keywords.workspace = true
512

613
[dependencies]
714
bytemuck.workspace = true
15+
mpl-token-metadata = "5.1.0"
816
num_enum.workspace = true
917
solana-program.workspace = true
10-
spl-token = "6.0.0"
18+
spl-token.workspace = true
1119
steel.workspace = true
12-
steel-program = { version = "0.1.0", path = "../program" }
1320
thiserror.workspace = true
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
/// Seed of the counter account PDA.
2-
pub const MINT: &[u8] = b"mint";
3-
4-
pub const MINT_NOISE: [u8; 16] = [
5-
89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41,
6-
];
7-
1+
/// Seed of the token metadata account PDA.
82
pub const METADATA: &[u8] = b"metadata";

tokens/create-token/steel/api/src/instruction.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ use steel::*;
22

33
#[repr(u8)]
44
#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)]
5-
pub enum SteelInstruction {
6-
Create_Token = 0,
5+
pub enum TokenInstruction {
6+
CreateToken = 0,
77
}
88

99
#[repr(C)]
1010
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
11-
pub struct Create_Token {
12-
pub token_name: [u8; 32],
13-
pub token_symbol: [u8; 8],
14-
pub token_uri: [u8; 64],
11+
pub struct CreateToken {
12+
pub name: [u8; 32],
13+
pub symbol: [u8; 8],
14+
pub uri: [u8; 128],
15+
pub decimals: u8,
1516
}
1617

17-
instruction!(SteelInstruction, Create_Token);
18+
instruction!(TokenInstruction, CreateToken);
Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
1-
use steel::*;
2-
31
use crate::prelude::*;
2+
use steel::*;
43

54
pub fn create_token(
6-
payer: PubKey,
7-
mint_authority: PubKey,
8-
token_name: String,
9-
token_symbol: String,
10-
token_uri: String,
5+
signer: Pubkey,
6+
mint: Pubkey,
7+
name: [u8; 32],
8+
symbol: [u8; 8],
9+
uri: [u8; 128],
10+
decimals: u8,
1111
) -> Instruction {
12-
let token_name: [u8; 32] = token_name
13-
.as_bytes()
14-
.try_into()
15-
.expect("token_name must be 32 bytes");
16-
let token_symbol: [u8; 8] = token_symbol
17-
.as_bytes()
18-
.try_into()
19-
.expect("token_symbol must be 32 bytes");
20-
let token_uri: [u8; 64] = token_uri
21-
.as_bytes()
22-
.try_into()
23-
.expect("token_uri must be 32 bytes");
12+
Instruction {
13+
program_id: crate::ID,
14+
accounts: vec![
15+
AccountMeta::new(signer, true),
16+
AccountMeta::new(mint, true),
17+
AccountMeta::new(metadata_pda(mint).0, false),
18+
AccountMeta::new_readonly(system_program::ID, false),
19+
AccountMeta::new_readonly(spl_token::ID, false),
20+
AccountMeta::new_readonly(mpl_token_metadata::ID, false),
21+
AccountMeta::new_readonly(sysvar::rent::ID, false),
22+
],
23+
data: CreateToken {
24+
name,
25+
symbol,
26+
uri,
27+
decimals,
28+
}
29+
.to_bytes(),
30+
}
31+
}
2432

25-
let mint_pda = PubKey::find_program_address(&[b"mint"], &crate::ID);
26-
let metadata_pda = PubKey::find_program_address();
33+
// Fetch PDA of a metadata account.
34+
pub fn metadata_pda(mint: Pubkey) -> (Pubkey, u8) {
35+
Pubkey::find_program_address(
36+
&[METADATA, mpl_token_metadata::ID.as_ref(), mint.as_ref()],
37+
&mpl_token_metadata::ID,
38+
)
2739
}

0 commit comments

Comments
 (0)