-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin-solution.sol
45 lines (34 loc) · 1.46 KB
/
coin-solution.sol
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
pragma solidity >=0.7.0 <0.9.0;
// Only the creator can mint coins
// Anyone can send coins to each other without registering (only need address & amount)
contract Coin {
//the keyword public is making the variables here accessible from other contracts
address public minter;
mapping(address => uint) public balances;
uint public totalSupply;
event Sent(address from, address to, uint amount);
// constructor only runs upon deployment
constructor() {
minter = msg.sender;
totalSupply = 100;
}
// make new coins and send them to an address
// make sure only an owner can send the coins
function mint(address receiver, uint amount) public {
require(msg.sender == minter); // only allow contract creator to mint
balances[receiver] += amount; // increase receiver balance
totalSupply += amount; // increase the total supply
}
// send any amount of coins to an existing address
error insufficientBalance(uint request, uint available);
function send(address receiver, uint amount) public {
if (amount > balances[msg.sender])
revert insufficientBalance({
request: amount,
available: balances[msg.sender]
});
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}