Skip to content

Commit

Permalink
[FA migration] no FA deposit event for FA migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmark committed Dec 5, 2024
1 parent 95e0d68 commit 8b5dbbd
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 19 deletions.
2 changes: 1 addition & 1 deletion aptos-move/framework/aptos-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,7 @@ or disallow upgradability of total supply.
<b>if</b> (<a href="coin.md#0x1_coin">coin</a>.value == 0) {
<a href="coin.md#0x1_coin_destroy_zero">destroy_zero</a>(<a href="coin.md#0x1_coin">coin</a>);
} <b>else</b> {
<a href="fungible_asset.md#0x1_fungible_asset_deposit">fungible_asset::deposit</a>(store, <a href="coin.md#0x1_coin_coin_to_fungible_asset">coin_to_fungible_asset</a>(<a href="coin.md#0x1_coin">coin</a>));
<a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events">fungible_asset::uncheck_deposit_without_events</a>(object_address(&store), <a href="coin.md#0x1_coin_coin_to_fungible_asset">coin_to_fungible_asset</a>(<a href="coin.md#0x1_coin">coin</a>));
};
// Note:
// It is possible the primary fungible store may already exist before this function call.
Expand Down
82 changes: 72 additions & 10 deletions aptos-move/framework/aptos-framework/doc/fungible_asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ metadata object can be any object that equipped with <code><a href="fungible_ass
- [Function `extract`](#0x1_fungible_asset_extract)
- [Function `merge`](#0x1_fungible_asset_merge)
- [Function `destroy_zero`](#0x1_fungible_asset_destroy_zero)
- [Function `uncheck_deposit_without_events`](#0x1_fungible_asset_uncheck_deposit_without_events)
- [Function `uncheck_deposit_without_events_inline`](#0x1_fungible_asset_uncheck_deposit_without_events_inline)
- [Function `deposit_internal`](#0x1_fungible_asset_deposit_internal)
- [Function `withdraw_internal`](#0x1_fungible_asset_withdraw_internal)
- [Function `increase_supply`](#0x1_fungible_asset_increase_supply)
Expand Down Expand Up @@ -3314,13 +3316,14 @@ Destroy an empty fungible asset.

</details>

<a id="0x1_fungible_asset_deposit_internal"></a>
<a id="0x1_fungible_asset_uncheck_deposit_without_events"></a>

## Function `deposit_internal`
## Function `uncheck_deposit_without_events`

Only called for coin->FA migration


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_deposit_internal">deposit_internal</a>(store_addr: <b>address</b>, fa: <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">fungible_asset::FungibleAsset</a>)
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events">uncheck_deposit_without_events</a>(store_addr: <b>address</b>, fa: <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">fungible_asset::FungibleAsset</a>)
</code></pre>


Expand All @@ -3329,21 +3332,80 @@ Destroy an empty fungible asset.
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_deposit_internal">deposit_internal</a>(store_addr: <b>address</b>, fa: <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> {
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events">uncheck_deposit_without_events</a>(
store_addr: <b>address</b>,
fa: <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> {
<a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events_inline">uncheck_deposit_without_events_inline</a>(store_addr, fa);
}
</code></pre>



</details>

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

## Function `uncheck_deposit_without_events_inline`



<pre><code><b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events_inline">uncheck_deposit_without_events_inline</a>(store_addr: <b>address</b>, fa: <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">fungible_asset::FungibleAsset</a>): u64
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code>inline <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events_inline">uncheck_deposit_without_events_inline</a>(
store_addr: <b>address</b>,
fa: <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">FungibleAsset</a>
): 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> <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">FungibleAsset</a> { metadata, amount } = fa;
<b>assert</b>!(<b>exists</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>&gt;(store_addr), <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="fungible_asset.md#0x1_fungible_asset_EFUNGIBLE_STORE_EXISTENCE">EFUNGIBLE_STORE_EXISTENCE</a>));
<b>let</b> store = <b>borrow_global_mut</b>&lt;<a href="fungible_asset.md#0x1_fungible_asset_FungibleStore">FungibleStore</a>&gt;(store_addr);
<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>if</b> (amount == 0) <b>return</b>;
<b>if</b> (amount != 0) {
<b>if</b> (store.balance == 0 && <a href="fungible_asset.md#0x1_fungible_asset_concurrent_fungible_balance_exists_inline">concurrent_fungible_balance_exists_inline</a>(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);
<a href="aggregator_v2.md#0x1_aggregator_v2_add">aggregator_v2::add</a>(&<b>mut</b> balance_resource.balance, amount);
} <b>else</b> {
store.balance = store.balance + amount;
};

<b>if</b> (store.balance == 0 && <a href="fungible_asset.md#0x1_fungible_asset_concurrent_fungible_balance_exists_inline">concurrent_fungible_balance_exists_inline</a>(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);
<a href="aggregator_v2.md#0x1_aggregator_v2_add">aggregator_v2::add</a>(&<b>mut</b> balance_resource.balance, amount);
} <b>else</b> {
store.balance = store.balance + amount;
<a href="event.md#0x1_event_emit">event::emit</a>(<a href="fungible_asset.md#0x1_fungible_asset_Deposit">Deposit</a> { store: store_addr, amount });
};
amount
}
</code></pre>



</details>

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

## Function `deposit_internal`



<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_deposit_internal">deposit_internal</a>(store_addr: <b>address</b>, fa: <a href="fungible_asset.md#0x1_fungible_asset_FungibleAsset">fungible_asset::FungibleAsset</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="fungible_asset.md#0x1_fungible_asset_deposit_internal">deposit_internal</a>(
store_addr: <b>address</b>,
fa: <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>let</b> amount = <a href="fungible_asset.md#0x1_fungible_asset_uncheck_deposit_without_events_inline">uncheck_deposit_without_events_inline</a>(store_addr, fa);
<a href="event.md#0x1_event_emit">event::emit</a>(<a href="fungible_asset.md#0x1_fungible_asset_Deposit">Deposit</a> { store: store_addr, amount });
}
</code></pre>
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/framework/aptos-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 27 additions & 7 deletions aptos-move/framework/aptos-framework/sources/fungible_asset.move
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
}

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 });
}

Expand Down

0 comments on commit 8b5dbbd

Please sign in to comment.