-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathFixedMoonToken.sol
79 lines (58 loc) · 2.63 KB
/
FixedMoonToken.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
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
// SPDX-License-Identifier: BSL-1.0 (Boost Software License 1.0)
//-------------------------------------------------------------------------------------//
// Copyright (c) 2022 - 2023 serial-coder: Phuwanai Thummavet ([email protected]) //
//-------------------------------------------------------------------------------------//
// For more info, please refer to my article:
// - On Medium: https://medium.com/valixconsulting/solidity-smart-contract-security-by-example-06-integer-overflow-e1f444f3cc4
// - On serial-coder.com: https://www.serial-coder.com/post/solidity-smart-contract-security-by-example-06-integer-overflow/
pragma solidity 0.6.12;
// Simplified SafeMath
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
}
contract FixedMoonToken {
using SafeMath for uint256;
mapping (address => uint256) private userBalances;
uint256 public constant TOKEN_PRICE = 1 ether;
string public constant name = "Moon Token";
string public constant symbol = "MOON";
// The token is non-divisible
// You can buy/sell 1, 2, 3, or 46 tokens but not 33.5
uint8 public constant decimals = 0;
function buy(uint256 _tokenToBuy) external payable {
require(
msg.value == _tokenToBuy.mul(TOKEN_PRICE), // FIX: Apply SafeMath
"Ether received and Token amount to buy mismatch"
);
userBalances[msg.sender] = userBalances[msg.sender].add(_tokenToBuy); // FIX: Apply SafeMath
}
function sell(uint256 _tokenToSell) external {
require(userBalances[msg.sender] >= _tokenToSell, "Insufficient balance");
userBalances[msg.sender] = userBalances[msg.sender].sub(_tokenToSell); // FIX: Apply SafeMath
(bool success, ) = msg.sender.call{value: _tokenToSell.mul(TOKEN_PRICE)}(""); // FIX: Apply SafeMath
require(success, "Failed to send Ether");
}
function getEtherBalance() external view returns (uint256) {
return address(this).balance;
}
function getUserBalance(address _user) external view returns (uint256) {
return userBalances[_user];
}
}