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
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
wip: add per collateral accounting
  • Loading branch information
leomassazza committed May 13, 2024
commit e3ab470f4684c4d3597fd08a3241ed8a47bfaae0
33 changes: 33 additions & 0 deletions protocol/synthetix/contracts/storage/AccountDelegationIntents.sol
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ library AccountDelegationIntents {
using SafeCastU128 for uint128;
using SafeCastU256 for uint256;
using SetUtil for SetUtil.UintSet;
using SetUtil for SetUtil.AddressSet;

error InvalidAccountDelegationIntents();

@@ -21,11 +22,17 @@ library AccountDelegationIntents {
SetUtil.UintSet intentsId;
mapping(bytes32 => SetUtil.UintSet) intentsByPair; // poolId/collateralId => intentIds[]
// accounting for the intents collateral delegated
// Per Pool
SetUtil.UintSet delegatedPools;
mapping(uint128 => uint256) delegatedCollateralAmountPerPool; // poolId => delegatedCollateralAmount
uint256 delegateAcountCachedCollateral;
mapping(uint128 => uint256) undelegatedCollateralAmountPerPool; // poolId => undelegatedCollateralAmount
uint256 undelegateAcountCachedCollateral;
// Per Collateral
SetUtil.AddressSet delegatedCollaterals;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we can avoid storing this and only track it off-chain, save about 2-3 storage slots!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this one is possible tho

mapping(address => uint256) delegatedAmountPerCollateral; // collateralType => delegatedCollateralAmount
mapping(address => uint256) undelegatedAmountPerCollateral; // collateralType => undelegatedCollateralAmount
// Global
int256 netAcountCachedDelegatedCollateral;
}

@@ -63,21 +70,33 @@ library AccountDelegationIntents {
.add(delegationIntent.id);

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

if (!self.delegatedPools.contains(delegationIntent.poolId)) {
self.delegatedPools.add(delegationIntent.poolId);
}

if (!self.delegatedCollaterals.contains(delegationIntent.collateralType)) {
self.delegatedCollaterals.add(delegationIntent.collateralType);
}

self.netAcountCachedDelegatedCollateral += delegationIntent.collateralDeltaAmountD18;
}

@@ -97,13 +116,19 @@ library AccountDelegationIntents {
.remove(delegationIntent.id);

if (delegationIntent.collateralDeltaAmountD18 >= 0) {
self.delegatedAmountPerCollateral[delegationIntent.collateralType] -= delegationIntent
.collateralDeltaAmountD18
.toUint();
self.delegatedCollateralAmountPerPool[delegationIntent.poolId] -= delegationIntent
.collateralDeltaAmountD18
.toUint();
self.delegateAcountCachedCollateral -= delegationIntent
.collateralDeltaAmountD18
.toUint();
} else {
self.undelegatedAmountPerCollateral[
delegationIntent.collateralType
] -= (delegationIntent.collateralDeltaAmountD18 * -1).toUint();
self.undelegatedCollateralAmountPerPool[delegationIntent.poolId] -= (delegationIntent
.collateralDeltaAmountD18 * -1).toUint();
self.undelegateAcountCachedCollateral -= (delegationIntent.collateralDeltaAmountD18 *
@@ -144,5 +169,13 @@ library AccountDelegationIntents {
self.undelegatedCollateralAmountPerPool[pools[i].to128()] = 0;
self.delegatedPools.remove(pools[i]);
}

// Clear the cached collateral per collateral
address[] memory addresses = self.delegatedCollaterals.values();
for (uint256 i = 0; i < addresses.length; i++) {
self.delegatedAmountPerCollateral[addresses[i]] = 0;
self.undelegatedAmountPerCollateral[addresses[i]] = 0;
self.delegatedCollaterals.remove(addresses[i]);
}
}
}