-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVoterProxy.sol
286 lines (243 loc) · 9.21 KB
/
VoterProxy.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./Interfaces/IGauge.sol";
import "./Interfaces/IVoteEscrow.sol";
import "./Interfaces/IDeposit.sol";
import "./Interfaces/IFeeDistro.sol";
import "./Interfaces/IVoting.sol";
import "./Interfaces/ITokenMinter.sol";
contract VoterProxy {
using SafeERC20 for IERC20;
using Address for address;
using SafeMath for uint256;
address public immutable veAsset;
address public immutable escrow;
address public immutable gaugeProxy;
address public immutable minter;
address public owner;
address public operator;
address public depositor;
string public name;
IVoteEscrow.EscrowModle public escrowModle;
mapping(address => bool) private protectedTokens;
mapping(address => bool) private stashPool;
mapping(bytes32 => bool) private votes;
bytes4 internal constant EIP1271_MAGIC_VALUE = 0x1626ba7e;
event VoteSet(bytes32 hash, bool valid);
constructor(
string memory _name,
address _veAsset,
address _escrow,
address _gaugeProxy,
address _minter,
IVoteEscrow.EscrowModle _escrowModle
) {
name = _name;
veAsset = _veAsset;
escrow = _escrow;
gaugeProxy = _gaugeProxy;
owner = msg.sender;
minter = _minter;
escrowModle = _escrowModle;
}
function getName() external view returns (string memory) {
return name;
}
function setOwner(address _owner) external {
require(msg.sender == owner, "!auth");
owner = _owner;
}
function setOperator(address _operator) external {
require(msg.sender == owner, "!auth");
require(
operator == address(0) || IDeposit(operator).isShutdown() == true,
"needs shutdown"
);
operator = _operator;
}
function setDepositor(address _depositor) external {
require(msg.sender == owner, "!auth");
depositor = _depositor;
}
function setStashAccess(address _stash, bool _status) external returns (bool) {
require(msg.sender == operator, "!auth");
if (_stash != address(0)) {
stashPool[_stash] = _status;
}
return true;
}
function deposit(address _token, address _gauge) external returns (bool) {
require(msg.sender == operator, "!auth");
if (protectedTokens[_token] == false) {
protectedTokens[_token] = true;
}
if (protectedTokens[_gauge] == false) {
protectedTokens[_gauge] = true;
}
uint256 balance = IERC20(_token).balanceOf(address(this));
if (balance > 0) {
IERC20(_token).safeApprove(_gauge, 0);
IERC20(_token).safeApprove(_gauge, balance);
IGauge(_gauge).deposit(balance);
}
return true;
}
//stash only function for pulling extra incentive reward tokens out
function withdraw(IERC20 _asset) external returns (uint256 balance) {
require(stashPool[msg.sender] == true, "!auth");
//check protection
if (protectedTokens[address(_asset)] == true) {
return 0;
}
balance = _asset.balanceOf(address(this));
_asset.safeTransfer(msg.sender, balance);
return balance;
}
// Withdraw partial funds
function withdraw(
address _token,
address _gauge,
uint256 _amount
) public returns (bool) {
require(msg.sender == operator, "!auth");
uint256 _balance = IERC20(_token).balanceOf(address(this));
if (_balance < _amount) {
_amount = _withdrawSome(_gauge, _amount.sub(_balance));
_amount = _amount.add(_balance);
}
IERC20(_token).safeTransfer(msg.sender, _amount);
return true;
}
function withdrawAll(address _token, address _gauge) external returns (bool) {
require(msg.sender == operator, "!auth");
uint256 amount = balanceOfPool(_gauge).add(IERC20(_token).balanceOf(address(this)));
withdraw(_token, _gauge, amount);
return true;
}
function _withdrawSome(address _gauge, uint256 _amount) internal returns (uint256) {
IGauge(_gauge).withdraw(_amount);
return _amount;
}
function createLock(uint256 _value, uint256 _unlockTime) external returns (bool) {
require(msg.sender == depositor, "!auth");
IERC20(veAsset).safeApprove(escrow, 0);
IERC20(veAsset).safeApprove(escrow, _value);
IVoteEscrow(escrow).create_lock(_value, _unlockTime);
return true;
}
function increaseAmount(uint256 _value) external returns (bool) {
require(msg.sender == depositor, "!auth");
IERC20(veAsset).safeApprove(escrow, 0);
IERC20(veAsset).safeApprove(escrow, _value);
IVoteEscrow(escrow).increase_amount(_value);
return true;
}
function increaseTime(uint256 _value) external returns (bool) {
require(msg.sender == depositor, "!auth");
IVoteEscrow(escrow).increase_unlock_time(_value);
return true;
}
function release() external returns (bool) {
require(msg.sender == depositor, "!auth");
IVoteEscrow(escrow).withdraw();
return true;
}
/**
* @notice Save a vote hash so when snapshot.org asks this contract if
* a vote signature is valid we are able to check for a valid hash
* and return the appropriate response inline with EIP 1721
* @param _hash Hash of vote signature that was sent to snapshot.org
* @param _valid Is the hash valid
*/
function setVote(bytes32 _hash, bool _valid) external {
require(msg.sender == operator, "!auth");
votes[_hash] = _valid;
emit VoteSet(_hash, _valid);
}
/**
* @notice Verifies that the hash is valid
* @dev Snapshot Hub will call this function when a vote is submitted using
* snapshot.js on behalf of this contract. Snapshot Hub will call this
* function with the hash and the signature of the vote that was cast.
* @param _hash Hash of the message that was sent to Snapshot Hub to cast a vote
* @return EIP1271 magic value if the signature is value
*/
function isValidSignature(bytes32 _hash, bytes memory) public view returns (bytes4) {
if (votes[_hash]) {
return EIP1271_MAGIC_VALUE;
} else {
return 0xffffffff;
}
}
function voteGaugeWeight(address[] calldata _tokenVote, uint256[] calldata _weight)
external
returns (bool)
{
require(msg.sender == operator, "!auth");
if (escrowModle == IVoteEscrow.EscrowModle.PICKLE) {
//vote
IVoting(gaugeProxy).vote(_tokenVote, _weight);
} else {
for (uint256 i = 0; i < _tokenVote.length; i++) {
IVoting(gaugeProxy).vote_for_gauge_weights(_tokenVote[i], _weight[i]);
}
}
return true;
}
function claimVeAsset(address _gauge) external returns (uint256) {
require(msg.sender == operator, "!auth");
uint256 _balance = 0;
if (escrowModle == IVoteEscrow.EscrowModle.PICKLE) {
try IGauge(_gauge).getReward() {} catch {
return _balance;
}
} else if (
escrowModle == IVoteEscrow.EscrowModle.CURVE ||
escrowModle == IVoteEscrow.EscrowModle.RIBBON
) {
try ITokenMinter(minter).mint(_gauge) {} catch {
return _balance;
}
} else if (escrowModle == IVoteEscrow.EscrowModle.IDLE) {
try ITokenMinter(minter).distribute(_gauge) {} catch {
return _balance;
}
} else if (escrowModle == IVoteEscrow.EscrowModle.ANGLE) {
try IGauge(_gauge).claim_rewards() {} catch {
return _balance;
}
}
_balance = IERC20(veAsset).balanceOf(address(this));
IERC20(veAsset).safeTransfer(operator, _balance);
return _balance;
}
function claimRewards(address _gauge) external returns (bool) {
require(msg.sender == operator, "!auth");
IGauge(_gauge).claim_rewards();
return true;
}
function claimFees(address _distroContract, address _token) external returns (uint256) {
require(msg.sender == operator, "!auth");
IFeeDistro(_distroContract).claim();
uint256 _balance = IERC20(_token).balanceOf(address(this));
IERC20(_token).safeTransfer(operator, _balance);
return _balance;
}
function balanceOfPool(address _gauge) public view returns (uint256) {
return IGauge(_gauge).balanceOf(address(this));
}
function execute(
address _to,
uint256 _value,
bytes calldata _data
) external returns (bool, bytes memory) {
require(msg.sender == operator, "!auth");
(bool success, bytes memory result) = _to.call{value: _value}(_data);
require(success, "!success");
return (success, result);
}
}