-
Notifications
You must be signed in to change notification settings - Fork 483
Fix/rewrite steel learning example #365
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
Merged
jacobcreech
merged 6 commits into
solana-developers:main
from
ubadineke:fix/rewrite-steel-learning-example
Apr 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c30159
docs(anchor-program-example): correct all occurrences of 'DESCRIMINAT…
ubadineke e98e979
feat(example): overhaul code to improve correctness
ubadineke 8180e4b
refactor: improve design structure, add tests & enhance CI/CD
ubadineke 7391f78
add checks for mint and metadata
Perelyn-sama 57252cd
Merge pull request #1 from Perelyn-sama/patch-1
ubadineke 1a21122
added extra checks for token and nft mint and metadata
ubadineke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
basics/account-data/anchor/programs/anchor-program-example/src/constants.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| pub const ANCHOR_DESCRIMINATOR_SIZE: usize = 8; | ||
| pub const ANCHOR_DISCRIMINATOR_SIZE: usize = 8; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,32 @@ | ||
| # 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 ... | ||
|
|
||
| - [`Create-Token`](program/src/create_token.rs) – Create Token ... | ||
|
|
||
| ## State | ||
| - [`User`](api/src/state/user.rs) – User ... | ||
|
|
||
| ## Tests | ||
| - [`Token`](api/src/state/token.rs) – Token ... | ||
|
|
||
| ## Get started | ||
|
|
||
| To run the test suit, use the Solana toolchain: | ||
| Compile your program: | ||
|
|
||
| ```sh | ||
| steel build | ||
| ``` | ||
| cargo test-sbf | ||
|
|
||
| Run unit and integration tests: | ||
|
|
||
| ```sh | ||
| steel test | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,20 @@ | ||
| [package] | ||
| name = "steel-api" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| description = "API for interacting with the Steel program" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| homepage.workspace = true | ||
| documentation.workspace = true | ||
| repository.workspace = true | ||
| readme.workspace = true | ||
| keywords.workspace = true | ||
|
|
||
| [dependencies] | ||
| bytemuck.workspace = true | ||
| mpl-token-metadata = "5.1.0" | ||
| num_enum.workspace = true | ||
| solana-program.workspace = true | ||
| spl-token = "6.0.0" | ||
| spl-token.workspace = true | ||
| steel.workspace = true | ||
| steel-program = { version = "0.1.0", path = "../program" } | ||
| thiserror.workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,2 @@ | ||
| /// Seed of the counter account PDA. | ||
| pub const MINT: &[u8] = b"mint"; | ||
|
|
||
| pub const MINT_NOISE: [u8; 16] = [ | ||
| 89, 157, 88, 232, 243, 249, 197, 132, 199, 49, 19, 234, 91, 94, 150, 41, | ||
| ]; | ||
|
|
||
| /// Seed of the token metadata account PDA. | ||
| pub const METADATA: &[u8] = b"metadata"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,16 @@ | ||
| use steel::*; | ||
| use crate::state::*; | ||
|
|
||
| #[repr(u8)] | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] | ||
| pub enum SteelInstruction { | ||
| Create_Token = 0, | ||
| pub enum TokenInstruction { | ||
| CreateToken = 0, | ||
| } | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Clone, Copy, Debug, Pod, Zeroable)] | ||
| pub struct Create_Token { | ||
| pub token_name: [u8; 32], | ||
| pub token_symbol: [u8; 8], | ||
| pub token_uri: [u8; 64], | ||
| pub struct CreateToken { | ||
| pub data: Token | ||
| } | ||
|
|
||
| instruction!(SteelInstruction, Create_Token); | ||
| instruction!(TokenInstruction, CreateToken); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,22 @@ | ||
| use steel::*; | ||
|
|
||
| use crate::prelude::*; | ||
|
|
||
| pub fn create_token( | ||
| payer: PubKey, | ||
| mint_authority: PubKey, | ||
| token_name: String, | ||
| token_symbol: String, | ||
| token_uri: String, | ||
| signer: Pubkey, | ||
| mint: Pubkey, | ||
| data: Token | ||
| ) -> Instruction { | ||
| let token_name: [u8; 32] = token_name | ||
| .as_bytes() | ||
| .try_into() | ||
| .expect("token_name must be 32 bytes"); | ||
| let token_symbol: [u8; 8] = token_symbol | ||
| .as_bytes() | ||
| .try_into() | ||
| .expect("token_symbol must be 32 bytes"); | ||
| let token_uri: [u8; 64] = token_uri | ||
| .as_bytes() | ||
| .try_into() | ||
| .expect("token_uri must be 32 bytes"); | ||
|
|
||
| let mint_pda = PubKey::find_program_address(&[b"mint"], &crate::ID); | ||
| let metadata_pda = PubKey::find_program_address(); | ||
| Instruction { | ||
| program_id: crate::ID, | ||
| accounts: vec![ | ||
| AccountMeta::new(signer, true), | ||
| AccountMeta::new(mint, true), | ||
| AccountMeta::new(metadata_pda(&mint).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: CreateToken { data }.to_bytes() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| mod token; | ||
|
|
||
| pub use token::*; | ||
|
|
||
| use steel::*; | ||
|
|
||
| use crate::consts::*; | ||
|
|
||
| #[repr(u8)] | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq, IntoPrimitive, TryFromPrimitive)] | ||
| pub enum TokenAccount { | ||
| Token = 0 | ||
| } | ||
|
|
||
| /// Fetch PDA of the token metadata account. | ||
| pub fn metadata_pda(mint: &Pubkey) -> (Pubkey, u8) { | ||
| Pubkey::find_program_address(&[METADATA, mpl_token_metadata::ID.as_ref(), mint.as_ref()], &mpl_token_metadata::ID) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| use steel::*; | ||
|
|
||
| use super::TokenAccount; | ||
|
|
||
| #[repr(C)] | ||
| #[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)] | ||
| pub struct Token { | ||
| pub name: [u8; 32], | ||
| pub symbol: [u8; 8], | ||
| pub uri: [u8; 128], | ||
| pub decimals: u8 | ||
| } | ||
|
|
||
| account!(TokenAccount, Token); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // build.rs | ||
| use std::fs; | ||
| use std::process::Command; | ||
|
|
||
| fn main() { | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
|
|
||
| // Create the fixtures directory path | ||
| fs::create_dir_all("tests/fixtures").expect("Failed to create fixtures directory"); | ||
|
|
||
| let status = Command::new("solana") | ||
| .args([ | ||
| "program", | ||
| "dump", | ||
| "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s", | ||
| "tests/fixtures/token_metadata.so", | ||
| ]) | ||
| .status() | ||
| .expect("Failed to run solana program dump command"); | ||
|
|
||
| if !status.success() { | ||
| panic!("Failed to dump Solana program"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the state mod in this example, especially the
TokentypeAdd the token fields in api::instruction::CreateToken like so:
since you're removing the state mod, you'd need get the metadata pda pubkey in api::sdk:: create_token function. This would work: