-
Notifications
You must be signed in to change notification settings - Fork 1
/
MerkleDrop.sol
138 lines (109 loc) · 4.89 KB
/
MerkleDrop.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
/* Please read and review the Terms and Conditions governing this
Merkle Drop by visiting the Trustlines Foundation homepage. Any
interaction with this smart contract, including but not limited to
claiming Trustlines Network Tokens, is subject to these Terms and
Conditions.
*/
pragma solidity ^0.5.8;
import "./ERC20Interface.sol";
contract MerkleDrop {
bytes32 public root;
ERC20Interface public droppedToken;
uint public decayStartTime;
uint public decayDurationInSeconds;
uint public initialBalance;
uint public remainingValue; // The total of not withdrawn entitlements, not considering decay
uint public spentTokens; // The total tokens spent by the contract, burnt or withdrawn
mapping (address => bool) public withdrawn;
event Withdraw(address recipient, uint value, uint originalValue);
event Burn(uint value);
constructor(ERC20Interface _droppedToken, uint _initialBalance, bytes32 _root, uint _decayStartTime, uint _decayDurationInSeconds) public {
// The _initialBalance should be equal to the sum of airdropped tokens
droppedToken = _droppedToken;
initialBalance = _initialBalance;
remainingValue = _initialBalance;
root = _root;
decayStartTime = _decayStartTime;
decayDurationInSeconds = _decayDurationInSeconds;
}
function withdraw(uint value, bytes32[] memory proof) public {
require(verifyEntitled(msg.sender, value, proof), "The proof could not be verified.");
require(! withdrawn[msg.sender], "You have already withdrawn your entitled token.");
burnUnusableTokens();
uint valueToSend = decayedEntitlementAtTime(value, now, false);
assert(valueToSend <= value);
require(droppedToken.balanceOf(address(this)) >= valueToSend, "The MerkleDrop does not have tokens to drop yet / anymore.");
require(valueToSend != 0, "The decayed entitled value is now zero.");
withdrawn[msg.sender] = true;
remainingValue -= value;
spentTokens += valueToSend;
require(droppedToken.transfer(msg.sender, valueToSend));
emit Withdraw(msg.sender, valueToSend, value);
}
function verifyEntitled(address recipient, uint value, bytes32[] memory proof) public view returns (bool) {
// We need to pack the 20 bytes address to the 32 bytes value
// to match with the proof made with the python merkle-drop package
bytes32 leaf = keccak256(abi.encodePacked(recipient, value));
return verifyProof(leaf, proof);
}
function decayedEntitlementAtTime(uint value, uint time, bool roundUp) public view returns (uint) {
if (time <= decayStartTime) {
return value;
} else if (time >= decayStartTime + decayDurationInSeconds) {
return 0;
} else {
uint timeDecayed = time - decayStartTime;
uint valueDecay = decay(value, timeDecayed, decayDurationInSeconds, !roundUp);
assert(valueDecay <= value);
return value - valueDecay;
}
}
function burnUnusableTokens() public {
if (now <= decayStartTime) {
return;
}
// The amount of tokens that should be held within the contract after burning
uint targetBalance = decayedEntitlementAtTime(remainingValue, now, true);
// toBurn = (initial balance - target balance) - what we already removed from initial balance
uint currentBalance = initialBalance - spentTokens;
assert(targetBalance <= currentBalance);
uint toBurn = currentBalance - targetBalance;
spentTokens += toBurn;
burn(toBurn);
}
function deleteContract() public {
require(now >= decayStartTime + decayDurationInSeconds, "The storage cannot be deleted before the end of the merkle drop.");
burnUnusableTokens();
selfdestruct(address(0));
}
function verifyProof(bytes32 leaf, bytes32[] memory proof) internal view returns (bool) {
bytes32 currentHash = leaf;
for (uint i = 0; i < proof.length; i += 1) {
currentHash = parentHash(currentHash, proof[i]);
}
return currentHash == root;
}
function parentHash(bytes32 a, bytes32 b) internal pure returns (bytes32) {
if (a < b) {
return keccak256(abi.encode(a, b));
} else {
return keccak256(abi.encode(b, a));
}
}
function burn(uint value) internal {
if (value == 0) {
return;
}
emit Burn(value);
droppedToken.burn(value);
}
function decay(uint value, uint timeToDecay, uint totalDecayTime, bool roundUp) internal pure returns (uint) {
uint decay;
if (roundUp) {
decay = (value*timeToDecay+totalDecayTime-1)/totalDecayTime;
} else {
decay = value*timeToDecay/totalDecayTime;
}
return decay >= value ? value : decay;
}
}