-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDripFeed.sol
More file actions
102 lines (83 loc) · 3.74 KB
/
Copy pathDripFeed.sol
File metadata and controls
102 lines (83 loc) · 3.74 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC20 {
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
function transfer(address recipient, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}
contract DripFeed {
address public owner;
address public usdcToken;
struct Allowance {
uint256 amountPerInterval;
uint256 interval;
uint256 lastWithdrawn;
uint256 deposited;
bool active;
}
mapping(address => mapping(address => Allowance)) public allowances;
mapping(address => uint256) public totalDeposited;
mapping(address => uint256) public totalWithdrawn;
event AllowanceCreated(address indexed sender, address indexed recipient, uint256 amountPerInterval, uint256 interval);
event Dripped(address indexed recipient, address indexed sender, uint256 amount);
event Withdrawn(address indexed recipient, address indexed sender, uint256 amount);
event AllowanceCancelled(address indexed sender, address indexed recipient);
modifier onlyOwner() {
require(msg.sender == owner, "Only owner");
_;
}
constructor(address _usdcToken) {
require(_usdcToken != address(0), "Invalid USDC");
owner = msg.sender;
usdcToken = _usdcToken;
}
function createAllowance(address recipient, uint256 amountPerInterval, uint256 interval) public {
require(recipient != address(0), "Invalid recipient");
require(recipient != msg.sender, "Cannot allow yourself");
require(amountPerInterval > 0, "Amount must be > 0");
require(interval > 0, "Interval must be > 0");
Allowance storage a = allowances[msg.sender][recipient];
require(!a.active, "Allowance exists");
IERC20(usdcToken).transferFrom(msg.sender, address(this), amountPerInterval);
a.amountPerInterval = amountPerInterval;
a.interval = interval;
a.lastWithdrawn = block.timestamp;
a.deposited = amountPerInterval;
a.active = true;
totalDeposited[msg.sender] += amountPerInterval;
emit AllowanceCreated(msg.sender, recipient, amountPerInterval, interval);
}
function drip(address sender) public {
Allowance storage a = allowances[sender][msg.sender];
require(a.active, "No allowance");
uint256 available = availableToWithdraw(sender, msg.sender);
require(available > 0, "Nothing to withdraw yet");
a.lastWithdrawn = block.timestamp;
a.deposited -= available;
totalWithdrawn[msg.sender] += available;
IERC20(usdcToken).transfer(msg.sender, available);
emit Dripped(msg.sender, sender, available);
emit Withdrawn(msg.sender, sender, available);
}
function availableToWithdraw(address sender, address recipient) public view returns (uint256) {
Allowance storage a = allowances[sender][recipient];
if (!a.active) return 0;
uint256 intervalsPassed = (block.timestamp - a.lastWithdrawn) / a.interval;
uint256 amount = intervalsPassed * a.amountPerInterval;
if (amount > a.deposited) amount = a.deposited;
return amount;
}
function cancelAllowance(address recipient) public {
Allowance storage a = allowances[msg.sender][recipient];
require(a.active, "No allowance");
uint256 remaining = a.deposited;
a.active = false;
if (remaining > 0) {
IERC20(usdcToken).transfer(msg.sender, remaining);
}
emit AllowanceCancelled(msg.sender, recipient);
}
function getContractBalance() public view returns (uint256) {
return IERC20(usdcToken).balanceOf(address(this));
}
}