Skip to content

Commit

Permalink
[framework] Rename register to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Dec 31, 2024
1 parent f285c5f commit 36d1a8e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
22 changes: 11 additions & 11 deletions frameworks/rooch-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ This module provides the foundation for typesafe Coins.
- [Struct `Coin`](#0x3_coin_Coin)
- [Resource `CoinInfo`](#0x3_coin_CoinInfo)
- [Resource `CoinMetadata`](#0x3_coin_CoinMetadata)
- [Resource `CoinRegister`](#0x3_coin_CoinRegister)
- [Resource `CoinRegistry`](#0x3_coin_CoinRegistry)
- [Struct `MintEvent`](#0x3_coin_MintEvent)
- [Struct `BurnEvent`](#0x3_coin_BurnEvent)
- [Constants](#@Constants_0)
- [Function `genesis_init`](#0x3_coin_genesis_init)
- [Function `init_coin_register`](#0x3_coin_init_coin_register)
- [Function `init_coin_registry`](#0x3_coin_init_coin_registry)
- [Function `coin_address`](#0x3_coin_coin_address)
- [Function `check_coin_info_registered`](#0x3_coin_check_coin_info_registered)
- [Function `is_registered`](#0x3_coin_is_registered)
Expand Down Expand Up @@ -90,22 +90,22 @@ CoinInfo<CoinType> is a named Object, the <code>coin_type</code> is the unique k

## Resource `CoinMetadata`

Coin metadata is copied from CoinInfo, and stored as dynamic field of CoinRegister
Coin metadata is copied from CoinInfo, and stored as dynamic field of CoinRegistry


<pre><code><b>struct</b> <a href="coin.md#0x3_coin_CoinMetadata">CoinMetadata</a> <b>has</b> store, key
</code></pre>



<a name="0x3_coin_CoinRegister"></a>
<a name="0x3_coin_CoinRegistry"></a>

## Resource `CoinRegister`
## Resource `CoinRegistry`

The register of all coin types.
The registry of all coin types.


<pre><code><b>struct</b> <a href="coin.md#0x3_coin_CoinRegister">CoinRegister</a> <b>has</b> key
<pre><code><b>struct</b> <a href="coin.md#0x3_coin_CoinRegistry">CoinRegistry</a> <b>has</b> key
</code></pre>


Expand Down Expand Up @@ -297,14 +297,14 @@ Coin amount cannot be zero



<a name="0x3_coin_init_coin_register"></a>
<a name="0x3_coin_init_coin_registry"></a>

## Function `init_coin_register`
## Function `init_coin_registry`

Initialize the CoinRegister, this function is for framework upgrade.
Initialize the CoinRegistry, this function is for framework upgrade.


<pre><code>entry <b>fun</b> <a href="coin.md#0x3_coin_init_coin_register">init_coin_register</a>()
<pre><code>entry <b>fun</b> <a href="coin.md#0x3_coin_init_coin_registry">init_coin_registry</a>()
</code></pre>


Expand Down
62 changes: 36 additions & 26 deletions frameworks/rooch-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module rooch_framework::coin {
supply: u256,
}

/// Coin metadata is copied from CoinInfo, and stored as dynamic field of CoinRegister
/// Coin metadata is copied from CoinInfo, and stored as dynamic field of CoinRegistry
struct CoinMetadata has key, store {
coin_info_id: ObjectID,
coin_type: string::String,
Expand All @@ -109,8 +109,8 @@ module rooch_framework::coin {
supply: u256,
}

/// The register of all coin types.
struct CoinRegister has key {
/// The registry of all coin types.
struct CoinRegistry has key {
}

/// Event emitted when coin minted.
Expand All @@ -130,15 +130,15 @@ module rooch_framework::coin {
}

public(friend) fun genesis_init(__genesis_account: &signer) {
init_coin_register();
init_coin_registry();
}

/// Initialize the CoinRegister, this function is for framework upgrade.
entry fun init_coin_register() {
let coin_register_id = object::named_object_id<CoinRegister>();
assert!(!object::exists_object_with_type<CoinRegister>(coin_register_id), ErrorCoinRegisterAlreadyInitialized);
let coin_register = object::new_named_object(CoinRegister {});
object::transfer_extend(coin_register, @rooch_framework);
/// Initialize the CoinRegistry, this function is for framework upgrade.
entry fun init_coin_registry() {
let coin_registry_id = object::named_object_id<CoinRegistry>();
assert!(!object::exists_object_with_type<CoinRegistry>(coin_registry_id), ErrorCoinRegisterAlreadyInitialized);
let coin_registry = object::new_named_object(CoinRegistry {});
object::transfer_extend(coin_registry, @rooch_framework);
}

//
Expand Down Expand Up @@ -175,7 +175,9 @@ 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>();
let coin_metadata: &CoinMetadata = object::borrow_field(borrow_register(), coin_type);
let registry = borrow_registry();
assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered);
let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type);
coin_metadata.name
}

Expand All @@ -187,7 +189,9 @@ 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>();
let coin_metadata: &CoinMetadata = object::borrow_field(borrow_register(), coin_type);
let registry = borrow_registry();
assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered);
let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type);
coin_metadata.symbol
}

Expand All @@ -201,7 +205,9 @@ 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>();
let coin_metadata: &CoinMetadata = object::borrow_field(borrow_register(), coin_type);
let registry = borrow_registry();
assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered);
let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type);
coin_metadata.decimals
}

Expand All @@ -213,7 +219,9 @@ 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>();
let coin_metadata: &CoinMetadata = object::borrow_field(borrow_register(), coin_type);
let registry = borrow_registry();
assert!(object::contains_field(registry, coin_type), ErrorCoinInfoNotRegistered);
let coin_metadata: &CoinMetadata = object::borrow_field(registry, coin_type);
coin_metadata.supply
}

Expand All @@ -225,7 +233,9 @@ 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 coin_metadata: &CoinMetadata = object::borrow_field(borrow_register(), coin_type);
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
}

Expand Down Expand Up @@ -332,8 +342,8 @@ module rooch_framework::coin {
decimals,
supply: 0u256,
};
let coin_register = borrow_mut_register();
object::add_field(coin_register, coin_type, coin_metadata);
let coin_registry = borrow_mut_registry();
object::add_field(coin_registry, coin_type, coin_metadata);

coin_info_obj
}
Expand Down Expand Up @@ -417,20 +427,20 @@ module rooch_framework::coin {
coin_metadata.icon_url = option::some(icon_url);
}

fun borrow_register(): &Object<CoinRegister> {
object::borrow_object<CoinRegister>(object::named_object_id<CoinRegister>())
fun borrow_registry(): &Object<CoinRegistry> {
object::borrow_object<CoinRegistry>(object::named_object_id<CoinRegistry>())
}

fun borrow_mut_register(): &mut Object<CoinRegister> {
object::borrow_mut_object_extend<CoinRegister>(object::named_object_id<CoinRegister>())
fun borrow_mut_registry(): &mut Object<CoinRegistry> {
object::borrow_mut_object_extend<CoinRegistry>(object::named_object_id<CoinRegistry>())
}

fun borrow_mut_coin_metadata<CoinType: key>(coin_info: &Object<CoinInfo<CoinType>>) : &mut CoinMetadata {
let coin_type = type_info::type_name<CoinType>();
let register = borrow_mut_register();
// If the coin metadata is not initialized, it is the Coin register before v19
let registry = borrow_mut_registry();
// If the coin metadata is not initialized, it is the Coin registered before v19
// We need to initialize the coin metadata here
if (!object::contains_field(register, coin_type)){
if (!object::contains_field(registry, coin_type)){
let coin_metadata = CoinMetadata {
coin_info_id: object::id(coin_info),
coin_type,
Expand All @@ -440,9 +450,9 @@ module rooch_framework::coin {
decimals: 0,
supply: 0,
};
object::add_field(register, coin_type, coin_metadata);
object::add_field(registry, coin_type, coin_metadata);
};
object::borrow_mut_field(register, coin_type)
object::borrow_mut_field(registry, coin_type)
}

// Unpack the Coin and return the value
Expand Down
6 changes: 6 additions & 0 deletions frameworks/rooch-framework/sources/tests/coin_test.move
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ module rooch_framework::coin_test{
assert!(symbol<FakeCoin>(coin_info) == symbol, 2);
assert!(decimals<FakeCoin>(coin_info) == decimals, 3);
};
//coin info by type
assert!(coin::supply_by_type<FakeCoin>() == 0, 4);
assert!(coin::name_by_type<FakeCoin>() == name, 5);
assert!(coin::symbol_by_type<FakeCoin>() == symbol, 6);
assert!(coin::decimals_by_type<FakeCoin>() == decimals, 7);
assert!(coin::icon_url_by_type<FakeCoin>() == option::none(), 8);

let coins_minted = mint_extend<FakeCoin>(&mut coin_info_obj, 100);

Expand Down

0 comments on commit 36d1a8e

Please sign in to comment.