@evvm/testnet-contracts 2.2.3 → 3.0.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.
Files changed (71) hide show
  1. package/LICENSE +145 -118
  2. package/README.md +162 -39
  3. package/contracts/core/Core.sol +1394 -0
  4. package/contracts/core/lib/CoreStorage.sol +171 -0
  5. package/contracts/nameService/NameService.sol +666 -586
  6. package/contracts/nameService/lib/IdentityValidation.sol +18 -3
  7. package/contracts/p2pSwap/P2PSwap.sol +439 -285
  8. package/contracts/staking/Estimator.sol +128 -40
  9. package/contracts/staking/Staking.sol +329 -322
  10. package/contracts/treasury/Treasury.sol +48 -37
  11. package/contracts/treasuryTwoChains/TreasuryExternalChainStation.sol +585 -198
  12. package/contracts/treasuryTwoChains/TreasuryHostChainStation.sol +425 -174
  13. package/contracts/treasuryTwoChains/lib/PayloadUtils.sol +2 -4
  14. package/interfaces/{IEvvm.sol → ICore.sol} +67 -29
  15. package/interfaces/IEstimator.sol +1 -1
  16. package/interfaces/INameService.sol +58 -52
  17. package/interfaces/IP2PSwap.sol +18 -17
  18. package/interfaces/IStaking.sol +22 -17
  19. package/interfaces/ITreasury.sol +2 -1
  20. package/interfaces/ITreasuryExternalChainStation.sol +15 -9
  21. package/interfaces/ITreasuryHostChainStation.sol +14 -11
  22. package/interfaces/IUserValidator.sol +6 -0
  23. package/library/Erc191TestBuilder.sol +350 -297
  24. package/library/EvvmService.sol +38 -27
  25. package/library/errors/CoreError.sol +116 -0
  26. package/library/errors/CrossChainTreasuryError.sol +36 -0
  27. package/library/errors/NameServiceError.sol +79 -0
  28. package/library/errors/StakingError.sol +79 -0
  29. package/library/errors/TreasuryError.sol +33 -0
  30. package/library/primitives/SignatureRecover.sol +33 -0
  31. package/library/structs/CoreStructs.sol +146 -0
  32. package/library/structs/ExternalChainStationStructs.sol +92 -0
  33. package/library/structs/HostChainStationStructs.sol +77 -0
  34. package/library/structs/NameServiceStructs.sol +47 -0
  35. package/library/structs/P2PSwapStructs.sol +127 -0
  36. package/library/structs/StakingStructs.sol +67 -0
  37. package/library/utils/AdvancedStrings.sol +84 -5
  38. package/library/utils/CAUtils.sol +29 -0
  39. package/library/utils/SignatureUtil.sol +34 -0
  40. package/library/utils/governance/Admin.sol +66 -0
  41. package/library/utils/governance/ProposalStructs.sol +49 -0
  42. package/library/utils/service/CoreExecution.sol +177 -0
  43. package/library/utils/service/StakingServiceUtils.sol +30 -3
  44. package/library/utils/signature/CoreHashUtils.sol +73 -0
  45. package/library/utils/signature/NameServiceHashUtils.sol +156 -0
  46. package/library/utils/signature/P2PSwapHashUtils.sol +65 -0
  47. package/library/utils/signature/StakingHashUtils.sol +41 -0
  48. package/library/utils/signature/TreasuryCrossChainHashUtils.sol +40 -0
  49. package/package.json +2 -1
  50. package/contracts/evvm/Evvm.sol +0 -1327
  51. package/contracts/evvm/lib/ErrorsLib.sol +0 -18
  52. package/contracts/evvm/lib/EvvmStorage.sol +0 -62
  53. package/contracts/evvm/lib/EvvmStructs.sol +0 -90
  54. package/contracts/evvm/lib/SignatureUtils.sol +0 -120
  55. package/contracts/nameService/lib/ErrorsLib.sol +0 -21
  56. package/contracts/nameService/lib/NameServiceStructs.sol +0 -69
  57. package/contracts/nameService/lib/SignatureUtils.sol +0 -245
  58. package/contracts/p2pSwap/lib/P2PSwapStructs.sol +0 -59
  59. package/contracts/p2pSwap/lib/SignatureUtils.sol +0 -98
  60. package/contracts/staking/lib/ErrorsLib.sol +0 -22
  61. package/contracts/staking/lib/SignatureUtils.sol +0 -39
  62. package/contracts/staking/lib/StakingStructs.sol +0 -94
  63. package/contracts/treasury/lib/ErrorsLib.sol +0 -11
  64. package/contracts/treasuryTwoChains/lib/ErrorsLib.sol +0 -48
  65. package/contracts/treasuryTwoChains/lib/ExternalChainStationStructs.sol +0 -80
  66. package/contracts/treasuryTwoChains/lib/HostChainStationStructs.sol +0 -87
  67. package/contracts/treasuryTwoChains/lib/SignatureUtils.sol +0 -79
  68. package/library/utils/GovernanceUtils.sol +0 -81
  69. package/library/utils/nonces/AsyncNonce.sol +0 -32
  70. package/library/utils/nonces/SyncNonce.sol +0 -27
  71. package/library/utils/service/EvvmPayments.sol +0 -79
@@ -2,6 +2,12 @@
2
2
  // Full license terms available at: https://www.evvm.info/docs/EVVMNoncommercialLicense
3
3
 
4
4
  pragma solidity ^0.8.0;
5
+
6
+ import {Staking} from "@evvm/testnet-contracts/contracts/staking/Staking.sol";
7
+ import {
8
+ StakingStructs
9
+ } from "@evvm/testnet-contracts/library/structs/StakingStructs.sol";
10
+
5
11
  /**
6
12
  MM""""""""`M dP oo dP
7
13
  MM mmmmmmmM 88 88
@@ -11,27 +17,38 @@ MM MMMMMMMM 88 88 88 88 88 88 88. .88 88 88. .88 88
11
17
  MM .M `88888P' dP dP dP dP dP `88888P8 dP `88888P' dP
12
18
  MMMMMMMMMMMM
13
19
 
14
- * @title Staking Estimator Contract
15
- * @author Mate labs
20
+ * @title EVVM Staking Estimator
21
+ * @author Mate Labs
22
+ * @notice Calculates validator rewards using time-weighted averages.
23
+ * @dev Collaborates with Staking.sol to track epochs, total staked amounts, and distribution pools.
24
+ * Features time-delayed governance for administrative changes.
16
25
  */
17
26
 
18
- import {Staking} from "@evvm/testnet-contracts/contracts/staking/Staking.sol";
19
- import {StakingStructs} from "@evvm/testnet-contracts/contracts/staking/lib/StakingStructs.sol";
20
- import {Evvm} from "@evvm/testnet-contracts/contracts/evvm/Evvm.sol";
27
+
21
28
 
22
29
  contract Estimator {
30
+ /// @dev Struct for managing address change proposals with time delay
23
31
  struct AddressTypeProposal {
24
32
  address actual;
25
33
  address proposal;
26
34
  uint256 timeToAccept;
27
35
  }
28
36
 
37
+ /// @dev Struct for managing uint256 value proposals with time delay
29
38
  struct UintTypeProposal {
30
39
  uint256 actual;
31
40
  uint256 proposal;
32
41
  uint256 timeToAccept;
33
42
  }
34
43
 
44
+ /**
45
+ * @dev Struct containing epoch metadata for reward calculations
46
+ * @param tokenPool Address of the token being distributed as rewards
47
+ * @param totalPool Total amount of tokens available for distribution
48
+ * @param totalStaked Total staking tokens staked during this epoch
49
+ * @param tFinal Timestamp when the epoch ended
50
+ * @param tStart Timestamp when the epoch started
51
+ */
35
52
  struct EpochMetadata {
36
53
  address tokenPool;
37
54
  uint256 totalPool;
@@ -40,45 +57,73 @@ contract Estimator {
40
57
  uint256 tStart;
41
58
  }
42
59
 
60
+ /// @dev Current epoch metadata storage
43
61
  EpochMetadata private epoch;
62
+ /// @dev Proposal system for activator address changes
44
63
  AddressTypeProposal private activator;
45
- AddressTypeProposal private evvmAddress;
64
+ /// @dev Proposal system for EVVM address changes
65
+ AddressTypeProposal private coreAddress;
66
+ /// @dev Proposal system for Staking contract address changes
46
67
  AddressTypeProposal private addressStaking;
68
+ /// @dev Proposal system for admin address changes
47
69
  AddressTypeProposal private admin;
48
70
 
71
+ /// @dev Transaction type identifier for deposit (staking) operations
49
72
  bytes32 constant DEPOSIT_IDENTIFIER = bytes32(uint256(1));
73
+ /// @dev Transaction type identifier for withdraw (unstaking) operations
50
74
  bytes32 constant WITHDRAW_IDENTIFIER = bytes32(uint256(2));
75
+ /// @dev Beginning identifier same as withdraw for epoch tracking
51
76
  bytes32 constant BEGUIN_IDENTIFIER = WITHDRAW_IDENTIFIER;
52
77
 
78
+ /// @dev Current epoch identifier, increments with each new epoch
53
79
  bytes32 epochId = bytes32(uint256(3));
54
80
 
81
+ /// @dev Restricts function access to the Staking contract only
55
82
  modifier onlyStaking() {
56
83
  if (msg.sender != addressStaking.actual) revert();
57
84
  _;
58
85
  }
59
86
 
87
+ /// @dev Restricts function access to the activator address only
60
88
  modifier onlyActivator() {
61
89
  if (msg.sender != activator.actual) revert();
62
90
  _;
63
91
  }
64
92
 
93
+ /// @dev Restricts function access to the admin address only
65
94
  modifier onlyAdmin() {
66
95
  if (msg.sender != admin.actual) revert();
67
96
  _;
68
97
  }
69
98
 
99
+ /**
100
+ * @notice Initializes the Estimator contract
101
+ * @dev Sets up all required addresses for contract operation
102
+ * @param _activator Address authorized to start new epochs
103
+ * @param _coreAddress Address of the EVVM core contract
104
+ * @param _addressStaking Address of the Staking contract
105
+ * @param _admin Address with administrative privileges
106
+ */
70
107
  constructor(
71
108
  address _activator,
72
- address _evvmAddress,
109
+ address _coreAddress,
73
110
  address _addressStaking,
74
111
  address _admin
75
112
  ) {
76
113
  activator.actual = _activator;
77
- evvmAddress.actual = _evvmAddress;
114
+ coreAddress.actual = _coreAddress;
78
115
  addressStaking.actual = _addressStaking;
79
116
  admin.actual = _admin;
80
117
  }
81
118
 
119
+ /**
120
+ * @notice Starts a new reward epoch with the provided parameters
121
+ * @dev Only callable by the activator address. Records epoch metadata for reward calculations.
122
+ * @param tokenPool Address of the token to be distributed as rewards
123
+ * @param totalPool Total amount of tokens available for distribution this epoch
124
+ * @param totalStaked Total staking tokens staked at epoch start
125
+ * @param tStart Timestamp when the epoch started
126
+ */
82
127
  function notifyNewEpoch(
83
128
  address tokenPool,
84
129
  uint256 totalPool,
@@ -94,6 +139,17 @@ contract Estimator {
94
139
  });
95
140
  }
96
141
 
142
+ /**
143
+ * @notice Calculates and returns the reward amount for a specific user
144
+ * @dev Only callable by the Staking contract. Uses time-weighted average calculation
145
+ * to determine proportional rewards based on staking duration and amount.
146
+ * @param _user Address of the user to calculate rewards for
147
+ * @return epochAnswer Epoch identifier to record in user history
148
+ * @return tokenAddress Address of the reward token
149
+ * @return amountTotalToBeRewarded Calculated reward amount for the user
150
+ * @return idToOverwrite Index in user history to update with reward info
151
+ * @return timestampToOverwrite Timestamp to record for the reward transaction
152
+ */
97
153
  function makeEstimation(
98
154
  address _user
99
155
  )
@@ -107,7 +163,6 @@ contract Estimator {
107
163
  uint256 timestampToOverwrite
108
164
  )
109
165
  {
110
-
111
166
  uint256 totSmLast;
112
167
  uint256 sumSmT;
113
168
 
@@ -125,7 +180,6 @@ contract Estimator {
125
180
 
126
181
  if (size == 1) totSmLast = h.totalStaked;
127
182
 
128
-
129
183
  if (h.timestamp > epoch.tFinal) {
130
184
  if (totSmLast > 0) sumSmT += (epoch.tFinal - tLast) * totSmLast;
131
185
 
@@ -138,7 +192,6 @@ contract Estimator {
138
192
 
139
193
  if (totSmLast > 0) sumSmT += (h.timestamp - tLast) * totSmLast;
140
194
 
141
-
142
195
  tLast = h.timestamp;
143
196
  totSmLast = h.totalStaked;
144
197
  idToOverwrite = i;
@@ -168,7 +221,6 @@ contract Estimator {
168
221
 
169
222
  timestampToOverwrite = epoch.tFinal;
170
223
 
171
-
172
224
  epoch.totalPool -= amountTotalToBeRewarded;
173
225
  epoch.totalStaked -= h.totalStaked;
174
226
  }
@@ -177,18 +229,20 @@ contract Estimator {
177
229
  // Admin functions
178
230
  //⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻
179
231
 
180
- function setActivatorProposal(
181
- address _proposal
182
- ) external onlyActivator {
232
+ /// @notice Proposes a new activator address with 1-day time delay
233
+ /// @param _proposal Address of the proposed new activator
234
+ function setActivatorProposal(address _proposal) external onlyActivator {
183
235
  activator.proposal = _proposal;
184
236
  activator.timeToAccept = block.timestamp + 1 days;
185
237
  }
186
238
 
239
+ /// @notice Cancels the pending activator proposal
187
240
  function cancelActivatorProposal() external onlyActivator {
188
241
  activator.proposal = address(0);
189
242
  activator.timeToAccept = 0;
190
243
  }
191
244
 
245
+ /// @notice Accepts the activator proposal after time delay
192
246
  function acceptActivatorProposal() external {
193
247
  if (block.timestamp < activator.timeToAccept) revert();
194
248
 
@@ -197,38 +251,42 @@ contract Estimator {
197
251
  activator.timeToAccept = 0;
198
252
  }
199
253
 
200
- function setEvvmAddressProposal(
201
- address _proposal
202
- ) external onlyAdmin {
203
- evvmAddress.proposal = _proposal;
204
- evvmAddress.timeToAccept = block.timestamp + 1 days;
254
+ /// @notice Proposes a new EVVM address with 1-day time delay
255
+ /// @param _proposal Address of the proposed new EVVM contract
256
+ function setEvvmAddressProposal(address _proposal) external onlyAdmin {
257
+ coreAddress.proposal = _proposal;
258
+ coreAddress.timeToAccept = block.timestamp + 1 days;
205
259
  }
206
260
 
261
+ /// @notice Cancels the pending EVVM address proposal
207
262
  function cancelEvvmAddressProposal() external onlyAdmin {
208
- evvmAddress.proposal = address(0);
209
- evvmAddress.timeToAccept = 0;
263
+ coreAddress.proposal = address(0);
264
+ coreAddress.timeToAccept = 0;
210
265
  }
211
266
 
267
+ /// @notice Accepts the EVVM address proposal after time delay
212
268
  function acceptEvvmAddressProposal() external onlyAdmin {
213
- if (block.timestamp < evvmAddress.timeToAccept) revert();
269
+ if (block.timestamp < coreAddress.timeToAccept) revert();
214
270
 
215
- evvmAddress.actual = evvmAddress.proposal;
216
- evvmAddress.proposal = address(0);
217
- evvmAddress.timeToAccept = 0;
271
+ coreAddress.actual = coreAddress.proposal;
272
+ coreAddress.proposal = address(0);
273
+ coreAddress.timeToAccept = 0;
218
274
  }
219
275
 
220
- function setAddressStakingProposal(
221
- address _proposal
222
- ) external onlyAdmin {
276
+ /// @notice Proposes a new Staking contract address with 1-day time delay
277
+ /// @param _proposal Address of the proposed new Staking contract
278
+ function setAddressStakingProposal(address _proposal) external onlyAdmin {
223
279
  addressStaking.proposal = _proposal;
224
280
  addressStaking.timeToAccept = block.timestamp + 1 days;
225
281
  }
226
282
 
283
+ /// @notice Cancels the pending Staking address proposal
227
284
  function cancelAddressStakingProposal() external onlyAdmin {
228
285
  addressStaking.proposal = address(0);
229
286
  addressStaking.timeToAccept = 0;
230
287
  }
231
288
 
289
+ /// @notice Accepts the Staking address proposal after time delay
232
290
  function acceptAddressStakingProposal() external onlyAdmin {
233
291
  if (block.timestamp < addressStaking.timeToAccept) revert();
234
292
 
@@ -237,18 +295,20 @@ contract Estimator {
237
295
  addressStaking.timeToAccept = 0;
238
296
  }
239
297
 
240
- function setAdminProposal(
241
- address _proposal
242
- ) external onlyAdmin {
298
+ /// @notice Proposes a new admin address with 1-day time delay
299
+ /// @param _proposal Address of the proposed new admin
300
+ function setAdminProposal(address _proposal) external onlyAdmin {
243
301
  admin.proposal = _proposal;
244
302
  admin.timeToAccept = block.timestamp + 1 days;
245
303
  }
246
304
 
305
+ /// @notice Cancels the pending admin proposal
247
306
  function cancelAdminProposal() external onlyAdmin {
248
307
  admin.proposal = address(0);
249
308
  admin.timeToAccept = 0;
250
309
  }
251
310
 
311
+ /// @notice Accepts the admin proposal after time delay
252
312
  function acceptAdminProposal() external {
253
313
  if (block.timestamp < admin.timeToAccept) revert();
254
314
 
@@ -261,30 +321,46 @@ contract Estimator {
261
321
  // Getters
262
322
  //⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎽⎼⎻⎺⎺⎻⎼⎽⎼⎻⎺⎺⎻
263
323
 
324
+ /// @notice Returns the current epoch metadata
325
+ /// @return Complete EpochMetadata struct with pool and timing information
264
326
  function getEpochMetadata() external view returns (EpochMetadata memory) {
265
327
  return epoch;
266
328
  }
267
329
 
330
+ /// @notice Returns the current epoch number as uint256
331
+ /// @return Current epoch number (epochId - 2)
268
332
  function getActualEpochInUint() external view returns (uint256) {
269
333
  return uint256(epochId) - 2;
270
334
  }
271
335
 
336
+ /// @notice Returns the current epoch identifier in bytes32 format
337
+ /// @return Current epoch identifier
272
338
  function getActualEpochInFormat() external view returns (bytes32) {
273
339
  return epochId;
274
340
  }
275
341
 
276
- function getActivatorMetadata() external view returns (AddressTypeProposal memory) {
342
+ /// @notice Returns the activator address proposal information
343
+ /// @return Complete AddressTypeProposal struct for activator
344
+ function getActivatorMetadata()
345
+ external
346
+ view
347
+ returns (AddressTypeProposal memory)
348
+ {
277
349
  return activator;
278
350
  }
279
351
 
280
- function getEvvmAddressMetadata()
352
+ /// @notice Returns the EVVM address proposal information
353
+ /// @return Complete AddressTypeProposal struct for EVVM
354
+ function getCoreAddressMetadata()
281
355
  external
282
356
  view
283
357
  returns (AddressTypeProposal memory)
284
358
  {
285
- return evvmAddress;
359
+ return coreAddress;
286
360
  }
287
361
 
362
+ /// @notice Returns the Staking contract address proposal information
363
+ /// @return Complete AddressTypeProposal struct for Staking
288
364
  function getAddressStakingMetadata()
289
365
  external
290
366
  view
@@ -293,12 +369,26 @@ contract Estimator {
293
369
  return addressStaking;
294
370
  }
295
371
 
296
- function getAdminMetadata() external view returns (AddressTypeProposal memory) {
372
+ /// @notice Returns the admin address proposal information
373
+ /// @return Complete AddressTypeProposal struct for admin
374
+ function getAdminMetadata()
375
+ external
376
+ view
377
+ returns (AddressTypeProposal memory)
378
+ {
297
379
  return admin;
298
380
  }
299
381
 
300
-
301
-
382
+ /**
383
+ * @notice Simulates reward estimation without modifying state
384
+ * @dev View function for previewing rewards before claiming
385
+ * @param _user Address of the user to simulate rewards for
386
+ * @return epochAnswer Epoch identifier that would be recorded
387
+ * @return tokenAddress Address of the reward token
388
+ * @return amountTotalToBeRewarded Calculated reward amount
389
+ * @return idToOverwrite Index in user history that would be updated
390
+ * @return timestampToOverwrite Timestamp that would be recorded
391
+ */
302
392
  function simulteEstimation(
303
393
  address _user
304
394
  )
@@ -354,6 +444,4 @@ contract Estimator {
354
444
 
355
445
  timestampToOverwrite = epoch.tFinal;
356
446
  }
357
-
358
-
359
447
  }