Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sip 366 implementation #2145

Open
wants to merge 59 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
2209136
wip sip366
leomassazza May 8, 2024
45dfd41
wip 366
leomassazza May 9, 2024
cebc9c4
wip 366 start fixing tests
leomassazza May 9, 2024
f41d109
fix, wrong params for pair
leomassazza May 9, 2024
e3ab470
wip: add per collateral accounting
leomassazza May 13, 2024
18de031
Test MarketManagerModule
leomassazza May 13, 2024
de561f0
Fix LiquidationModule test
leomassazza May 13, 2024
1f49264
wip: fix tests
leomassazza May 13, 2024
5a28067
checkpoint: fixing vault module test (wip)
leomassazza May 13, 2024
13a5120
checkpoint: use delta for intent declarations
leomassazza May 13, 2024
b2cb01c
checkpoint: added some events
leomassazza May 13, 2024
7280c58
Link to liquidation
leomassazza May 13, 2024
64030e3
checkpoint: more tests fixed and bugs smashed
leomassazza May 14, 2024
3f68d81
Fix tests
leomassazza May 15, 2024
c60b1c4
fix other project tests
leomassazza May 16, 2024
6ae5345
fix another tests on bfp
leomassazza May 16, 2024
8289f11
Only owner can delete all intents
leomassazza May 17, 2024
c6b5332
Make window time flexible with configuration changes
leomassazza May 20, 2024
55cf924
wip: timing tests
leomassazza May 20, 2024
383d27e
test edge cases
leomassazza May 20, 2024
18a4308
Reduce AccountDelegationIntents footprint
leomassazza May 21, 2024
a47d06c
Cleanup and comments
leomassazza May 21, 2024
12db077
update storage.dump
leomassazza May 22, 2024
1027f7f
add skipped test - multiple markets with diff timing
leomassazza May 22, 2024
3a4ac69
deps fix
leomassazza May 22, 2024
ed9d256
undo deps fix
leomassazza May 22, 2024
fc9cf62
Merge branch 'SIP-366-implementation' of https://github.com/Synthetix…
leomassazza May 22, 2024
f0531e2
fix deps
leomassazza May 22, 2024
d03d3ca
typos + remove account owner check on delete expired intents
leomassazza May 22, 2024
0cf5549
PR review fixes (partial)
leomassazza May 27, 2024
6c54a5d
fix: ensure intentId to process is still valid for the account (not r…
leomassazza May 27, 2024
3c9f5be
fix pair using wrong key
leomassazza May 27, 2024
50b6b61
fix tests
leomassazza May 27, 2024
f62dad5
propagate storage change
leomassazza May 27, 2024
901f542
rename function (wip)
leomassazza May 27, 2024
30953ee
add two new views
leomassazza May 28, 2024
05cd81f
some fixes
leomassazza May 30, 2024
2e5ddcd
add views test + small fix
leomassazza Jun 4, 2024
9e836d5
PR Fixes -> SIP 366 (#2171)
leomassazza Jun 26, 2024
cb02ac7
Merge branch 'main' into SIP-366-implementation
leomassazza Jun 26, 2024
4061dec
fix bfp tests
leomassazza Jun 26, 2024
f9ba8da
more fixes on bfp (roll back changes)
leomassazza Jun 26, 2024
cc0bd12
add missing empty line
leomassazza Jun 27, 2024
62de774
add more info to view
leomassazza Jun 27, 2024
9a44955
fix wrong sign + a small refactor
leomassazza Jun 27, 2024
6272a49
fix test
leomassazza Jun 27, 2024
ac3ad55
rename FFs
leomassazza Jul 1, 2024
c11d354
fix tests
leomassazza Jul 1, 2024
c4fa5d3
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 2, 2024
0a2d08e
push storage dump
leomassazza Jul 3, 2024
b190bfc
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 15, 2024
dde2c7d
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 18, 2024
4ff64db
Reduce delegationIntent footprint
leomassazza Jul 19, 2024
cd8a7f9
fix missing references to intent.id
leomassazza Jul 19, 2024
eb05eba
Merge branch 'main' into SIP-366-implementation
leomassazza Jul 27, 2024
1f46654
Merge branch 'main' into SIP-366-implementation
leomassazza Aug 13, 2024
f960f81
fix test
leomassazza Aug 13, 2024
712921c
Merge branch 'main' into SIP-366-implementation
leomassazza Aug 27, 2024
dcb9ea4
Merge branch 'main' into SIP-366-implementation
leomassazza Aug 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make window time flexible with configuration changes
  • Loading branch information
leomassazza committed May 20, 2024
commit c6b5332b8d5ce48c88fa823254a841fe5783c5ef
3 changes: 0 additions & 3 deletions protocol/synthetix/contracts/modules/core/PoolModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ contract PoolModule is IPoolModule {
) external override {
Pool.Data storage pool = Pool.loadExisting(poolId);
Pool.onlyPoolOwner(poolId, ERC2771Context._msgSender());
// TODO LJM

// pool.requireMinDelegationTimeElapsed(pool.lastConfigurationTime);

// Update each market's pro-rata liquidity and collect accumulated debt into the pool's debt distribution.
// Note: This follows the same pattern as Pool.recalculateVaultCollateral(),
Expand Down
21 changes: 5 additions & 16 deletions protocol/synthetix/contracts/modules/core/VaultModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,16 @@ contract VaultModule is IVaultModule {
);
}

// Prepare data for storing the new intent.
(uint32 requiredDelayTime, uint32 requiredWindowTime) = Pool
.loadExisting(poolId)
.getRequiredDelegationDelayAndWindow(
deltaCollateralAmountD18 +
accountIntents.netDelegatedCollateralAmountPerPool[poolId] >
0
);

// Create a new delegation intent.
intentId = DelegationIntent.nextId();
DelegationIntent.Data storage intent = DelegationIntent.load(intentId);
intent.id = intentId;
intent.accountId = accountId;
intent.poolId = poolId;
intent.collateralType = collateralType;
intent.collateralDeltaAmountD18 = deltaCollateralAmountD18;
intent.deltaCollateralAmountD18 = deltaCollateralAmountD18;
intent.leverage = leverage;
intent.declarationTime = block.timestamp.to32();
intent.processingStartTime = intent.declarationTime + requiredDelayTime;
intent.processingEndTime = intent.processingStartTime + requiredWindowTime;

// Add intent to the account's delegation intents.
AccountDelegationIntents.getValid(intent.accountId).addIntent(intent);
Expand All @@ -147,8 +136,8 @@ contract VaultModule is IVaultModule {
leverage,
intentId,
intent.declarationTime,
intent.processingStartTime,
intent.processingEndTime,
intent.processingStartTime(),
intent.processingEndTime(),
ERC2771Context._msgSender()
);
}
Expand Down Expand Up @@ -198,7 +187,7 @@ contract VaultModule is IVaultModule {
accountId,
intent.poolId,
intent.collateralType,
intent.collateralDeltaAmountD18,
intent.deltaCollateralAmountD18,
intent.leverage
);

Expand Down Expand Up @@ -455,7 +444,7 @@ contract VaultModule is IVaultModule {
collateralType,
newCollateralAmountD18,
leverage,
ERC2771Context._msgSender() // TODO LJM this is the executor address, not the account owner or authorized (the one that posted the intent)
ERC2771Context._msgSender() // this is the executor address, not the account owner or authorized (the one that posted the intent)
);

vault.updateRewards(
Expand Down
40 changes: 20 additions & 20 deletions protocol/synthetix/contracts/storage/AccountDelegationIntents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,29 @@ library AccountDelegationIntents {
]
.add(delegationIntent.id);

if (delegationIntent.collateralDeltaAmountD18 >= 0) {
if (delegationIntent.deltaCollateralAmountD18 >= 0) {
self.delegatedAmountPerCollateral[delegationIntent.collateralType] += delegationIntent
.collateralDeltaAmountD18
.deltaCollateralAmountD18
.toUint();
self.delegatedCollateralAmountPerPool[delegationIntent.poolId] += delegationIntent
.collateralDeltaAmountD18
.deltaCollateralAmountD18
.toUint();
self.delegateAcountCachedCollateral += delegationIntent
.collateralDeltaAmountD18
.deltaCollateralAmountD18
.toUint();
} else {
self.undelegatedAmountPerCollateral[
delegationIntent.collateralType
] += (delegationIntent.collateralDeltaAmountD18 * -1).toUint();
] += (delegationIntent.deltaCollateralAmountD18 * -1).toUint();
self.undelegatedCollateralAmountPerPool[delegationIntent.poolId] += (delegationIntent
.collateralDeltaAmountD18 * -1).toUint();
self.undelegateAcountCachedCollateral += (delegationIntent.collateralDeltaAmountD18 *
.deltaCollateralAmountD18 * -1).toUint();
self.undelegateAcountCachedCollateral += (delegationIntent.deltaCollateralAmountD18 *
-1).toUint();
}
self.netDelegatedAmountPerCollateral[delegationIntent.collateralType] += delegationIntent
.collateralDeltaAmountD18;
.deltaCollateralAmountD18;
self.netDelegatedCollateralAmountPerPool[delegationIntent.poolId] += delegationIntent
.collateralDeltaAmountD18;
.deltaCollateralAmountD18;

if (!self.delegatedPools.contains(delegationIntent.poolId)) {
self.delegatedPools.add(delegationIntent.poolId);
Expand All @@ -104,7 +104,7 @@ library AccountDelegationIntents {
self.delegatedCollaterals.add(delegationIntent.collateralType);
}

self.netAcountCachedDelegatedCollateral += delegationIntent.collateralDeltaAmountD18;
self.netAcountCachedDelegatedCollateral += delegationIntent.deltaCollateralAmountD18;
}

function removeIntent(
Expand All @@ -122,31 +122,31 @@ library AccountDelegationIntents {
]
.remove(delegationIntent.id);

if (delegationIntent.collateralDeltaAmountD18 >= 0) {
if (delegationIntent.deltaCollateralAmountD18 >= 0) {
self.delegatedAmountPerCollateral[delegationIntent.collateralType] -= delegationIntent
.collateralDeltaAmountD18
.deltaCollateralAmountD18
.toUint();
self.delegatedCollateralAmountPerPool[delegationIntent.poolId] -= delegationIntent
.collateralDeltaAmountD18
.deltaCollateralAmountD18
.toUint();
self.delegateAcountCachedCollateral -= delegationIntent
.collateralDeltaAmountD18
.deltaCollateralAmountD18
.toUint();
} else {
self.undelegatedAmountPerCollateral[
delegationIntent.collateralType
] -= (delegationIntent.collateralDeltaAmountD18 * -1).toUint();
] -= (delegationIntent.deltaCollateralAmountD18 * -1).toUint();
self.undelegatedCollateralAmountPerPool[delegationIntent.poolId] -= (delegationIntent
.collateralDeltaAmountD18 * -1).toUint();
self.undelegateAcountCachedCollateral -= (delegationIntent.collateralDeltaAmountD18 *
.deltaCollateralAmountD18 * -1).toUint();
self.undelegateAcountCachedCollateral -= (delegationIntent.deltaCollateralAmountD18 *
-1).toUint();
}
self.netDelegatedAmountPerCollateral[delegationIntent.collateralType] -= delegationIntent
.collateralDeltaAmountD18;
.deltaCollateralAmountD18;
self.netDelegatedCollateralAmountPerPool[delegationIntent.poolId] -= delegationIntent
.collateralDeltaAmountD18;
.deltaCollateralAmountD18;

self.netAcountCachedDelegatedCollateral -= delegationIntent.collateralDeltaAmountD18;
self.netAcountCachedDelegatedCollateral -= delegationIntent.deltaCollateralAmountD18;
}

/**
Expand Down
84 changes: 61 additions & 23 deletions protocol/synthetix/contracts/storage/DelegationIntent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
pragma solidity >=0.8.11 <0.9.0;

import "./Config.sol";
import "./Pool.sol";

/**
* @title Represents a delegation (or undelegation) intent.
*/
library DelegationIntent {
using Pool for Pool.Data;

error InvalidDelegationIntentId();
error DelegationIntentNotReady(uint32 declarationTime, uint32 processingStartTime);
error DelegationIntentExpired(uint32 declarationTime, uint32 processingEndTime);
Expand Down Expand Up @@ -51,7 +54,7 @@ library DelegationIntent {
* outstanding intent to delegate/undelegate to the pool,
* denominated with 18 decimals of precision
*/
int256 collateralDeltaAmountD18;
int256 deltaCollateralAmountD18;
/**
* @notice The intended amount of leverage associated with the new
* amount of collateral that the account has an outstanding intent
Expand All @@ -63,21 +66,6 @@ library DelegationIntent {
* @notice The timestamp at which the intent was declared
*/
uint32 declarationTime;
/**
* @notice The timestamp before which the intent cannot be processed
* @dev dependent on
* {MarketManagerModule.undelegateCollateralDelay} or
* {MarketManagerModule.delegateCollateralDelay}
*/
uint32 processingStartTime;
/**
* @notice The timestamp after which the intent cannot be processed
* (i.e., intent expiry)
* @dev dependent on
* {MarketManagerModule.undelegateCollateralWindow} or
* {MarketManagerModule.delegateCollateralWindow}
*/
uint32 processingEndTime;
}

/**
Expand Down Expand Up @@ -110,19 +98,69 @@ library DelegationIntent {
Config.put(_ATOMIC_VALUE_LATEST_ID, bytes32(id));
}

function processingStartTime(Data storage self) internal view returns (uint32) {
(uint32 requiredDelayTime, ) = Pool
.loadExisting(self.poolId)
.getRequiredDelegationDelayAndWindow(self.deltaCollateralAmountD18 > 0);
return self.declarationTime + requiredDelayTime;
}

function processingEndTime(Data storage self) internal view returns (uint32) {
(uint32 requiredDelayTime, uint32 requiredWindowTime) = Pool
.loadExisting(self.poolId)
.getRequiredDelegationDelayAndWindow(self.deltaCollateralAmountD18 > 0);

// Apply default (forever) window time if not set
if (requiredWindowTime == 0) {
requiredWindowTime = 86400 * 360; // 1 year
leomassazza marked this conversation as resolved.
Show resolved Hide resolved
}

return self.declarationTime + requiredDelayTime + requiredWindowTime;
}

function checkIsExecutable(Data storage self) internal view {
kaleb-keny marked this conversation as resolved.
Show resolved Hide resolved
if (block.timestamp < self.processingStartTime)
revert DelegationIntentNotReady(self.declarationTime, self.processingStartTime);
if (block.timestamp >= self.processingEndTime)
revert DelegationIntentExpired(self.declarationTime, self.processingEndTime);
(uint32 requiredDelayTime, uint32 requiredWindowTime) = Pool
.loadExisting(self.poolId)
.getRequiredDelegationDelayAndWindow(self.deltaCollateralAmountD18 > 0);

// Apply default (forever) window time if not set
if (requiredWindowTime == 0) {
requiredWindowTime = 86400 * 360; // 1 year
}

uint32 _processingStartTime = self.declarationTime + requiredDelayTime;
uint32 _processingEndTime = _processingStartTime + requiredWindowTime;

if (block.timestamp < _processingStartTime)
revert DelegationIntentNotReady(self.declarationTime, _processingStartTime);
if (block.timestamp >= _processingEndTime)
revert DelegationIntentExpired(self.declarationTime, _processingEndTime);
}

function isExecutable(Data storage self) internal view returns (bool) {
return
block.timestamp >= self.processingStartTime && block.timestamp < self.processingEndTime;
(uint32 requiredDelayTime, uint32 requiredWindowTime) = Pool
.loadExisting(self.poolId)
.getRequiredDelegationDelayAndWindow(self.deltaCollateralAmountD18 > 0);

// Apply default (forever) window time if not set
if (requiredWindowTime == 0) {
requiredWindowTime = 86400 * 360; // 1 year
}

uint32 _processingStartTime = self.declarationTime + requiredDelayTime;
uint32 _processingEndTime = _processingStartTime + requiredWindowTime;

return block.timestamp >= _processingStartTime && block.timestamp < _processingEndTime;
}

function intentExpired(Data storage self) internal view returns (bool) {
return block.timestamp >= self.processingEndTime;
(uint32 requiredDelayTime, uint32 requiredWindowTime) = Pool
.loadExisting(self.poolId)
.getRequiredDelegationDelayAndWindow(self.deltaCollateralAmountD18 > 0);

// Note: here we don't apply the forever defaul if window time is not set to allow the intent to expire. If it's zero it means is not configured, then it can expire immediately.

uint32 _processingEndTime = self.declarationTime + requiredDelayTime + requiredWindowTime;
return block.timestamp >= _processingEndTime;
}
}
53 changes: 0 additions & 53 deletions protocol/synthetix/contracts/storage/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -436,46 +436,8 @@ library Pool {
: market.delegateCollateralWindow;
}
}

if (requiredWindowTime == 0) {
requiredWindowTime = 86400 * 360; // 1 year
}

// TODO use global max delay and window ??
// // solhint-disable-next-line numcast/safe-cast
// uint32 maxMinDelegateTime = uint32(
// Config.readUint(_CONFIG_SET_MARKET_MIN_DELEGATE_MAX, 86400 * 30)
// );
// return
// maxMinDelegateTime < requiredMinDelegateTime
// ? maxMinDelegateTime
// : requiredMinDelegateTime;
}

// TODO LJM
// function getRequiredMinDelegationTime(
// Data storage self
// ) internal view returns (uint32 requiredMinDelegateTime) {
// for (uint256 i = 0; i < self.marketConfigurations.length; i++) {
// uint32 marketMinDelegateTime = Market
// .load(self.marketConfigurations[i].marketId)
// .minDelegateTime;

// if (marketMinDelegateTime > requiredMinDelegateTime) {
// requiredMinDelegateTime = marketMinDelegateTime;
// }
// }

// // solhint-disable-next-line numcast/safe-cast
// uint32 maxMinDelegateTime = uint32(
// Config.readUint(_CONFIG_SET_MARKET_MIN_DELEGATE_MAX, 86400 * 30)
// );
// return
// maxMinDelegateTime < requiredMinDelegateTime
// ? maxMinDelegateTime
// : requiredMinDelegateTime;
// }

/**
* @dev Returns the debt of the vault that tracks the given collateral type.
*
Expand Down Expand Up @@ -553,21 +515,6 @@ library Pool {
}
}

// TODO LJM
// function requireMinDelegationTimeElapsed(
// Data storage self,
// uint64 lastDelegationTime
// ) internal view {
// uint32 requiredMinDelegationTime = getRequiredMinDelegationTime(self);
// if (block.timestamp < lastDelegationTime + requiredMinDelegationTime) {
// revert MinDelegationTimeoutPending(
// self.id,
// // solhint-disable-next-line numcast/safe-cast
// uint32(lastDelegationTime + requiredMinDelegationTime - block.timestamp)
// );
// }
// }

function checkPoolCollateralLimit(
Data storage self,
address collateralType,
Expand Down