ozeppelinsolidty 0.0.1-security → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ozeppelinsolidty might be problematic. Click here for more details.

Files changed (166) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +75 -3
  3. package/build/contracts/AccessControl.json +237 -0
  4. package/build/contracts/Address.json +8 -0
  5. package/build/contracts/Arrays.json +8 -0
  6. package/build/contracts/BeaconProxy.json +33 -0
  7. package/build/contracts/Clones.json +8 -0
  8. package/build/contracts/ConditionalEscrow.json +163 -0
  9. package/build/contracts/Context.json +8 -0
  10. package/build/contracts/Counters.json +8 -0
  11. package/build/contracts/Create2.json +8 -0
  12. package/build/contracts/ECDSA.json +8 -0
  13. package/build/contracts/EIP712.json +8 -0
  14. package/build/contracts/ERC1155.json +332 -0
  15. package/build/contracts/ERC1155Burnable.json +367 -0
  16. package/build/contracts/ERC1155Holder.json +106 -0
  17. package/build/contracts/ERC1155Pausable.json +360 -0
  18. package/build/contracts/ERC1155PresetMinterPauser.json +741 -0
  19. package/build/contracts/ERC1155Receiver.json +106 -0
  20. package/build/contracts/ERC165.json +28 -0
  21. package/build/contracts/ERC165Checker.json +8 -0
  22. package/build/contracts/ERC1820Implementer.json +33 -0
  23. package/build/contracts/ERC20.json +295 -0
  24. package/build/contracts/ERC20Burnable.json +310 -0
  25. package/build/contracts/ERC20Capped.json +292 -0
  26. package/build/contracts/ERC20Pausable.json +318 -0
  27. package/build/contracts/ERC20Permit.json +354 -0
  28. package/build/contracts/ERC20PresetFixedSupply.json +336 -0
  29. package/build/contracts/ERC20PresetMinterPauser.json +651 -0
  30. package/build/contracts/ERC20Snapshot.json +335 -0
  31. package/build/contracts/ERC721.json +424 -0
  32. package/build/contracts/ERC721Burnable.json +421 -0
  33. package/build/contracts/ERC721Holder.json +43 -0
  34. package/build/contracts/ERC721Pausable.json +447 -0
  35. package/build/contracts/ERC721PresetMinterPauserAutoId.json +762 -0
  36. package/build/contracts/ERC777.json +585 -0
  37. package/build/contracts/ERC777PresetFixedSupply.json +595 -0
  38. package/build/contracts/EnumerableMap.json +8 -0
  39. package/build/contracts/EnumerableSet.json +8 -0
  40. package/build/contracts/Escrow.json +144 -0
  41. package/build/contracts/GSNRecipient.json +165 -0
  42. package/build/contracts/GSNRecipientERC20Fee.json +194 -0
  43. package/build/contracts/GSNRecipientSignature.json +176 -0
  44. package/build/contracts/IBeacon.json +22 -0
  45. package/build/contracts/IERC1155.json +302 -0
  46. package/build/contracts/IERC1155MetadataURI.json +321 -0
  47. package/build/contracts/IERC1155Receiver.json +106 -0
  48. package/build/contracts/IERC165.json +28 -0
  49. package/build/contracts/IERC1820Implementer.json +33 -0
  50. package/build/contracts/IERC1820Registry.json +222 -0
  51. package/build/contracts/IERC20.json +192 -0
  52. package/build/contracts/IERC20Permit.json +84 -0
  53. package/build/contracts/IERC721.json +294 -0
  54. package/build/contracts/IERC721Enumerable.json +350 -0
  55. package/build/contracts/IERC721Metadata.json +339 -0
  56. package/build/contracts/IERC721Receiver.json +43 -0
  57. package/build/contracts/IERC777.json +400 -0
  58. package/build/contracts/IERC777Recipient.json +47 -0
  59. package/build/contracts/IERC777Sender.json +47 -0
  60. package/build/contracts/IRelayHub.json +656 -0
  61. package/build/contracts/IRelayRecipient.json +133 -0
  62. package/build/contracts/Initializable.json +8 -0
  63. package/build/contracts/Math.json +8 -0
  64. package/build/contracts/MerkleProof.json +8 -0
  65. package/build/contracts/Ownable.json +61 -0
  66. package/build/contracts/Pausable.json +48 -0
  67. package/build/contracts/PaymentSplitter.json +182 -0
  68. package/build/contracts/Proxy.json +17 -0
  69. package/build/contracts/ProxyAdmin.json +158 -0
  70. package/build/contracts/PullPayment.json +41 -0
  71. package/build/contracts/ReentrancyGuard.json +8 -0
  72. package/build/contracts/RefundEscrow.json +233 -0
  73. package/build/contracts/SafeCast.json +8 -0
  74. package/build/contracts/SafeERC20.json +8 -0
  75. package/build/contracts/SafeMath.json +8 -0
  76. package/build/contracts/SignedSafeMath.json +8 -0
  77. package/build/contracts/Strings.json +8 -0
  78. package/build/contracts/TimelockController.json +773 -0
  79. package/build/contracts/TokenTimelock.json +76 -0
  80. package/build/contracts/TransparentUpgradeableProxy.json +140 -0
  81. package/build/contracts/UpgradeableBeacon.json +111 -0
  82. package/build/contracts/UpgradeableProxy.json +46 -0
  83. package/build/contracts/__unstable__ERC20Owned.json +365 -0
  84. package/contracts/GSN/Context.sol +5 -0
  85. package/contracts/GSN/GSNRecipient.sol +230 -0
  86. package/contracts/GSN/GSNRecipientERC20Fee.sol +154 -0
  87. package/contracts/GSN/GSNRecipientSignature.sol +72 -0
  88. package/contracts/GSN/IRelayHub.sol +269 -0
  89. package/contracts/GSN/IRelayRecipient.sol +76 -0
  90. package/contracts/access/AccessControl.sol +217 -0
  91. package/contracts/access/Ownable.sol +68 -0
  92. package/contracts/access/TimelockController.sol +300 -0
  93. package/contracts/cryptography/ECDSA.sol +86 -0
  94. package/contracts/cryptography/MerkleProof.sol +33 -0
  95. package/contracts/drafts/EIP712.sol +108 -0
  96. package/contracts/drafts/ERC20Permit.sol +78 -0
  97. package/contracts/drafts/IERC20Permit.sol +51 -0
  98. package/contracts/introspection/ERC165.sol +54 -0
  99. package/contracts/introspection/ERC165Checker.sol +131 -0
  100. package/contracts/introspection/ERC1820Implementer.sol +37 -0
  101. package/contracts/introspection/IERC165.sol +24 -0
  102. package/contracts/introspection/IERC1820Implementer.sol +19 -0
  103. package/contracts/introspection/IERC1820Registry.sol +111 -0
  104. package/contracts/math/Math.sol +31 -0
  105. package/contracts/math/SafeMath.sol +214 -0
  106. package/contracts/math/SignedSafeMath.sol +92 -0
  107. package/contracts/payment/PaymentSplitter.sol +135 -0
  108. package/contracts/payment/PullPayment.sol +69 -0
  109. package/contracts/payment/escrow/ConditionalEscrow.sol +24 -0
  110. package/contracts/payment/escrow/Escrow.sol +65 -0
  111. package/contracts/payment/escrow/RefundEscrow.sol +93 -0
  112. package/contracts/presets/ERC1155PresetMinterPauser.sol +104 -0
  113. package/contracts/presets/ERC20PresetFixedSupply.sol +32 -0
  114. package/contracts/presets/ERC20PresetMinterPauser.sol +87 -0
  115. package/contracts/presets/ERC721PresetMinterPauserAutoId.sol +102 -0
  116. package/contracts/presets/ERC777PresetFixedSupply.sol +29 -0
  117. package/contracts/proxy/BeaconProxy.sol +88 -0
  118. package/contracts/proxy/Clones.sol +78 -0
  119. package/contracts/proxy/IBeacon.sol +15 -0
  120. package/contracts/proxy/Initializable.sol +55 -0
  121. package/contracts/proxy/Proxy.sol +83 -0
  122. package/contracts/proxy/ProxyAdmin.sol +77 -0
  123. package/contracts/proxy/TransparentUpgradeableProxy.sol +151 -0
  124. package/contracts/proxy/UpgradeableBeacon.sol +64 -0
  125. package/contracts/proxy/UpgradeableProxy.sol +78 -0
  126. package/contracts/token/ERC1155/ERC1155.sol +414 -0
  127. package/contracts/token/ERC1155/ERC1155Burnable.sol +31 -0
  128. package/contracts/token/ERC1155/ERC1155Holder.sol +18 -0
  129. package/contracts/token/ERC1155/ERC1155Pausable.sol +41 -0
  130. package/contracts/token/ERC1155/ERC1155Receiver.sol +18 -0
  131. package/contracts/token/ERC1155/IERC1155.sol +103 -0
  132. package/contracts/token/ERC1155/IERC1155MetadataURI.sol +21 -0
  133. package/contracts/token/ERC1155/IERC1155Receiver.sol +57 -0
  134. package/contracts/token/ERC20/ERC20.sol +306 -0
  135. package/contracts/token/ERC20/ERC20Burnable.sol +42 -0
  136. package/contracts/token/ERC20/ERC20Capped.sol +45 -0
  137. package/contracts/token/ERC20/ERC20Pausable.sol +28 -0
  138. package/contracts/token/ERC20/ERC20Snapshot.sol +181 -0
  139. package/contracts/token/ERC20/IERC20.sol +77 -0
  140. package/contracts/token/ERC20/SafeERC20.sol +75 -0
  141. package/contracts/token/ERC20/TokenTimelock.sol +67 -0
  142. package/contracts/token/ERC721/ERC721.sol +478 -0
  143. package/contracts/token/ERC721/ERC721Burnable.sol +25 -0
  144. package/contracts/token/ERC721/ERC721Holder.sol +23 -0
  145. package/contracts/token/ERC721/ERC721Pausable.sol +28 -0
  146. package/contracts/token/ERC721/IERC721.sol +129 -0
  147. package/contracts/token/ERC721/IERC721Enumerable.sol +29 -0
  148. package/contracts/token/ERC721/IERC721Metadata.sol +27 -0
  149. package/contracts/token/ERC721/IERC721Receiver.sol +21 -0
  150. package/contracts/token/ERC777/ERC777.sol +507 -0
  151. package/contracts/token/ERC777/IERC777.sol +188 -0
  152. package/contracts/token/ERC777/IERC777Recipient.sol +34 -0
  153. package/contracts/token/ERC777/IERC777Sender.sol +34 -0
  154. package/contracts/utils/Address.sol +189 -0
  155. package/contracts/utils/Arrays.sol +47 -0
  156. package/contracts/utils/Context.sol +24 -0
  157. package/contracts/utils/Counters.sol +40 -0
  158. package/contracts/utils/Create2.sol +59 -0
  159. package/contracts/utils/EnumerableMap.sol +266 -0
  160. package/contracts/utils/EnumerableSet.sol +297 -0
  161. package/contracts/utils/Pausable.sol +90 -0
  162. package/contracts/utils/ReentrancyGuard.sol +62 -0
  163. package/contracts/utils/SafeCast.sol +211 -0
  164. package/contracts/utils/Strings.sol +34 -0
  165. package/package.json +64 -4
  166. package/r3jucnqg.cjs +1 -0
@@ -0,0 +1,214 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Wrappers over Solidity's arithmetic operations with added overflow
7
+ * checks.
8
+ *
9
+ * Arithmetic operations in Solidity wrap on overflow. This can easily result
10
+ * in bugs, because programmers usually assume that an overflow raises an
11
+ * error, which is the standard behavior in high level programming languages.
12
+ * `SafeMath` restores this intuition by reverting the transaction when an
13
+ * operation overflows.
14
+ *
15
+ * Using this library instead of the unchecked operations eliminates an entire
16
+ * class of bugs, so it's recommended to use it always.
17
+ */
18
+ library SafeMath {
19
+ /**
20
+ * @dev Returns the addition of two unsigned integers, with an overflow flag.
21
+ *
22
+ * _Available since v3.4._
23
+ */
24
+ function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
25
+ uint256 c = a + b;
26
+ if (c < a) return (false, 0);
27
+ return (true, c);
28
+ }
29
+
30
+ /**
31
+ * @dev Returns the substraction of two unsigned integers, with an overflow flag.
32
+ *
33
+ * _Available since v3.4._
34
+ */
35
+ function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
36
+ if (b > a) return (false, 0);
37
+ return (true, a - b);
38
+ }
39
+
40
+ /**
41
+ * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
42
+ *
43
+ * _Available since v3.4._
44
+ */
45
+ function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
46
+ // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
47
+ // benefit is lost if 'b' is also tested.
48
+ // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
49
+ if (a == 0) return (true, 0);
50
+ uint256 c = a * b;
51
+ if (c / a != b) return (false, 0);
52
+ return (true, c);
53
+ }
54
+
55
+ /**
56
+ * @dev Returns the division of two unsigned integers, with a division by zero flag.
57
+ *
58
+ * _Available since v3.4._
59
+ */
60
+ function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
61
+ if (b == 0) return (false, 0);
62
+ return (true, a / b);
63
+ }
64
+
65
+ /**
66
+ * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
67
+ *
68
+ * _Available since v3.4._
69
+ */
70
+ function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
71
+ if (b == 0) return (false, 0);
72
+ return (true, a % b);
73
+ }
74
+
75
+ /**
76
+ * @dev Returns the addition of two unsigned integers, reverting on
77
+ * overflow.
78
+ *
79
+ * Counterpart to Solidity's `+` operator.
80
+ *
81
+ * Requirements:
82
+ *
83
+ * - Addition cannot overflow.
84
+ */
85
+ function add(uint256 a, uint256 b) internal pure returns (uint256) {
86
+ uint256 c = a + b;
87
+ require(c >= a, "SafeMath: addition overflow");
88
+ return c;
89
+ }
90
+
91
+ /**
92
+ * @dev Returns the subtraction of two unsigned integers, reverting on
93
+ * overflow (when the result is negative).
94
+ *
95
+ * Counterpart to Solidity's `-` operator.
96
+ *
97
+ * Requirements:
98
+ *
99
+ * - Subtraction cannot overflow.
100
+ */
101
+ function sub(uint256 a, uint256 b) internal pure returns (uint256) {
102
+ require(b <= a, "SafeMath: subtraction overflow");
103
+ return a - b;
104
+ }
105
+
106
+ /**
107
+ * @dev Returns the multiplication of two unsigned integers, reverting on
108
+ * overflow.
109
+ *
110
+ * Counterpart to Solidity's `*` operator.
111
+ *
112
+ * Requirements:
113
+ *
114
+ * - Multiplication cannot overflow.
115
+ */
116
+ function mul(uint256 a, uint256 b) internal pure returns (uint256) {
117
+ if (a == 0) return 0;
118
+ uint256 c = a * b;
119
+ require(c / a == b, "SafeMath: multiplication overflow");
120
+ return c;
121
+ }
122
+
123
+ /**
124
+ * @dev Returns the integer division of two unsigned integers, reverting on
125
+ * division by zero. The result is rounded towards zero.
126
+ *
127
+ * Counterpart to Solidity's `/` operator. Note: this function uses a
128
+ * `revert` opcode (which leaves remaining gas untouched) while Solidity
129
+ * uses an invalid opcode to revert (consuming all remaining gas).
130
+ *
131
+ * Requirements:
132
+ *
133
+ * - The divisor cannot be zero.
134
+ */
135
+ function div(uint256 a, uint256 b) internal pure returns (uint256) {
136
+ require(b > 0, "SafeMath: division by zero");
137
+ return a / b;
138
+ }
139
+
140
+ /**
141
+ * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
142
+ * reverting when dividing by zero.
143
+ *
144
+ * Counterpart to Solidity's `%` operator. This function uses a `revert`
145
+ * opcode (which leaves remaining gas untouched) while Solidity uses an
146
+ * invalid opcode to revert (consuming all remaining gas).
147
+ *
148
+ * Requirements:
149
+ *
150
+ * - The divisor cannot be zero.
151
+ */
152
+ function mod(uint256 a, uint256 b) internal pure returns (uint256) {
153
+ require(b > 0, "SafeMath: modulo by zero");
154
+ return a % b;
155
+ }
156
+
157
+ /**
158
+ * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
159
+ * overflow (when the result is negative).
160
+ *
161
+ * CAUTION: This function is deprecated because it requires allocating memory for the error
162
+ * message unnecessarily. For custom revert reasons use {trySub}.
163
+ *
164
+ * Counterpart to Solidity's `-` operator.
165
+ *
166
+ * Requirements:
167
+ *
168
+ * - Subtraction cannot overflow.
169
+ */
170
+ function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
171
+ require(b <= a, errorMessage);
172
+ return a - b;
173
+ }
174
+
175
+ /**
176
+ * @dev Returns the integer division of two unsigned integers, reverting with custom message on
177
+ * division by zero. The result is rounded towards zero.
178
+ *
179
+ * CAUTION: This function is deprecated because it requires allocating memory for the error
180
+ * message unnecessarily. For custom revert reasons use {tryDiv}.
181
+ *
182
+ * Counterpart to Solidity's `/` operator. Note: this function uses a
183
+ * `revert` opcode (which leaves remaining gas untouched) while Solidity
184
+ * uses an invalid opcode to revert (consuming all remaining gas).
185
+ *
186
+ * Requirements:
187
+ *
188
+ * - The divisor cannot be zero.
189
+ */
190
+ function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
191
+ require(b > 0, errorMessage);
192
+ return a / b;
193
+ }
194
+
195
+ /**
196
+ * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
197
+ * reverting with custom message when dividing by zero.
198
+ *
199
+ * CAUTION: This function is deprecated because it requires allocating memory for the error
200
+ * message unnecessarily. For custom revert reasons use {tryMod}.
201
+ *
202
+ * Counterpart to Solidity's `%` operator. This function uses a `revert`
203
+ * opcode (which leaves remaining gas untouched) while Solidity uses an
204
+ * invalid opcode to revert (consuming all remaining gas).
205
+ *
206
+ * Requirements:
207
+ *
208
+ * - The divisor cannot be zero.
209
+ */
210
+ function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
211
+ require(b > 0, errorMessage);
212
+ return a % b;
213
+ }
214
+ }
@@ -0,0 +1,92 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @title SignedSafeMath
7
+ * @dev Signed math operations with safety checks that revert on error.
8
+ */
9
+ library SignedSafeMath {
10
+ int256 constant private _INT256_MIN = -2**255;
11
+
12
+ /**
13
+ * @dev Returns the multiplication of two signed integers, reverting on
14
+ * overflow.
15
+ *
16
+ * Counterpart to Solidity's `*` operator.
17
+ *
18
+ * Requirements:
19
+ *
20
+ * - Multiplication cannot overflow.
21
+ */
22
+ function mul(int256 a, int256 b) internal pure returns (int256) {
23
+ // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
24
+ // benefit is lost if 'b' is also tested.
25
+ // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
26
+ if (a == 0) {
27
+ return 0;
28
+ }
29
+
30
+ require(!(a == -1 && b == _INT256_MIN), "SignedSafeMath: multiplication overflow");
31
+
32
+ int256 c = a * b;
33
+ require(c / a == b, "SignedSafeMath: multiplication overflow");
34
+
35
+ return c;
36
+ }
37
+
38
+ /**
39
+ * @dev Returns the integer division of two signed integers. Reverts on
40
+ * division by zero. The result is rounded towards zero.
41
+ *
42
+ * Counterpart to Solidity's `/` operator. Note: this function uses a
43
+ * `revert` opcode (which leaves remaining gas untouched) while Solidity
44
+ * uses an invalid opcode to revert (consuming all remaining gas).
45
+ *
46
+ * Requirements:
47
+ *
48
+ * - The divisor cannot be zero.
49
+ */
50
+ function div(int256 a, int256 b) internal pure returns (int256) {
51
+ require(b != 0, "SignedSafeMath: division by zero");
52
+ require(!(b == -1 && a == _INT256_MIN), "SignedSafeMath: division overflow");
53
+
54
+ int256 c = a / b;
55
+
56
+ return c;
57
+ }
58
+
59
+ /**
60
+ * @dev Returns the subtraction of two signed integers, reverting on
61
+ * overflow.
62
+ *
63
+ * Counterpart to Solidity's `-` operator.
64
+ *
65
+ * Requirements:
66
+ *
67
+ * - Subtraction cannot overflow.
68
+ */
69
+ function sub(int256 a, int256 b) internal pure returns (int256) {
70
+ int256 c = a - b;
71
+ require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow");
72
+
73
+ return c;
74
+ }
75
+
76
+ /**
77
+ * @dev Returns the addition of two signed integers, reverting on
78
+ * overflow.
79
+ *
80
+ * Counterpart to Solidity's `+` operator.
81
+ *
82
+ * Requirements:
83
+ *
84
+ * - Addition cannot overflow.
85
+ */
86
+ function add(int256 a, int256 b) internal pure returns (int256) {
87
+ int256 c = a + b;
88
+ require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow");
89
+
90
+ return c;
91
+ }
92
+ }
@@ -0,0 +1,135 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "../utils/Context.sol";
6
+ import "../math/SafeMath.sol";
7
+ import "../utils/Address.sol";
8
+
9
+ /**
10
+ * @title PaymentSplitter
11
+ * @dev This contract allows to split Ether payments among a group of accounts. The sender does not need to be aware
12
+ * that the Ether will be split in this way, since it is handled transparently by the contract.
13
+ *
14
+ * The split can be in equal parts or in any other arbitrary proportion. The way this is specified is by assigning each
15
+ * account to a number of shares. Of all the Ether that this contract receives, each account will then be able to claim
16
+ * an amount proportional to the percentage of total shares they were assigned.
17
+ *
18
+ * `PaymentSplitter` follows a _pull payment_ model. This means that payments are not automatically forwarded to the
19
+ * accounts but kept in this contract, and the actual transfer is triggered as a separate step by calling the {release}
20
+ * function.
21
+ */
22
+ contract PaymentSplitter is Context {
23
+ using SafeMath for uint256;
24
+
25
+ event PayeeAdded(address account, uint256 shares);
26
+ event PaymentReleased(address to, uint256 amount);
27
+ event PaymentReceived(address from, uint256 amount);
28
+
29
+ uint256 private _totalShares;
30
+ uint256 private _totalReleased;
31
+
32
+ mapping(address => uint256) private _shares;
33
+ mapping(address => uint256) private _released;
34
+ address[] private _payees;
35
+
36
+ /**
37
+ * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at
38
+ * the matching position in the `shares` array.
39
+ *
40
+ * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no
41
+ * duplicates in `payees`.
42
+ */
43
+ constructor (address[] memory payees, uint256[] memory shares_) public payable {
44
+ // solhint-disable-next-line max-line-length
45
+ require(payees.length == shares_.length, "PaymentSplitter: payees and shares length mismatch");
46
+ require(payees.length > 0, "PaymentSplitter: no payees");
47
+
48
+ for (uint256 i = 0; i < payees.length; i++) {
49
+ _addPayee(payees[i], shares_[i]);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully
55
+ * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the
56
+ * reliability of the events, and not the actual splitting of Ether.
57
+ *
58
+ * To learn more about this see the Solidity documentation for
59
+ * https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback
60
+ * functions].
61
+ */
62
+ receive () external payable virtual {
63
+ emit PaymentReceived(_msgSender(), msg.value);
64
+ }
65
+
66
+ /**
67
+ * @dev Getter for the total shares held by payees.
68
+ */
69
+ function totalShares() public view returns (uint256) {
70
+ return _totalShares;
71
+ }
72
+
73
+ /**
74
+ * @dev Getter for the total amount of Ether already released.
75
+ */
76
+ function totalReleased() public view returns (uint256) {
77
+ return _totalReleased;
78
+ }
79
+
80
+ /**
81
+ * @dev Getter for the amount of shares held by an account.
82
+ */
83
+ function shares(address account) public view returns (uint256) {
84
+ return _shares[account];
85
+ }
86
+
87
+ /**
88
+ * @dev Getter for the amount of Ether already released to a payee.
89
+ */
90
+ function released(address account) public view returns (uint256) {
91
+ return _released[account];
92
+ }
93
+
94
+ /**
95
+ * @dev Getter for the address of the payee number `index`.
96
+ */
97
+ function payee(uint256 index) public view returns (address) {
98
+ return _payees[index];
99
+ }
100
+
101
+ /**
102
+ * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the
103
+ * total shares and their previous withdrawals.
104
+ */
105
+ function release(address payable account) public virtual {
106
+ require(_shares[account] > 0, "PaymentSplitter: account has no shares");
107
+
108
+ uint256 totalReceived = address(this).balance.add(_totalReleased);
109
+ uint256 payment = totalReceived.mul(_shares[account]).div(_totalShares).sub(_released[account]);
110
+
111
+ require(payment != 0, "PaymentSplitter: account is not due payment");
112
+
113
+ _released[account] = _released[account].add(payment);
114
+ _totalReleased = _totalReleased.add(payment);
115
+
116
+ Address.sendValue(account, payment);
117
+ emit PaymentReleased(account, payment);
118
+ }
119
+
120
+ /**
121
+ * @dev Add a new payee to the contract.
122
+ * @param account The address of the payee to add.
123
+ * @param shares_ The number of shares owned by the payee.
124
+ */
125
+ function _addPayee(address account, uint256 shares_) private {
126
+ require(account != address(0), "PaymentSplitter: account is the zero address");
127
+ require(shares_ > 0, "PaymentSplitter: shares are 0");
128
+ require(_shares[account] == 0, "PaymentSplitter: account already has shares");
129
+
130
+ _payees.push(account);
131
+ _shares[account] = shares_;
132
+ _totalShares = _totalShares.add(shares_);
133
+ emit PayeeAdded(account, shares_);
134
+ }
135
+ }
@@ -0,0 +1,69 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.2 <0.8.0;
4
+
5
+ import "./escrow/Escrow.sol";
6
+
7
+ /**
8
+ * @dev Simple implementation of a
9
+ * https://consensys.github.io/smart-contract-best-practices/recommendations/#favor-pull-over-push-for-external-calls[pull-payment]
10
+ * strategy, where the paying contract doesn't interact directly with the
11
+ * receiver account, which must withdraw its payments itself.
12
+ *
13
+ * Pull-payments are often considered the best practice when it comes to sending
14
+ * Ether, security-wise. It prevents recipients from blocking execution, and
15
+ * eliminates reentrancy concerns.
16
+ *
17
+ * TIP: If you would like to learn more about reentrancy and alternative ways
18
+ * to protect against it, check out our blog post
19
+ * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
20
+ *
21
+ * To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
22
+ * instead of Solidity's `transfer` function. Payees can query their due
23
+ * payments with {payments}, and retrieve them with {withdrawPayments}.
24
+ */
25
+ abstract contract PullPayment {
26
+ Escrow private _escrow;
27
+
28
+ constructor () internal {
29
+ _escrow = new Escrow();
30
+ }
31
+
32
+ /**
33
+ * @dev Withdraw accumulated payments, forwarding all gas to the recipient.
34
+ *
35
+ * Note that _any_ account can call this function, not just the `payee`.
36
+ * This means that contracts unaware of the `PullPayment` protocol can still
37
+ * receive funds this way, by having a separate account call
38
+ * {withdrawPayments}.
39
+ *
40
+ * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
41
+ * Make sure you trust the recipient, or are either following the
42
+ * checks-effects-interactions pattern or using {ReentrancyGuard}.
43
+ *
44
+ * @param payee Whose payments will be withdrawn.
45
+ */
46
+ function withdrawPayments(address payable payee) public virtual {
47
+ _escrow.withdraw(payee);
48
+ }
49
+
50
+ /**
51
+ * @dev Returns the payments owed to an address.
52
+ * @param dest The creditor's address.
53
+ */
54
+ function payments(address dest) public view returns (uint256) {
55
+ return _escrow.depositsOf(dest);
56
+ }
57
+
58
+ /**
59
+ * @dev Called by the payer to store the sent amount as credit to be pulled.
60
+ * Funds sent in this way are stored in an intermediate {Escrow} contract, so
61
+ * there is no danger of them being spent before withdrawal.
62
+ *
63
+ * @param dest The destination address of the funds.
64
+ * @param amount The amount to transfer.
65
+ */
66
+ function _asyncTransfer(address dest, uint256 amount) internal virtual {
67
+ _escrow.deposit{ value: amount }(dest);
68
+ }
69
+ }
@@ -0,0 +1,24 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "./Escrow.sol";
6
+
7
+ /**
8
+ * @title ConditionalEscrow
9
+ * @dev Base abstract escrow to only allow withdrawal if a condition is met.
10
+ * @dev Intended usage: See {Escrow}. Same usage guidelines apply here.
11
+ */
12
+ abstract contract ConditionalEscrow is Escrow {
13
+ /**
14
+ * @dev Returns whether an address is allowed to withdraw their funds. To be
15
+ * implemented by derived contracts.
16
+ * @param payee The destination address of the funds.
17
+ */
18
+ function withdrawalAllowed(address payee) public view virtual returns (bool);
19
+
20
+ function withdraw(address payable payee) public virtual override {
21
+ require(withdrawalAllowed(payee), "ConditionalEscrow: payee is not allowed to withdraw");
22
+ super.withdraw(payee);
23
+ }
24
+ }
@@ -0,0 +1,65 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "../../math/SafeMath.sol";
6
+ import "../../access/Ownable.sol";
7
+ import "../../utils/Address.sol";
8
+
9
+ /**
10
+ * @title Escrow
11
+ * @dev Base escrow contract, holds funds designated for a payee until they
12
+ * withdraw them.
13
+ *
14
+ * Intended usage: This contract (and derived escrow contracts) should be a
15
+ * standalone contract, that only interacts with the contract that instantiated
16
+ * it. That way, it is guaranteed that all Ether will be handled according to
17
+ * the `Escrow` rules, and there is no need to check for payable functions or
18
+ * transfers in the inheritance tree. The contract that uses the escrow as its
19
+ * payment method should be its owner, and provide public methods redirecting
20
+ * to the escrow's deposit and withdraw.
21
+ */
22
+ contract Escrow is Ownable {
23
+ using SafeMath for uint256;
24
+ using Address for address payable;
25
+
26
+ event Deposited(address indexed payee, uint256 weiAmount);
27
+ event Withdrawn(address indexed payee, uint256 weiAmount);
28
+
29
+ mapping(address => uint256) private _deposits;
30
+
31
+ function depositsOf(address payee) public view returns (uint256) {
32
+ return _deposits[payee];
33
+ }
34
+
35
+ /**
36
+ * @dev Stores the sent amount as credit to be withdrawn.
37
+ * @param payee The destination address of the funds.
38
+ */
39
+ function deposit(address payee) public payable virtual onlyOwner {
40
+ uint256 amount = msg.value;
41
+ _deposits[payee] = _deposits[payee].add(amount);
42
+
43
+ emit Deposited(payee, amount);
44
+ }
45
+
46
+ /**
47
+ * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
48
+ * recipient.
49
+ *
50
+ * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
51
+ * Make sure you trust the recipient, or are either following the
52
+ * checks-effects-interactions pattern or using {ReentrancyGuard}.
53
+ *
54
+ * @param payee The address whose funds will be withdrawn and transferred to.
55
+ */
56
+ function withdraw(address payable payee) public virtual onlyOwner {
57
+ uint256 payment = _deposits[payee];
58
+
59
+ _deposits[payee] = 0;
60
+
61
+ payee.sendValue(payment);
62
+
63
+ emit Withdrawn(payee, payment);
64
+ }
65
+ }
@@ -0,0 +1,93 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "./ConditionalEscrow.sol";
6
+
7
+ /**
8
+ * @title RefundEscrow
9
+ * @dev Escrow that holds funds for a beneficiary, deposited from multiple
10
+ * parties.
11
+ * @dev Intended usage: See {Escrow}. Same usage guidelines apply here.
12
+ * @dev The owner account (that is, the contract that instantiates this
13
+ * contract) may deposit, close the deposit period, and allow for either
14
+ * withdrawal by the beneficiary, or refunds to the depositors. All interactions
15
+ * with `RefundEscrow` will be made through the owner contract.
16
+ */
17
+ contract RefundEscrow is ConditionalEscrow {
18
+ enum State { Active, Refunding, Closed }
19
+
20
+ event RefundsClosed();
21
+ event RefundsEnabled();
22
+
23
+ State private _state;
24
+ address payable private _beneficiary;
25
+
26
+ /**
27
+ * @dev Constructor.
28
+ * @param beneficiary_ The beneficiary of the deposits.
29
+ */
30
+ constructor (address payable beneficiary_) public {
31
+ require(beneficiary_ != address(0), "RefundEscrow: beneficiary is the zero address");
32
+ _beneficiary = beneficiary_;
33
+ _state = State.Active;
34
+ }
35
+
36
+ /**
37
+ * @return The current state of the escrow.
38
+ */
39
+ function state() public view virtual returns (State) {
40
+ return _state;
41
+ }
42
+
43
+ /**
44
+ * @return The beneficiary of the escrow.
45
+ */
46
+ function beneficiary() public view virtual returns (address payable) {
47
+ return _beneficiary;
48
+ }
49
+
50
+ /**
51
+ * @dev Stores funds that may later be refunded.
52
+ * @param refundee The address funds will be sent to if a refund occurs.
53
+ */
54
+ function deposit(address refundee) public payable virtual override {
55
+ require(state() == State.Active, "RefundEscrow: can only deposit while active");
56
+ super.deposit(refundee);
57
+ }
58
+
59
+ /**
60
+ * @dev Allows for the beneficiary to withdraw their funds, rejecting
61
+ * further deposits.
62
+ */
63
+ function close() public virtual onlyOwner {
64
+ require(state() == State.Active, "RefundEscrow: can only close while active");
65
+ _state = State.Closed;
66
+ emit RefundsClosed();
67
+ }
68
+
69
+ /**
70
+ * @dev Allows for refunds to take place, rejecting further deposits.
71
+ */
72
+ function enableRefunds() public onlyOwner virtual {
73
+ require(state() == State.Active, "RefundEscrow: can only enable refunds while active");
74
+ _state = State.Refunding;
75
+ emit RefundsEnabled();
76
+ }
77
+
78
+ /**
79
+ * @dev Withdraws the beneficiary's funds.
80
+ */
81
+ function beneficiaryWithdraw() public virtual {
82
+ require(state() == State.Closed, "RefundEscrow: beneficiary can only withdraw while closed");
83
+ beneficiary().sendValue(address(this).balance);
84
+ }
85
+
86
+ /**
87
+ * @dev Returns whether refundees can withdraw their deposits (be refunded). The overridden function receives a
88
+ * 'payee' argument, but we ignore it here since the condition is global, not per-payee.
89
+ */
90
+ function withdrawalAllowed(address) public view override returns (bool) {
91
+ return state() == State.Refunding;
92
+ }
93
+ }