Skip to content

Commit 832aab9

Browse files
authored
zkgm: better name and symbol handling, and handle decimals (#3813)
## Decimal - Add decimals to `FungibleAssetOrder` and make it V1. - `move` implementation dropped support for `FungibleAssetOrderV0`. - `cosmwasm` and `evm` implementations are backwards compatible with `FungibleAssetOrderV0`. - All implementations now default to `FungibleAssetOrderV1` in their `transfer` function. ## Token name and symbol - Fixed a bug where we don't handle minimum lengths in symbols and names in `cw20`. - Added names and symbols to the `move` impl.
2 parents 8f40207 + df04fb7 commit 832aab9

File tree

10 files changed

+569
-182
lines changed

10 files changed

+569
-182
lines changed

aptos/ibc/sources/cometbls_lc.move

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ module ibc::cometbls_lc {
707707
next_validators_hash: x"0000000000000000000000000000000000000000000000000000000000000000"
708708
};
709709

710-
let (cs, cons, _counterparty_channel_id) =
710+
let (cs, cons, _counterparty_channel_id, _) =
711711
create_client(
712712
ibc_signer,
713713
0,
@@ -734,7 +734,7 @@ module ibc::cometbls_lc {
734734
client_state.trusting_period = 2;
735735
consensus_state.timestamp = 20000;
736736

737-
let (cs, cons, _counterparty_channel_id) =
737+
let (cs, cons, _counterparty_channel_id, _) =
738738
create_client(
739739
ibc_signer,
740740
2,

aptos/ucs03-zkgm/sources/fungible_asset_order.move

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ module zkgm::fungible_asset_order {
7373
base_token_name: String,
7474
base_token_path: u256,
7575
quote_token: vector<u8>,
76-
quote_amount: u256
76+
quote_amount: u256,
77+
decimals: u8
7778
}
7879

7980
public fun new(
@@ -85,7 +86,8 @@ module zkgm::fungible_asset_order {
8586
base_token_name: String,
8687
base_token_path: u256,
8788
quote_token: vector<u8>,
88-
quote_amount: u256
89+
quote_amount: u256,
90+
decimals: u8
8991
): FungibleAssetOrder {
9092
FungibleAssetOrder {
9193
sender,
@@ -96,7 +98,8 @@ module zkgm::fungible_asset_order {
9698
base_token_name,
9799
base_token_path,
98100
quote_token,
99-
quote_amount
101+
quote_amount,
102+
decimals
100103
}
101104
}
102105

@@ -136,6 +139,10 @@ module zkgm::fungible_asset_order {
136139
order.quote_amount
137140
}
138141

142+
public fun decimals(order: &FungibleAssetOrder): u8 {
143+
order.decimals
144+
}
145+
139146
public fun encode(order: &FungibleAssetOrder): vector<u8> {
140147
let buf = vector::empty();
141148

@@ -173,6 +180,7 @@ module zkgm::fungible_asset_order {
173180
// quote_token offset
174181
zkgm_ethabi::encode_uint<u64>(&mut buf, dyn_offset);
175182
zkgm_ethabi::encode_uint<u256>(&mut buf, order.quote_amount);
183+
zkgm_ethabi::encode_uint<u8>(&mut buf, order.decimals);
176184

177185
vector::append(&mut buf, sender);
178186
vector::append(&mut buf, receiver);
@@ -195,7 +203,8 @@ module zkgm::fungible_asset_order {
195203
base_token_name: zkgm_ethabi::decode_string_from_offset(buf, &mut index),
196204
base_token_path: zkgm_ethabi::decode_uint(buf, &mut index),
197205
quote_token: zkgm_ethabi::decode_bytes_from_offset(buf, &mut index),
198-
quote_amount: zkgm_ethabi::decode_uint(buf, &mut index)
206+
quote_amount: zkgm_ethabi::decode_uint(buf, &mut index),
207+
decimals: (zkgm_ethabi::decode_uint(buf, &mut index) as u8)
199208
}
200209
}
201210

aptos/ucs03-zkgm/sources/token_denom.move

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ module zkgm::fa_coin {
7373
use aptos_framework::primary_fungible_store;
7474
use std::error;
7575
use std::signer;
76-
use std::string::{Self};
76+
use std::string::{Self, String};
7777
use std::option;
78+
use std::vector;
7879

7980
/// Only fungible asset metadata owner can make changes.
8081
const E_NOT_OWNER: u64 = 1;
@@ -112,8 +113,8 @@ module zkgm::fa_coin {
112113
primary_fungible_store::create_primary_store_enabled_fungible_asset(
113114
constructor_ref,
114115
option::none(),
115-
name,
116-
symbol,
116+
sanitize_token_name(name),
117+
sanitize_token_symbol(symbol),
117118
decimals,
118119
icon,
119120
project
@@ -181,6 +182,11 @@ module zkgm::fa_coin {
181182
fungible_asset::symbol<Metadata>(asset)
182183
}
183184

185+
#[view]
186+
public fun decimals_with_metadata(asset: Object<Metadata>): u8 {
187+
fungible_asset::decimals<Metadata>(asset)
188+
}
189+
184190
/// Deposit function override to ensure that the account is not denylisted and the FA coin is not paused.
185191
/// OPTIONAL
186192
public fun deposit<T: key>(
@@ -317,12 +323,51 @@ module zkgm::fa_coin {
317323
borrow_global<ManagedFungibleAsset>(object::object_address(&asset))
318324
}
319325

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+
320352
const TEST_NAME: vector<u8> = b"Test Coin";
321353
const TEST_SYMBOL: vector<u8> = b"TST";
322354
const TEST_DECIMALS: u8 = 8;
323355
const TEST_ICON: vector<u8> = b"https://example.com/icon.png";
324356
const TEST_PROJECT: vector<u8> = b"Test Project";
325357

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+
326371
#[test(creator = @0x28873b2d4265e6e14bc0739ef876dce858f06380905279ed090b82d0c75f6e57)]
327372
public fun test_burn_with_metadata(creator: &signer) acquires ManagedFungibleAsset {
328373
initialize(

0 commit comments

Comments
 (0)