-
Notifications
You must be signed in to change notification settings - Fork 19
/
GovernanceStrategyV2.sol
216 lines (198 loc) · 5.71 KB
/
GovernanceStrategyV2.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.7.5;
pragma abicoder v2;
import { IGovernancePowerDelegationERC20 } from '../../interfaces/IGovernancePowerDelegationERC20.sol';
import { IGovernanceStrategy } from '../../interfaces/IGovernanceStrategy.sol';
import { GovernancePowerDelegationERC20Mixin } from '../token/GovernancePowerDelegationERC20Mixin.sol';
interface IDydxToken {
function _totalSupplySnapshots(
uint256
)
external
view
returns (GovernancePowerDelegationERC20Mixin.Snapshot memory);
function _totalSupplySnapshotsCount()
external
view
returns (uint256);
}
/**
* @title GovernanceStrategyV2
* @author dYdX
*
* @notice Smart contract containing logic to measure users' relative governance power for creating
* and voting on proposals.
*
* User Power = User Power from each of: DYDX, stkDYDX, wethDYDX.
* User Power from Token = Token Power + Token Power as Delegatee [- Token Power if user has delegated]
* Two wrapper functions linked to DYDX tokens's GovernancePowerDelegationERC20Mixin.sol implementation
* - getPropositionPowerAt: fetching a user Proposition Power at a specified block
* - getVotingPowerAt: fetching a user Voting Power at a specified block
*/
contract GovernanceStrategyV2 is
IGovernanceStrategy
{
// ============ Constants ============
/// @notice The DYDX governance token.
address public immutable DYDX_TOKEN;
/// @notice Token representing staked positions of the DYDX token.
address public immutable STAKED_DYDX_TOKEN;
/// @notice Token representing Wrapped Ethereum DYDX tokens.
address public immutable WRAPPED_ETHEREUM_DYDX_TOKEN;
// ============ Constructor ============
constructor(
address dydxToken,
address stakedDydxToken,
address wrappedEthereumDydxToken
) {
DYDX_TOKEN = dydxToken;
STAKED_DYDX_TOKEN = stakedDydxToken;
WRAPPED_ETHEREUM_DYDX_TOKEN = wrappedEthereumDydxToken;
}
// ============ Other Functions ============
/**
* @notice Get the total supply of proposition power, for the purpose of determining if a
* proposing threshold was reached.
*
* @param blockNumber Block number at which to evaluate.
*
* @return The total proposition power supply at the given block number.
*/
function getTotalPropositionSupplyAt(
uint256 blockNumber
)
public
view
override
returns (uint256)
{
return _getTotalSupplyAt(blockNumber);
}
/**
* @notice Get the total supply of voting power, for the purpose of determining if quorum or vote
* differential tresholds were reached.
*
* @param blockNumber Block number at which to evaluate.
*
* @return The total voting power supply at the given block number.
*/
function getTotalVotingSupplyAt(
uint256 blockNumber
)
public
view
override
returns (uint256)
{
return _getTotalSupplyAt(blockNumber);
}
/**
* @notice The proposition power of an address at a given block number.
*
* @param user Address to check.
* @param blockNumber Block number at which to evaluate.
*
* @return The proposition power of the address at the given block number.
*/
function getPropositionPowerAt(
address user,
uint256 blockNumber
)
public
view
override
returns (uint256)
{
return _getPowerByTypeAt(
user,
blockNumber,
IGovernancePowerDelegationERC20.DelegationType.PROPOSITION_POWER
);
}
/**
* @notice The voting power of an address at a given block number.
*
* @param user Address of the user.
* @param blockNumber Block number at which to evaluate.
*
* @return The voting power of the address at the given block number.
*/
function getVotingPowerAt(
address user,
uint256 blockNumber
)
public
view
override
returns (uint256)
{
return _getPowerByTypeAt(
user,
blockNumber,
IGovernancePowerDelegationERC20.DelegationType.VOTING_POWER
);
}
function _getPowerByTypeAt(
address user,
uint256 blockNumber,
IGovernancePowerDelegationERC20.DelegationType powerType
)
internal
view
returns (uint256)
{
return (
IGovernancePowerDelegationERC20(DYDX_TOKEN).getPowerAtBlock(
user,
blockNumber,
powerType
) +
IGovernancePowerDelegationERC20(STAKED_DYDX_TOKEN).getPowerAtBlock(
user,
blockNumber,
powerType
) +
IGovernancePowerDelegationERC20(WRAPPED_ETHEREUM_DYDX_TOKEN).getPowerAtBlock(
user,
blockNumber,
powerType
)
);
}
/**
* @dev The total supply of the DYDX token at a given block number.
*
* @param blockNumber Block number at which to evaluate.
*
* @return The total DYDX token supply at the given block number.
*/
function _getTotalSupplyAt(
uint256 blockNumber
)
internal
view
returns (uint256)
{
IDydxToken dydxToken = IDydxToken(DYDX_TOKEN);
uint256 snapshotsCount = dydxToken._totalSupplySnapshotsCount();
// Iterate in reverse over the total supply snapshots, up to index 1.
for (uint256 i = snapshotsCount - 1; i != 0; i--) {
GovernancePowerDelegationERC20Mixin.Snapshot memory snapshot = (
dydxToken._totalSupplySnapshots(i)
);
if (snapshot.blockNumber <= blockNumber) {
return snapshot.value;
}
}
// If blockNumber was on or after the first snapshot, then return the initial supply.
// Else, blockNumber is before token launch so return 0.
GovernancePowerDelegationERC20Mixin.Snapshot memory firstSnapshot = (
dydxToken._totalSupplySnapshots(0)
);
if (firstSnapshot.blockNumber <= blockNumber) {
return firstSnapshot.value;
} else {
return 0;
}
}
}