-
Notifications
You must be signed in to change notification settings - Fork 19
/
OptimisticTokenVotingPluginSetup.sol
346 lines (294 loc) · 15.2 KB
/
OptimisticTokenVotingPluginSetup.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.17;
import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {ERC165Checker} from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import {IERC20Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import {IVotesUpgradeable} from "@openzeppelin/contracts-upgradeable/governance/utils/IVotesUpgradeable.sol";
import {IDAO} from "@aragon/osx/core/dao/IDAO.sol";
import {DAO} from "@aragon/osx/core/dao/DAO.sol";
import {PermissionLib} from "@aragon/osx/core/permission/PermissionLib.sol";
import {PluginSetup, IPluginSetup} from "@aragon/osx/framework/plugin/setup/PluginSetup.sol";
import {GovernanceERC20} from "@aragon/osx/token/ERC20/governance/GovernanceERC20.sol";
import {GovernanceWrappedERC20} from "@aragon/osx/token/ERC20/governance/GovernanceWrappedERC20.sol";
import {IGovernanceWrappedERC20} from "@aragon/osx/token/ERC20/governance/IGovernanceWrappedERC20.sol";
import {OptimisticTokenVotingPlugin} from "../OptimisticTokenVotingPlugin.sol";
import {StandardProposalCondition} from "../conditions/StandardProposalCondition.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {ITaikoL1} from "../adapted-dependencies/ITaikoL1.sol";
/// @title OptimisticTokenVotingPluginSetup
/// @author Aragon Association - 2022-2023
/// @notice The setup contract of the `OptimisticTokenVoting` plugin.
/// @custom:security-contact [email protected]
contract OptimisticTokenVotingPluginSetup is PluginSetup {
using Address for address;
using Clones for address;
using ERC165Checker for address;
/// @notice The address of the `OptimisticTokenVotingPlugin` base contract.
OptimisticTokenVotingPlugin private immutable optimisticTokenVotingPluginBase;
/// @notice The address of the `GovernanceERC20` base contract.
address public immutable governanceERC20Base;
/// @notice The address of the `GovernanceWrappedERC20` base contract.
address public immutable governanceWrappedERC20Base;
/// @notice The token settings struct.
/// @param addr The token address. If this is `address(0)`, a new `GovernanceERC20` token is deployed. If not, the existing token is wrapped as an `GovernanceWrappedERC20`.
/// @param name The token name. This parameter is only relevant if the token address is `address(0)`.
/// @param symbol The token symbol. This parameter is only relevant if the token address is `address(0)`.
struct TokenSettings {
address addr;
string name;
string symbol;
}
struct InstallationParameters {
OptimisticTokenVotingPlugin.OptimisticGovernanceSettings votingSettings;
TokenSettings tokenSettings;
// only used for GovernanceERC20 (when token is not passed)
GovernanceERC20.MintSettings mintSettings;
address taikoL1;
address taikoBridge;
uint64 stdProposalMinDuration;
address stdProposer;
address emergencyProposer;
}
/// @notice Thrown if token address is passed which is not a token.
/// @param token The token address
error TokenNotContract(address token);
/// @notice Thrown if token address is not ERC20.
/// @param token The token address
error TokenNotERC20(address token);
/// @notice Thrown if passed helpers array is of wrong length.
/// @param length The array length of passed helpers.
error WrongHelpersArrayLength(uint256 length);
/// @notice The contract constructor deploying the plugin implementation contract and receiving the governance token base contracts to clone from.
/// @param _governanceERC20Base The base `GovernanceERC20` contract to create clones from.
/// @param _governanceWrappedERC20Base The base `GovernanceWrappedERC20` contract to create clones from.
constructor(GovernanceERC20 _governanceERC20Base, GovernanceWrappedERC20 _governanceWrappedERC20Base) {
optimisticTokenVotingPluginBase = new OptimisticTokenVotingPlugin();
governanceERC20Base = address(_governanceERC20Base);
governanceWrappedERC20Base = address(_governanceWrappedERC20Base);
}
/// @inheritdoc IPluginSetup
function prepareInstallation(address _dao, bytes calldata _installParameters)
external
returns (address plugin, PreparedSetupData memory preparedSetupData)
{
// Decode `_installParameters` to extract the params needed for deploying and initializing `OptimisticTokenVoting` plugin,
// and the required helpers
InstallationParameters memory installationParams = decodeInstallationParams(_installParameters);
address token = installationParams.tokenSettings.addr;
// Prepare helpers.
address[] memory helpers = new address[](1);
if (token != address(0x0)) {
if (!token.isContract()) {
revert TokenNotContract(token);
} else if (!_supportsErc20(token)) {
revert TokenNotERC20(token);
}
if (!_supportsIVotes(token) && !_supportsIGovernanceWrappedERC20(token)) {
// Wrap the token
token = governanceWrappedERC20Base.clone();
// User already has a token. We need to wrap it in
// GovernanceWrappedERC20 in order to make the token
// include governance functionality.
GovernanceWrappedERC20(token).initialize(
IERC20Upgradeable(installationParams.tokenSettings.addr),
installationParams.tokenSettings.name,
installationParams.tokenSettings.symbol
);
}
} else {
// Create a brand new token
token = governanceERC20Base.clone();
GovernanceERC20(token).initialize(
IDAO(_dao),
installationParams.tokenSettings.name,
installationParams.tokenSettings.symbol,
installationParams.mintSettings
);
}
helpers[0] = token;
// Prepare and deploy plugin proxy.
plugin = createERC1967Proxy(
address(optimisticTokenVotingPluginBase),
abi.encodeCall(
OptimisticTokenVotingPlugin.initialize,
(
IDAO(_dao),
installationParams.votingSettings,
IVotesUpgradeable(token),
installationParams.taikoL1,
installationParams.taikoBridge
)
)
);
// Prepare permissions
PermissionLib.MultiTargetPermission[] memory permissions =
new PermissionLib.MultiTargetPermission[](installationParams.tokenSettings.addr != address(0) ? 5 : 6);
// Request the permissions to be granted
// The DAO can update the plugin settings
permissions[0] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Grant,
where: plugin,
who: _dao,
condition: PermissionLib.NO_CONDITION,
permissionId: optimisticTokenVotingPluginBase.UPDATE_OPTIMISTIC_GOVERNANCE_SETTINGS_PERMISSION_ID()
});
// The DAO can upgrade the plugin implementation
permissions[1] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Grant,
where: plugin,
who: _dao,
condition: PermissionLib.NO_CONDITION,
permissionId: optimisticTokenVotingPluginBase.UPGRADE_PLUGIN_PERMISSION_ID()
});
// The plugin can make the DAO execute actions
permissions[2] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Grant,
where: _dao,
who: plugin,
condition: PermissionLib.NO_CONDITION,
permissionId: DAO(payable(_dao)).EXECUTE_PERMISSION_ID()
});
{
// Deploy the Std proposal condition
StandardProposalCondition stdProposalCondition =
new StandardProposalCondition(address(_dao), installationParams.stdProposalMinDuration);
// Proposer plugins can create proposals
permissions[3] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Grant,
where: plugin,
who: installationParams.stdProposer,
condition: address(stdProposalCondition),
permissionId: optimisticTokenVotingPluginBase.PROPOSER_PERMISSION_ID()
});
}
permissions[4] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Grant,
where: plugin,
who: installationParams.emergencyProposer,
condition: PermissionLib.NO_CONDITION,
permissionId: optimisticTokenVotingPluginBase.PROPOSER_PERMISSION_ID()
});
if (installationParams.tokenSettings.addr == address(0x0)) {
// The DAO can mint ERC20 tokens
permissions[5] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Grant,
where: token,
who: _dao,
condition: PermissionLib.NO_CONDITION,
permissionId: GovernanceERC20(token).MINT_PERMISSION_ID()
});
}
preparedSetupData.helpers = helpers;
preparedSetupData.permissions = permissions;
}
/// @inheritdoc IPluginSetup
function prepareUninstallation(address _dao, SetupPayload calldata _payload)
external
view
returns (PermissionLib.MultiTargetPermission[] memory permissions)
{
// Prepare permissions.
uint256 helperLength = _payload.currentHelpers.length;
if (helperLength != 1) {
revert WrongHelpersArrayLength({length: helperLength});
}
// token can be either GovernanceERC20, GovernanceWrappedERC20, or IVotesUpgradeable, which
// does not follow the GovernanceERC20 and GovernanceWrappedERC20 standard.
address token = _payload.currentHelpers[0];
bool isGovernanceERC20 =
_supportsErc20(token) && _supportsIVotes(token) && !_supportsIGovernanceWrappedERC20(token);
permissions = new PermissionLib.MultiTargetPermission[](isGovernanceERC20 ? 4 : 3);
// Set permissions to be Revoked.
permissions[0] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Revoke,
where: _payload.plugin,
who: _dao,
condition: PermissionLib.NO_CONDITION,
permissionId: optimisticTokenVotingPluginBase.UPDATE_OPTIMISTIC_GOVERNANCE_SETTINGS_PERMISSION_ID()
});
permissions[1] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Revoke,
where: _payload.plugin,
who: _dao,
condition: PermissionLib.NO_CONDITION,
permissionId: optimisticTokenVotingPluginBase.UPGRADE_PLUGIN_PERMISSION_ID()
});
permissions[2] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Revoke,
where: _dao,
who: _payload.plugin,
condition: PermissionLib.NO_CONDITION,
permissionId: DAO(payable(_dao)).EXECUTE_PERMISSION_ID()
});
// Note: It no longer matters if proposers can still create proposals
// Revocation of permission is necessary only if the deployed token is GovernanceERC20,
// as GovernanceWrapped does not possess this permission. Only return the following
// if it's type of GovernanceERC20, otherwise revoking this permission wouldn't have any effect.
if (isGovernanceERC20) {
permissions[3] = PermissionLib.MultiTargetPermission({
operation: PermissionLib.Operation.Revoke,
where: token,
who: _dao,
condition: PermissionLib.NO_CONDITION,
permissionId: GovernanceERC20(token).MINT_PERMISSION_ID()
});
}
}
/// @inheritdoc IPluginSetup
function implementation() external view virtual override returns (address) {
return address(optimisticTokenVotingPluginBase);
}
/// @notice Encodes the given installation parameters into a byte array
function encodeInstallationParams(InstallationParameters memory installationParams)
external
pure
returns (bytes memory)
{
return abi.encode(installationParams);
}
/// @notice Decodes the given byte array into the original installation parameters
function decodeInstallationParams(bytes memory _data)
public
pure
returns (InstallationParameters memory installationParams)
{
installationParams = abi.decode(_data, (InstallationParameters));
}
/// @notice Unsatisfiably determines if the contract is an ERC20 token.
/// @dev It's important to first check whether token is a contract prior to this call.
/// @param token The token address
function _supportsErc20(address token) private view returns (bool) {
(bool success, bytes memory data) =
token.staticcall(abi.encodeCall(IERC20Upgradeable.balanceOf, (address(this))));
if (!success || data.length != 0x20) return false;
(success, data) = token.staticcall(abi.encodeCall(IERC20Upgradeable.totalSupply, ()));
if (!success || data.length != 0x20) return false;
(success, data) = token.staticcall(abi.encodeCall(IERC20Upgradeable.allowance, (address(this), address(this))));
if (!success || data.length != 0x20) return false;
return true;
}
/// @notice Unsatisfiably determines if the contract is an ERC20 token.
/// @dev It's important to first check whether token is a contract prior to this call.
/// @param token The token address
function _supportsIVotes(address token) private view returns (bool) {
(bool success, bytes memory data) =
token.staticcall(abi.encodeCall(IVotesUpgradeable.getVotes, (address(this))));
if (!success || data.length != 0x20) return false;
(success, data) = token.staticcall(abi.encodeCall(IVotesUpgradeable.getPastVotes, (address(this), 0)));
if (!success || data.length != 0x20) return false;
(success, data) = token.staticcall(abi.encodeCall(IVotesUpgradeable.getPastTotalSupply, (0)));
if (!success || data.length != 0x20) return false;
(success, data) = token.staticcall(abi.encodeCall(IVotesUpgradeable.delegates, (address(this))));
if (!success || data.length != 0x20) return false;
return true;
}
/// @notice Unsatisfiably determines if the contract is an ERC20 token.
/// @dev It's important to first check whether token is a contract prior to this call.
/// @param token The token address
function _supportsIGovernanceWrappedERC20(address token) private view returns (bool) {
(bool success, bytes memory data) = token.staticcall(abi.encodeCall(IERC165.supportsInterface, (bytes4(0))));
if (!success || data.length != 0x20) return false;
return IERC165(token).supportsInterface(type(IGovernanceWrappedERC20).interfaceId);
}
}