Skip to content

Commit

Permalink
[Framework] Support get coin info by coin type name (#3400)
Browse files Browse the repository at this point in the history
* draft implements

* support get coin info by coin type name
  • Loading branch information
baichuan3 authored Mar 6, 2025
1 parent 2c75948 commit 0184cb4
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 17 deletions.
77 changes: 77 additions & 0 deletions frameworks/rooch-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -381,6 +387,18 @@ Returns the name of the coin by the type <code>CoinType</code>



<a name="0x3_coin_name_by_type_name"></a>

## Function `name_by_type_name`

Returns the name of the coin by the coin type name


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_name_by_type_name">name_by_type_name</a>(coin_type_name: &<a href="_String">string::String</a>): <a href="_String">string::String</a>
</code></pre>



<a name="0x3_coin_symbol"></a>

## Function `symbol`
Expand All @@ -405,6 +423,18 @@ Returns the symbol of the coin by the type <code>CoinType</code>



<a name="0x3_coin_symbol_by_type_name"></a>

## Function `symbol_by_type_name`

Returns the symbol of the coin by the coin type name


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_symbol_by_type_name">symbol_by_type_name</a>(coin_type_name: &<a href="_String">string::String</a>): <a href="_String">string::String</a>
</code></pre>



<a name="0x3_coin_decimals"></a>

## Function `decimals`
Expand All @@ -431,6 +461,18 @@ Returns the decimals of the coin by the type <code>CoinType</code>



<a name="0x3_coin_decimals_by_type_name"></a>

## Function `decimals_by_type_name`

Returns the decimals of the coin by the coin type name


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_decimals_by_type_name">decimals_by_type_name</a>(coin_type_name: &<a href="_String">string::String</a>): u8
</code></pre>



<a name="0x3_coin_supply"></a>

## Function `supply`
Expand All @@ -455,6 +497,18 @@ Returns the amount of coin in existence by the type <code>CoinType</code>



<a name="0x3_coin_supply_by_type_name"></a>

## Function `supply_by_type_name`

Returns the amount of coin in existence by the coin type name


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_supply_by_type_name">supply_by_type_name</a>(coin_type_name: &<a href="_String">string::String</a>): <a href="">u256</a>
</code></pre>



<a name="0x3_coin_icon_url"></a>

## Function `icon_url`
Expand All @@ -479,6 +533,18 @@ Returns the icon url of coin by the type <code>CoinType</code>



<a name="0x3_coin_icon_url_by_type_name"></a>

## Function `icon_url_by_type_name`

Returns the icon url of the coin by the coin type name


<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_icon_url_by_type_name">icon_url_by_type_name</a>(coin_type_name: &<a href="_String">string::String</a>): <a href="_Option">option::Option</a>&lt;<a href="_String">string::String</a>&gt;
</code></pre>



<a name="0x3_coin_is_same_coin"></a>

## Function `is_same_coin`
Expand Down Expand Up @@ -577,6 +643,17 @@ Borrow the CoinInfo<CoinType>



<a name="0x3_coin_get_coin_info_by_type_name"></a>

## Function `get_coin_info_by_type_name`



<pre><code><b>public</b> <b>fun</b> <a href="coin.md#0x3_coin_get_coin_info_by_type_name">get_coin_info_by_type_name</a>(coin_type_name: &<a href="_String">string::String</a>): <a href="_Option">option::Option</a>&lt;<a href="_ObjectID">object::ObjectID</a>&gt;
</code></pre>



<a name="0x3_coin_upsert_icon_url"></a>

## Function `upsert_icon_url`
Expand Down
7 changes: 0 additions & 7 deletions frameworks/rooch-framework/sources/account_coin_store.move
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

module rooch_framework::account_coin_store {

use moveos_std::account;
use moveos_std::object::{Self, ObjectID, Object};
use moveos_std::table;
Expand Down Expand Up @@ -98,7 +97,6 @@ module rooch_framework::account_coin_store {
/// Withdraw specified `amount` of coin `CoinType` from the signing account.
/// This public entry function requires the `CoinType` to have `key` and `store` abilities.
public fun withdraw<CoinType: key + store>(

account: &signer,
amount: u256,
): Coin<CoinType> {
Expand All @@ -116,7 +114,6 @@ module rooch_framework::account_coin_store {
/// Transfer `amount` of coins `CoinType` from `from` to `to`.
/// Any account and module can call this function to transfer coins, the `CoinType` must have `key` and `store` abilities.
public fun transfer<CoinType: key + store>(

from: &signer,
to: address,
amount: u256,
Expand All @@ -143,7 +140,6 @@ module rooch_framework::account_coin_store {
/// Withdraw specified `amount` of coin `CoinType` from any addr, this function does not check the Coin `frozen` attribute
/// This function is only called by the `CoinType` module, for the developer to extend custom withdraw logic
public fun withdraw_extend<CoinType: key>(

addr: address,
amount: u256,
): Coin<CoinType> {
Expand All @@ -161,7 +157,6 @@ module rooch_framework::account_coin_store {
/// Transfer `amount` of coins `CoinType` from `from` to `to`.
/// This function is only called by the `CoinType` module, for the developer to extend custom transfer logic
public fun transfer_extend<CoinType: key>(

from: address,
to: address,
amount: u256,
Expand Down Expand Up @@ -220,7 +215,6 @@ module rooch_framework::account_coin_store {


fun withdraw_internal<CoinType: key>(

addr: address,
amount: u256,
): Coin<CoinType> {
Expand All @@ -238,7 +232,6 @@ module rooch_framework::account_coin_store {
}

fun transfer_internal<CoinType: key>(

from: address,
to: address,
amount: u256,
Expand Down
56 changes: 46 additions & 10 deletions frameworks/rooch-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,14 @@ module rooch_framework::coin {
/// Returns the name of the coin by the type `CoinType`
public fun name_by_type<CoinType: key>(): string::String {
let coin_type = type_info::type_name<CoinType>();
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
}

Expand All @@ -189,9 +194,14 @@ module rooch_framework::coin {
/// Returns the symbol of the coin by the type `CoinType`
public fun symbol_by_type<CoinType: key>(): string::String {
let coin_type = type_info::type_name<CoinType>();
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), 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.symbol
}

Expand All @@ -205,9 +215,14 @@ module rooch_framework::coin {
/// Returns the decimals of the coin by the type `CoinType`
public fun decimals_by_type<CoinType: key>(): u8 {
let coin_type = type_info::type_name<CoinType>();
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
}

Expand All @@ -219,9 +234,14 @@ module rooch_framework::coin {
/// Returns the amount of coin in existence by the type `CoinType`
public fun supply_by_type<CoinType: key>(): u256 {
let coin_type = type_info::type_name<CoinType>();
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
}

Expand All @@ -233,9 +253,14 @@ module rooch_framework::coin {
/// Returns the icon url of coin by the type `CoinType`
public fun icon_url_by_type<CoinType: key>(): Option<String> {
let coin_type = type_info::type_name<CoinType>();
icon_url_by_type_name(&coin_type)
}

/// Returns the icon url of the coin by the coin type name
public fun icon_url_by_type_name(coin_type_name: &String): Option<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.icon_url
}

Expand Down Expand Up @@ -292,6 +317,17 @@ module rooch_framework::coin {
object::borrow(coin_info_obj)
}

// Add this function to help with dynamic coin operations
public fun get_coin_info_by_type_name(coin_type_name: &String): Option<ObjectID> {
let registry = borrow_registry();
if (!object::contains_field(registry, *coin_type_name)) {
option::none()
} else {
let metadata: &CoinMetadata = object::borrow_field(registry, *coin_type_name);
option::some(metadata.coin_info_id)
}
}

//
// Extend functions
//
Expand Down

0 comments on commit 0184cb4

Please sign in to comment.