From 8b5dbbd40c12c8c6fa2727bbd5ca893cba3f4b12 Mon Sep 17 00:00:00 2001 From: Aaron Gao Date: Thu, 5 Dec 2024 17:40:15 -0600 Subject: [PATCH] [FA migration] no FA deposit event for FA migration --- .../framework/aptos-framework/doc/coin.md | 2 +- .../aptos-framework/doc/fungible_asset.md | 82 ++++++++++++++++--- .../aptos-framework/sources/coin.move | 2 +- .../sources/fungible_asset.move | 34 ++++++-- 4 files changed, 101 insertions(+), 19 deletions(-) diff --git a/aptos-move/framework/aptos-framework/doc/coin.md b/aptos-move/framework/aptos-framework/doc/coin.md index 98f56f007e8b3d..436f7377e6dc73 100644 --- a/aptos-move/framework/aptos-framework/doc/coin.md +++ b/aptos-move/framework/aptos-framework/doc/coin.md @@ -2059,7 +2059,7 @@ or disallow upgradability of total supply. if (coin.value == 0) { destroy_zero(coin); } else { - fungible_asset::deposit(store, coin_to_fungible_asset(coin)); + fungible_asset::uncheck_deposit_without_events(object_address(&store), coin_to_fungible_asset(coin)); }; // Note: // It is possible the primary fungible store may already exist before this function call. diff --git a/aptos-move/framework/aptos-framework/doc/fungible_asset.md b/aptos-move/framework/aptos-framework/doc/fungible_asset.md index 17ee3056667dad..b4f759002c34f0 100644 --- a/aptos-move/framework/aptos-framework/doc/fungible_asset.md +++ b/aptos-move/framework/aptos-framework/doc/fungible_asset.md @@ -95,6 +95,8 @@ metadata object can be any object that equipped with + -## Function `deposit_internal` +## Function `uncheck_deposit_without_events` +Only called for coin->FA migration -
public(friend) fun deposit_internal(store_addr: address, fa: fungible_asset::FungibleAsset)
+
public(friend) fun uncheck_deposit_without_events(store_addr: address, fa: fungible_asset::FungibleAsset)
 
@@ -3329,21 +3332,80 @@ Destroy an empty fungible asset. Implementation -
public(friend) fun deposit_internal(store_addr: address, fa: FungibleAsset) acquires FungibleStore, ConcurrentFungibleBalance {
+
public(friend) fun uncheck_deposit_without_events(
+    store_addr: address,
+    fa: FungibleAsset
+) acquires FungibleStore, ConcurrentFungibleBalance {
+    uncheck_deposit_without_events_inline(store_addr, fa);
+}
+
+ + + + + + + +## Function `uncheck_deposit_without_events_inline` + + + +
fun uncheck_deposit_without_events_inline(store_addr: address, fa: fungible_asset::FungibleAsset): u64
+
+ + + +
+Implementation + + +
inline fun uncheck_deposit_without_events_inline(
+    store_addr: address,
+    fa: FungibleAsset
+): u64 acquires FungibleStore, ConcurrentFungibleBalance {
     let FungibleAsset { metadata, amount } = fa;
     assert!(exists<FungibleStore>(store_addr), error::not_found(EFUNGIBLE_STORE_EXISTENCE));
     let store = borrow_global_mut<FungibleStore>(store_addr);
     assert!(metadata == store.metadata, error::invalid_argument(EFUNGIBLE_ASSET_AND_STORE_MISMATCH));
 
-    if (amount == 0) return;
+    if (amount != 0) {
+        if (store.balance == 0 && concurrent_fungible_balance_exists_inline(store_addr)) {
+            let balance_resource = borrow_global_mut<ConcurrentFungibleBalance>(store_addr);
+            aggregator_v2::add(&mut balance_resource.balance, amount);
+        } else {
+            store.balance = store.balance + amount;
+        };
 
-    if (store.balance == 0 && concurrent_fungible_balance_exists_inline(store_addr)) {
-        let balance_resource = borrow_global_mut<ConcurrentFungibleBalance>(store_addr);
-        aggregator_v2::add(&mut balance_resource.balance, amount);
-    } else {
-        store.balance = store.balance + amount;
+        event::emit(Deposit { store: store_addr, amount });
     };
+    amount
+}
+
+ + + +
+ + + +## Function `deposit_internal` + + +
public(friend) fun deposit_internal(store_addr: address, fa: fungible_asset::FungibleAsset)
+
+ + + +
+Implementation + + +
public(friend) fun deposit_internal(
+    store_addr: address,
+    fa: FungibleAsset
+) acquires FungibleStore, ConcurrentFungibleBalance {
+    let amount = uncheck_deposit_without_events_inline(store_addr, fa);
     event::emit(Deposit { store: store_addr, amount });
 }
 
diff --git a/aptos-move/framework/aptos-framework/sources/coin.move b/aptos-move/framework/aptos-framework/sources/coin.move index 2ea6aca5762ec0..21c89403886fd7 100644 --- a/aptos-move/framework/aptos-framework/sources/coin.move +++ b/aptos-move/framework/aptos-framework/sources/coin.move @@ -586,7 +586,7 @@ module aptos_framework::coin { if (coin.value == 0) { destroy_zero(coin); } else { - fungible_asset::deposit(store, coin_to_fungible_asset(coin)); + fungible_asset::uncheck_deposit_without_events(object_address(&store), coin_to_fungible_asset(coin)); }; // Note: // It is possible the primary fungible store may already exist before this function call. diff --git a/aptos-move/framework/aptos-framework/sources/fungible_asset.move b/aptos-move/framework/aptos-framework/sources/fungible_asset.move index 50a77348c630c8..fda9aed2861019 100644 --- a/aptos-move/framework/aptos-framework/sources/fungible_asset.move +++ b/aptos-move/framework/aptos-framework/sources/fungible_asset.move @@ -1019,21 +1019,41 @@ module aptos_framework::fungible_asset { assert!(amount == 0, error::invalid_argument(EAMOUNT_IS_NOT_ZERO)); } - public(friend) fun deposit_internal(store_addr: address, fa: FungibleAsset) acquires FungibleStore, ConcurrentFungibleBalance { + /// Only called for coin->FA migration + public(friend) fun uncheck_deposit_without_events( + store_addr: address, + fa: FungibleAsset + ) acquires FungibleStore, ConcurrentFungibleBalance { + uncheck_deposit_without_events_inline(store_addr, fa); + } + + inline fun uncheck_deposit_without_events_inline( + store_addr: address, + fa: FungibleAsset + ): u64 acquires FungibleStore, ConcurrentFungibleBalance { let FungibleAsset { metadata, amount } = fa; assert!(exists(store_addr), error::not_found(EFUNGIBLE_STORE_EXISTENCE)); let store = borrow_global_mut(store_addr); assert!(metadata == store.metadata, error::invalid_argument(EFUNGIBLE_ASSET_AND_STORE_MISMATCH)); - if (amount == 0) return; + if (amount != 0) { + if (store.balance == 0 && concurrent_fungible_balance_exists_inline(store_addr)) { + let balance_resource = borrow_global_mut(store_addr); + aggregator_v2::add(&mut balance_resource.balance, amount); + } else { + store.balance = store.balance + amount; + }; - if (store.balance == 0 && concurrent_fungible_balance_exists_inline(store_addr)) { - let balance_resource = borrow_global_mut(store_addr); - aggregator_v2::add(&mut balance_resource.balance, amount); - } else { - store.balance = store.balance + amount; + event::emit(Deposit { store: store_addr, amount }); }; + amount + } + public(friend) fun deposit_internal( + store_addr: address, + fa: FungibleAsset + ) acquires FungibleStore, ConcurrentFungibleBalance { + let amount = uncheck_deposit_without_events_inline(store_addr, fa); event::emit(Deposit { store: store_addr, amount }); }