forked from move-language/move
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCoin.move
126 lines (107 loc) · 4.6 KB
/
BasicCoin.move
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/// This module defines a minimal Coin and Balance.
module NamedAddr::BasicCoin {
use std::signer;
/// Address of the owner of this module
const MODULE_OWNER: address = @NamedAddr;
/// Error codes
const ENOT_MODULE_OWNER: u64 = 0;
const EINSUFFICIENT_BALANCE: u64 = 1;
const EALREADY_HAS_BALANCE: u64 = 2;
struct Coin has store {
value: u64
}
/// Struct representing the balance of each address.
struct Balance has key {
coin: Coin
}
/// Publish an empty balance resource under `account`'s address. This function must be called before
/// minting or transferring to the account.
public fun publish_balance(account: &signer) {
let empty_coin = Coin { value: 0 };
assert!(!exists<Balance>(signer::address_of(account)), EALREADY_HAS_BALANCE);
move_to(account, Balance { coin: empty_coin });
}
/// Mint `amount` tokens to `mint_addr`. Mint must be approved by the module owner.
public fun mint(module_owner: &signer, mint_addr: address, amount: u64) acquires Balance {
// Only the owner of the module can initialize this module
assert!(signer::address_of(module_owner) == MODULE_OWNER, ENOT_MODULE_OWNER);
// Deposit `amount` of tokens to `mint_addr`'s balance
deposit(mint_addr, Coin { value: amount });
}
/// Returns the balance of `owner`.
public fun balance_of(owner: address): u64 acquires Balance {
borrow_global<Balance>(owner).coin.value
}
/// Transfers `amount` of tokens from `from` to `to`.
public fun transfer(from: &signer, to: address, amount: u64) acquires Balance {
let check = withdraw(signer::address_of(from), amount);
deposit(to, check);
}
/// Withdraw `amount` number of tokens from the balance under `addr`.
fun withdraw(addr: address, amount: u64) : Coin acquires Balance {
let balance = balance_of(addr);
// balance must be greater than the withdraw amount
assert!(balance >= amount, EINSUFFICIENT_BALANCE);
let balance_ref = &mut borrow_global_mut<Balance>(addr).coin.value;
*balance_ref = balance - amount;
Coin { value: amount }
}
/// Deposit `amount` number of tokens to the balance under `addr`.
fun deposit(addr: address, check: Coin) acquires Balance{
let balance = balance_of(addr);
let balance_ref = &mut borrow_global_mut<Balance>(addr).coin.value;
let Coin { value } = check;
*balance_ref = balance + value;
}
#[test(account = @0x1)] // Creates a signer for the `account` argument with address `@0x1`
#[expected_failure] // This test should abort
fun mint_non_owner(account: signer) acquires Balance {
// Make sure the address we've chosen doesn't match the module
// owner address
publish_balance(&account);
assert!(signer::address_of(&account) != MODULE_OWNER, 0);
mint(&account, @0x1, 10);
}
#[test(account = @NamedAddr)] // Creates a signer for the `account` argument with the value of the named address `NamedAddr`
fun mint_check_balance(account: signer) acquires Balance {
let addr = signer::address_of(&account);
publish_balance(&account);
mint(&account, @NamedAddr, 42);
assert!(balance_of(addr) == 42, 0);
}
#[test(account = @0x1)]
fun publish_balance_has_zero(account: signer) acquires Balance {
let addr = signer::address_of(&account);
publish_balance(&account);
assert!(balance_of(addr) == 0, 0);
}
#[test(account = @0x1)]
#[expected_failure(abort_code = EALREADY_HAS_BALANCE)] // Can specify an abort code
fun publish_balance_already_exists(account: signer) {
publish_balance(&account);
publish_balance(&account);
}
// EXERCISE: Write `balance_of_dne` test here!
#[test]
#[expected_failure]
fun withdraw_dne() acquires Balance {
// Need to unpack the coin since `Coin` is a resource
Coin { value: _ } = withdraw(@0x1, 0);
}
#[test(account = @0x1)]
#[expected_failure] // This test should fail
fun withdraw_too_much(account: signer) acquires Balance {
let addr = signer::address_of(&account);
publish_balance(&account);
Coin { value: _ } = withdraw(addr, 1);
}
#[test(account = @NamedAddr)]
fun can_withdraw_amount(account: signer) acquires Balance {
publish_balance(&account);
let amount = 1000;
let addr = signer::address_of(&account);
mint(&account, addr, amount);
let Coin { value } = withdraw(addr, amount);
assert!(value == amount, 0);
}
}