@evvm/testnet-contracts 2.2.2 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +145 -118
- package/README.md +135 -42
- package/contracts/evvm/Evvm.sol +154 -181
- package/contracts/evvm/lib/ErrorsLib.sol +119 -6
- package/contracts/evvm/lib/EvvmStorage.sol +164 -9
- package/contracts/evvm/lib/EvvmStructs.sol +124 -6
- package/contracts/evvm/lib/SignatureUtils.sol +103 -61
- package/contracts/nameService/NameService.sol +165 -155
- package/contracts/nameService/lib/ErrorsLib.sol +142 -8
- package/contracts/nameService/lib/IdentityValidation.sol +21 -0
- package/contracts/nameService/lib/NameServiceStructs.sol +75 -19
- package/contracts/nameService/lib/SignatureUtils.sol +235 -60
- package/contracts/p2pSwap/P2PSwap.sol +205 -164
- package/contracts/staking/Estimator.sol +131 -24
- package/contracts/staking/Staking.sol +98 -113
- package/contracts/staking/lib/ErrorsLib.sol +79 -3
- package/contracts/staking/lib/SignatureUtils.sol +82 -16
- package/contracts/staking/lib/StakingStructs.sol +12 -0
- package/contracts/treasury/Treasury.sol +30 -12
- package/contracts/treasury/lib/ErrorsLib.sol +30 -0
- package/contracts/treasuryTwoChains/TreasuryHostChainStation.sol +3 -3
- package/interfaces/IEvvm.sol +9 -4
- package/interfaces/INameService.sol +12 -3
- package/interfaces/IStaking.sol +2 -1
- package/library/Erc191TestBuilder.sol +188 -0
- package/library/EvvmService.sol +55 -0
- package/library/primitives/SignatureRecover.sol +33 -0
- package/library/utils/AdvancedStrings.sol +61 -0
- package/library/utils/SignatureUtil.sol +34 -0
- package/library/utils/nonces/AsyncNonce.sol +42 -0
- package/library/utils/nonces/SyncNonce.sol +44 -0
- package/library/utils/service/EvvmPayments.sol +68 -1
- package/library/utils/service/StakingServiceUtils.sol +44 -0
- package/package.json +2 -1
|
@@ -12,26 +12,62 @@ MM .M `88888P' dP dP dP dP dP `88888P8 dP `88888P' dP
|
|
|
12
12
|
MMMMMMMMMMMM
|
|
13
13
|
|
|
14
14
|
* @title Staking Estimator Contract
|
|
15
|
-
* @author Mate
|
|
15
|
+
* @author Mate Labs
|
|
16
|
+
* @notice Contract responsible for calculating staking rewards based on time-weighted averages
|
|
17
|
+
* @dev This contract works in conjunction with the Staking contract to calculate
|
|
18
|
+
* and distribute rewards to stakers based on their participation during epochs.
|
|
19
|
+
*
|
|
20
|
+
* Key Responsibilities:
|
|
21
|
+
* - Track epoch metadata (pool, total staked, time periods)
|
|
22
|
+
* - Calculate time-weighted average stake for each user
|
|
23
|
+
* - Determine proportional reward distribution
|
|
24
|
+
*
|
|
25
|
+
* Reward Calculation Formula:
|
|
26
|
+
* The time-weighted average is calculated as:
|
|
27
|
+
* Sum of [(ti - ti-1) x Si-1] x 10^18 / (tFinal - tStart)
|
|
28
|
+
*
|
|
29
|
+
* Where:
|
|
30
|
+
* - ti: timestamp of current iteration
|
|
31
|
+
* - ti-1: timestamp of previous iteration
|
|
32
|
+
* - Si-1: stake amount at previous iteration
|
|
33
|
+
* - tFinal: epoch end timestamp
|
|
34
|
+
* - tStart: epoch start timestamp
|
|
35
|
+
*
|
|
36
|
+
* Access Control:
|
|
37
|
+
* - onlyStaking: Functions callable only by the Staking contract
|
|
38
|
+
* - onlyActivator: Functions callable only by the epoch activator
|
|
39
|
+
* - onlyAdmin: Administrative functions for governance
|
|
16
40
|
*/
|
|
17
41
|
|
|
18
42
|
import {Staking} from "@evvm/testnet-contracts/contracts/staking/Staking.sol";
|
|
19
|
-
import {
|
|
43
|
+
import {
|
|
44
|
+
StakingStructs
|
|
45
|
+
} from "@evvm/testnet-contracts/contracts/staking/lib/StakingStructs.sol";
|
|
20
46
|
import {Evvm} from "@evvm/testnet-contracts/contracts/evvm/Evvm.sol";
|
|
21
47
|
|
|
22
48
|
contract Estimator {
|
|
49
|
+
/// @dev Struct for managing address change proposals with time delay
|
|
23
50
|
struct AddressTypeProposal {
|
|
24
51
|
address actual;
|
|
25
52
|
address proposal;
|
|
26
53
|
uint256 timeToAccept;
|
|
27
54
|
}
|
|
28
55
|
|
|
56
|
+
/// @dev Struct for managing uint256 value proposals with time delay
|
|
29
57
|
struct UintTypeProposal {
|
|
30
58
|
uint256 actual;
|
|
31
59
|
uint256 proposal;
|
|
32
60
|
uint256 timeToAccept;
|
|
33
61
|
}
|
|
34
62
|
|
|
63
|
+
/**
|
|
64
|
+
* @dev Struct containing epoch metadata for reward calculations
|
|
65
|
+
* @param tokenPool Address of the token being distributed as rewards
|
|
66
|
+
* @param totalPool Total amount of tokens available for distribution
|
|
67
|
+
* @param totalStaked Total staking tokens staked during this epoch
|
|
68
|
+
* @param tFinal Timestamp when the epoch ended
|
|
69
|
+
* @param tStart Timestamp when the epoch started
|
|
70
|
+
*/
|
|
35
71
|
struct EpochMetadata {
|
|
36
72
|
address tokenPool;
|
|
37
73
|
uint256 totalPool;
|
|
@@ -40,33 +76,53 @@ contract Estimator {
|
|
|
40
76
|
uint256 tStart;
|
|
41
77
|
}
|
|
42
78
|
|
|
79
|
+
/// @dev Current epoch metadata storage
|
|
43
80
|
EpochMetadata private epoch;
|
|
81
|
+
/// @dev Proposal system for activator address changes
|
|
44
82
|
AddressTypeProposal private activator;
|
|
83
|
+
/// @dev Proposal system for EVVM address changes
|
|
45
84
|
AddressTypeProposal private evvmAddress;
|
|
85
|
+
/// @dev Proposal system for Staking contract address changes
|
|
46
86
|
AddressTypeProposal private addressStaking;
|
|
87
|
+
/// @dev Proposal system for admin address changes
|
|
47
88
|
AddressTypeProposal private admin;
|
|
48
89
|
|
|
90
|
+
/// @dev Transaction type identifier for deposit (staking) operations
|
|
49
91
|
bytes32 constant DEPOSIT_IDENTIFIER = bytes32(uint256(1));
|
|
92
|
+
/// @dev Transaction type identifier for withdraw (unstaking) operations
|
|
50
93
|
bytes32 constant WITHDRAW_IDENTIFIER = bytes32(uint256(2));
|
|
94
|
+
/// @dev Beginning identifier same as withdraw for epoch tracking
|
|
51
95
|
bytes32 constant BEGUIN_IDENTIFIER = WITHDRAW_IDENTIFIER;
|
|
52
96
|
|
|
97
|
+
/// @dev Current epoch identifier, increments with each new epoch
|
|
53
98
|
bytes32 epochId = bytes32(uint256(3));
|
|
54
99
|
|
|
100
|
+
/// @dev Restricts function access to the Staking contract only
|
|
55
101
|
modifier onlyStaking() {
|
|
56
102
|
if (msg.sender != addressStaking.actual) revert();
|
|
57
103
|
_;
|
|
58
104
|
}
|
|
59
105
|
|
|
106
|
+
/// @dev Restricts function access to the activator address only
|
|
60
107
|
modifier onlyActivator() {
|
|
61
108
|
if (msg.sender != activator.actual) revert();
|
|
62
109
|
_;
|
|
63
110
|
}
|
|
64
111
|
|
|
112
|
+
/// @dev Restricts function access to the admin address only
|
|
65
113
|
modifier onlyAdmin() {
|
|
66
114
|
if (msg.sender != admin.actual) revert();
|
|
67
115
|
_;
|
|
68
116
|
}
|
|
69
117
|
|
|
118
|
+
/**
|
|
119
|
+
* @notice Initializes the Estimator contract
|
|
120
|
+
* @dev Sets up all required addresses for contract operation
|
|
121
|
+
* @param _activator Address authorized to start new epochs
|
|
122
|
+
* @param _evvmAddress Address of the EVVM core contract
|
|
123
|
+
* @param _addressStaking Address of the Staking contract
|
|
124
|
+
* @param _admin Address with administrative privileges
|
|
125
|
+
*/
|
|
70
126
|
constructor(
|
|
71
127
|
address _activator,
|
|
72
128
|
address _evvmAddress,
|
|
@@ -79,6 +135,14 @@ contract Estimator {
|
|
|
79
135
|
admin.actual = _admin;
|
|
80
136
|
}
|
|
81
137
|
|
|
138
|
+
/**
|
|
139
|
+
* @notice Starts a new reward epoch with the provided parameters
|
|
140
|
+
* @dev Only callable by the activator address. Records epoch metadata for reward calculations.
|
|
141
|
+
* @param tokenPool Address of the token to be distributed as rewards
|
|
142
|
+
* @param totalPool Total amount of tokens available for distribution this epoch
|
|
143
|
+
* @param totalStaked Total staking tokens staked at epoch start
|
|
144
|
+
* @param tStart Timestamp when the epoch started
|
|
145
|
+
*/
|
|
82
146
|
function notifyNewEpoch(
|
|
83
147
|
address tokenPool,
|
|
84
148
|
uint256 totalPool,
|
|
@@ -94,6 +158,17 @@ contract Estimator {
|
|
|
94
158
|
});
|
|
95
159
|
}
|
|
96
160
|
|
|
161
|
+
/**
|
|
162
|
+
* @notice Calculates and returns the reward amount for a specific user
|
|
163
|
+
* @dev Only callable by the Staking contract. Uses time-weighted average calculation
|
|
164
|
+
* to determine proportional rewards based on staking duration and amount.
|
|
165
|
+
* @param _user Address of the user to calculate rewards for
|
|
166
|
+
* @return epochAnswer Epoch identifier to record in user history
|
|
167
|
+
* @return tokenAddress Address of the reward token
|
|
168
|
+
* @return amountTotalToBeRewarded Calculated reward amount for the user
|
|
169
|
+
* @return idToOverwrite Index in user history to update with reward info
|
|
170
|
+
* @return timestampToOverwrite Timestamp to record for the reward transaction
|
|
171
|
+
*/
|
|
97
172
|
function makeEstimation(
|
|
98
173
|
address _user
|
|
99
174
|
)
|
|
@@ -107,7 +182,6 @@ contract Estimator {
|
|
|
107
182
|
uint256 timestampToOverwrite
|
|
108
183
|
)
|
|
109
184
|
{
|
|
110
|
-
|
|
111
185
|
uint256 totSmLast;
|
|
112
186
|
uint256 sumSmT;
|
|
113
187
|
|
|
@@ -125,7 +199,6 @@ contract Estimator {
|
|
|
125
199
|
|
|
126
200
|
if (size == 1) totSmLast = h.totalStaked;
|
|
127
201
|
|
|
128
|
-
|
|
129
202
|
if (h.timestamp > epoch.tFinal) {
|
|
130
203
|
if (totSmLast > 0) sumSmT += (epoch.tFinal - tLast) * totSmLast;
|
|
131
204
|
|
|
@@ -138,7 +211,6 @@ contract Estimator {
|
|
|
138
211
|
|
|
139
212
|
if (totSmLast > 0) sumSmT += (h.timestamp - tLast) * totSmLast;
|
|
140
213
|
|
|
141
|
-
|
|
142
214
|
tLast = h.timestamp;
|
|
143
215
|
totSmLast = h.totalStaked;
|
|
144
216
|
idToOverwrite = i;
|
|
@@ -168,7 +240,6 @@ contract Estimator {
|
|
|
168
240
|
|
|
169
241
|
timestampToOverwrite = epoch.tFinal;
|
|
170
242
|
|
|
171
|
-
|
|
172
243
|
epoch.totalPool -= amountTotalToBeRewarded;
|
|
173
244
|
epoch.totalStaked -= h.totalStaked;
|
|
174
245
|
}
|
|
@@ -177,18 +248,20 @@ contract Estimator {
|
|
|
177
248
|
// Admin functions
|
|
178
249
|
//⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻
|
|
179
250
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
) external onlyActivator {
|
|
251
|
+
/// @notice Proposes a new activator address with 1-day time delay
|
|
252
|
+
/// @param _proposal Address of the proposed new activator
|
|
253
|
+
function setActivatorProposal(address _proposal) external onlyActivator {
|
|
183
254
|
activator.proposal = _proposal;
|
|
184
255
|
activator.timeToAccept = block.timestamp + 1 days;
|
|
185
256
|
}
|
|
186
257
|
|
|
258
|
+
/// @notice Cancels the pending activator proposal
|
|
187
259
|
function cancelActivatorProposal() external onlyActivator {
|
|
188
260
|
activator.proposal = address(0);
|
|
189
261
|
activator.timeToAccept = 0;
|
|
190
262
|
}
|
|
191
263
|
|
|
264
|
+
/// @notice Accepts the activator proposal after time delay
|
|
192
265
|
function acceptActivatorProposal() external {
|
|
193
266
|
if (block.timestamp < activator.timeToAccept) revert();
|
|
194
267
|
|
|
@@ -197,18 +270,20 @@ contract Estimator {
|
|
|
197
270
|
activator.timeToAccept = 0;
|
|
198
271
|
}
|
|
199
272
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
) external onlyAdmin {
|
|
273
|
+
/// @notice Proposes a new EVVM address with 1-day time delay
|
|
274
|
+
/// @param _proposal Address of the proposed new EVVM contract
|
|
275
|
+
function setEvvmAddressProposal(address _proposal) external onlyAdmin {
|
|
203
276
|
evvmAddress.proposal = _proposal;
|
|
204
277
|
evvmAddress.timeToAccept = block.timestamp + 1 days;
|
|
205
278
|
}
|
|
206
279
|
|
|
280
|
+
/// @notice Cancels the pending EVVM address proposal
|
|
207
281
|
function cancelEvvmAddressProposal() external onlyAdmin {
|
|
208
282
|
evvmAddress.proposal = address(0);
|
|
209
283
|
evvmAddress.timeToAccept = 0;
|
|
210
284
|
}
|
|
211
285
|
|
|
286
|
+
/// @notice Accepts the EVVM address proposal after time delay
|
|
212
287
|
function acceptEvvmAddressProposal() external onlyAdmin {
|
|
213
288
|
if (block.timestamp < evvmAddress.timeToAccept) revert();
|
|
214
289
|
|
|
@@ -217,18 +292,20 @@ contract Estimator {
|
|
|
217
292
|
evvmAddress.timeToAccept = 0;
|
|
218
293
|
}
|
|
219
294
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
) external onlyAdmin {
|
|
295
|
+
/// @notice Proposes a new Staking contract address with 1-day time delay
|
|
296
|
+
/// @param _proposal Address of the proposed new Staking contract
|
|
297
|
+
function setAddressStakingProposal(address _proposal) external onlyAdmin {
|
|
223
298
|
addressStaking.proposal = _proposal;
|
|
224
299
|
addressStaking.timeToAccept = block.timestamp + 1 days;
|
|
225
300
|
}
|
|
226
301
|
|
|
302
|
+
/// @notice Cancels the pending Staking address proposal
|
|
227
303
|
function cancelAddressStakingProposal() external onlyAdmin {
|
|
228
304
|
addressStaking.proposal = address(0);
|
|
229
305
|
addressStaking.timeToAccept = 0;
|
|
230
306
|
}
|
|
231
307
|
|
|
308
|
+
/// @notice Accepts the Staking address proposal after time delay
|
|
232
309
|
function acceptAddressStakingProposal() external onlyAdmin {
|
|
233
310
|
if (block.timestamp < addressStaking.timeToAccept) revert();
|
|
234
311
|
|
|
@@ -237,18 +314,20 @@ contract Estimator {
|
|
|
237
314
|
addressStaking.timeToAccept = 0;
|
|
238
315
|
}
|
|
239
316
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
) external onlyAdmin {
|
|
317
|
+
/// @notice Proposes a new admin address with 1-day time delay
|
|
318
|
+
/// @param _proposal Address of the proposed new admin
|
|
319
|
+
function setAdminProposal(address _proposal) external onlyAdmin {
|
|
243
320
|
admin.proposal = _proposal;
|
|
244
321
|
admin.timeToAccept = block.timestamp + 1 days;
|
|
245
322
|
}
|
|
246
323
|
|
|
324
|
+
/// @notice Cancels the pending admin proposal
|
|
247
325
|
function cancelAdminProposal() external onlyAdmin {
|
|
248
326
|
admin.proposal = address(0);
|
|
249
327
|
admin.timeToAccept = 0;
|
|
250
328
|
}
|
|
251
329
|
|
|
330
|
+
/// @notice Accepts the admin proposal after time delay
|
|
252
331
|
function acceptAdminProposal() external {
|
|
253
332
|
if (block.timestamp < admin.timeToAccept) revert();
|
|
254
333
|
|
|
@@ -261,22 +340,36 @@ contract Estimator {
|
|
|
261
340
|
// Getters
|
|
262
341
|
//⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻
|
|
263
342
|
|
|
343
|
+
/// @notice Returns the current epoch metadata
|
|
344
|
+
/// @return Complete EpochMetadata struct with pool and timing information
|
|
264
345
|
function getEpochMetadata() external view returns (EpochMetadata memory) {
|
|
265
346
|
return epoch;
|
|
266
347
|
}
|
|
267
348
|
|
|
349
|
+
/// @notice Returns the current epoch number as uint256
|
|
350
|
+
/// @return Current epoch number (epochId - 2)
|
|
268
351
|
function getActualEpochInUint() external view returns (uint256) {
|
|
269
352
|
return uint256(epochId) - 2;
|
|
270
353
|
}
|
|
271
354
|
|
|
355
|
+
/// @notice Returns the current epoch identifier in bytes32 format
|
|
356
|
+
/// @return Current epoch identifier
|
|
272
357
|
function getActualEpochInFormat() external view returns (bytes32) {
|
|
273
358
|
return epochId;
|
|
274
359
|
}
|
|
275
360
|
|
|
276
|
-
|
|
361
|
+
/// @notice Returns the activator address proposal information
|
|
362
|
+
/// @return Complete AddressTypeProposal struct for activator
|
|
363
|
+
function getActivatorMetadata()
|
|
364
|
+
external
|
|
365
|
+
view
|
|
366
|
+
returns (AddressTypeProposal memory)
|
|
367
|
+
{
|
|
277
368
|
return activator;
|
|
278
369
|
}
|
|
279
370
|
|
|
371
|
+
/// @notice Returns the EVVM address proposal information
|
|
372
|
+
/// @return Complete AddressTypeProposal struct for EVVM
|
|
280
373
|
function getEvvmAddressMetadata()
|
|
281
374
|
external
|
|
282
375
|
view
|
|
@@ -285,6 +378,8 @@ contract Estimator {
|
|
|
285
378
|
return evvmAddress;
|
|
286
379
|
}
|
|
287
380
|
|
|
381
|
+
/// @notice Returns the Staking contract address proposal information
|
|
382
|
+
/// @return Complete AddressTypeProposal struct for Staking
|
|
288
383
|
function getAddressStakingMetadata()
|
|
289
384
|
external
|
|
290
385
|
view
|
|
@@ -293,12 +388,26 @@ contract Estimator {
|
|
|
293
388
|
return addressStaking;
|
|
294
389
|
}
|
|
295
390
|
|
|
296
|
-
|
|
391
|
+
/// @notice Returns the admin address proposal information
|
|
392
|
+
/// @return Complete AddressTypeProposal struct for admin
|
|
393
|
+
function getAdminMetadata()
|
|
394
|
+
external
|
|
395
|
+
view
|
|
396
|
+
returns (AddressTypeProposal memory)
|
|
397
|
+
{
|
|
297
398
|
return admin;
|
|
298
399
|
}
|
|
299
400
|
|
|
300
|
-
|
|
301
|
-
|
|
401
|
+
/**
|
|
402
|
+
* @notice Simulates reward estimation without modifying state
|
|
403
|
+
* @dev View function for previewing rewards before claiming
|
|
404
|
+
* @param _user Address of the user to simulate rewards for
|
|
405
|
+
* @return epochAnswer Epoch identifier that would be recorded
|
|
406
|
+
* @return tokenAddress Address of the reward token
|
|
407
|
+
* @return amountTotalToBeRewarded Calculated reward amount
|
|
408
|
+
* @return idToOverwrite Index in user history that would be updated
|
|
409
|
+
* @return timestampToOverwrite Timestamp that would be recorded
|
|
410
|
+
*/
|
|
302
411
|
function simulteEstimation(
|
|
303
412
|
address _user
|
|
304
413
|
)
|
|
@@ -354,6 +463,4 @@ contract Estimator {
|
|
|
354
463
|
|
|
355
464
|
timestampToOverwrite = epoch.tFinal;
|
|
356
465
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
466
|
}
|