-
Notifications
You must be signed in to change notification settings - Fork 0
/
feeCoin.sol
120 lines (100 loc) · 3.71 KB
/
feeCoin.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
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
pragma solidity ^0.8.0;
contract TransferToken {
string private _name;
string private _symbol;
mapping(address => uint256) balances;
address public _owner;
uint256 public _totalSupply = 1000000 * 10**18;
uint256 public _decimals = 18;
uint256 public _transferFee;
event Transfer(address _from, address _to, uint256 _amount);
event TransferWithFee(address _from, uint256 _amount, uint256 _fee);
event ChangedFee(address _from, uint256 _previousFee, uint256 _newFee);
constructor(
string memory name_,
string memory symbol_,
uint256 _fee
) {
// Set name and symbol.
_name = name_;
_symbol = symbol_;
// Set owner as creator of the contract.
_owner = msg.sender;
// Allocate totalSupply to owner.
balances[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
// Set transfer fee.
changeTransferFee(_fee);
}
// ERC20 functionalities (will be overwritten by OpenZeppelin ERC20 contract in next version).
// Return name of the token.
function name() public view returns (string memory) {
return _name;
}
// Return symbol of the token.
function symbol() public view returns (string memory) {
return _symbol;
}
// Return totalSupply.
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
// Return the number of decimals. The OpenZeppelin default is 18 but we use 0 here.
function decimals() public view returns (uint256) {
return _decimals;
}
// Return the balance of an address.
function balanceOf(address _account) public view returns (uint256) {
return balances[_account];
}
function transfer(address _to, uint256 _amount) public returns (bool) {
_transferWithFee(msg.sender, _to, _amount);
return true;
}
function transferFrom(
address _from,
address _to,
uint256 _amount
) public returns (bool) {
_transferWithFee(_from, _to, _amount);
return true;
}
// Transfer tokens from one address to another. Includes a 1% fee mechanism.
function _transferWithFee(
address _from,
address _to,
uint256 _amount
) public {
require(
_to != address(0),
"ERROR transferWithFee: Cannot transfer to zero address."
);
require(_amount > 0, "ERROR transferWithFee: Invalid amount.");
// Calculate fee and add it to obtain the total amount of the tx.
uint256 transferFee = (_amount * _transferFee) / 100;
uint256 totalAmount = _amount + transferFee;
require(
balances[_from] >= totalAmount,
"ERROR transferWithFee: Balance insuficcient for transfer."
);
// Transfer _amount and send the fee to the contract.
balances[_from] -= totalAmount;
balances[_to] += _amount;
balances[address(this)] += transferFee;
// Add event incl. transfer and sending of fee to the contract.
emit Transfer(_from, _to, _amount);
// emit TransferWithFee(_from, _amount, transferFee);
}
// Set the transfer fee. Should be formatted as a percentage, e.g. 1 (i.e. 1%).
function changeTransferFee(uint256 _fee) public {
// Only the contract owner can set the fee. Can be changed later to a voting mechanism.
require(
_owner == msg.sender,
"ERROR setTransferFee: Function must be called by contract owner."
);
uint256 previousFee = _transferFee;
_transferFee = _fee;
// Add event.
emit ChangedFee(msg.sender, previousFee, _transferFee);
}
}