11use {
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 ) ]
1920pub 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}
0 commit comments