Skip to content

Commit 3a29232

Browse files
committed
create-token/steel - str
1 parent 512fcc5 commit 3a29232

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

tokens/create-token/steel/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ bytemuck = "1.4"
99
num_enum = "0.7"
1010
spl-token = { version = "4.0.0", features = [ "no-entrypoint" ] }
1111
mpl-token-metadata = { version = "4.1.2" }
12+
thiserror = "2.0.3"

tokens/create-token/steel/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ bytemuck.workspace = true
1313
num_enum.workspace = true
1414
spl-token.workspace = true
1515
mpl-token-metadata.workspace = true
16+
thiserror.workspace = true
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("Failed to parse string from bytes")]
7+
ParseError = 0,
8+
}
9+
10+
error!(SteelError);

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

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use {
2-
mpl_token_metadata::instructions as mpl_instruction,
2+
crate::error::*,
3+
mpl_token_metadata::{instructions as mpl_instruction, types::DataV2},
34
solana_program::{msg, program::invoke, program_pack::Pack, rent::Rent, system_instruction},
45
spl_token::state::Mint,
56
std::ffi::CStr,
@@ -17,9 +18,9 @@ instruction!(SteelInstruction, CreateToken);
1718
#[repr(C)]
1819
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
1920
pub struct CreateToken {
20-
pub token_name: [u8; 32],
21-
pub token_symbol: [u8; 10],
22-
pub token_uri: [u8; 256],
21+
pub token_name: [u8; 32], // Metaplex metadata name: 32 bytes max
22+
pub token_symbol: [u8; 10], // Metaplex metadata symbol: 10 bytes max
23+
pub token_uri: [u8; 256], // Metaplex metadata uri: 200 bytes max
2324
pub decimals: u8,
2425
}
2526

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

75-
let name = CStr::from_bytes_until_nul(&args.token_name)
76-
.unwrap()
77-
.to_str()
78-
.unwrap()
79-
.to_string();
80-
let symbol = CStr::from_bytes_until_nul(&args.token_symbol)
81-
.unwrap()
82-
.to_str()
83-
.unwrap()
84-
.to_string();
85-
let uri = CStr::from_bytes_until_nul(&args.token_uri)
86-
.unwrap()
87-
.to_str()
88-
.unwrap()
89-
.to_string();
76+
let name = Self::str_from_bytes(&mut args.token_name.to_vec())?.to_string();
77+
let symbol = Self::str_from_bytes(&mut args.token_symbol.to_vec())?.to_string();
78+
let uri = Self::str_from_bytes(&mut args.token_uri.to_vec())?.to_string();
9079

9180
mpl_instruction::CreateMetadataAccountV3Cpi {
9281
__program: token_metadata_program,
@@ -97,8 +86,8 @@ impl CreateToken {
9786
update_authority: (mint_authority, true),
9887
system_program,
9988
rent: Some(rent),
100-
__args: mpl_token_metadata::instructions::CreateMetadataAccountV3InstructionArgs {
101-
data: mpl_token_metadata::types::DataV2 {
89+
__args: mpl_instruction::CreateMetadataAccountV3InstructionArgs {
90+
data: DataV2 {
10291
name,
10392
symbol,
10493
uri,
@@ -117,4 +106,17 @@ impl CreateToken {
117106

118107
Ok(())
119108
}
109+
110+
fn str_from_bytes(bytes: &mut Vec<u8>) -> Result<&str, ProgramError> {
111+
// add an extra null byte, in the case every position is occupied with a non-null byte
112+
bytes.push(0);
113+
114+
// remove excess null bytes
115+
if let Ok(cstr) = CStr::from_bytes_until_nul(bytes) {
116+
if let Ok(str) = cstr.to_str() {
117+
return Ok(str);
118+
}
119+
}
120+
Err(SteelError::ParseError.into())
121+
}
120122
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
pub mod error;
12
pub mod instruction;
23

34
pub mod prelude {
5+
pub use crate::error::*;
46
pub use crate::instruction::*;
57
}
68

0 commit comments

Comments
 (0)