Skip to content

Commit beb4f8c

Browse files
committed
use declarationTime as id for intents
1 parent acc814b commit beb4f8c

File tree

9 files changed

+88
-108
lines changed

9 files changed

+88
-108
lines changed

markets/bfp-market/storage.dump.sol

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,15 @@ library Config {
212212
library DelegationIntent {
213213
bytes32 private constant _ATOMIC_VALUE_LATEST_ID = "delegateIntent_idAsNonce";
214214
struct Data {
215-
uint256 id;
215+
uint32 declarationTime;
216216
uint128 accountId;
217217
uint128 poolId;
218218
address collateralType;
219219
int256 deltaCollateralAmountD18;
220220
uint256 leverage;
221-
uint32 declarationTime;
222221
}
223-
function load(uint256 id) internal pure returns (Data storage delegationIntent) {
224-
bytes32 s = keccak256(abi.encode("io.synthetix.synthetix.DelegationIntent", id));
222+
function load(uint32 declarationTime) internal pure returns (Data storage delegationIntent) {
223+
bytes32 s = keccak256(abi.encode("io.synthetix.synthetix.DelegationIntent", declarationTime));
225224
assembly {
226225
delegationIntent.slot := s
227226
}

markets/perps-market/storage.dump.sol

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,15 @@ library Config {
211211
library DelegationIntent {
212212
bytes32 private constant _ATOMIC_VALUE_LATEST_ID = "delegateIntent_idAsNonce";
213213
struct Data {
214-
uint256 id;
214+
uint32 declarationTime;
215215
uint128 accountId;
216216
uint128 poolId;
217217
address collateralType;
218218
int256 deltaCollateralAmountD18;
219219
uint256 leverage;
220-
uint32 declarationTime;
221220
}
222-
function load(uint256 id) internal pure returns (Data storage delegationIntent) {
223-
bytes32 s = keccak256(abi.encode("io.synthetix.synthetix.DelegationIntent", id));
221+
function load(uint32 declarationTime) internal pure returns (Data storage delegationIntent) {
222+
bytes32 s = keccak256(abi.encode("io.synthetix.synthetix.DelegationIntent", declarationTime));
224223
assembly {
225224
delegationIntent.slot := s
226225
}

protocol/synthetix/contracts/interfaces/IMarketManagerModule.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ interface IMarketManagerModule {
251251
function getMarketMinDelegateTime(uint128 marketId) external view returns (uint32);
252252

253253
/**
254-
* @notice allows for a market to set its delegation and delegation delay and window times. (See SIP-366). By default, there is no delay and infinite windows.
254+
* @notice allows for a market to set its delegation and undelegation delay and window times. (See SIP-366). By default, there is no delay and infinite windows.
255255
* @param marketId the id of the market that wants to set delegation times.
256256
* @param delegateCollateralDelay the minimum number of delay seconds to delegation
257257
* @param delegateCollateralWindow the maximum number of seconds that an delegation can be executed after the delay.
@@ -267,7 +267,7 @@ interface IMarketManagerModule {
267267
) external;
268268

269269
/**
270-
* @notice Retrieve the delegation and delegation delay and window times of a market
270+
* @notice Retrieve the delegation and undelegation delay and window times of a market
271271
* @param marketId the id of the market
272272
*/
273273
function getDelegationCollateralConfiguration(

protocol/synthetix/contracts/interfaces/IVaultModule.sol

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ interface IVaultModule {
4545
/**
4646
* @notice Thrown when the specified intent is not expired yet.
4747
*/
48-
error DelegationIntentNotExpired(uint256 intentId);
48+
error DelegationIntentNotExpired(uint32 intentId);
4949

5050
/**
5151
* @notice Thrown when the specified intent is not in current epoch (it was nuked in a liquidation or administrative fix).
5252
*/
53-
error DelegationIntentNotInCurrentEpoch(uint256 intentId);
53+
error DelegationIntentNotInCurrentEpoch(uint32 intentId);
5454

5555
/**
5656
* @notice Thrown when the specified intent is not executable due to pending intents.
@@ -92,7 +92,6 @@ interface IVaultModule {
9292
* @param collateralType The address of the collateral associated to the position.
9393
* @param collateralDeltaAmount The new amount of the position, denominated with 18 decimals of precision.
9494
* @param leverage The new leverage value of the position, denominated with 18 decimals of precision.
95-
* @param intentId The id of the intent to update the position.
9695
* @param declarationTime The time at which the intent was declared.
9796
* @param processingStartTime The time at which the intent can be processed.
9897
* @param processingEndTime The time at which the intent will no longer be able to be processed.
@@ -104,7 +103,6 @@ interface IVaultModule {
104103
address collateralType,
105104
int256 collateralDeltaAmount,
106105
uint256 leverage,
107-
uint256 intentId,
108106
uint32 declarationTime,
109107
uint32 processingStartTime,
110108
uint32 processingEndTime,
@@ -113,41 +111,41 @@ interface IVaultModule {
113111

114112
/**
115113
* @notice Emitted when an intent is removed (due to succesful execution or expiration).
116-
* @param intentId The id of the intent to update the position.
114+
* @param intentId The id (declaration timestamp) of the intent to update the position.
117115
* @param accountId The id of the account whose position was updated.
118116
* @param poolId The id of the pool in which the position was updated.
119117
* @param collateralType The address of the collateral associated to the position.
120118
*/
121119
event DelegationIntentRemoved(
122-
uint256 intentId,
120+
uint32 intentId,
123121
uint128 indexed accountId,
124122
uint128 indexed poolId,
125123
address collateralType
126124
);
127125

128126
/**
129127
* @notice Emitted when an intent is skipped due to the intent not being executable at that time.
130-
* @param intentId The id of the intent to update the position.
128+
* @param intentId The id (declaration timestamp) of the intent to update the position.
131129
* @param accountId The id of the account whose position was updated.
132130
* @param poolId The id of the pool in which the position was updated.
133131
* @param collateralType The address of the collateral associated to the position.
134132
*/
135133
event DelegationIntentSkipped(
136-
uint256 intentId,
134+
uint32 intentId,
137135
uint128 indexed accountId,
138136
uint128 indexed poolId,
139137
address collateralType
140138
);
141139

142140
/**
143141
* @notice Emitted when an intent is processed.
144-
* @param intentId The id of the intent to update the position.
142+
* @param intentId The id (declaration timestamp) of the intent to update the position.
145143
* @param accountId The id of the account whose position was updated.
146144
* @param poolId The id of the pool in which the position was updated.
147145
* @param collateralType The address of the collateral associated to the position.
148146
*/
149147
event DelegationIntentProcessed(
150-
uint256 intentId,
148+
uint32 intentId,
151149
uint128 indexed accountId,
152150
uint128 indexed poolId,
153151
address collateralType
@@ -184,7 +182,7 @@ interface IVaultModule {
184182
* @param collateralType The address of the collateral used in the position.
185183
* @param deltaAmountD18 The delta amount of collateral delegated in the position, denominated with 18 decimals of precision.
186184
* @param leverage The new leverage amount used in the position, denominated with 18 decimals of precision.
187-
* @return intentId The id of the new intent to update the delegated amount.
185+
* @return intentDeclarationTime The intent declaration time used as the id of the new intent to update the delegated amount.
188186
* Requirements:
189187
*
190188
* - `ERC2771Context._msgSender()` must be the owner of the account, have the `ADMIN` permission, or have the `DELEGATE` permission.
@@ -199,7 +197,7 @@ interface IVaultModule {
199197
address collateralType,
200198
int256 deltaAmountD18,
201199
uint256 leverage
202-
) external returns (uint256 intentId);
200+
) external returns (uint32 intentDeclarationTime);
203201

204202
/**
205203
* @notice Attempt to process the outstanding intents to update the delegated amount of collateral by intent ids.
@@ -266,7 +264,7 @@ interface IVaultModule {
266264
/**
267265
* @notice Returns details of the requested intent.
268266
* @param accountId The id of the account owning the intent.
269-
* @param intentId The id of the intents.
267+
* @param intentId The id (declaration timestamp) of the intents.
270268
* @return poolId The id of the pool associated with the position.
271269
* @return collateralType The address of the collateral used in the position.
272270
* @return deltaCollateralAmountD18 The delta amount of collateral delegated in the position, denominated with 18 decimals of precision.
@@ -275,7 +273,7 @@ interface IVaultModule {
275273
*/
276274
function getAccountIntent(
277275
uint128 accountId,
278-
uint256 intentId
276+
uint32 intentId
279277
)
280278
external
281279
view

protocol/synthetix/contracts/modules/core/VaultModule.sol

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ contract VaultModule is IVaultModule {
8484
address collateralType,
8585
int256 deltaCollateralAmountD18,
8686
uint256 leverage
87-
) external override returns (uint256 intentId) {
87+
) external override returns (uint32 intentDeclarationTime) {
8888
// Ensure the caller is authorized to represent the account.
8989
FeatureFlag.ensureAccessToFeature(_TWO_STEPS_DELEGATE_FEATURE_FLAG);
9090
Account.Data storage account = Account.loadAccountAndValidatePermission(
@@ -153,15 +153,14 @@ contract VaultModule is IVaultModule {
153153
}
154154

155155
// Create a new delegation intent.
156-
intentId = DelegationIntent.nextId();
157-
DelegationIntent.Data storage intent = DelegationIntent.load(intentId);
158-
intent.id = intentId;
156+
intentDeclarationTime = block.timestamp.to32();
157+
DelegationIntent.Data storage intent = DelegationIntent.load(intentDeclarationTime);
158+
intent.declarationTime = intentDeclarationTime;
159159
intent.accountId = accountId;
160160
intent.poolId = poolId;
161161
intent.collateralType = collateralType;
162162
intent.deltaCollateralAmountD18 = deltaCollateralAmountD18;
163163
intent.leverage = leverage;
164-
intent.declarationTime = block.timestamp.to32();
165164

166165
// Add intent to the account's delegation intents.
167166
accountIntents.addIntent(intent);
@@ -173,7 +172,6 @@ contract VaultModule is IVaultModule {
173172
collateralType,
174173
deltaCollateralAmountD18,
175174
leverage,
176-
intentId,
177175
intent.declarationTime,
178176
intent.processingStartTime(),
179177
intent.processingEndTime(),
@@ -198,15 +196,15 @@ contract VaultModule is IVaultModule {
198196
.getDelegationIntents();
199197

200198
for (uint256 i = 0; i < intentIds.length; i++) {
201-
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i]);
202-
if (!accountIntents.isInCurrentEpoch(intent.id)) {
203-
revert DelegationIntentNotInCurrentEpoch(intent.id);
199+
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i].to32());
200+
if (!accountIntents.isInCurrentEpoch(intent.declarationTime)) {
201+
revert DelegationIntentNotInCurrentEpoch(intent.declarationTime);
204202
}
205203

206204
if (!intent.isExecutable()) {
207205
// emit an Skipped event
208206
emit DelegationIntentSkipped(
209-
intent.id,
207+
intent.declarationTime,
210208
accountId,
211209
intent.poolId,
212210
intent.collateralType
@@ -216,7 +214,7 @@ contract VaultModule is IVaultModule {
216214
if (intent.intentExpired()) {
217215
accountIntents.removeIntent(intent);
218216
emit DelegationIntentRemoved(
219-
intent.id,
217+
intent.declarationTime,
220218
accountId,
221219
intent.poolId,
222220
intent.collateralType
@@ -242,15 +240,15 @@ contract VaultModule is IVaultModule {
242240
// Remove the intent.
243241
accountIntents.removeIntent(intent);
244242
emit DelegationIntentRemoved(
245-
intent.id,
243+
intent.declarationTime,
246244
accountId,
247245
intent.poolId,
248246
intent.collateralType
249247
);
250248

251249
// emit an event
252250
emit DelegationIntentProcessed(
253-
intent.id,
251+
intent.declarationTime,
254252
accountId,
255253
intent.poolId,
256254
intent.collateralType
@@ -289,7 +287,7 @@ contract VaultModule is IVaultModule {
289287
.load(accountId)
290288
.getDelegationIntents();
291289
for (uint256 i = 0; i < intentIds.length; i++) {
292-
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i]);
290+
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i].to32());
293291
accountIntents.removeIntent(intent);
294292
}
295293
}
@@ -312,12 +310,12 @@ contract VaultModule is IVaultModule {
312310
.load(accountId)
313311
.getDelegationIntents();
314312
for (uint256 i = 0; i < intentIds.length; i++) {
315-
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i]);
313+
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i].to32());
316314
if (intent.accountId != accountId) {
317315
revert InvalidDelegationIntent();
318316
}
319317
if (!intent.intentExpired()) {
320-
revert DelegationIntentNotExpired(intent.id);
318+
revert DelegationIntentNotExpired(intent.declarationTime);
321319
}
322320
accountIntents.removeIntent(intent);
323321
}
@@ -418,7 +416,7 @@ contract VaultModule is IVaultModule {
418416
*/
419417
function getAccountIntent(
420418
uint128 accountId,
421-
uint256 intentId
419+
uint32 intentId
422420
) external view override returns (uint128, address, int256, uint256, uint32) {
423421
DelegationIntent.Data storage intent = Account
424422
.load(accountId)
@@ -459,7 +457,7 @@ contract VaultModule is IVaultModule {
459457
: maxProcessableIntent;
460458
expiredIntents = new uint256[](max);
461459
for (uint256 i = 0; i < max; i++) {
462-
if (DelegationIntent.load(allIntents[i]).intentExpired()) {
460+
if (DelegationIntent.load(allIntents[i].to32()).intentExpired()) {
463461
expiredIntents[foundItems] = allIntents[i];
464462
foundItems++;
465463
}
@@ -483,7 +481,7 @@ contract VaultModule is IVaultModule {
483481
: maxProcessableIntent;
484482
executableIntents = new uint256[](max);
485483
for (uint256 i = 0; i < max; i++) {
486-
if (DelegationIntent.load(allIntents[i]).isExecutable()) {
484+
if (DelegationIntent.load(allIntents[i].to32()).isExecutable()) {
487485
executableIntents[foundItems] = allIntents[i];
488486
foundItems++;
489487
}
@@ -517,7 +515,7 @@ contract VaultModule is IVaultModule {
517515
);
518516
accumulatedIntentDelta = 0;
519517
for (uint256 i = 0; i < intentIds.length; i++) {
520-
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i]);
518+
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i].to32());
521519
if (!intent.intentExpired()) {
522520
accumulatedIntentDelta += intent.deltaCollateralAmountD18;
523521
}

protocol/synthetix/contracts/storage/AccountDelegationIntents.sol

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ library AccountDelegationIntents {
2929
}
3030

3131
function addIntent(Data storage self, DelegationIntent.Data storage delegationIntent) internal {
32-
self.intentsId.add(delegationIntent.id);
32+
self.intentsId.add(delegationIntent.declarationTime);
3333
self
3434
.intentsByPair[
3535
keccak256(
3636
abi.encodePacked(delegationIntent.poolId, delegationIntent.collateralType)
3737
)
3838
]
39-
.add(delegationIntent.id);
39+
.add(delegationIntent.declarationTime);
4040

4141
self.netDelegatedAmountPerCollateral[delegationIntent.collateralType] += delegationIntent
4242
.deltaCollateralAmountD18;
@@ -50,31 +50,31 @@ library AccountDelegationIntents {
5050
Data storage self,
5151
DelegationIntent.Data storage delegationIntent
5252
) internal {
53-
if (!self.intentsId.contains(delegationIntent.id)) {
53+
if (!self.intentsId.contains(delegationIntent.declarationTime)) {
5454
return;
5555
}
5656

57-
self.intentsId.remove(delegationIntent.id);
57+
self.intentsId.remove(delegationIntent.declarationTime);
5858
self
5959
.intentsByPair[
6060
keccak256(
6161
abi.encodePacked(delegationIntent.poolId, delegationIntent.collateralType)
6262
)
6363
]
64-
.remove(delegationIntent.id);
64+
.remove(delegationIntent.declarationTime);
6565

6666
self.netDelegatedAmountPerCollateral[delegationIntent.collateralType] -= delegationIntent
6767
.deltaCollateralAmountD18;
6868
}
6969

7070
function getIntent(
7171
Data storage self,
72-
uint256 intentId
72+
uint32 declarationTime
7373
) internal view returns (DelegationIntent.Data storage) {
74-
if (!self.intentsId.contains(intentId)) {
74+
if (!self.intentsId.contains(declarationTime)) {
7575
revert IVaultModule.InvalidDelegationIntent();
7676
}
77-
return DelegationIntent.load(intentId);
77+
return DelegationIntent.load(declarationTime);
7878
}
7979

8080
/**
@@ -88,10 +88,13 @@ library AccountDelegationIntents {
8888
return self.intentsByPair[keccak256(abi.encodePacked(poolId, collateralType))].values();
8989
}
9090

91-
function isInCurrentEpoch(Data storage self, uint256 intentId) internal view returns (bool) {
91+
function isInCurrentEpoch(
92+
Data storage self,
93+
uint32 declarationTime
94+
) internal view returns (bool) {
9295
// Notice: not checking that `self.delegationIntentsEpoch == account.currentDelegationIntentsEpoch` since
9396
// it was loadValid and getValid use it at load time
94-
return self.intentsId.contains(intentId);
97+
return self.intentsId.contains(declarationTime);
9598
}
9699

97100
/**
@@ -100,7 +103,7 @@ library AccountDelegationIntents {
100103
function cleanAllExpiredIntents(Data storage self) internal {
101104
uint256[] memory intentIds = self.intentsId.values();
102105
for (uint256 i = 0; i < intentIds.length; i++) {
103-
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i]);
106+
DelegationIntent.Data storage intent = DelegationIntent.load(intentIds[i].to32());
104107
if (intent.intentExpired()) {
105108
removeIntent(self, intent);
106109
}

0 commit comments

Comments
 (0)