-
Notifications
You must be signed in to change notification settings - Fork 175
/
TransferFromSelf.sol
62 lines (54 loc) · 2.12 KB
/
TransferFromSelf.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
// Copyright (C) 2017, 2018, 2019, 2020 dbrock, rain, mrchico, d-xo
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.6.12;
contract Math {
// --- Math ---
function add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x);
}
function sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x);
}
}
contract TransferFromSelfToken is Math {
// --- ERC20 Data ---
string public constant name = "Token";
string public constant symbol = "TKN";
uint8 public constant decimals = 18;
uint256 public totalSupply;
mapping (address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
// --- Init ---
constructor(uint _totalSupply) public {
totalSupply = _totalSupply;
balanceOf[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
// --- Token ---
function transfer(address dst, uint wad) virtual public returns (bool) {
_transfer(msg.sender, dst, wad);
return true;
}
function transferFrom(address src, address dst, uint wad) virtual public returns (bool) {
if (allowance[src][msg.sender] != type(uint).max) {
require(allowance[src][msg.sender] >= wad, "insufficient-allowance");
allowance[src][msg.sender] = sub(allowance[src][msg.sender], wad);
}
_transfer(src, dst, wad);
return true;
}
function approve(address usr, uint wad) virtual public returns (bool) {
allowance[msg.sender][usr] = wad;
emit Approval(msg.sender, usr, wad);
return true;
}
// --- Internal ---
function _transfer(address src, address dst, uint wad) private {
require(balanceOf[src] >= wad, "insufficient-balance");
balanceOf[src] = sub(balanceOf[src], wad);
balanceOf[dst] = add(balanceOf[dst], wad);
emit Transfer(src, dst, wad);
}
}