-
Notifications
You must be signed in to change notification settings - Fork 0
CYS3-06: Policies Can Be Backed By Unswappable Collateral #106
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,10 @@ contract Swapper is UUPSUpgradeable, AccessControlUpgradeable, PausableUpgradeab | |
|
|
||
| mapping(bytes32 routeKey => SwapRoute) private _swapRoutes; | ||
|
|
||
| address public override claimManager; | ||
| mapping(bytes32 routeKey => uint256 lockedUntil) private _routeLockedUntil; | ||
| mapping(address target => uint256 lockedUntil) private _targetLockedUntil; | ||
|
Comment on lines
+52
to
+54
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these change will not collide with the existing storage variables, right?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we will just extend the storage, not replace it. |
||
|
|
||
| /// @custom:oz-upgrades-unsafe-allow constructor | ||
| constructor() { | ||
| _disableInitializers(); | ||
|
|
@@ -113,6 +117,10 @@ contract Swapper is UUPSUpgradeable, AccessControlUpgradeable, PausableUpgradeab | |
| */ | ||
| function setSwapTargetWhitelist(address target, bool whitelisted) external override onlyRole(DEFAULT_ADMIN_ROLE) { | ||
| require(target != address(0), ZeroAddress()); | ||
| if (!whitelisted && whitelistedTargets[target]) { | ||
| uint256 lockExpiry = _targetLockedUntil[target]; | ||
| require(block.timestamp >= lockExpiry, TargetLockedByActivePolicies(target, lockExpiry)); | ||
| } | ||
| whitelistedTargets[target] = whitelisted; | ||
| emit SwapTargetWhitelistSet(target, whitelisted); | ||
| } | ||
|
|
@@ -140,6 +148,54 @@ contract Swapper is UUPSUpgradeable, AccessControlUpgradeable, PausableUpgradeab | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ISwapper | ||
| */ | ||
| function setClaimManager(address claimManager_) external override onlyRole(DEFAULT_ADMIN_ROLE) { | ||
| require(claimManager_ != address(0), ZeroAddress()); | ||
| claimManager = claimManager_; | ||
| emit ClaimManagerSet(claimManager_); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ISwapper | ||
| */ | ||
| function lockRouteUntil(address tokenIn, address tokenOut, uint256 lockedUntil) external override { | ||
| require(msg.sender == claimManager, OnlyClaimManager()); | ||
|
|
||
| address actualTokenIn = tokenIn == NATIVE_ETH ? nativeWrapper : tokenIn; | ||
| address actualTokenOut = tokenOut == NATIVE_ETH ? nativeWrapper : tokenOut; | ||
|
|
||
| if (actualTokenIn == actualTokenOut) return; | ||
|
|
||
| bytes32 routeKey = _getRouteKey(actualTokenIn, actualTokenOut); | ||
| SwapRoute storage route = _swapRoutes[routeKey]; | ||
| require(route.swapTarget != address(0), SwapRouteNotEnabled(tokenIn, tokenOut)); | ||
|
|
||
| if (lockedUntil > _routeLockedUntil[routeKey]) { | ||
| _routeLockedUntil[routeKey] = lockedUntil; | ||
| } | ||
| if (lockedUntil > _targetLockedUntil[route.swapTarget]) { | ||
| _targetLockedUntil[route.swapTarget] = lockedUntil; | ||
| } | ||
|
|
||
| emit RouteLocked(actualTokenIn, actualTokenOut, route.swapTarget, _routeLockedUntil[routeKey]); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ISwapper | ||
| */ | ||
| function routeLockedUntil(address tokenIn, address tokenOut) external view override returns (uint256) { | ||
| return _routeLockedUntil[_getRouteKey(tokenIn, tokenOut)]; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ISwapper | ||
| */ | ||
| function targetLockedUntil(address target) external view override returns (uint256) { | ||
| return _targetLockedUntil[target]; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc ISwapper | ||
| */ | ||
|
|
@@ -163,6 +219,13 @@ contract Swapper is UUPSUpgradeable, AccessControlUpgradeable, PausableUpgradeab | |
| bytes32 routeKey = _getRouteKey(tokenIn, tokenOut); | ||
| SwapRoute storage route = _swapRoutes[routeKey]; | ||
|
|
||
| bool material = | ||
| route.swapTarget != address(0) && (route.swapTarget != swapTarget || (route.enabled && !enabled)); | ||
| if (material) { | ||
| uint256 lockExpiry = _routeLockedUntil[routeKey]; | ||
| require(block.timestamp >= lockExpiry, RouteLockedByActivePolicies(tokenIn, tokenOut, lockExpiry)); | ||
| } | ||
|
|
||
| route.tokenIn = tokenIn; | ||
| route.tokenOut = tokenOut; | ||
| route.swapTarget = swapTarget; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you moved total stake calculation inside this function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stake computation was moved inside precisely to enforce the invariant from the audit:
collateral should only count toward backing if it can actually be used for settlement. If the stake loop lived separately in bindPolicy(), we would have a logical gap where totalStakeUSD gets computed unconditionally for all tokens, but route validation could revert later.