-
Notifications
You must be signed in to change notification settings - Fork 1
/
MerkleRedeem.sol
206 lines (187 loc) · 6.42 KB
/
MerkleRedeem.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-License-Identifier: MIT
/**
* Original code taken from: https://github.com/balancer-labs/erc20-redeemable/blob/13d478a043ec7bfce7abefe708d027dfe3e2ea84/merkle/contracts/MerkleRedeem.sol
* Only comments and events were added, some variable names changed for clarity and the compiler version was upgraded to 0.7.x.
*
* @reviewers: [@hbarcelos]
* @auditors: []
* @bounties: []
* @deployments: []
*/
pragma solidity 0.6.8;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/**
* @title Distribution of tokens in a recurrent fashion.
*/
contract MerkleRedeem is Ownable {
/// @dev The address of the token being distributed.
IERC20 public token;
/**
* @dev To be emitted when a claim is made.
* @param _claimant The address of the claimant.
* @param _balance The amount being claimed.
*/
event Claimed(address _claimant, uint256 _balance);
/// @dev The merkle roots of each week. weekMerkleRoots[week].
mapping(uint => bytes32) public weekMerkleRoots;
/// @dev Keeps track of the claim status for the given period and claimant. claimed[period][claimant].
mapping(uint => mapping(address => bool)) public claimed;
/**
* @param _token The address of the token being distributed.
*/
constructor(
address _token
) public {
token = IERC20(_token);
}
/**
* @dev Effectively pays a claimant.
* @param _liquidityProvider The address of the claimant.
* @param _balance The amount being claimed.
*/
function disburse(
address _liquidityProvider,
uint _balance
)
private
{
if (_balance > 0) {
emit Claimed(_liquidityProvider, _balance);
require(token.transfer(_liquidityProvider, _balance), "ERR_TRANSFER_FAILED");
}
}
/**
* @notice Makes a claim for a given claimant in a week.
* @param _liquidityProvider The address of the claimant.
* @param _week The week for the claim.
* @param _claimedBalance The amount being claimed.
* @param _merkleProof The merkle proof for the claim, sorted from the leaf to the root of the tree.
*/
function claimWeek(
address _liquidityProvider,
uint _week,
uint _claimedBalance,
bytes32[] memory _merkleProof
)
public
{
require(!claimed[_week][_liquidityProvider]);
require(verifyClaim(_liquidityProvider, _week, _claimedBalance, _merkleProof), 'Incorrect merkle proof');
claimed[_week][_liquidityProvider] = true;
disburse(_liquidityProvider, _claimedBalance);
}
struct Claim {
// The week the claim is related to.
uint week;
// The amount being claimed.
uint balance;
// The merkle proof for the claim, sorted from the leaf to the root of the tree.
bytes32[] merkleProof;
}
/**
* @notice Makes multiple claims for a given claimant.
* @param _liquidityProvider The address of the claimant.
* @param claims An array of claims containing the week, balance and the merkle proof.
*/
function claimWeeks(
address _liquidityProvider,
Claim[] memory claims
)
public
{
uint totalBalance = 0;
Claim memory claim ;
for(uint i = 0; i < claims.length; i++) {
claim = claims[i];
require(!claimed[claim.week][_liquidityProvider]);
require(verifyClaim(_liquidityProvider, claim.week, claim.balance, claim.merkleProof), 'Incorrect merkle proof');
totalBalance += claim.balance;
claimed[claim.week][_liquidityProvider] = true;
}
disburse(_liquidityProvider, totalBalance);
}
/**
* @notice Gets the claim status for given claimant from `_begin` to `_end` weeks.
* @param _liquidityProvider The address of the claimant.
* @param _begin The week to start with (inclusive).
* @param _end The week to end with (inclusive).
*/
function claimStatus(
address _liquidityProvider,
uint _begin,
uint _end
)
external
view
returns (bool[] memory)
{
uint size = 1 + _end - _begin;
bool[] memory arr = new bool[](size);
for(uint i = 0; i < size; i++) {
arr[i] = claimed[_begin + i][_liquidityProvider];
}
return arr;
}
/**
* @notice Gets all merkle roots for from `_begin` to `_end` weeks.
* @param _begin The week to start with (inclusive).
* @param _end The week to end with (inclusive).
*/
function merkleRoots(
uint _begin,
uint _end
)
external
view
returns (bytes32[] memory)
{
uint size = 1 + _end - _begin;
bytes32[] memory arr = new bytes32[](size);
for(uint i = 0; i < size; i++) {
arr[i] = weekMerkleRoots[_begin + i];
}
return arr;
}
/**
* @notice Verifies a claim.
* @param _liquidityProvider The address of the claimant.
* @param _week The week for the claim.
* @param _claimedBalance The amount being claimed.
* @param _merkleProof The merkle proof for the claim, sorted from the leaf to the root of the tree.
*/
function verifyClaim(
address _liquidityProvider,
uint _week,
uint _claimedBalance,
bytes32[] memory _merkleProof
)
public
view
returns (bool valid)
{
bytes32 leaf = keccak256(abi.encodePacked(_liquidityProvider, _claimedBalance));
return MerkleProof.verify(_merkleProof, weekMerkleRoots[_week], leaf);
}
/**
* @notice Seeds a new round for the airdrop.
* @dev Will transfer tokens from the owner to this contract.
* @param _week The airdrop week.
* @param _merkleRoot The merkle root of the claims for that period.
* @param _totalAllocation The amount of tokens allocated for the distribution.
*/
function seedAllocations(
uint _week,
bytes32 _merkleRoot,
uint _totalAllocation
)
external
onlyOwner
{
require(weekMerkleRoots[_week] == bytes32(0), "cannot rewrite merkle root");
weekMerkleRoots[_week] = _merkleRoot;
require(token.transferFrom(msg.sender, address(this), _totalAllocation), "ERR_TRANSFER_FAILED");
}
}