/// This module defines a minimal and generic Coin and Balance. /// modified from https://github.com/move-language/move/tree/main/language/documentation/tutorial module aptos_framework::aptos_coin { use std::error; use std::signer; use std::string; use std::vector; use std::option::{Self, Option}; use aptos_framework::coin::{Self, BurnCapability, MintCapability}; use aptos_framework::system_addresses; friend aptos_framework::genesis; /// Account does not have mint capability const ENO_CAPABILITIES: u64 = 1; /// Mint capability has already been delegated to this specified address const EALREADY_DELEGATED: u64 = 2; /// Cannot find delegation of mint capability to this account const EDELEGATION_NOT_FOUND: u64 = 3; struct AptosCoin has key {} struct MintCapStore has key { mint_cap: MintCapability, } /// Delegation token created by delegator and can be claimed by the delegatee as MintCapability. struct DelegatedMintCapability has store { to: address } /// The container stores the current pending delegations. struct Delegations has key { inner: vector, } /// Can only called during genesis to initialize the Aptos coin. public(friend) fun initialize(aptos_framework: &signer): (BurnCapability, MintCapability) { system_addresses::assert_aptos_framework(aptos_framework); let (burn_cap, freeze_cap, mint_cap) = coin::initialize( aptos_framework, string::utf8(b"Aptos Coin"), string::utf8(b"APT"), 8, // decimals true, // monitor_supply ); // Aptos framework needs mint cap to mint coins to initial validators. This will be revoked once the validators // have been initialized. move_to(aptos_framework, MintCapStore { mint_cap }); coin::destroy_freeze_cap(freeze_cap); (burn_cap, mint_cap) } /// Used to upgrade supply to use an integer, deprecating Aggregator V1. entry fun upgrade_supply(aptos_framework: &signer) { system_addresses::assert_aptos_framework(aptos_framework); coin::upgrade_supply(aptos_framework); } public fun has_mint_capability(account: &signer): bool { exists(signer::address_of(account)) } /// Only called during genesis to destroy the aptos framework account's mint capability once all initial validators /// and accounts have been initialized during genesis. public(friend) fun destroy_mint_cap(aptos_framework: &signer) acquires MintCapStore { system_addresses::assert_aptos_framework(aptos_framework); let MintCapStore { mint_cap } = move_from(@aptos_framework); coin::destroy_mint_cap(mint_cap); } /// Can only be called during genesis for tests to grant mint capability to aptos framework and core resources /// accounts. /// Expects account and APT store to be registered before calling. public(friend) fun configure_accounts_for_test( aptos_framework: &signer, core_resources: &signer, mint_cap: MintCapability, ) { system_addresses::assert_aptos_framework(aptos_framework); // Mint the core resource account AptosCoin for gas so it can execute system transactions. let coins = coin::mint( 18446744073709551615, &mint_cap, ); coin::deposit(signer::address_of(core_resources), coins); move_to(core_resources, MintCapStore { mint_cap }); move_to(core_resources, Delegations { inner: vector::empty() }); } /// Only callable in tests and testnets where the core resources account exists. /// Create new coins and deposit them into dst_addr's account. public entry fun mint( account: &signer, dst_addr: address, amount: u64, ) acquires MintCapStore { let account_addr = signer::address_of(account); assert!( exists(account_addr), error::not_found(ENO_CAPABILITIES), ); let mint_cap = &borrow_global(account_addr).mint_cap; let coins_minted = coin::mint(amount, mint_cap); coin::deposit(dst_addr, coins_minted); } /// Only callable in tests and testnets where the core resources account exists. /// Create delegated token for the address so the account could claim MintCapability later. public entry fun delegate_mint_capability(account: signer, to: address) acquires Delegations { system_addresses::assert_core_resource(&account); let delegations = &mut borrow_global_mut(@core_resources).inner; delegations.for_each_ref(|element| { let element: &DelegatedMintCapability = element; assert!(element.to != to, error::invalid_argument(EALREADY_DELEGATED)); }); delegations.push_back(DelegatedMintCapability { to }); } /// Only callable in tests and testnets where the core resources account exists. /// Claim the delegated mint capability and destroy the delegated token. public entry fun claim_mint_capability(account: &signer) acquires Delegations, MintCapStore { let maybe_index = find_delegation(signer::address_of(account)); assert!(maybe_index.is_some(), EDELEGATION_NOT_FOUND); let idx = *maybe_index.borrow(); let delegations = &mut borrow_global_mut(@core_resources).inner; let DelegatedMintCapability { to: _ } = delegations.swap_remove(idx); // Make a copy of mint cap and give it to the specified account. let mint_cap = borrow_global(@core_resources).mint_cap; move_to(account, MintCapStore { mint_cap }); } fun find_delegation(addr: address): Option acquires Delegations { let delegations = &borrow_global(@core_resources).inner; let i = 0; let len = delegations.length(); let index = option::none(); while (i < len) { let element = delegations.borrow(i); if (element.to == addr) { index = option::some(i); break }; i += 1; }; index } #[test_only] use aptos_framework::account; #[test_only] use aptos_framework::aggregator_factory; #[test_only] use aptos_framework::fungible_asset::FungibleAsset; #[test_only] public fun mint_apt_fa_for_test(amount: u64): FungibleAsset acquires MintCapStore { ensure_initialized_with_apt_fa_metadata_for_test(); coin::coin_to_fungible_asset( coin::mint( amount, &borrow_global(@aptos_framework).mint_cap ) ) } #[test_only] public fun ensure_initialized_with_apt_fa_metadata_for_test() { let aptos_framework = account::create_signer_for_test(@aptos_framework); if (!exists(@aptos_framework)) { if (!aggregator_factory::aggregator_factory_exists_for_testing()) { aggregator_factory::initialize_aggregator_factory_for_test(&aptos_framework); }; let (burn_cap, mint_cap) = initialize(&aptos_framework); coin::destroy_burn_cap(burn_cap); coin::destroy_mint_cap(mint_cap); }; coin::create_coin_conversion_map(&aptos_framework); coin::create_pairing(&aptos_framework); } #[test_only] public fun initialize_for_test(aptos_framework: &signer): (BurnCapability, MintCapability) { aggregator_factory::initialize_aggregator_factory_for_test(aptos_framework); let (burn_cap, mint_cap) = initialize(aptos_framework); coin::create_coin_conversion_map(aptos_framework); coin::create_pairing(aptos_framework); (burn_cap, mint_cap) } // This is particularly useful if the aggregator_factory is already initialized via another call path. #[test_only] public fun initialize_for_test_without_aggregator_factory( aptos_framework: &signer ): (BurnCapability, MintCapability) { let (burn_cap, mint_cap) = initialize(aptos_framework); coin::create_coin_conversion_map(aptos_framework); coin::create_pairing(aptos_framework); (burn_cap, mint_cap) } }