Skip to content

Commit

Permalink
[AptosFramework] add coin info
Browse files Browse the repository at this point in the history
  • Loading branch information
zekun000 committed Mar 10, 2022
1 parent 05ef4d2 commit f9ea551
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ compiled_package_info:
? address: "00000000000000000000000000000001"
name: Vector
: Std
source_digest: 998470F4180D63C7369BAC79E3D9C99418C8FACFDF0FEF49F4A174A779D88202
source_digest: 593B60A1D491957F4A0D5328AA1E7520E44F697120B95D3A3E16EEC791B706F5
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<a href="AptosValidatorConfig.md#0x1_AptosValidatorConfig_initialize">AptosValidatorConfig::initialize</a>(core_resource_account);
<a href="AptosValidatorOperatorConfig.md#0x1_AptosValidatorOperatorConfig_initialize">AptosValidatorOperatorConfig::initialize</a>(core_resource_account);

<a href="TestCoin.md#0x1_TestCoin_initialize">TestCoin::initialize</a>(core_resource_account);
<a href="TestCoin.md#0x1_TestCoin_initialize">TestCoin::initialize</a>(core_resource_account, 1000000);

// Pad the event counter for the Diem Root account <b>to</b> match DPN. This
// _MUST_ match the new epoch event counter otherwise all manner of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ modified from https://github.com/diem/move/tree/main/language/documentation/tuto

- [Struct `Coin`](#0x1_TestCoin_Coin)
- [Resource `Balance`](#0x1_TestCoin_Balance)
- [Resource `CoinInfo`](#0x1_TestCoin_CoinInfo)
- [Resource `MintCapability`](#0x1_TestCoin_MintCapability)
- [Resource `BurnCapability`](#0x1_TestCoin_BurnCapability)
- [Constants](#@Constants_0)
Expand All @@ -22,6 +23,8 @@ modified from https://github.com/diem/move/tree/main/language/documentation/tuto
- [Function `burn`](#0x1_TestCoin_burn)
- [Function `burn_with_capability`](#0x1_TestCoin_burn_with_capability)
- [Function `burn_gas`](#0x1_TestCoin_burn_gas)
- [Function `total_supply`](#0x1_TestCoin_total_supply)
- [Function `scaling_factor`](#0x1_TestCoin_scaling_factor)


<pre><code><b>use</b> <a href="../../../../../../../aptos-framework/releases/artifacts/current/build/MoveStdlib/docs/Errors.md#0x1_Errors">0x1::Errors</a>;
Expand Down Expand Up @@ -84,6 +87,40 @@ Struct representing the balance of each address.
</dl>


</details>

<a name="0x1_TestCoin_CoinInfo"></a>

## Resource `CoinInfo`

Represnets the metadata of the coin, store @CoreResources.


<pre><code><b>struct</b> <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> <b>has</b> key
</code></pre>



<details>
<summary>Fields</summary>


<dl>
<dt>
<code>total_value: u128</code>
</dt>
<dd>

</dd>
<dt>
<code>scaling_factor: u64</code>
</dt>
<dd>

</dd>
</dl>


</details>

<a name="0x1_TestCoin_MintCapability"></a>
Expand Down Expand Up @@ -179,7 +216,7 @@ Error codes



<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_initialize">initialize</a>(core_resource: &signer)
<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_initialize">initialize</a>(core_resource: &signer, scaling_factor: u64)
</code></pre>


Expand All @@ -188,10 +225,11 @@ Error codes
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_initialize">initialize</a>(core_resource: &signer) {
<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_initialize">initialize</a>(core_resource: &signer, scaling_factor: u64) {
<a href="../../../../../../../aptos-framework/releases/artifacts/current/build/CoreFramework/docs/SystemAddresses.md#0x1_SystemAddresses_assert_core_resource">SystemAddresses::assert_core_resource</a>(core_resource);
<b>move_to</b>(core_resource, <a href="TestCoin.md#0x1_TestCoin_MintCapability">MintCapability</a> {});
<b>move_to</b>(core_resource, <a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a> {});
<b>move_to</b>(core_resource, <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> { total_value: 0, scaling_factor });
<a href="TestCoin.md#0x1_TestCoin_register">register</a>(core_resource);
}
</code></pre>
Expand Down Expand Up @@ -244,10 +282,14 @@ Mint coins with capability.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_mint">mint</a>(account: &signer, mint_addr: <b>address</b>, amount: u64) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_Balance">Balance</a>, <a href="TestCoin.md#0x1_TestCoin_MintCapability">MintCapability</a> {
<b>let</b> _cap = <b>borrow_global</b>&lt;<a href="TestCoin.md#0x1_TestCoin_MintCapability">MintCapability</a>&gt;(<a href="../../../../../../../aptos-framework/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(account));
<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_mint">mint</a>(account: &signer, mint_addr: <b>address</b>, amount: u64) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_Balance">Balance</a>, <a href="TestCoin.md#0x1_TestCoin_MintCapability">MintCapability</a>, <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> {
<b>let</b> sender_addr = <a href="../../../../../../../aptos-framework/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(account);
<b>let</b> _cap = <b>borrow_global</b>&lt;<a href="TestCoin.md#0x1_TestCoin_MintCapability">MintCapability</a>&gt;(sender_addr);
// Deposit `amount` of tokens <b>to</b> `mint_addr`'s balance
<a href="TestCoin.md#0x1_TestCoin_deposit">deposit</a>(mint_addr, <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a> { value: amount });
// Update the total supply
<b>let</b> coin_info = <b>borrow_global_mut</b>&lt;<a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a>&gt;(sender_addr);
coin_info.total_value = coin_info.total_value + (amount <b>as</b> u128);
}
</code></pre>

Expand Down Expand Up @@ -382,7 +424,7 @@ Burn coins with capability.
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_burn">burn</a>(account: &signer, coins: <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a>) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a> {
<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_burn">burn</a>(account: &signer, coins: <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a>) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a>, <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> {
<b>let</b> cap = <b>borrow_global</b>&lt;<a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a>&gt;(<a href="../../../../../../../aptos-framework/releases/artifacts/current/build/MoveStdlib/docs/Signer.md#0x1_Signer_address_of">Signer::address_of</a>(account));
<a href="TestCoin.md#0x1_TestCoin_burn_with_capability">burn_with_capability</a>(coins, cap);
}
Expand All @@ -407,8 +449,11 @@ Burn coins with capability.
<summary>Implementation</summary>


<pre><code><b>fun</b> <a href="TestCoin.md#0x1_TestCoin_burn_with_capability">burn_with_capability</a>(coins: <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a>, _cap: &<a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a>) {
<b>let</b> <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a> { value: _value } = coins;
<pre><code><b>fun</b> <a href="TestCoin.md#0x1_TestCoin_burn_with_capability">burn_with_capability</a>(coins: <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a>, _cap: &<a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a>) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> {
<b>let</b> <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a> { value } = coins;
// Update the total supply
<b>let</b> coin_info = <b>borrow_global_mut</b>&lt;<a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a>&gt;(@CoreResources);
coin_info.total_value = coin_info.total_value - (value <b>as</b> u128);
}
</code></pre>

Expand All @@ -432,12 +477,61 @@ Burn transaction gas.
<summary>Implementation</summary>


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_burn_gas">burn_gas</a>(fee: <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a>) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a> {
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_burn_gas">burn_gas</a>(fee: <a href="TestCoin.md#0x1_TestCoin_Coin">Coin</a>) <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a>, <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> {
<b>let</b> cap = <b>borrow_global</b>&lt;<a href="TestCoin.md#0x1_TestCoin_BurnCapability">BurnCapability</a>&gt;(@CoreResources);
<a href="TestCoin.md#0x1_TestCoin_burn_with_capability">burn_with_capability</a>(fee, cap);
}
</code></pre>



</details>

<a name="0x1_TestCoin_total_supply"></a>

## Function `total_supply`

Get the current total supply of the coin.


<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_total_supply">total_supply</a>(): u128
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_total_supply">total_supply</a>(): u128 <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> {
<b>borrow_global</b>&lt;<a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a>&gt;(@CoreResources).total_value
}
</code></pre>



</details>

<a name="0x1_TestCoin_scaling_factor"></a>

## Function `scaling_factor`



<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_scaling_factor">scaling_factor</a>(): u64
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="TestCoin.md#0x1_TestCoin_scaling_factor">scaling_factor</a>(): u64 <b>acquires</b> <a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a> {
<b>borrow_global</b>&lt;<a href="TestCoin.md#0x1_TestCoin_CoinInfo">CoinInfo</a>&gt;(@CoreResources).scaling_factor
}
</code></pre>



</details>
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module AptosFramework::Genesis {
AptosValidatorConfig::initialize(core_resource_account);
AptosValidatorOperatorConfig::initialize(core_resource_account);

TestCoin::initialize(core_resource_account);
TestCoin::initialize(core_resource_account, 1000000);

// Pad the event counter for the Diem Root account to match DPN. This
// _MUST_ match the new epoch event counter otherwise all manner of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@ module AptosFramework::TestCoin {
coin: Coin
}

/// Represnets the metadata of the coin, store @CoreResources.
struct CoinInfo has key {
total_value: u128,
scaling_factor: u64,
}

struct MintCapability has key, store { }
struct BurnCapability has key, store { }

public fun initialize(core_resource: &signer) {
public fun initialize(core_resource: &signer, scaling_factor: u64) {
SystemAddresses::assert_core_resource(core_resource);
move_to(core_resource, MintCapability {});
move_to(core_resource, BurnCapability {});
move_to(core_resource, CoinInfo { total_value: 0, scaling_factor });
register(core_resource);
}

Expand All @@ -41,10 +48,14 @@ module AptosFramework::TestCoin {
}

/// Mint coins with capability.
public fun mint(account: &signer, mint_addr: address, amount: u64) acquires Balance, MintCapability {
let _cap = borrow_global<MintCapability>(Signer::address_of(account));
public fun mint(account: &signer, mint_addr: address, amount: u64) acquires Balance, MintCapability, CoinInfo {
let sender_addr = Signer::address_of(account);
let _cap = borrow_global<MintCapability>(sender_addr);
// Deposit `amount` of tokens to `mint_addr`'s balance
deposit(mint_addr, Coin { value: amount });
// Update the total supply
let coin_info = borrow_global_mut<CoinInfo>(sender_addr);
coin_info.total_value = coin_info.total_value + (amount as u128);
}

/// Returns the balance of `owner`.
Expand Down Expand Up @@ -79,24 +90,36 @@ module AptosFramework::TestCoin {
}

/// Burn coins with capability.
public fun burn(account: &signer, coins: Coin) acquires BurnCapability {
public fun burn(account: &signer, coins: Coin) acquires BurnCapability, CoinInfo {
let cap = borrow_global<BurnCapability>(Signer::address_of(account));
burn_with_capability(coins, cap);
}

fun burn_with_capability(coins: Coin, _cap: &BurnCapability) {
let Coin { value: _value } = coins;
fun burn_with_capability(coins: Coin, _cap: &BurnCapability) acquires CoinInfo {
let Coin { value } = coins;
// Update the total supply
let coin_info = borrow_global_mut<CoinInfo>(@CoreResources);
coin_info.total_value = coin_info.total_value - (value as u128);
}

/// Burn transaction gas.
public(friend) fun burn_gas(fee: Coin) acquires BurnCapability {
public(friend) fun burn_gas(fee: Coin) acquires BurnCapability, CoinInfo {
let cap = borrow_global<BurnCapability>(@CoreResources);
burn_with_capability(fee, cap);
}

/// Get the current total supply of the coin.
public fun total_supply(): u128 acquires CoinInfo {
borrow_global<CoinInfo>(@CoreResources).total_value
}

public fun scaling_factor(): u64 acquires CoinInfo {
borrow_global<CoinInfo>(@CoreResources).scaling_factor
}

#[test(account = @0x1)]
#[expected_failure] // This test should abort
fun mint_non_owner(account: signer) acquires Balance, MintCapability {
fun mint_non_owner(account: signer) acquires Balance, MintCapability, CoinInfo {
// Make sure the address we've chosen doesn't match the module
// owner address
register(&account);
Expand All @@ -105,11 +128,12 @@ module AptosFramework::TestCoin {
}

#[test(account = @CoreResources)]
fun mint_check_balance(account: signer) acquires Balance, MintCapability {
initialize(&account);
fun mint_check_balance_and_supply(account: signer) acquires Balance, MintCapability, CoinInfo {
initialize(&account, 1000000);
let addr = Signer::address_of(&account);
mint(&account, @CoreResources, 42);
assert!(balance_of(addr) == 42, 0);
assert!(total_supply() == 42, 0);
}

#[test(account = @0x1)]
Expand Down Expand Up @@ -147,8 +171,8 @@ module AptosFramework::TestCoin {
}

#[test(account = @CoreResources)]
fun can_withdraw_amount(account: signer) acquires Balance, MintCapability {
initialize(&account);
fun can_withdraw_amount(account: signer) acquires Balance, MintCapability, CoinInfo {
initialize(&account, 1000000);
let amount = 1000;
let addr = Signer::address_of(&account);
mint(&account, addr, amount);
Expand All @@ -157,28 +181,30 @@ module AptosFramework::TestCoin {
}

#[test(account = @CoreResources)]
fun successful_burn(account: signer) acquires Balance, MintCapability, BurnCapability {
initialize(&account);
fun successful_burn(account: signer) acquires Balance, MintCapability, BurnCapability, CoinInfo {
initialize(&account, 1000000);
let amount = 1000;
let addr = Signer::address_of(&account);
mint(&account, addr, amount);
burn(&account, withdraw(&account, 100));
assert!(balance_of(addr) == 900, 0);
assert!(total_supply() == 900, 0);
}

#[test(account = @CoreResources, another = @0x1)]
#[expected_failure]
fun failed_burn(account: signer, another: signer) acquires Balance, MintCapability, BurnCapability {
initialize(&account);
fun failed_burn(account: signer, another: signer) acquires Balance, MintCapability, BurnCapability, CoinInfo {
initialize(&account, 1000000);
let amount = 1000;
let addr = Signer::address_of(&another);
mint(&account, addr, amount);
burn(&another, withdraw(&another, 100));
assert!(total_supply() == 1000, 0);
}

#[test(account = @CoreResources, receiver = @0x1)]
fun transfer_test(account: signer, receiver: signer) acquires Balance, MintCapability {
initialize(&account);
fun transfer_test(account: signer, receiver: signer) acquires Balance, MintCapability, CoinInfo {
initialize(&account, 1000000);
register(&receiver);
let amount = 1000;
let addr = Signer::address_of(&account);
Expand All @@ -188,5 +214,6 @@ module AptosFramework::TestCoin {
transfer(&account, addr1, 400);
assert!(balance_of(addr) == 600, 0);
assert!(balance_of(addr1) == 400, 0);
assert!(total_supply() == 1000, 0);
}
}
2 changes: 1 addition & 1 deletion aptos-move/framework/aptos-framework/sources/Genesis.move
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module AptosFramework::Genesis {
AptosValidatorConfig::initialize(core_resource_account);
AptosValidatorOperatorConfig::initialize(core_resource_account);

TestCoin::initialize(core_resource_account);
TestCoin::initialize(core_resource_account, 1000000);

// Pad the event counter for the Diem Root account to match DPN. This
// _MUST_ match the new epoch event counter otherwise all manner of
Expand Down
Loading

0 comments on commit f9ea551

Please sign in to comment.