-
Notifications
You must be signed in to change notification settings - Fork 180
/
Uint96.sol
78 lines (66 loc) · 2.44 KB
/
Uint96.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
// Copyright (C) 2017, 2018, 2019, 2020 dbrock, rain, mrchico, d-xo
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.6.12;
contract Uint96ERC20 {
// --- ERC20 Data ---
string public constant name = "Token";
string public constant symbol = "TKN";
uint8 public decimals = 18;
uint96 internal supply;
mapping (address => uint96) internal balances;
mapping (address => mapping (address => uint96)) internal allowances;
event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
// --- Math ---
function add(uint96 x, uint96 y) internal pure returns (uint96 z) {
require((z = x + y) >= x);
}
function sub(uint96 x, uint96 y) internal pure returns (uint96 z) {
require((z = x - y) <= x);
}
function safe96(uint256 n) internal pure returns (uint96) {
require(n < 2**96);
return uint96(n);
}
// --- Init ---
constructor(uint96 _supply) public {
supply = _supply;
balances[msg.sender] = _supply;
emit Transfer(address(0), msg.sender, _supply);
}
// --- Getters ---
function totalSupply() external view returns (uint) {
return supply;
}
function balanceOf(address usr) external view returns (uint) {
return balances[usr];
}
function allowance(address src, address dst) external view returns (uint) {
return allowances[src][dst];
}
// --- Token ---
function transfer(address dst, uint wad) virtual public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}
function transferFrom(address src, address dst, uint wad) virtual public returns (bool) {
uint96 amt = safe96(wad);
if (src != msg.sender && allowances[src][msg.sender] != type(uint96).max) {
allowances[src][msg.sender] = sub(allowances[src][msg.sender], amt);
}
balances[src] = sub(balances[src], amt);
balances[dst] = add(balances[dst], amt);
emit Transfer(src, dst, wad);
return true;
}
function approve(address usr, uint wad) virtual public returns (bool) {
uint96 amt;
if (wad == type(uint).max) {
amt = type(uint96).max;
} else {
amt = safe96(wad);
}
allowances[msg.sender][usr] = amt;
emit Approval(msg.sender, usr, amt);
return true;
}
}