Skip to content

Commit

Permalink
support get coin info by coin type name
Browse files Browse the repository at this point in the history
  • Loading branch information
baichuan3 committed Mar 6, 2025
1 parent 0a3421c commit ae5e635
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 49 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
24 changes: 0 additions & 24 deletions frameworks/rooch-framework/sources/account_coin_store.move
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -124,18 +122,6 @@ module rooch_framework::account_coin_store {
transfer_internal<CoinType>(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<CoinType>(from_addr, to, amount);
}

public fun exist_account_coin_store<CoinType: key>(addr: address): bool {
let account_coin_store_id = account_coin_store_id<CoinType>(addr);
object::exists_object_with_type<CoinStore<CoinType>>(account_coin_store_id)
Expand Down Expand Up @@ -253,14 +239,4 @@ module rooch_framework::account_coin_store {
let coin = withdraw_internal<CoinType>(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<CoinType>(from, amount);
deposit_internal(to, coin);
}
}
51 changes: 35 additions & 16 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 @@ -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`).
Expand All @@ -202,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 @@ -216,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 @@ -230,26 +253,22 @@ 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>();
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<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
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`
public fun is_same_coin<CoinType1, CoinType2>(): bool {
return type_info::type_of<CoinType1>() == type_info::type_of<CoinType2>()
}


/// 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<CoinType: key>(zero_coin: Coin<CoinType>) {
Expand Down
9 changes: 0 additions & 9 deletions frameworks/rooch-framework/sources/transfer.move
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit ae5e635

Please sign in to comment.