Module 0x1::primary_fungible_store

This module provides a way for creators of fungible assets to enable support for creating primary (deterministic) stores for their users. This is useful for assets that are meant to be used as a currency, as it allows users to easily create a store for their account and deposit/withdraw/transfer fungible assets to/from it.

The transfer flow works as below:

  1. The sender calls transfer on the fungible asset metadata object to transfer amount of fungible asset to recipient.
  2. The fungible asset metadata object calls ensure_primary_store_exists to ensure that both the sender's and the recipient's primary stores exist. If either doesn't, it will be created.
  3. The fungible asset metadata object calls withdraw on the sender's primary store to withdraw amount of fungible asset from it. This emits a withdraw event.
  4. The fungible asset metadata object calls deposit on the recipient's primary store to deposit amount of fungible asset to it. This emits an deposit event.
use 0x1::dispatchable_fungible_asset;
use 0x1::error;
use 0x1::fungible_asset;
use 0x1::object;
use 0x1::option;
use 0x1::signer;
use 0x1::string;

Resource DeriveRefPod

A resource that holds the derive ref for the fungible asset metadata object. This is used to create primary stores for users with deterministic addresses so that users can easily deposit/withdraw/transfer fungible assets.

#[resource_group_member(#[group = 0x1::object::ObjectGroup])]
struct DeriveRefPod has key
Fields
metadata_derive_ref: object::DeriveRef

Constants

The permissioned signer feature has been removed.

const EPERMISSIONED_SIGNER_REMOVED: u64 = 1;

Function create_primary_store_enabled_fungible_asset

Create a fungible asset with primary store support. When users transfer fungible assets to each other, their primary stores will be created automatically if they don't exist. Primary stores have deterministic addresses so that users can easily deposit/withdraw/transfer fungible assets.

public fun create_primary_store_enabled_fungible_asset(constructor_ref: &object::ConstructorRef, maximum_supply: option::Option<u128>, name: string::String, symbol: string::String, decimals: u8, icon_uri: string::String, project_uri: string::String)
Implementation
public fun create_primary_store_enabled_fungible_asset(
    constructor_ref: &ConstructorRef,
    maximum_supply: Option<u128>,
    name: String,
    symbol: String,
    decimals: u8,
    icon_uri: String,
    project_uri: String,
) {
    fungible_asset::add_fungibility(
        constructor_ref,
        maximum_supply,
        name,
        symbol,
        decimals,
        icon_uri,
        project_uri,
    );
    let metadata_obj = &constructor_ref.generate_signer();
    move_to(metadata_obj, DeriveRefPod {
        metadata_derive_ref: constructor_ref.generate_derive_ref(),
    });
}

Function ensure_primary_store_exists

Ensure that the primary store object for the given address exists. If it doesn't, create it.

public fun ensure_primary_store_exists<T: key>(owner: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>
Implementation
public fun ensure_primary_store_exists<T: key>(
    owner: address,
    metadata: Object<T>,
): Object<FungibleStore> acquires DeriveRefPod {
    let store_addr = primary_store_address(owner, metadata);
    if (fungible_asset::store_exists(store_addr)) {
        object::address_to_object(store_addr)
    } else {
        create_primary_store(owner, metadata)
    }
}

Function create_primary_store

Create a primary store object to hold fungible asset for the given address.

public fun create_primary_store<T: key>(owner_addr: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>
Implementation
public fun create_primary_store<T: key>(
    owner_addr: address,
    metadata: Object<T>,
): Object<FungibleStore> acquires DeriveRefPod {
    let metadata_addr = metadata.object_address();
    object::address_to_object<Metadata>(metadata_addr);
    let derive_ref = &borrow_global<DeriveRefPod>(metadata_addr).metadata_derive_ref;
    let constructor_ref = &object::create_user_derived_object(owner_addr, derive_ref);
    // Disable ungated transfer as deterministic stores shouldn't be transferrable.
    let transfer_ref = &constructor_ref.generate_transfer_ref();
    transfer_ref.disable_ungated_transfer();

    fungible_asset::create_store(constructor_ref, metadata)
}

Function primary_store_address

Get the address of the primary store for the given account.

#[view]
public fun primary_store_address<T: key>(owner: address, metadata: object::Object<T>): address
Implementation
public fun primary_store_address<T: key>(owner: address, metadata: Object<T>): address {
    let metadata_addr = metadata.object_address();
    object::create_user_derived_object_address(owner, metadata_addr)
}

Function primary_store

Get the primary store object for the given account.

#[view]
public fun primary_store<T: key>(owner: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>
Implementation
public fun primary_store<T: key>(owner: address, metadata: Object<T>): Object<FungibleStore> {
    let store = primary_store_address(owner, metadata);
    object::address_to_object<FungibleStore>(store)
}

Function primary_store_exists

Return whether the given account's primary store exists.

#[view]
public fun primary_store_exists<T: key>(account: address, metadata: object::Object<T>): bool
Implementation
public fun primary_store_exists<T: key>(account: address, metadata: Object<T>): bool {
    fungible_asset::store_exists(primary_store_address(account, metadata))
}

Function primary_store_address_inlined

Get the address of the primary store for the given account. Use instead of the corresponding view functions for dispatchable hooks to avoid circular dependencies of modules.

public fun primary_store_address_inlined<T: key>(owner: address, metadata: object::Object<T>): address
Implementation
public inline fun primary_store_address_inlined<T: key>(owner: address, metadata: Object<T>): address {
    let metadata_addr = metadata.object_address();
    object::create_user_derived_object_address(owner, metadata_addr)
}

Function primary_store_inlined

Get the primary store object for the given account. Use instead of the corresponding view functions for dispatchable hooks to avoid circular dependencies of modules.

public fun primary_store_inlined<T: key>(owner: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>
Implementation
public inline fun primary_store_inlined<T: key>(owner: address, metadata: Object<T>): Object<FungibleStore> {
    let store = primary_store_address_inlined(owner, metadata);
    object::address_to_object(store)
}

Function primary_store_exists_inlined

Return whether the given account's primary store exists. Use instead of the corresponding view functions for dispatchable hooks to avoid circular dependencies of modules.

public fun primary_store_exists_inlined<T: key>(account: address, metadata: object::Object<T>): bool
Implementation
public inline fun primary_store_exists_inlined<T: key>(account: address, metadata: Object<T>): bool {
    fungible_asset::store_exists(primary_store_address_inlined(account, metadata))
}

Function grant_permission

#[deprecated]
public fun grant_permission<T: key>(_master: &signer, _permissioned: &signer, _metadata: object::Object<T>, _amount: u64)
Implementation
public fun grant_permission<T: key>(
    _master: &signer,
    _permissioned: &signer,
    _metadata: Object<T>,
    _amount: u64
) {
    abort error::unavailable(EPERMISSIONED_SIGNER_REMOVED)
}

Function grant_apt_permission

#[deprecated]
public fun grant_apt_permission(_master: &signer, _permissioned: &signer, _amount: u64)
Implementation
public fun grant_apt_permission(
    _master: &signer,
    _permissioned: &signer,
    _amount: u64
) {
    abort error::unavailable(EPERMISSIONED_SIGNER_REMOVED)
}

Function balance

Get the balance of account's primary store.

#[view]
public fun balance<T: key>(account: address, metadata: object::Object<T>): u64
Implementation
public fun balance<T: key>(account: address, metadata: Object<T>): u64 {
    if (primary_store_exists(account, metadata)) {
        dispatchable_fungible_asset::derived_balance(primary_store(account, metadata))
    } else {
        0
    }
}

Function is_balance_at_least

#[view]
public fun is_balance_at_least<T: key>(account: address, metadata: object::Object<T>, amount: u64): bool
Implementation
public fun is_balance_at_least<T: key>(account: address, metadata: Object<T>, amount: u64): bool {
    if (primary_store_exists(account, metadata)) {
        dispatchable_fungible_asset::is_derived_balance_at_least(primary_store(account, metadata), amount)
    } else {
        amount == 0
    }
}

Function is_frozen

Return whether the given account's primary store is frozen.

#[view]
public fun is_frozen<T: key>(account: address, metadata: object::Object<T>): bool
Implementation
public fun is_frozen<T: key>(account: address, metadata: Object<T>): bool {
    if (primary_store_exists(account, metadata)) {
        fungible_asset::is_frozen(primary_store(account, metadata))
    } else {
        false
    }
}

Function withdraw

Withdraw amount of fungible asset from the given account's primary store.

public fun withdraw<T: key>(owner: &signer, metadata: object::Object<T>, amount: u64): fungible_asset::FungibleAsset
Implementation
public fun withdraw<T: key>(owner: &signer, metadata: Object<T>, amount: u64): FungibleAsset acquires DeriveRefPod {
    let store = ensure_primary_store_exists(signer::address_of(owner), metadata);
    // Check if the store object has been burnt or not. If so, unburn it first.
    may_be_unburn(owner, store);
    dispatchable_fungible_asset::withdraw(owner, store, amount)
}

Function deposit

Deposit fungible asset fa to the given account's primary store.

public fun deposit(owner: address, fa: fungible_asset::FungibleAsset)
Implementation
public fun deposit(owner: address, fa: FungibleAsset) acquires DeriveRefPod {
    let metadata = fa.asset_metadata();
    let store = ensure_primary_store_exists(owner, metadata);
    dispatchable_fungible_asset::deposit(store, fa);
}

Function deposit_with_signer

Deposit fungible asset fa to the given account's primary store using signer.

public fun deposit_with_signer(owner: &signer, fa: fungible_asset::FungibleAsset)
Implementation
public fun deposit_with_signer(owner: &signer, fa: FungibleAsset) acquires DeriveRefPod {
    let metadata = fa.asset_metadata();
    let store = ensure_primary_store_exists(signer::address_of(owner), metadata);
    dispatchable_fungible_asset::deposit(store, fa);
}

Function transfer

Transfer amount of fungible asset from sender's primary store to receiver's primary store.

public entry fun transfer<T: key>(sender: &signer, metadata: object::Object<T>, recipient: address, amount: u64)
Implementation
public entry fun transfer<T: key>(
    sender: &signer,
    metadata: Object<T>,
    recipient: address,
    amount: u64,
) acquires DeriveRefPod {
    let sender_store = ensure_primary_store_exists(signer::address_of(sender), metadata);
    // Check if the sender store object has been burnt or not. If so, unburn it first.
    may_be_unburn(sender, sender_store);
    let recipient_store = ensure_primary_store_exists(recipient, metadata);
    dispatchable_fungible_asset::transfer(sender, sender_store, recipient_store, amount);
}

Function transfer_assert_minimum_deposit

Transfer amount of fungible asset from sender's primary store to receiver's primary store. Use the minimum deposit assertion api to make sure receipient will receive a minimum amount of fund.

public entry fun transfer_assert_minimum_deposit<T: key>(sender: &signer, metadata: object::Object<T>, recipient: address, amount: u64, expected: u64)
Implementation
public entry fun transfer_assert_minimum_deposit<T: key>(
    sender: &signer,
    metadata: Object<T>,
    recipient: address,
    amount: u64,
    expected: u64,
) acquires DeriveRefPod {
    let sender_store = ensure_primary_store_exists(signer::address_of(sender), metadata);
    // Check if the sender store object has been burnt or not. If so, unburn it first.
    may_be_unburn(sender, sender_store);
    let recipient_store = ensure_primary_store_exists(recipient, metadata);
    dispatchable_fungible_asset::transfer_assert_minimum_deposit(
        sender,
        sender_store,
        recipient_store,
        amount,
        expected
    );
}

Function mint

Mint to the primary store of owner.

public fun mint(mint_ref: &fungible_asset::MintRef, owner: address, amount: u64)
Implementation
public fun mint(mint_ref: &MintRef, owner: address, amount: u64) acquires DeriveRefPod {
    let primary_store = ensure_primary_store_exists(owner, mint_ref.mint_ref_metadata());
    mint_ref.mint_to(primary_store, amount);
}

Function burn

Burn from the primary store of owner.

public fun burn(burn_ref: &fungible_asset::BurnRef, owner: address, amount: u64)
Implementation
public fun burn(burn_ref: &BurnRef, owner: address, amount: u64) {
    let primary_store = primary_store(owner, burn_ref.burn_ref_metadata());
    burn_ref.burn_from(primary_store, amount);
}

Function set_frozen_flag

Freeze/Unfreeze the primary store of owner.

public fun set_frozen_flag(transfer_ref: &fungible_asset::TransferRef, owner: address, frozen: bool)
Implementation
public fun set_frozen_flag(transfer_ref: &TransferRef, owner: address, frozen: bool) acquires DeriveRefPod {
    let primary_store = ensure_primary_store_exists(owner, transfer_ref.transfer_ref_metadata());
    transfer_ref.set_frozen_flag(primary_store, frozen);
}

Function withdraw_with_ref

Withdraw from the primary store of owner ignoring frozen flag.

public fun withdraw_with_ref(transfer_ref: &fungible_asset::TransferRef, owner: address, amount: u64): fungible_asset::FungibleAsset
Implementation
public fun withdraw_with_ref(transfer_ref: &TransferRef, owner: address, amount: u64): FungibleAsset {
    let from_primary_store = primary_store(owner, transfer_ref.transfer_ref_metadata());
    transfer_ref.withdraw_with_ref(from_primary_store, amount)
}

Function deposit_with_ref

Deposit to the primary store of owner ignoring frozen flag.

public fun deposit_with_ref(transfer_ref: &fungible_asset::TransferRef, owner: address, fa: fungible_asset::FungibleAsset)
Implementation
public fun deposit_with_ref(transfer_ref: &TransferRef, owner: address, fa: FungibleAsset) acquires DeriveRefPod {
    let to_primary_store = ensure_primary_store_exists(
        owner,
        transfer_ref.transfer_ref_metadata()
    );
    transfer_ref.deposit_with_ref(to_primary_store, fa);
}

Function transfer_with_ref

Transfer amount of FA from the primary store of from to that of to ignoring frozen flag.

public fun transfer_with_ref(transfer_ref: &fungible_asset::TransferRef, from: address, to: address, amount: u64)
Implementation
public fun transfer_with_ref(
    transfer_ref: &TransferRef,
    from: address,
    to: address,
    amount: u64
) acquires DeriveRefPod {
    let from_primary_store = primary_store(from, transfer_ref.transfer_ref_metadata());
    let to_primary_store = ensure_primary_store_exists(to, transfer_ref.transfer_ref_metadata());
    transfer_ref.transfer_with_ref(from_primary_store, to_primary_store, amount);
}

Function upgrade_to_concurrent

public entry fun upgrade_to_concurrent(owner: &signer, metadata: object::Object<fungible_asset::Metadata>)
Implementation
public entry fun upgrade_to_concurrent(owner: &signer, metadata: Object<Metadata>) acquires DeriveRefPod {
    let store = ensure_primary_store_exists(signer::address_of(owner), metadata);
    fungible_asset::upgrade_store_to_concurrent(owner, store);
}

Function may_be_unburn

fun may_be_unburn(owner: &signer, store: object::Object<fungible_asset::FungibleStore>)
Implementation
fun may_be_unburn(owner: &signer, store: Object<FungibleStore>) {
    if (store.is_burnt()) {
        object::unburn(owner, store);
    };
}

Specification

High-level Requirements

No.RequirementCriticalityImplementationEnforcement
1 Creating a fungible asset with primary store support should initiate a derived reference and store it under the metadata object. Medium The function create_primary_store_enabled_fungible_asset makes an existing object, fungible, via the fungible_asset::add_fungibility function and initializes the DeriveRefPod resource by generating a DeriveRef for the object and then stores it under the object address. Formally verified via create_primary_store_enabled_fungible_asset that the DeriveRefPod is stored under the metadata object, and via the module invariant that its derive ref derives from that object.
2 Fetching and creating a primary fungible store of an asset should only succeed if the object supports primary store. Low The function create_primary_store is used to create a primary store by borrowing the DeriveRef resource from the object. In case the resource does not exist, creation will fail. The function ensure_primary_store_exists is used to fetch the primary store if it exists, otherwise it will create one via the create_primary function. Formally verified via CreatePrimaryStoreAbortsIf that it aborts if the DeriveRefPod doesn't exist, and via CreatePrimaryStoreAbortsIf that it aborts if the FungibleStore resource exists already under the object address.
3 It should be possible to create a primary store to hold a fungible asset. Medium The function create_primary_store borrows the DeriveRef resource from DeriveRefPod and then creates the store which is returned. Formally verified via create_primary_store and create_primary_store that it returns the newly created FungibleStore.
4 Fetching the balance or the frozen status of a primary store should never abort. Low The function balance returns the balance of the store, if the store exists, otherwise it returns 0. The function is_frozen returns the frozen flag of the fungible store, if the store exists, otherwise it returns false. Audited that the balance function returns the balance of the FungibleStore. Audited that the is_frozen function returns the frozen status of the FungibleStore resource. Audited that it never aborts.
5 The ability to withdraw, deposit, transfer, mint and burn should only be available for assets with primary store support. Medium The primary store is fetched before performing either of withdraw, deposit, transfer, mint, burn operation. If the FungibleStore resource doesn't exist the operation will fail. Formally verified via burn and withdraw_with_ref that burning and withdrawing with a ref abort if the primary store doesn't exist, and via CreatePrimaryStoreAbortsIf that mint, deposit_with_ref, transfer_with_ref and set_frozen_flag create a missing store only for an asset with primary store support. Audited for withdraw, deposit and transfer, whose store-creation aborts are not specified.
6 The action of depositing a fungible asset of the same type as the store should never fail if the store is not frozen. Medium The function deposit fetches the owner's store, if it doesn't exist it will be created, and then deposits the fungible asset to it. The function deposit_with_ref fetches the owner's store, if it doesn't exist it will be created, and then deposit the fungible asset via the fungible_asset::deposit_with_ref function. Depositing fails if the metadata of the FungibleStore and FungibleAsset differs. Formally verified that deposit aborts if the store is frozen via deposit and, without a deposit hook, if the metadata of the store and the asset differs via deposit; that deposit_with_ref aborts on differing metadata via deposit_with_ref; and that the store balance is increased by the deposit amount via PrimaryDepositBalance (deposit without a deposit hook) and deposit_with_ref. That deposit fails for no other reason is audited, as its abort conditions are partial.
7 Withdrawing should only be allowed to the owner of an existing store with sufficient balance. Critical The withdraw function fetches the owner's store via the primary_store function and then calls fungible_asset::withdraw which validates the owner of the store, checks the frozen status and the balance of the store. The withdraw_with_ref function fetches the store of the owner via primary_store function and calls the fungible_asset::withdraw_with_ref which validates transfer_ref's metadata with the withdrawing stores metadata, and the balance of the store. Formally verified that withdraw aborts if the store is frozen via withdraw; that withdraw_with_ref aborts if the transfer ref's metadata doesn't match the store's metadata via withdraw_with_ref; that it aborts if the store doesn't have sufficient balance via withdraw (without a withdraw hook) and withdraw_with_ref; and that the store balance is decreased by the amount withdrawn via withdraw (without a withdraw hook) and withdraw_with_ref. Audited that it aborts if the owner doesn't own the store and that the store is unburned (withdraw).
8 Only the fungible store owner is allowed to unburn a burned store. High The function may_be_unburn checks if the store is burned and then proceeds to call object::unburn which ensures that the owner of the object matches the address of the signer. Audited that the store is unburned successfully.
9 Only the owner of a primary store can transfer its balance to any recipient's primary store. High The function transfer fetches sender and recipient's primary stores, if the sender's store is burned it unburns the store and calls the fungile_asset::transfer to proceed with the transfer, which first withdraws the assets from the sender's store and then deposits to the recipient's store. The function transfer_with_ref fetches the sender's and recipient's stores and calls the fungible_asset::transfer_with_ref function which withdraws the asset with the ref from the sender and deposits the asset to the recipient with the ref. Formally verified that the sender's store balance is decreased by the specified amount and the recipient's increased by it via PlainTransferBalancesToNew (transfer without dispatch hooks) and transfer_with_ref. Audited the owner check and that the sender's store is unburned (transfer), and transfers with dispatch hooks.
10 Minting an amount of assets to an unfrozen store is only allowed with a valid mint reference. High The mint function fetches the primary store and calls the fungible_asset::mint_to, which mints with MintRef's metadata which internally validates the amount and the increases the total supply of the asset. And the minted asset is deposited to the provided store by validating that the store is unfrozen and the store's metadata is the same as the depositing asset's metadata. Formally verified via mint that it aborts if the store is frozen, via mint that it aborts if the mint_ref's metadata is not the same as the store's metadata, via mint that the balance of the store is increased by the minted amount, and via mint that the asset's total supply is increased by the amount minted.
11 Burning an amount of assets from an existing unfrozen store is only allowed with a valid burn reference. High The burn function fetches the primary store and calls the fungible_asset::burn_from function which withdraws the amount from the store while enforcing that the store has enough balance and burns the withdrawn asset after validating the asset's metadata and the BurnRef's metadata followed by decreasing the supply of the asset. Formally verified via burn that it aborts if the store doesn't have enough balance, via burn that it aborts if the metadata of the store is not same as the BurnRef's metadata, via burn that the store's balance is reduced by the amount burned, and via burn that the total supply of the asset is decreased by it.
12 Setting the frozen flag of a store is only allowed with a valid reference. High The function set_frozen_flag fetches the primary store and calls fungible_asset::set_frozen_flag which validates the TransferRef's metadata with the store's metadata and then updates the frozen flag. Formally verified via set_frozen_flag and set_frozen_flag.

Module-level Specification

pragma verify = true;
// This enforces high-level requirement 1:
invariant forall m: address {exists<DeriveRefPod>(m)} where exists<DeriveRefPod>(m):
    global<DeriveRefPod>(m).metadata_derive_ref.self == m;

fun spec_primary_store_exists<T: key>(account: address, metadata: Object<T>): bool {
   fungible_asset::store_exists(spec_primary_store_address(account, metadata))
}

fun spec_primary_store_address<T: key>(owner: address, metadata: Object<T>): address {
   let metadata_addr = object::object_address(metadata);
   object::spec_create_user_derived_object_address(owner, metadata_addr)
}

Function create_primary_store_enabled_fungible_asset

public fun create_primary_store_enabled_fungible_asset(constructor_ref: &object::ConstructorRef, maximum_supply: option::Option<u128>, name: string::String, symbol: string::String, decimals: u8, icon_uri: string::String, project_uri: string::String)

Makes the object a fungible asset whose primary stores can be derived from it.

let addr = constructor_ref.self;
modifies global<Metadata>(addr);
modifies global<fungible_asset::Supply>(addr);
modifies global<fungible_asset::ConcurrentSupply>(addr);
modifies global<DeriveRefPod>(addr);
include fungible_asset::AddFungibilityAbortsIf;
aborts_if exists<DeriveRefPod>(addr);
// This enforces high-level requirement 1:
ensures exists<DeriveRefPod>(addr);
ensures global<Metadata>(addr) == fungible_asset::Metadata { name, symbol, decimals, icon_uri, project_uri };

Function ensure_primary_store_exists

public fun ensure_primary_store_exists<T: key>(owner: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>

Returns the primary store of owner, creating it if missing.

let metadata_addr = metadata.inner;
let store_addr = spec_primary_store_address(owner, metadata);
let existing = exists<FungibleStore>(store_addr);
aborts_if existing && !exists<object::ObjectCore>(store_addr);
include !existing ==> CreatePrimaryStoreAbortsIf<T> { owner_addr: owner };
ensures result.inner == store_addr;
ensures exists<FungibleStore>(store_addr);
ensures existing ==> global<FungibleStore>(store_addr) == old(global<FungibleStore>(store_addr));
ensures !existing ==> global<FungibleStore>(store_addr).metadata.inner == metadata_addr
    && !global<FungibleStore>(store_addr).frozen
    && fungible_asset::spec_balance(store_addr) == 0
    && global<object::ObjectCore>(store_addr).owner == owner;

Function create_primary_store

public fun create_primary_store<T: key>(owner_addr: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>
let store_addr = spec_primary_store_address(owner_addr, metadata);
include CreatePrimaryStoreAbortsIf<T>;
// This enforces high-level requirement 3:
ensures result.inner == store_addr;
// This enforces high-level requirement 3:
ensures exists<FungibleStore>(store_addr);
ensures global<object::ObjectCore>(store_addr).owner == owner_addr;
ensures !global<object::ObjectCore>(store_addr).allow_ungated_transfer;

schema CreatePrimaryStoreAbortsIf<T> {
    owner_addr: address;
    metadata: Object<T>;
    let metadata_addr = metadata.inner;
    let store_addr = spec_primary_store_address(owner_addr, metadata);
    aborts_if !exists<object::ObjectCore>(metadata_addr) || !exists<Metadata>(metadata_addr);
    // This enforces high-level requirement 2:
    aborts_if !exists<DeriveRefPod>(metadata_addr);
    aborts_if exists<object::ObjectCore>(store_addr);
    aborts_if exists<object::Untransferable>(store_addr);
    // This enforces high-level requirement 2:
    aborts_if exists<FungibleStore>(store_addr);
    aborts_if std::features::spec_is_enabled(std::features::DEFAULT_TO_CONCURRENT_FUNGIBLE_BALANCE)
        && exists<fungible_asset::ConcurrentFungibleBalance>(store_addr);
}

Function primary_store_address

#[view]
public fun primary_store_address<T: key>(owner: address, metadata: object::Object<T>): address
pragma opaque;
aborts_if false;
ensures result == spec_primary_store_address(owner, metadata);

Function primary_store

#[view]
public fun primary_store<T: key>(owner: address, metadata: object::Object<T>): object::Object<fungible_asset::FungibleStore>
pragma opaque;
let store_addr = spec_primary_store_address(owner, metadata);
aborts_if !exists<object::ObjectCore>(store_addr);
aborts_if !object::spec_exists_at<FungibleStore>(store_addr);
ensures object::object_address(result) == store_addr;

Function primary_store_exists

#[view]
public fun primary_store_exists<T: key>(account: address, metadata: object::Object<T>): bool
pragma opaque;
aborts_if false;
ensures result == spec_primary_store_exists(account, metadata);

Function withdraw

public fun withdraw<T: key>(owner: &signer, metadata: object::Object<T>, amount: u64): fungible_asset::FungibleAsset
pragma aborts_if_is_partial;
let store_addr = spec_primary_store_address(signer::address_of(owner), metadata);
let existing = exists<FungibleStore>(store_addr);
let store = global<FungibleStore>(store_addr);
// This enforces high-level requirement 7:
aborts_if existing && store.frozen;
// This enforces high-level requirement 7:
aborts_if existing && fungible_asset::spec_store_no_withdraw_hook(store_addr)
    && fungible_asset::spec_balance(store_addr) < amount;
// This enforces high-level requirement 7:
ensures existing && fungible_asset::spec_store_no_withdraw_hook(store_addr) ==>
    fungible_asset::spec_balance(store_addr) == old(fungible_asset::spec_balance(store_addr)) - amount
    && result == FungibleAsset { metadata: store.metadata, amount };

Function deposit

public fun deposit(owner: address, fa: fungible_asset::FungibleAsset)
pragma aborts_if_is_partial;
let store_addr = spec_primary_store_address(owner, fa.metadata);
let existing = exists<FungibleStore>(store_addr);
let store = global<FungibleStore>(store_addr);
// This enforces high-level requirement 6:
aborts_if existing && store.frozen;
// This enforces high-level requirement 6:
aborts_if existing && fungible_asset::spec_store_no_deposit_hook(store_addr)
    && fa.metadata != store.metadata;
include PrimaryDepositBalance;

Without a deposit hook, fa lands in the primary store of owner, which is created if missing.

schema PrimaryDepositBalance {
    owner: address;
    fa: FungibleAsset;
    let store_addr = spec_primary_store_address(owner, fa.metadata);
    let existing = exists<FungibleStore>(store_addr);
    let no_hook = if (existing) {
        fungible_asset::spec_store_no_deposit_hook(store_addr)
    } else {
        spec_metadata_no_deposit_hook(fa.metadata.inner)
    };
    // This enforces high-level requirement 6:
    ensures no_hook ==> exists<FungibleStore>(store_addr)
        && global<FungibleStore>(store_addr).metadata == fa.metadata
        && fungible_asset::spec_balance(store_addr)
            == (if (existing) { old(fungible_asset::spec_balance(store_addr)) } else { 0 }) + fa.amount;
}

The asset at m has no deposit hook.

fun spec_metadata_no_deposit_hook(m: address): bool {
   !exists<fungible_asset::DispatchFunctionStore>(m)
       || std::option::is_none(global<fungible_asset::DispatchFunctionStore>(m).deposit_function)
}

The store at from exists and the transfer runs without hooks; a missing store at to is created for asset m.

fun spec_is_plain_transfer_to(from: address, to: address, m: address): bool {
   exists<FungibleStore>(from) && fungible_asset::spec_store_no_withdraw_hook(from)
       && if (exists<FungibleStore>(to)) {
           fungible_asset::spec_store_no_deposit_hook(to)
       } else {
           spec_metadata_no_deposit_hook(m)
       }
}

If plain, amount moves from the store at from to the store at to, where a store missing at to is created with balance amount; a self-transfer changes nothing.

schema PlainTransferBalancesToNew {
    from: address;
    to: address;
    amount: u64;
    plain: bool;
    // This enforces high-level requirement 9:
    ensures plain && from != to ==>
        fungible_asset::spec_balance(from) == old(fungible_asset::spec_balance(from)) - amount
        && exists<FungibleStore>(to)
        && fungible_asset::spec_balance(to) == (if (old(exists<FungibleStore>(to))) {
            old(fungible_asset::spec_balance(to))
        } else { 0 }) + amount;
    ensures plain && from == to ==>
        fungible_asset::spec_balance(from) == old(fungible_asset::spec_balance(from));
}

Moves amount between the primary stores of sender and recipient.

schema PrimaryTransfer<T> {
    sender: &signer;
    metadata: Object<T>;
    recipient: address;
    amount: u64;
    let from = spec_primary_store_address(signer::address_of(sender), metadata);
    let to = spec_primary_store_address(recipient, metadata);
    let plain = spec_is_plain_transfer_to(from, to, metadata.inner);
    aborts_if exists<FungibleStore>(from) && global<FungibleStore>(from).frozen;
    aborts_if exists<FungibleStore>(from) && exists<FungibleStore>(to)
        && fungible_asset::spec_store_no_withdraw_hook(from) && global<FungibleStore>(to).frozen;
    aborts_if plain && fungible_asset::spec_balance(from) < amount;
    include PlainTransferBalancesToNew { from, to, amount, plain };
}

Function deposit_with_signer

public fun deposit_with_signer(owner: &signer, fa: fungible_asset::FungibleAsset)
pragma aborts_if_is_partial;
let store_addr = spec_primary_store_address(signer::address_of(owner), fa.metadata);
let existing = exists<FungibleStore>(store_addr);
let store = global<FungibleStore>(store_addr);
aborts_if existing && store.frozen;
aborts_if existing && fungible_asset::spec_store_no_deposit_hook(store_addr)
    && fa.metadata != store.metadata;
include PrimaryDepositBalance { owner: signer::address_of(owner) };

Function transfer

public entry fun transfer<T: key>(sender: &signer, metadata: object::Object<T>, recipient: address, amount: u64)
pragma aborts_if_is_partial;
include PrimaryTransfer<T>;

Function transfer_assert_minimum_deposit

public entry fun transfer_assert_minimum_deposit<T: key>(sender: &signer, metadata: object::Object<T>, recipient: address, amount: u64, expected: u64)
pragma aborts_if_is_partial;
let from = spec_primary_store_address(signer::address_of(sender), metadata);
let to = spec_primary_store_address(recipient, metadata);
include PrimaryTransfer<T>;
aborts_if spec_is_plain_transfer_to(from, to, metadata.inner) && from != to && amount < expected;

Function mint

public fun mint(mint_ref: &fungible_asset::MintRef, owner: address, amount: u64)

Mints amount into the primary store of owner, which is created if missing.

let metadata = mint_ref.metadata;
let store_addr = spec_primary_store_address(owner, metadata);
let existing = exists<FungibleStore>(store_addr);
aborts_if existing && !exists<object::ObjectCore>(store_addr);
include !existing ==> CreatePrimaryStoreAbortsIf<Metadata> { owner_addr: owner };
// This enforces high-level requirement 10:
aborts_if existing && global<FungibleStore>(store_addr).frozen;
// This enforces high-level requirement 10:
aborts_if existing && global<FungibleStore>(store_addr).metadata != metadata;
include fungible_asset::IncreaseSupplyAbortsIf { metadata_addr: metadata.inner };
include existing ==> fungible_asset::UncheckedDepositAbortsIf {
    store_addr, fa: FungibleAsset { metadata, amount }
};
ensures exists<FungibleStore>(store_addr);
// This enforces high-level requirement 10:
ensures fungible_asset::spec_balance(store_addr)
    == (if (existing) { old(fungible_asset::spec_balance(store_addr)) } else { 0 }) + amount;
// This enforces high-level requirement 10:
ensures amount != 0 ==> fungible_asset::spec_supply(metadata.inner)
    == old(fungible_asset::spec_supply(metadata.inner)) + amount;

Function burn

public fun burn(burn_ref: &fungible_asset::BurnRef, owner: address, amount: u64)

Burns amount from the primary store of owner, ignoring its frozen flag.

let metadata = burn_ref.metadata;
let store_addr = spec_primary_store_address(owner, metadata);
aborts_if !exists<object::ObjectCore>(store_addr);
// This enforces high-level requirement 5:
aborts_if !exists<FungibleStore>(store_addr);
// This enforces high-level requirement 11:
aborts_if fungible_asset::spec_balance(store_addr) < amount;
// This enforces high-level requirement 11:
aborts_if metadata != global<FungibleStore>(store_addr).metadata;
aborts_if amount != 0 && !exists<fungible_asset::ConcurrentSupply>(metadata.inner)
    && !exists<fungible_asset::Supply>(metadata.inner);
aborts_if amount != 0 && fungible_asset::spec_supply(metadata.inner) < amount;
// This enforces high-level requirement 11:
ensures fungible_asset::spec_balance(store_addr) == old(fungible_asset::spec_balance(store_addr)) - amount;
// This enforces high-level requirement 11:
ensures amount != 0 ==> fungible_asset::spec_supply(metadata.inner)
    == old(fungible_asset::spec_supply(metadata.inner)) - amount;

Function set_frozen_flag

public fun set_frozen_flag(transfer_ref: &fungible_asset::TransferRef, owner: address, frozen: bool)
let metadata = transfer_ref.metadata;
let store_addr = spec_primary_store_address(owner, metadata);
let existing = exists<FungibleStore>(store_addr);
aborts_if existing && !exists<object::ObjectCore>(store_addr);
include !existing ==> CreatePrimaryStoreAbortsIf<Metadata> { owner_addr: owner };
// This enforces high-level requirement 12:
aborts_if exists<FungibleStore>(store_addr)
    && global<FungibleStore>(store_addr).metadata != transfer_ref.metadata;
ensures exists<FungibleStore>(store_addr);
// This enforces high-level requirement 12:
ensures global<FungibleStore>(store_addr).frozen == frozen;

Function withdraw_with_ref

public fun withdraw_with_ref(transfer_ref: &fungible_asset::TransferRef, owner: address, amount: u64): fungible_asset::FungibleAsset

Withdraws from the primary store of owner, ignoring its frozen flag.

let store_addr = spec_primary_store_address(owner, transfer_ref.metadata);
aborts_if !exists<object::ObjectCore>(store_addr);
// This enforces high-level requirement 5:
aborts_if !exists<FungibleStore>(store_addr);
// This enforces high-level requirement 7:
aborts_if transfer_ref.metadata != global<FungibleStore>(store_addr).metadata;
// This enforces high-level requirement 7:
aborts_if fungible_asset::spec_balance(store_addr) < amount;
// This enforces high-level requirement 7:
ensures fungible_asset::spec_balance(store_addr) == old(fungible_asset::spec_balance(store_addr)) - amount;
ensures result == FungibleAsset { metadata: transfer_ref.metadata, amount };

Function deposit_with_ref

public fun deposit_with_ref(transfer_ref: &fungible_asset::TransferRef, owner: address, fa: fungible_asset::FungibleAsset)

Deposits into the primary store of owner, which is created if missing, ignoring its frozen flag.

let metadata = transfer_ref.metadata;
let store_addr = spec_primary_store_address(owner, metadata);
let existing = exists<FungibleStore>(store_addr);
aborts_if existing && !exists<object::ObjectCore>(store_addr);
include !existing ==> CreatePrimaryStoreAbortsIf<Metadata> { owner_addr: owner };
aborts_if transfer_ref.metadata != fa.metadata;
// This enforces high-level requirement 6:
aborts_if existing && global<FungibleStore>(store_addr).metadata != fa.metadata;
include existing ==> fungible_asset::UncheckedDepositAbortsIf { store_addr };
ensures exists<FungibleStore>(store_addr);
// This enforces high-level requirement 6:
ensures fungible_asset::spec_balance(store_addr)
    == (if (existing) { old(fungible_asset::spec_balance(store_addr)) } else { 0 }) + fa.amount;

Function transfer_with_ref

public fun transfer_with_ref(transfer_ref: &fungible_asset::TransferRef, from: address, to: address, amount: u64)

Moves amount between the primary stores of from and to, ignoring their frozen flags.

let metadata = transfer_ref.metadata;
let from_addr = spec_primary_store_address(from, metadata);
let to_addr = spec_primary_store_address(to, metadata);
let to_existing = exists<FungibleStore>(to_addr);
let to_concurrent = global<fungible_asset::ConcurrentFungibleBalance>(to_addr).balance;
aborts_if !exists<object::ObjectCore>(from_addr);
aborts_if !exists<FungibleStore>(from_addr);
aborts_if to_existing && !exists<object::ObjectCore>(to_addr);
include !to_existing ==> CreatePrimaryStoreAbortsIf<Metadata> { owner_addr: to };
aborts_if transfer_ref.metadata != global<FungibleStore>(from_addr).metadata;
aborts_if fungible_asset::spec_balance(from_addr) < amount;
include to_existing && from_addr != to_addr ==> fungible_asset::UncheckedDepositAbortsIf {
    store_addr: to_addr, fa: FungibleAsset { metadata, amount }
};
aborts_if amount != 0 && from_addr == to_addr && fungible_asset::spec_is_concurrent_balance(to_addr)
    && aptos_framework::aggregator_v2::spec_get_value(to_concurrent)
        > aptos_framework::aggregator_v2::spec_get_max_value(to_concurrent);
// This enforces high-level requirement 9:
ensures from_addr != to_addr ==>
    fungible_asset::spec_balance(from_addr) == old(fungible_asset::spec_balance(from_addr)) - amount
    && fungible_asset::spec_balance(to_addr) == (if (old(exists<FungibleStore>(to_addr))) {
        old(fungible_asset::spec_balance(to_addr))
    } else { 0 }) + amount;
ensures from_addr == to_addr ==>
    fungible_asset::spec_balance(from_addr) == old(fungible_asset::spec_balance(from_addr));

Function upgrade_to_concurrent

public entry fun upgrade_to_concurrent(owner: &signer, metadata: object::Object<fungible_asset::Metadata>)
let owner_address = signer::address_of(owner);
let store_addr = spec_primary_store_address(owner_address, metadata);
let existing = exists<FungibleStore>(store_addr);
let concurrent = exists<fungible_asset::ConcurrentFungibleBalance>(store_addr);
let created_concurrent = !existing
    && std::features::spec_is_enabled(std::features::DEFAULT_TO_CONCURRENT_FUNGIBLE_BALANCE);
aborts_if existing && !exists<object::ObjectCore>(store_addr);
aborts_if existing && !concurrent && object::spec_owns_aborts(store_addr, owner_address);
aborts_if existing && !concurrent && !object::spec_owns(store_addr, owner_address);
aborts_if existing && !concurrent && global<FungibleStore>(store_addr).frozen;
aborts_if !concurrent && !created_concurrent
    && !std::features::spec_is_enabled(std::features::CONCURRENT_FUNGIBLE_BALANCE);
include !existing ==> CreatePrimaryStoreAbortsIf<Metadata> { owner_addr: signer::address_of(owner) };
ensures exists<fungible_asset::ConcurrentFungibleBalance>(store_addr);
ensures old(exists<FungibleStore>(store_addr)) ==> fungible_asset::spec_balance(store_addr)
    == old(fungible_asset::spec_balance(store_addr));