Skip to content

Commit

Permalink
create-token/steel - str
Browse files Browse the repository at this point in the history
  • Loading branch information
thewuhxyz committed Nov 23, 2024
1 parent 512fcc5 commit 3a29232
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions tokens/create-token/steel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ bytemuck = "1.4"
num_enum = "0.7"
spl-token = { version = "4.0.0", features = [ "no-entrypoint" ] }
mpl-token-metadata = { version = "4.1.2" }
thiserror = "2.0.3"
1 change: 1 addition & 0 deletions tokens/create-token/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ bytemuck.workspace = true
num_enum.workspace = true
spl-token.workspace = true
mpl-token-metadata.workspace = true
thiserror.workspace = true
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("Failed to parse string from bytes")]
ParseError = 0,
}

error!(SteelError);
44 changes: 23 additions & 21 deletions tokens/create-token/steel/api/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use {
mpl_token_metadata::instructions as mpl_instruction,
crate::error::*,
mpl_token_metadata::{instructions as mpl_instruction, types::DataV2},
solana_program::{msg, program::invoke, program_pack::Pack, rent::Rent, system_instruction},
spl_token::state::Mint,
std::ffi::CStr,
Expand All @@ -17,9 +18,9 @@ instruction!(SteelInstruction, CreateToken);
#[repr(C)]
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
pub struct CreateToken {
pub token_name: [u8; 32],
pub token_symbol: [u8; 10],
pub token_uri: [u8; 256],
pub token_name: [u8; 32], // Metaplex metadata name: 32 bytes max
pub token_symbol: [u8; 10], // Metaplex metadata symbol: 10 bytes max
pub token_uri: [u8; 256], // Metaplex metadata uri: 200 bytes max
pub decimals: u8,
}

Expand Down Expand Up @@ -72,21 +73,9 @@ impl CreateToken {
msg!("Creating metadata account...");
msg!("Metadata account address: {}", metadata_account.key);

let name = CStr::from_bytes_until_nul(&args.token_name)
.unwrap()
.to_str()
.unwrap()
.to_string();
let symbol = CStr::from_bytes_until_nul(&args.token_symbol)
.unwrap()
.to_str()
.unwrap()
.to_string();
let uri = CStr::from_bytes_until_nul(&args.token_uri)
.unwrap()
.to_str()
.unwrap()
.to_string();
let name = Self::str_from_bytes(&mut args.token_name.to_vec())?.to_string();
let symbol = Self::str_from_bytes(&mut args.token_symbol.to_vec())?.to_string();
let uri = Self::str_from_bytes(&mut args.token_uri.to_vec())?.to_string();

mpl_instruction::CreateMetadataAccountV3Cpi {
__program: token_metadata_program,
Expand All @@ -97,8 +86,8 @@ impl CreateToken {
update_authority: (mint_authority, true),
system_program,
rent: Some(rent),
__args: mpl_token_metadata::instructions::CreateMetadataAccountV3InstructionArgs {
data: mpl_token_metadata::types::DataV2 {
__args: mpl_instruction::CreateMetadataAccountV3InstructionArgs {
data: DataV2 {
name,
symbol,
uri,
Expand All @@ -117,4 +106,17 @@ impl CreateToken {

Ok(())
}

fn str_from_bytes(bytes: &mut Vec<u8>) -> Result<&str, ProgramError> {
// add an extra null byte, in the case every position is occupied with a non-null byte
bytes.push(0);

// remove excess null bytes
if let Ok(cstr) = CStr::from_bytes_until_nul(bytes) {
if let Ok(str) = cstr.to_str() {
return Ok(str);
}
}
Err(SteelError::ParseError.into())
}
}
2 changes: 2 additions & 0 deletions tokens/create-token/steel/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod error;
pub mod instruction;

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

Expand Down

0 comments on commit 3a29232

Please sign in to comment.