From ae5e6355c2d4f0d8b99260c0c6b507214063a729 Mon Sep 17 00:00:00 2001 From: baichuan3 Date: Thu, 6 Mar 2025 11:16:31 +0800 Subject: [PATCH] support get coin info by coin type name --- frameworks/rooch-framework/doc/coin.md | 77 +++++++++++++++++++ .../sources/account_coin_store.move | 24 ------ frameworks/rooch-framework/sources/coin.move | 51 ++++++++---- .../rooch-framework/sources/transfer.move | 9 --- 4 files changed, 112 insertions(+), 49 deletions(-) diff --git a/frameworks/rooch-framework/doc/coin.md b/frameworks/rooch-framework/doc/coin.md index e9cc475f96..6864492810 100644 --- a/frameworks/rooch-framework/doc/coin.md +++ b/frameworks/rooch-framework/doc/coin.md @@ -21,14 +21,19 @@ This module provides the foundation for typesafe Coins. - [Function `coin_info_id`](#0x3_coin_coin_info_id) - [Function `name`](#0x3_coin_name) - [Function `name_by_type`](#0x3_coin_name_by_type) +- [Function `name_by_type_name`](#0x3_coin_name_by_type_name) - [Function `symbol`](#0x3_coin_symbol) - [Function `symbol_by_type`](#0x3_coin_symbol_by_type) +- [Function `symbol_by_type_name`](#0x3_coin_symbol_by_type_name) - [Function `decimals`](#0x3_coin_decimals) - [Function `decimals_by_type`](#0x3_coin_decimals_by_type) +- [Function `decimals_by_type_name`](#0x3_coin_decimals_by_type_name) - [Function `supply`](#0x3_coin_supply) - [Function `supply_by_type`](#0x3_coin_supply_by_type) +- [Function `supply_by_type_name`](#0x3_coin_supply_by_type_name) - [Function `icon_url`](#0x3_coin_icon_url) - [Function `icon_url_by_type`](#0x3_coin_icon_url_by_type) +- [Function `icon_url_by_type_name`](#0x3_coin_icon_url_by_type_name) - [Function `is_same_coin`](#0x3_coin_is_same_coin) - [Function `destroy_zero`](#0x3_coin_destroy_zero) - [Function `extract`](#0x3_coin_extract) @@ -37,6 +42,7 @@ This module provides the foundation for typesafe Coins. - [Function `value`](#0x3_coin_value) - [Function `zero`](#0x3_coin_zero) - [Function `coin_info`](#0x3_coin_coin_info) +- [Function `get_coin_info_by_type_name`](#0x3_coin_get_coin_info_by_type_name) - [Function `upsert_icon_url`](#0x3_coin_upsert_icon_url) - [Function `register_extend`](#0x3_coin_register_extend) - [Function `init_metadata`](#0x3_coin_init_metadata) @@ -381,6 +387,18 @@ Returns the name of the coin by the type CoinType + + +## Function `name_by_type_name` + +Returns the name of the coin by the coin type name + + +
public fun name_by_type_name(coin_type_name: &string::String): string::String
+
+ + + ## Function `symbol` @@ -405,6 +423,18 @@ Returns the symbol of the coin by the type CoinType + + +## Function `symbol_by_type_name` + +Returns the symbol of the coin by the coin type name + + +
public fun symbol_by_type_name(coin_type_name: &string::String): string::String
+
+ + + ## Function `decimals` @@ -431,6 +461,18 @@ Returns the decimals of the coin by the type CoinType + + +## Function `decimals_by_type_name` + +Returns the decimals of the coin by the coin type name + + +
public fun decimals_by_type_name(coin_type_name: &string::String): u8
+
+ + + ## Function `supply` @@ -455,6 +497,18 @@ Returns the amount of coin in existence by the type CoinType + + +## Function `supply_by_type_name` + +Returns the amount of coin in existence by the coin type name + + +
public fun supply_by_type_name(coin_type_name: &string::String): u256
+
+ + + ## Function `icon_url` @@ -479,6 +533,18 @@ Returns the icon url of coin by the type CoinType + + +## Function `icon_url_by_type_name` + +Returns the icon url of the coin by the coin type name + + +
public fun icon_url_by_type_name(coin_type_name: &string::String): option::Option<string::String>
+
+ + + ## Function `is_same_coin` @@ -577,6 +643,17 @@ Borrow the CoinInfo + + +## Function `get_coin_info_by_type_name` + + + +
public fun get_coin_info_by_type_name(coin_type_name: &string::String): option::Option<object::ObjectID>
+
+ + + ## Function `upsert_icon_url` diff --git a/frameworks/rooch-framework/sources/account_coin_store.move b/frameworks/rooch-framework/sources/account_coin_store.move index c3d7101e9a..e097013266 100644 --- a/frameworks/rooch-framework/sources/account_coin_store.move +++ b/frameworks/rooch-framework/sources/account_coin_store.move @@ -2,8 +2,6 @@ // SPDX-License-Identifier: Apache-2.0 module rooch_framework::account_coin_store { - - use std::string; use moveos_std::account; use moveos_std::object::{Self, ObjectID, Object}; use moveos_std::table; @@ -124,18 +122,6 @@ module rooch_framework::account_coin_store { transfer_internal(from_addr, to, amount); } - /// Transfer `amount` of coins `coin_type_name` from `from` to `to`. - /// Any account and module can call this function to transfer coins, the `CoinType` corresponding to `coin_type_name` must must have `key` and `store` abilities. - public fun transfer_coin_by_type_name( - from: &signer, - to: address, - coin_type_name: string::String, - amount: u256, - ) { - let from_addr = signer::address_of(from); - transfer_internal(from_addr, to, amount); - } - public fun exist_account_coin_store(addr: address): bool { let account_coin_store_id = account_coin_store_id(addr); object::exists_object_with_type>(account_coin_store_id) @@ -253,14 +239,4 @@ module rooch_framework::account_coin_store { let coin = withdraw_internal(from, amount); deposit_internal(to, coin); } - - fun transfer_coin_by_type_name_internal( - from: address, - to: address, - coin_type_name: string::String, - amount: u256, - ) { - let coin = withdraw_internal(from, amount); - deposit_internal(to, coin); - } } \ No newline at end of file diff --git a/frameworks/rooch-framework/sources/coin.move b/frameworks/rooch-framework/sources/coin.move index 3d872db65c..7c5e0061e2 100644 --- a/frameworks/rooch-framework/sources/coin.move +++ b/frameworks/rooch-framework/sources/coin.move @@ -175,9 +175,14 @@ module rooch_framework::coin { /// Returns the name of the coin by the type `CoinType` public fun name_by_type(): string::String { let coin_type = type_info::type_name(); + name_by_type_name(&coin_type) + } + + /// Returns the name of the coin by the coin type name + public fun name_by_type_name(coin_type_name: &String): string::String { let registry = borrow_registry(); - assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered); - let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type); + assert!(object::contains_field(registry, *coin_type_name), ErrorCoinInfoNotRegistered); + let coin_metadata: &CoinMetadata = object::borrow_field(registry, *coin_type_name); coin_metadata.name } @@ -192,6 +197,14 @@ module rooch_framework::coin { symbol_by_type_name(&coin_type) } + /// Returns the symbol of the coin by the coin type name + public fun symbol_by_type_name(coin_type_name: &String): string::String { + let registry = borrow_registry(); + assert!(object::contains_field(registry, *coin_type_name), ErrorCoinInfoNotRegistered); + let coin_metadata: &CoinMetadata = object::borrow_field(registry, *coin_type_name); + coin_metadata.symbol + } + /// Returns the number of decimals used to get its user representation. /// For example, if `decimals` equals `2`, a balance of `505` coins should /// be displayed to a user as `5.05` (`505 / 10 ** 2`). @@ -202,9 +215,14 @@ module rooch_framework::coin { /// Returns the decimals of the coin by the type `CoinType` public fun decimals_by_type(): u8 { let coin_type = type_info::type_name(); + decimals_by_type_name(&coin_type) + } + + /// Returns the decimals of the coin by the coin type name + public fun decimals_by_type_name(coin_type_name: &String): u8 { let registry = borrow_registry(); - assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered); - let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type); + assert!(object::contains_field(registry, *coin_type_name), ErrorCoinInfoNotRegistered); + let coin_metadata: &CoinMetadata = object::borrow_field(registry, *coin_type_name); coin_metadata.decimals } @@ -216,9 +234,14 @@ module rooch_framework::coin { /// Returns the amount of coin in existence by the type `CoinType` public fun supply_by_type(): u256 { let coin_type = type_info::type_name(); + supply_by_type_name(&coin_type) + } + + /// Returns the amount of coin in existence by the coin type name + public fun supply_by_type_name(coin_type_name: &String): u256 { let registry = borrow_registry(); - assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered); - let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type); + assert!(object::contains_field(registry, *coin_type_name), ErrorCoinInfoNotRegistered); + let coin_metadata: &CoinMetadata = object::borrow_field(registry, *coin_type_name); coin_metadata.supply } @@ -230,18 +253,15 @@ module rooch_framework::coin { /// Returns the icon url of coin by the type `CoinType` public fun icon_url_by_type(): Option { let coin_type = type_info::type_name(); - let registry = borrow_registry(); - assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered); - let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type); - coin_metadata.icon_url + icon_url_by_type_name(&coin_type) } - /// Returns the symbol of the coin by the coin type name - public fun symbol_by_type_name(coin_type_name: &String): string::String { + /// Returns the icon url of the coin by the coin type name + public fun icon_url_by_type_name(coin_type_name: &String): Option { let registry = borrow_registry(); - assert!(object::contains_field(registry, coin_type_name), ErrorCoinInfoNotRegistered); - let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type_name); - coin_metadata.symbol + assert!(object::contains_field(registry, *coin_type_name), ErrorCoinInfoNotRegistered); + let coin_metadata: &CoinMetadata = object::borrow_field(registry, *coin_type_name); + coin_metadata.icon_url } /// Return true if the type `CoinType1` is same with `CoinType2` @@ -249,7 +269,6 @@ module rooch_framework::coin { return type_info::type_of() == type_info::type_of() } - /// Destroys a zero-value coin. Calls will fail if the `value` in the passed-in `coin` is non-zero /// so it is impossible to "burn" any non-zero amount of `Coin`. public fun destroy_zero(zero_coin: Coin) { diff --git a/frameworks/rooch-framework/sources/transfer.move b/frameworks/rooch-framework/sources/transfer.move index c55d6de768..92ccf9677b 100644 --- a/frameworks/rooch-framework/sources/transfer.move +++ b/frameworks/rooch-framework/sources/transfer.move @@ -66,13 +66,4 @@ module rooch_framework::transfer { address_mapping::bind_bitcoin_address_internal(rooch_address, btc_address); object::transfer(obj, rooch_address); } - - public entry fun transfer_coin_by_type_name( - from: &signer, - to: address, - coin_type_name: String, - amount: u256, - ) { - account_coin_store::transfer_by_type_name(from, to, coin_type_name, amount) - } }