Skip to content

Commit

Permalink
[fa] add back migration_flag for exchanges (#15738)
Browse files Browse the repository at this point in the history
  • Loading branch information
lightmark authored Jan 16, 2025
1 parent e36868a commit e504d11
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
19 changes: 10 additions & 9 deletions aptos-move/framework/aptos-framework/doc/coin.md
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,6 @@ The flag the existence of which indicates the primary fungible store is created


<pre><code>#[resource_group_member(#[group = <a href="object.md#0x1_object_ObjectGroup">0x1::object::ObjectGroup</a>])]
#[deprecated]
<b>struct</b> <a href="coin.md#0x1_coin_MigrationFlag">MigrationFlag</a> <b>has</b> key
</code></pre>

Expand Down Expand Up @@ -2092,6 +2091,7 @@ or disallow upgradability of total supply.

<b>let</b> metadata = <a href="coin.md#0x1_coin_ensure_paired_metadata">ensure_paired_metadata</a>&lt;CoinType&gt;();
<b>let</b> store = <a href="primary_fungible_store.md#0x1_primary_fungible_store_ensure_primary_store_exists">primary_fungible_store::ensure_primary_store_exists</a>(<a href="account.md#0x1_account">account</a>, metadata);
<b>let</b> store_address = <a href="object.md#0x1_object_object_address">object::object_address</a>(&store);
<b>if</b> (<b>exists</b>&lt;<a href="coin.md#0x1_coin_CoinStore">CoinStore</a>&lt;CoinType&gt;&gt;(<a href="account.md#0x1_account">account</a>)) {
<b>let</b> <a href="coin.md#0x1_coin_CoinStore">CoinStore</a>&lt;CoinType&gt; { <a href="coin.md#0x1_coin">coin</a>, frozen, deposit_events, withdraw_events } = <b>move_from</b>&lt;<a href="coin.md#0x1_coin_CoinStore">CoinStore</a>&lt;CoinType&gt;&gt;(
<a href="account.md#0x1_account">account</a>
Expand Down Expand Up @@ -2122,6 +2122,9 @@ or disallow upgradability of total supply.
<a href="fungible_asset.md#0x1_fungible_asset_set_frozen_flag_internal">fungible_asset::set_frozen_flag_internal</a>(store, frozen);
}
};
<b>if</b> (!<b>exists</b>&lt;<a href="coin.md#0x1_coin_MigrationFlag">MigrationFlag</a>&gt;(store_address)) {
<b>move_to</b>(&<a href="create_signer.md#0x1_create_signer_create_signer">create_signer::create_signer</a>(store_address), <a href="coin.md#0x1_coin_MigrationFlag">MigrationFlag</a> {});
}
}
</code></pre>

Expand Down Expand Up @@ -2707,13 +2710,11 @@ Deposit the coin balance into the recipient's account and emit an event.
account_address: <b>address</b>,
metadata: Object&lt;Metadata&gt;
): bool {
(<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_new_accounts_default_to_fa_apt_store_enabled">features::new_accounts_default_to_fa_apt_store_enabled</a>() && <a href="object.md#0x1_object_object_address">object::object_address</a>(&metadata) == @0xa) || {
<b>let</b> primary_store_address = <a href="primary_fungible_store.md#0x1_primary_fungible_store_primary_store_address">primary_fungible_store::primary_store_address</a>&lt;Metadata&gt;(
account_address,
metadata
);
<a href="fungible_asset.md#0x1_fungible_asset_store_exists">fungible_asset::store_exists</a>(primary_store_address)
}
<b>let</b> primary_store_address = <a href="primary_fungible_store.md#0x1_primary_fungible_store_primary_store_address">primary_fungible_store::primary_store_address</a>&lt;Metadata&gt;(account_address, metadata);
<a href="fungible_asset.md#0x1_fungible_asset_store_exists">fungible_asset::store_exists</a>(primary_store_address) && (
// migration flag is needed, until we start defaulting new accounts <b>to</b> APT PFS
<a href="../../aptos-stdlib/../move-stdlib/doc/features.md#0x1_features_new_accounts_default_to_fa_apt_store_enabled">features::new_accounts_default_to_fa_apt_store_enabled</a>() || <b>exists</b>&lt;<a href="coin.md#0x1_coin_MigrationFlag">MigrationFlag</a>&gt;(primary_store_address)
)
}
</code></pre>

Expand Down Expand Up @@ -2753,7 +2754,7 @@ This is for internal use only and doesn't emit an DepositEvent.
)) {
<b>let</b> fa = <a href="coin.md#0x1_coin_coin_to_fungible_asset">coin_to_fungible_asset</a>(<a href="coin.md#0x1_coin">coin</a>);
<b>let</b> metadata = <a href="fungible_asset.md#0x1_fungible_asset_asset_metadata">fungible_asset::asset_metadata</a>(&fa);
<b>let</b> store = <a href="primary_fungible_store.md#0x1_primary_fungible_store_ensure_primary_store_exists">primary_fungible_store::ensure_primary_store_exists</a>(account_addr, metadata);
<b>let</b> store = <a href="primary_fungible_store.md#0x1_primary_fungible_store_primary_store">primary_fungible_store::primary_store</a>(account_addr, metadata);
<a href="fungible_asset.md#0x1_fungible_asset_unchecked_deposit_with_no_events">fungible_asset::unchecked_deposit_with_no_events</a>(<a href="object.md#0x1_object_object_address">object::object_address</a>(&store), fa);
} <b>else</b> {
<b>abort</b> <a href="../../aptos-stdlib/../move-stdlib/doc/error.md#0x1_error_not_found">error::not_found</a>(<a href="coin.md#0x1_coin_ECOIN_STORE_NOT_PUBLISHED">ECOIN_STORE_NOT_PUBLISHED</a>)
Expand Down
40 changes: 20 additions & 20 deletions aptos-move/framework/aptos-framework/sources/coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ module aptos_framework::coin {

let metadata = ensure_paired_metadata<CoinType>();
let store = primary_fungible_store::ensure_primary_store_exists(account, metadata);
let store_address = object::object_address(&store);
if (exists<CoinStore<CoinType>>(account)) {
let CoinStore<CoinType> { coin, frozen, deposit_events, withdraw_events } = move_from<CoinStore<CoinType>>(
account
Expand Down Expand Up @@ -610,6 +611,9 @@ module aptos_framework::coin {
fungible_asset::set_frozen_flag_internal(store, frozen);
}
};
if (!exists<MigrationFlag>(store_address)) {
move_to(&create_signer::create_signer(store_address), MigrationFlag {});
}
}

/// Voluntarily migrate to fungible store for `CoinType` if not yet.
Expand Down Expand Up @@ -842,13 +846,11 @@ module aptos_framework::coin {
account_address: address,
metadata: Object<Metadata>
): bool {
(features::new_accounts_default_to_fa_apt_store_enabled() && object::object_address(&metadata) == @0xa) || {
let primary_store_address = primary_fungible_store::primary_store_address<Metadata>(
account_address,
metadata
);
fungible_asset::store_exists(primary_store_address)
}
let primary_store_address = primary_fungible_store::primary_store_address<Metadata>(account_address, metadata);
fungible_asset::store_exists(primary_store_address) && (
// migration flag is needed, until we start defaulting new accounts to APT PFS
features::new_accounts_default_to_fa_apt_store_enabled() || exists<MigrationFlag>(primary_store_address)
)
}

/// Deposit the coin balance into the recipient's account without checking if the account is frozen.
Expand All @@ -868,7 +870,7 @@ module aptos_framework::coin {
)) {
let fa = coin_to_fungible_asset(coin);
let metadata = fungible_asset::asset_metadata(&fa);
let store = primary_fungible_store::ensure_primary_store_exists(account_addr, metadata);
let store = primary_fungible_store::primary_store(account_addr, metadata);
fungible_asset::unchecked_deposit_with_no_events(object::object_address(&store), fa);
} else {
abort error::not_found(ECOIN_STORE_NOT_PUBLISHED)
Expand Down Expand Up @@ -1924,6 +1926,7 @@ module aptos_framework::coin {
}

#[test(account = @aptos_framework, aaron = @0xaa10, bob = @0xb0b)]
#[expected_failure(abort_code = 0x60005, location = Self)]
fun test_force_deposit(
account: &signer,
aaron: &signer,
Expand All @@ -1937,7 +1940,6 @@ module aptos_framework::coin {
account::create_account_for_test(bob_addr);
let (burn_cap, freeze_cap, mint_cap) = initialize_and_register_fake_money(account, 1, true);
maybe_convert_to_fungible_store<FakeMoney>(aaron_addr);
maybe_convert_to_fungible_store<FakeMoney>(bob_addr);
deposit(aaron_addr, mint<FakeMoney>(1, &mint_cap));

force_deposit(account_addr, mint<FakeMoney>(100, &mint_cap));
Expand Down Expand Up @@ -1967,8 +1969,6 @@ module aptos_framework::coin {
account::create_account_for_test(account_addr);
account::create_account_for_test(aaron_addr);
account::create_account_for_test(bob_addr);
let feature = features::get_new_accounts_default_to_fa_apt_store_feature();
features::change_feature_flags_for_testing(account, vector[], vector[feature]);
let (burn_cap, freeze_cap, mint_cap) = initialize_and_register_fake_money(account, 1, true);

assert!(coin_store_exists<FakeMoney>(account_addr), 0);
Expand All @@ -1977,13 +1977,6 @@ module aptos_framework::coin {
assert!(!coin_store_exists<FakeMoney>(aaron_addr), 0);
assert!(!is_account_registered<FakeMoney>(aaron_addr), 0);

register<FakeMoney>(bob);
assert!(coin_store_exists<FakeMoney>(bob_addr), 0);
maybe_convert_to_fungible_store<FakeMoney>(bob_addr);
assert!(!coin_store_exists<FakeMoney>(bob_addr), 0);
register<FakeMoney>(bob);
assert!(!coin_store_exists<FakeMoney>(bob_addr), 0);

maybe_convert_to_fungible_store<FakeMoney>(aaron_addr);
let coin = mint<FakeMoney>(100, &mint_cap);
deposit(aaron_addr, coin);
Expand All @@ -1995,8 +1988,15 @@ module aptos_framework::coin {
assert!(!coin_store_exists<FakeMoney>(account_addr), 0);
assert!(is_account_registered<FakeMoney>(account_addr), 0);

// Deposit FA to bob to created primary fungible store without `MigrationFlag`.
primary_fungible_store::deposit(bob_addr, coin_to_fungible_asset(mint<FakeMoney>(100, &mint_cap)));
assert!(!coin_store_exists<FakeMoney>(bob_addr), 0);
register<FakeMoney>(bob);
assert!(coin_store_exists<FakeMoney>(bob_addr), 0);
maybe_convert_to_fungible_store<FakeMoney>(bob_addr);
assert!(!coin_store_exists<FakeMoney>(bob_addr), 0);
register<FakeMoney>(bob);
assert!(!coin_store_exists<FakeMoney>(bob_addr), 0);

move_to(account, FakeMoneyCapabilities {
burn_cap,
Expand All @@ -2018,8 +2018,9 @@ module aptos_framework::coin {
assert!(coin_balance<FakeMoney>(account_addr) == 0, 0);
assert!(balance<FakeMoney>(account_addr) == 100, 0);
let coin = withdraw<FakeMoney>(account, 50);
assert!(can_receive_paired_fungible_asset(account_addr, ensure_paired_metadata<FakeMoney>()), 0);
assert!(!can_receive_paired_fungible_asset(account_addr, ensure_paired_metadata<FakeMoney>()), 0);
maybe_convert_to_fungible_store<FakeMoney>(account_addr);
assert!(can_receive_paired_fungible_asset(account_addr, ensure_paired_metadata<FakeMoney>()), 0);
deposit(account_addr, coin);
assert!(coin_balance<FakeMoney>(account_addr) == 0, 0);
assert!(balance<FakeMoney>(account_addr) == 100, 0);
Expand All @@ -2031,7 +2032,6 @@ module aptos_framework::coin {
});
}

#[deprecated]
#[resource_group_member(group = aptos_framework::object::ObjectGroup)]
/// The flag the existence of which indicates the primary fungible store is created by the migration from CoinStore.
struct MigrationFlag has key {}
Expand Down

0 comments on commit e504d11

Please sign in to comment.