Skip to content

Commit

Permalink
spl-token-minter/steel - str
Browse files Browse the repository at this point in the history
  • Loading branch information
thewuhxyz committed Nov 23, 2024
1 parent 9561333 commit 60f1c8b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
3 changes: 2 additions & 1 deletion tokens/spl-token-minter/steel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ bytemuck = "1.4"
num_enum = "0.7"
spl-token = { version = "4.0.0", features = [ "no-entrypoint" ] }
spl-associated-token-account = { version = "3.0.4", features = [ "no-entrypoint" ] }
mpl-token-metadata = { version = "4.1.2" }
mpl-token-metadata = { version = "4.1.2" }
thiserror = "2.0.3"
1 change: 1 addition & 0 deletions tokens/spl-token-minter/steel/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ num_enum.workspace = true
spl-token.workspace = true
spl-associated-token-account.workspace = true
mpl-token-metadata.workspace = true
thiserror.workspace = true
10 changes: 10 additions & 0 deletions tokens/spl-token-minter/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);
45 changes: 23 additions & 22 deletions tokens/spl-token-minter/steel/api/src/instruction/create.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use super::SteelInstruction;
use mpl_token_metadata::instructions as mpl_instruction;
use crate::error::*;
use mpl_token_metadata::{instructions as mpl_instruction, types::DataV2};
use solana_program::{msg, program::invoke, program_pack::Pack, rent::Rent, system_instruction};
use spl_token::state::Mint;
use std::ffi::CStr;
use steel::*;

// Using borsh for easier handling of dynamic types e.g strings
instruction!(SteelInstruction, CreateToken);
// CreateToken instruction
#[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 @@ -66,21 +66,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 @@ -91,8 +79,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 @@ -111,4 +99,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/spl-token-minter/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 60f1c8b

Please sign in to comment.