-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQPaygift.sol
135 lines (111 loc) · 4.19 KB
/
QPaygift.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
pragma solidity 0.4.18;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}
contract ERC20TokenInterface {
function totalSupply() constant public returns (uint256 supply);
function balanceOf(address _owner) constant public returns (uint256 balance);
function transfer(address _to, uint256 _value) public returns (bool success);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
function approve(address _spender, uint256 _value) public returns (bool success);
function allowance(address _owner, address _spender) constant public returns (uint256 remaining);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract ERC20Faucet {
using SafeMath for uint256;
uint256 public maxAllowanceInclusive;
mapping (address => uint256) public claimedTokens;
ERC20TokenInterface public erc20Contract;
address private mOwner;
bool private mIsPaused = false;
bool private mReentrancyLock = false;
event GetTokens(address requestor, uint256 amount);
event ReclaimTokens(address owner, uint256 tokenAmount);
event SetPause(address setter, bool newState, bool oldState);
event SetMaxAllowance(address setter, uint256 newState, uint256 oldState);
modifier notPaused() {
require(!mIsPaused);
_;
}
modifier onlyOwner() {
require(msg.sender == mOwner);
_;
}
modifier nonReentrant() {
require(!mReentrancyLock);
mReentrancyLock = true;
_;
mReentrancyLock = false;
}
function ERC20Faucet(ERC20TokenInterface _erc20ContractAddress, uint256 _maxAllowanceInclusive) public {
mOwner = msg.sender;
maxAllowanceInclusive = _maxAllowanceInclusive;
erc20Contract = _erc20ContractAddress;
}
function getTokens(uint256 amount) notPaused nonReentrant public returns (bool) {
require(claimedTokens[msg.sender].add(amount) <= maxAllowanceInclusive);
require(erc20Contract.balanceOf(this) >= amount);
claimedTokens[msg.sender] = claimedTokens[msg.sender].add(amount);
if (!erc20Contract.transfer(msg.sender, amount)) {
claimedTokens[msg.sender] = claimedTokens[msg.sender].sub(amount);
return false;
}
GetTokens(msg.sender, amount);
return true;
}
function setMaxAllowance(uint256 _maxAllowanceInclusive) onlyOwner nonReentrant public {
SetMaxAllowance(msg.sender, _maxAllowanceInclusive, maxAllowanceInclusive);
maxAllowanceInclusive = _maxAllowanceInclusive;
}
function reclaimTokens() onlyOwner nonReentrant public returns (bool) {
uint256 tokenBalance = erc20Contract.balanceOf(this);
if (!erc20Contract.transfer(msg.sender, tokenBalance)) {
return false;
}
ReclaimTokens(msg.sender, tokenBalance);
return true;
}
function setPause(bool isPaused) onlyOwner nonReentrant public {
SetPause(msg.sender, isPaused, mIsPaused);
mIsPaused = isPaused;
}
}