Skip to content

Commit

Permalink
Supra_coin test coverage improve (#54)
Browse files Browse the repository at this point in the history
* Fixed supra coin end_to_end test

* Mint overflow test case

* More test case and cleanup
  • Loading branch information
axiongsupra authored Sep 4, 2024
1 parent fb4a660 commit 28fa00c
Showing 1 changed file with 112 additions and 47 deletions.
159 changes: 112 additions & 47 deletions aptos-move/framework/supra-framework/sources/supra_coin.move
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module supra_framework::supra_coin {
use std::option::{Self, Option};

use supra_framework::coin::{Self, BurnCapability, MintCapability};
//use supra_framework::coin;
use supra_framework::system_addresses;

friend supra_framework::genesis;
Expand Down Expand Up @@ -197,6 +196,23 @@ module supra_framework::supra_coin {
(burn_cap, mint_cap)
}

#[test_only]
fun initialize_with_aggregator(supra_framework: &signer) {
let (burn_cap, freeze_cap, mint_cap) =coin::initialize_with_parallelizable_supply_with_limit<SupraCoin>(
supra_framework,
string::utf8(b"Supra Coin"),
string::utf8(b"SUP"),
8, // decimals
true, // monitor_supply
MAX_SUPRA_COIN_SUPPLY,
);
coin::destroy_freeze_cap(freeze_cap);
move_to(supra_framework, SupraCoinCapabilities {
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(
Expand All @@ -208,51 +224,100 @@ module supra_framework::supra_coin {
(burn_cap, mint_cap)
}

#[test_only]
struct SupraCoinCapabilities has key {
burn_cap: BurnCapability<SupraCoin>,
mint_cap: MintCapability<SupraCoin>,
}

#[test_only]
use supra_framework::supra_account;

// #[test(source = @0x1, destination = @0x2)]
// public entry fun end_to_end(
// source: signer,
// destination: signer,
// ) {
// let source_addr = signer::address_of(&source);
// account::create_account_for_test(source_addr);
// let destination_addr = signer::address_of(&destination);
// account::create_account_for_test(destination_addr);

// let name = string::utf8(b"Fake money");
// let symbol = string::utf8(b"FMD");

// aggregator_factory::initialize_aggregator_factory_for_test(&source);
// let (burn_cap, mint_cap) = initialize(
// &source,
// );
// coin::register<SupraCoin>(&source);
// coin::register<SupraCoin>(&destination);
// assert!(*option::borrow(&coin::supply<SupraCoin>()) == 0, 0);

// assert!(coin::name<SupraCoin>() == name, 1);
// assert!(coin::symbol<SupraCoin>() == symbol, 2);
// assert!(coin::decimals<SupraCoin>() == 18, 3);

// let coins_minted = coin::mint<SupraCoin>(100, &mint_cap);
// coin::deposit(source_addr, coins_minted);
// coin::transfer<SupraCoin>(&source, destination_addr, 50);

// assert!(coin::balance<SupraCoin>(source_addr) == 50, 4);
// assert!(coin::balance<SupraCoin>(destination_addr) == 50, 5);
// assert!(*option::borrow(&coin::supply<SupraCoin>()) == 100, 6);

// let coin = coin::withdraw<SupraCoin>(&source, 10);
// assert!(coin::value(&coin) == 10, 7);
// coin::burn(coin, &burn_cap);
// assert!(*option::borrow(&coin::supply<SupraCoin>()) == 90, 8);

// move_to(&source, SupraCoinCapabilities {
// burn_cap,
// mint_cap,
// });
// }
#[test(source = @0x1, destination = @0x2)]
public entry fun end_to_end(
source: signer,
destination: signer,
) {
let source_addr = signer::address_of(&source);
account::create_account_for_test(source_addr);
let destination_addr = signer::address_of(&destination);
account::create_account_for_test(destination_addr);

let name = string::utf8(b"Supra Coin");
let symbol = string::utf8(b"SUP");

aggregator_factory::initialize_aggregator_factory_for_test(&source);
let (burn_cap, mint_cap) = initialize(
&source,
);
coin::register<SupraCoin>(&source);
coin::register<SupraCoin>(&destination);
assert!(*option::borrow(&coin::supply<SupraCoin>()) == 0, 0);

assert!(coin::name<SupraCoin>() == name, 1);
assert!(coin::symbol<SupraCoin>() == symbol, 2);
assert!(coin::decimals<SupraCoin>() == 8, 3);

let coins_minted = coin::mint<SupraCoin>(100, &mint_cap);
coin::deposit(source_addr, coins_minted);
coin::transfer<SupraCoin>(&source, destination_addr, 50);

assert!(coin::balance<SupraCoin>(source_addr) == 50, 4);
assert!(coin::balance<SupraCoin>(destination_addr) == 50, 5);
assert!(*option::borrow(&coin::supply<SupraCoin>()) == 100, 6);

let coin = coin::withdraw<SupraCoin>(&source, 10);
assert!(coin::value(&coin) == 10, 7);
coin::burn(coin, &burn_cap);
assert!(*option::borrow(&coin::supply<SupraCoin>()) == 90, 8);

move_to(&source, SupraCoinCapabilities {
burn_cap,
mint_cap,
});
}

#[test(source = @0x1, destination = @0x2)]
public entry fun test_mint_no_overflow(
source: signer,
destination: signer,
){
let source_addr = signer::address_of(&source);
account::create_account_for_test(source_addr);
let destination_addr = signer::address_of(&destination);
account::create_account_for_test(destination_addr);

aggregator_factory::initialize_aggregator_factory_for_test(&source);
let (burn_cap, mint_cap) = initialize(
&source,
);
coin::register<SupraCoin>(&source);
coin::register<SupraCoin>(&destination);
assert!(*option::borrow(&coin::supply<SupraCoin>()) == 0, 0);
assert!(*option::borrow(&coin::supply<SupraCoin>()) == 0, 0);

let coins_minted = coin::mint<SupraCoin>((MAX_SUPRA_COIN_SUPPLY as u64), &mint_cap);
coin::deposit(source_addr, coins_minted);
coin::transfer<SupraCoin>(&source, destination_addr, (MAX_SUPRA_COIN_SUPPLY as u64));
coin::destroy_burn_cap(burn_cap);
coin::destroy_mint_cap(mint_cap);
}

#[test(source = @0x1)]
#[expected_failure(abort_code = 0x20001, location = supra_framework::aggregator)]
public entry fun test_mint_overflow(
source: signer,
) {
let source_addr = signer::address_of(&source);
account::create_account_for_test(source_addr);

aggregator_factory::initialize_aggregator_factory_for_test(&source);
let (burn_cap, mint_cap) = initialize(
&source,
);
coin::register<SupraCoin>(&source);
assert!(*option::borrow(&coin::supply<SupraCoin>()) == 0, 0);

let coins_minted = coin::mint<SupraCoin>((MAX_SUPRA_COIN_SUPPLY as u64)+1, &mint_cap);
coin::deposit(source_addr, coins_minted);
coin::destroy_burn_cap(burn_cap);
coin::destroy_mint_cap(mint_cap);
}
}

0 comments on commit 28fa00c

Please sign in to comment.