@@ -73,8 +73,9 @@ module zkgm::fa_coin {
73
73
use aptos_framework::primary_fungible_store;
74
74
use std::error;
75
75
use std::signer;
76
- use std::string::{Self };
76
+ use std::string::{Self , String };
77
77
use std::option;
78
+ use std::vector;
78
79
79
80
/// Only fungible asset metadata owner can make changes.
80
81
const E_NOT_OWNER : u64 = 1 ;
@@ -112,8 +113,8 @@ module zkgm::fa_coin {
112
113
primary_fungible_store::create_primary_store_enabled_fungible_asset (
113
114
constructor_ref,
114
115
option::none (),
115
- name,
116
- symbol,
116
+ sanitize_token_name ( name) ,
117
+ sanitize_token_symbol ( symbol) ,
117
118
decimals,
118
119
icon,
119
120
project
@@ -181,6 +182,11 @@ module zkgm::fa_coin {
181
182
fungible_asset::symbol <Metadata >(asset)
182
183
}
183
184
185
+ #[view]
186
+ public fun decimals_with_metadata (asset: Object <Metadata >): u8 {
187
+ fungible_asset::decimals <Metadata >(asset)
188
+ }
189
+
184
190
/// Deposit function override to ensure that the account is not denylisted and the FA coin is not paused.
185
191
/// OPTIONAL
186
192
public fun deposit <T : key >(
@@ -317,12 +323,51 @@ module zkgm::fa_coin {
317
323
borrow_global <ManagedFungibleAsset >(object::object_address (&asset))
318
324
}
319
325
326
+ public fun sanitize_token_str (name: String , max_len: u64 ): String {
327
+ let len = string::length (&name);
328
+ if (len > max_len) {
329
+ let token_name = string::sub_string (&name, len - max_len, len);
330
+ let i = max_len - 1 ;
331
+ let bytes = string::bytes (&token_name);
332
+ while (i > 0 ) {
333
+ if (*vector ::borrow (bytes, i) == 47 /* '/' */ && i != max_len - 1 ) {
334
+ return string::sub_string (&token_name, i + 1 , max_len)
335
+ };
336
+ i = i - 1 ;
337
+ };
338
+ token_name
339
+ } else {
340
+ name
341
+ }
342
+ }
343
+
344
+ public fun sanitize_token_name (name: String ): String {
345
+ sanitize_token_str (name, 32 )
346
+ }
347
+
348
+ public fun sanitize_token_symbol (symbol: String ): String {
349
+ sanitize_token_str (symbol, 10 )
350
+ }
351
+
320
352
const TEST_NAME : vector <u8 > = b"Test Coin ";
321
353
const TEST_SYMBOL : vector <u8 > = b"TST ";
322
354
const TEST_DECIMALS : u8 = 8 ;
323
355
const TEST_ICON : vector <u8 > = b"https://example.com/icon.png ";
324
356
const TEST_PROJECT : vector <u8 > = b"Test Project ";
325
357
358
+ #[test]
359
+ fun test_sanitize_token_works () {
360
+ use std::string::utf8;
361
+ assert !(sanitize_token_name (utf8 (b"alesdnleansdf ")) == utf8 (b"alesdnleansdf "), 1 );
362
+ assert !(sanitize_token_name (utf8 (b"verylongverylongverylongverylongverylongverylongverylongverylong ")) == utf8 (b"verylongverylongverylongverylong "), 2 );
363
+ assert !(sanitize_token_name (utf8 (b"factory/union12qdvmw22n72mem0ysff3nlyj2c76cuy4x60lua/clown ")) == utf8 (b"clown "), 3 );
364
+ assert !(sanitize_token_name (utf8 (b"factory/union12qdvmw22n72mem0ysff3nlyj2c76cuy4x60lua/clown/ ")) == utf8 (b"clown/ "), 4 );
365
+
366
+ assert !(sanitize_token_symbol (utf8 (b"verylongverylongverylongverylongverylongverylongverylongverylong ")) == utf8 (b"ngverylong "), 5 );
367
+ assert !(sanitize_token_symbol (utf8 (b"factory/union12qdvmw22n72mem0ysff3nlyj2c76cuy4x60lua/clown ")) == utf8 (b"clown "), 6 );
368
+ }
369
+
370
+
326
371
#[test(creator = @0x28873b2d4265e6e14bc0739ef876dce858f06380905279ed090b82d0c75f6e57 )]
327
372
public fun test_burn_with_metadata (creator: &signer ) acquires ManagedFungibleAsset {
328
373
initialize (
0 commit comments