-
Notifications
You must be signed in to change notification settings - Fork 5
/
Faucet.sol
82 lines (62 loc) · 2.53 KB
/
Faucet.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
// SPDX-License-Identifier: CC-BY-SA-4.0
// Version of Solidity compiler this program was written for
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "../interfaces/WalletValidator.sol";
contract Faucet is Ownable
{
address[] public validators;
uint256 immutable public participantRetryTime;
uint256 immutable public maxDistributionPerGrant;
mapping (address => uint256) private participants;
event Funding(string scenario, uint256 amount);
receive() external payable
{
// Allow contract to receive award funds.
}
constructor(uint256 participantRetryTimeIn, uint256 maxDistributionPerGrantIn, address[] memory inputValidators) payable
{
participantRetryTime = participantRetryTimeIn;
maxDistributionPerGrant = maxDistributionPerGrantIn;
configureValidators(inputValidators);
}
function configureValidators(address[] memory inputValidators) public onlyOwner
{
validators = inputValidators;
}
function calculatePayout(uint8 score, uint8 fromTotal) public view returns (uint256)
{
return (score * maxDistributionPerGrant) / fromTotal;
}
function grant(address payable recipient, uint8 score, uint8 fromTotal) public onlyOwner
{
validate(recipient);
require(getElapsedTime(recipient) > participantRetryTime, "Retry cooldown not met.");
uint256 transferAmount = calculatePayout(score, fromTotal);
uint256 currentBalance = address(this).balance;
require(currentBalance > 0, "Contract has zero token balance");
require(transferAmount <= currentBalance, "Not enough balance available in the contract.");
if(score > 0)
{
recipient.transfer(transferAmount);
}
participants[recipient] = block.timestamp;
}
function validate(address recipient) view public
{
for(uint8 i = 0; i < validators.length; ++i)
{
WalletValidator val = WalletValidator(validators[i]);
require(val.Validate(recipient), "Recipient does not qualify");
}
}
function getElapsedTime(address participant) public view returns (uint256)
{
return(block.timestamp - participants[participant]);
}
function recoverFunds() public onlyOwner
{
emit Funding("Contract drained and sent to contract owner.", address(this).balance);
payable(address(msg.sender)).transfer(address(this).balance);
}
}