Skip to content

Commit

Permalink
comment reviews
Browse files Browse the repository at this point in the history
gatting auto-upgrade
  • Loading branch information
igor-aptos committed Mar 8, 2024
1 parent 223cda9 commit e83a723
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 27 deletions.
47 changes: 36 additions & 11 deletions aptos-move/framework/aptos-framework/doc/fungible_asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ metadata object can be any object that equipped with <code><a href="fungible_ass
- [Constants](#@Constants_0)
- [Function `default_to_concurrent_fungible_supply`](#0x1_fungible_asset_default_to_concurrent_fungible_supply)
- [Function `default_to_concurrent_fungible_balance`](#0x1_fungible_asset_default_to_concurrent_fungible_balance)
- [Function `auto_upgrade_to_concurrent_fungible_balance`](#0x1_fungible_asset_auto_upgrade_to_concurrent_fungible_balance)
- [Function `add_fungibility`](#0x1_fungible_asset_add_fungibility)
- [Function `generate_mint_ref`](#0x1_fungible_asset_generate_mint_ref)
- [Function `generate_burn_ref`](#0x1_fungible_asset_generate_burn_ref)
Expand Down Expand Up @@ -254,7 +255,7 @@ The store object that holds fungible assets of a specific type associated with a

## Resource `ConcurrentFungibleBalance`

The store object that holds concurrent fungible asset balance
The store object that holds concurrent fungible asset balance.


<pre><code>#[resource_group_member(#[group = <a href="object.md#0x1_object_ObjectGroup">0x1::object::ObjectGroup</a>])]
Expand Down Expand Up @@ -946,6 +947,30 @@ URI for the icon of the fungible asset metadata is too long



</details>

<a id="0x1_fungible_asset_auto_upgrade_to_concurrent_fungible_balance"></a>

## Function `auto_upgrade_to_concurrent_fungible_balance`



<pre><code><b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_auto_upgrade_to_concurrent_fungible_balance">auto_upgrade_to_concurrent_fungible_balance</a>(): bool
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_auto_upgrade_to_concurrent_fungible_balance">auto_upgrade_to_concurrent_fungible_balance</a>(): bool {
<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_concurrent_fungible_assets_enabled">features::concurrent_fungible_assets_enabled</a>()
}
</code></pre>



</details>

<a id="0x1_fungible_asset_add_fungibility"></a>
Expand Down Expand Up @@ -1286,7 +1311,8 @@ Return whether the provided address has a store initialized.

## Function `concurrent_fungible_balance_exists`

Return whether the provided address has a store initialized.
Return whether the provided address has a concurrent fungible balance initialized,
at the fungible store address.


<pre><code>#[view]
Expand Down Expand Up @@ -1404,8 +1430,8 @@ Get the balance of a given store.
<pre><code><b>public</b> <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_balance">balance</a>&lt;T: key&gt;(store: Object&lt;T&gt;): u64 <b>acquires</b> <a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>, <a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a> {
<b>let</b> store_addr = <a href="object.md#0x1_object_object_address">object::object_address</a>(&store);
<b>if</b> (<a href="fungible_asset.md#0x1_fungible_asset_concurrent_fungible_balance_exists">concurrent_fungible_balance_exists</a>(store_addr)) {
<b>let</b> balance = <b>borrow_global</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a>&gt;(store_addr);
<a href="aggregator_v2.md#0x1_aggregator_v2_read">aggregator_v2::read</a>(&balance.balance)
<b>let</b> balance_resource = <b>borrow_global</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a>&gt;(store_addr);
<a href="aggregator_v2.md#0x1_aggregator_v2_read">aggregator_v2::read</a>(&balance_resource.balance)
} <b>else</b> <b>if</b> (<a href="fungible_asset.md#0x1_fungible_asset_store_exists">store_exists</a>(store_addr)) {
<a href="fungible_asset.md#0x1_fungible_asset_borrow_store_resource">borrow_store_resource</a>(&store).balance
} <b>else</b> {
Expand Down Expand Up @@ -1657,7 +1683,6 @@ Used to delete a store. Requires the store to be completely empty prior to remo
<a href="event.md#0x1_event_destroy_handle">event::destroy_handle</a>(deposit_events);
<a href="event.md#0x1_event_destroy_handle">event::destroy_handle</a>(withdraw_events);
<a href="event.md#0x1_event_destroy_handle">event::destroy_handle</a>(frozen_events);

};
}
</code></pre>
Expand Down Expand Up @@ -2112,10 +2137,10 @@ Destroy an empty fungible asset.
<b>assert</b>!(metadata == store_metadata, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="fungible_asset.md#0x1_fungible_asset_EFUNGIBLE_ASSET_AND_STORE_MISMATCH">EFUNGIBLE_ASSET_AND_STORE_MISMATCH</a>));
<b>let</b> store_addr = <a href="object.md#0x1_object_object_address">object::object_address</a>(&store);

<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_concurrent_fungible_assets_enabled">features::concurrent_fungible_assets_enabled</a>()) {
<b>if</b> (<a href="fungible_asset.md#0x1_fungible_asset_auto_upgrade_to_concurrent_fungible_balance">auto_upgrade_to_concurrent_fungible_balance</a>() || <a href="fungible_asset.md#0x1_fungible_asset_concurrent_fungible_balance_exists">concurrent_fungible_balance_exists</a>(store_addr)) {
<a href="fungible_asset.md#0x1_fungible_asset_ensure_store_upgraded_to_concurrent_internal">ensure_store_upgraded_to_concurrent_internal</a>(store_addr);
<b>let</b> balance = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a>&gt;(store_addr);
<a href="aggregator_v2.md#0x1_aggregator_v2_add">aggregator_v2::add</a>(&<b>mut</b> balance.balance, amount);
<b>let</b> balance_resource = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a>&gt;(store_addr);
<a href="aggregator_v2.md#0x1_aggregator_v2_add">aggregator_v2::add</a>(&<b>mut</b> balance_resource.balance, amount);
} <b>else</b> {
<b>let</b> store = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>&gt;(store_addr);
store.balance = store.balance + amount;
Expand Down Expand Up @@ -2151,11 +2176,11 @@ Extract <code>amount</code> of the fungible asset from <code>store</code>.
): <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">FungibleAsset</a> <b>acquires</b> <a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>, <a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a> {
<b>assert</b>!(amount != 0, <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="fungible_asset.md#0x1_fungible_asset_EAMOUNT_CANNOT_BE_ZERO">EAMOUNT_CANNOT_BE_ZERO</a>));

<b>if</b> (<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_concurrent_fungible_assets_enabled">features::concurrent_fungible_assets_enabled</a>()) {
<b>if</b> (<a href="fungible_asset.md#0x1_fungible_asset_auto_upgrade_to_concurrent_fungible_balance">auto_upgrade_to_concurrent_fungible_balance</a>() || <a href="fungible_asset.md#0x1_fungible_asset_concurrent_fungible_balance_exists">concurrent_fungible_balance_exists</a>(store_addr)) {
<a href="fungible_asset.md#0x1_fungible_asset_ensure_store_upgraded_to_concurrent_internal">ensure_store_upgraded_to_concurrent_internal</a>(store_addr);
<b>let</b> balance = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a>&gt;(store_addr);
<b>let</b> balance_resource = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_ConcurrentFungibleBalance">ConcurrentFungibleBalance</a>&gt;(store_addr);
<b>assert</b>!(
<a href="aggregator_v2.md#0x1_aggregator_v2_try_sub">aggregator_v2::try_sub</a>(&<b>mut</b> balance.balance, amount),
<a href="aggregator_v2.md#0x1_aggregator_v2_try_sub">aggregator_v2::try_sub</a>(&<b>mut</b> balance_resource.balance, amount),
<a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_invalid_argument">error::invalid_argument</a>(<a href="fungible_asset.md#0x1_fungible_asset_EINSUFFICIENT_BALANCE">EINSUFFICIENT_BALANCE</a>)
);
} <b>else</b> {
Expand Down
48 changes: 32 additions & 16 deletions aptos-move/framework/aptos-framework/sources/fungible_asset.move
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ module aptos_framework::fungible_asset {
}

#[resource_group_member(group = aptos_framework::object::ObjectGroup)]
/// The store object that holds concurrent fungible asset balance
/// The store object that holds concurrent fungible asset balance.
struct ConcurrentFungibleBalance has key {
/// The balance of the fungible metadata.
balance: Aggregator<u64>,
Expand Down Expand Up @@ -171,6 +171,10 @@ module aptos_framework::fungible_asset {
features::concurrent_fungible_assets_enabled()
}

fun auto_upgrade_to_concurrent_fungible_balance(): bool {
features::concurrent_fungible_assets_enabled()
}

/// Make an existing object fungible by adding the Metadata resource.
/// This returns the capabilities to mint, burn, and transfer.
/// maximum_supply defines the behavior of maximum supply when monitoring:
Expand Down Expand Up @@ -306,7 +310,8 @@ module aptos_framework::fungible_asset {
}

#[view]
/// Return whether the provided address has a store initialized.
/// Return whether the provided address has a concurrent fungible balance initialized,
/// at the fungible store address.
public fun concurrent_fungible_balance_exists(store: address): bool {
exists<ConcurrentFungibleBalance>(store)
}
Expand All @@ -332,8 +337,8 @@ module aptos_framework::fungible_asset {
public fun balance<T: key>(store: Object<T>): u64 acquires FungibleStore, ConcurrentFungibleBalance {
let store_addr = object::object_address(&store);
if (concurrent_fungible_balance_exists(store_addr)) {
let balance = borrow_global<ConcurrentFungibleBalance>(store_addr);
aggregator_v2::read(&balance.balance)
let balance_resource = borrow_global<ConcurrentFungibleBalance>(store_addr);
aggregator_v2::read(&balance_resource.balance)
} else if (store_exists(store_addr)) {
borrow_store_resource(&store).balance
} else {
Expand Down Expand Up @@ -425,7 +430,6 @@ module aptos_framework::fungible_asset {
event::destroy_handle(deposit_events);
event::destroy_handle(withdraw_events);
event::destroy_handle(frozen_events);

};
}

Expand Down Expand Up @@ -580,10 +584,10 @@ module aptos_framework::fungible_asset {
assert!(metadata == store_metadata, error::invalid_argument(EFUNGIBLE_ASSET_AND_STORE_MISMATCH));
let store_addr = object::object_address(&store);

if (features::concurrent_fungible_assets_enabled()) {
if (auto_upgrade_to_concurrent_fungible_balance() || concurrent_fungible_balance_exists(store_addr)) {
ensure_store_upgraded_to_concurrent_internal(store_addr);
let balance = borrow_global_mut<ConcurrentFungibleBalance>(store_addr);
aggregator_v2::add(&mut balance.balance, amount);
let balance_resource = borrow_global_mut<ConcurrentFungibleBalance>(store_addr);
aggregator_v2::add(&mut balance_resource.balance, amount);
} else {
let store = borrow_global_mut<FungibleStore>(store_addr);
store.balance = store.balance + amount;
Expand All @@ -599,11 +603,11 @@ module aptos_framework::fungible_asset {
): FungibleAsset acquires FungibleStore, ConcurrentFungibleBalance {
assert!(amount != 0, error::invalid_argument(EAMOUNT_CANNOT_BE_ZERO));

if (features::concurrent_fungible_assets_enabled()) {
if (auto_upgrade_to_concurrent_fungible_balance() || concurrent_fungible_balance_exists(store_addr)) {
ensure_store_upgraded_to_concurrent_internal(store_addr);
let balance = borrow_global_mut<ConcurrentFungibleBalance>(store_addr);
let balance_resource = borrow_global_mut<ConcurrentFungibleBalance>(store_addr);
assert!(
aggregator_v2::try_sub(&mut balance.balance, amount),
aggregator_v2::try_sub(&mut balance_resource.balance, amount),
error::invalid_argument(EINSUFFICIENT_BALANCE)
);
} else {
Expand Down Expand Up @@ -926,28 +930,40 @@ module aptos_framework::fungible_asset {
#[test(fx = @aptos_framework, creator = @0xcafe)]
fun test_fungible_asset_upgrade(fx: &signer, creator: &signer) acquires Supply, ConcurrentSupply, FungibleStore, ConcurrentFungibleBalance {
let feature = features::get_concurrent_fungible_assets_feature();
let agg_feature = features::get_aggregator_v2_api_feature();
features::change_feature_flags(fx, vector[], vector[feature, agg_feature]);
features::change_feature_flags(fx, vector[], vector[feature]);

let (creator_ref, token_object) = create_test_token(creator);
let (mint_ref, transfer_ref, _burn) = init_test_metadata(&creator_ref);
let test_token = object::convert<TestToken, Metadata>(token_object);
assert!(exists<Supply>(object::object_address(&test_token)), 1);
assert!(!exists<ConcurrentSupply>(object::object_address(&test_token)), 2);
let creator_store = create_test_store(creator, test_token);
assert!(exists<FungibleStore>(object::object_address(&creator_store)), 3);
assert!(!exists<ConcurrentFungibleBalance>(object::object_address(&creator_store)), 4);

let fa = mint(&mint_ref, 30);
assert!(supply(test_token) == option::some(30), 2);
assert!(supply(test_token) == option::some(30), 5);

deposit_with_ref(&transfer_ref, creator_store, fa);

features::change_feature_flags(fx, vector[feature, agg_feature], vector[]);
features::change_feature_flags(fx, vector[feature], vector[]);

let extend_ref = object::generate_extend_ref(&creator_ref);
// manual conversion of supply
upgrade_to_concurrent(&extend_ref);
assert!(!exists<Supply>(object::object_address(&test_token)), 6);
assert!(exists<ConcurrentSupply>(object::object_address(&test_token)), 7);

let fb = mint(&mint_ref, 20);
assert!(supply(test_token) == option::some(50), 3);
assert!(supply(test_token) == option::some(50), 8);

// automatic conversion of balance
deposit_with_ref(&transfer_ref, creator_store, fb);
// both store and new balance need to exist. Old balance should be 0.
assert!(exists<FungibleStore>(object::object_address(&creator_store)), 9);
assert!(borrow_store_resource(&creator_store).balance == 0, 10);
assert!(exists<ConcurrentFungibleBalance>(object::object_address(&creator_store)), 11);
assert!(aggregator_v2::read(&borrow_global<ConcurrentFungibleBalance>(object::object_address(&creator_store)).balance) == 50, 12);
}

#[deprecated]
Expand Down

0 comments on commit e83a723

Please sign in to comment.