Skip to content

Commit 88264b6

Browse files
authored
1 parent 6b11071 commit 88264b6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

03_coin.sol

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-3.0
2+
pragma solidity ^0.8.4;
3+
4+
contract Coin {
5+
// The keyword "public" makes variables
6+
// accessible from other contracts
7+
address public minter;
8+
mapping (address => uint) public balances;
9+
10+
// Events allow clients to react to specific
11+
// contract changes you declare
12+
event Sent(address from, address to, uint amount);
13+
14+
// Constructor code is only run when the contract
15+
// is created
16+
constructor() {
17+
minter = msg.sender;
18+
}
19+
20+
// Sends an amount of newly created coins to an address
21+
// Can only be called by the contract creator
22+
function mint(address receiver, uint amount) public {
23+
require(msg.sender == minter);
24+
balances[receiver] += amount;
25+
}
26+
27+
// Errors allow you to provide information about
28+
// why an operation failed. They are returned
29+
// to the caller of the function.
30+
error InsufficientBalance(uint requested, uint available);
31+
32+
// Sends an amount of existing coins
33+
// from any caller to an address
34+
function send(address receiver, uint amount) public {
35+
if (amount > balances[msg.sender])
36+
revert InsufficientBalance({
37+
requested: amount,
38+
available: balances[msg.sender]
39+
});
40+
41+
balances[msg.sender] -= amount;
42+
balances[receiver] += amount;
43+
emit Sent(msg.sender, receiver, amount);
44+
}
45+
}

0 commit comments

Comments
 (0)