Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
Feat/exceeds initial ask (#120)
Browse files Browse the repository at this point in the history
* initial incorrect implementation

* maybe broken liquidationinitialask test

* fix maxpotentialdebt and added invalid hash enum

* fix liquidationinitialask

* implemented expectreverts in reverttesting

* update testhelpers

Co-authored-by: Andrew Redden <=>
Co-authored-by: Andrew Redden <opensource@andrewredden.com>
  • Loading branch information
SantiagoGregory and androolloyd authored Nov 22, 2022
1 parent 8844343 commit 76ac3c8
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 44 deletions.
28 changes: 23 additions & 5 deletions src/AstariaRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,18 @@ contract AstariaRouter is Auth, ERC4626Router, Pausable, IAstariaRouter {
address[] memory allowList = new address[](2);
allowList[0] = address(msg.sender);
allowList[1] = delegate;
RouterStorage storage s = _loadRouterSlot();

return
_newVault(uint256(0), delegate, uint256(0), true, allowList, uint256(0));
_newVault(
s,
uint256(0),
delegate,
uint256(0),
true,
allowList,
uint256(0)
);
}

function newPublicVault(
Expand All @@ -439,8 +448,20 @@ contract AstariaRouter is Auth, ERC4626Router, Pausable, IAstariaRouter {
address[] calldata allowList,
uint256 depositCap
) public whenNotPaused returns (address) {
RouterStorage storage s = _loadRouterSlot();
if (s.minEpochLength > epochLength) {
revert IPublicVault.InvalidState(
IPublicVault.InvalidStates.EPOCH_TOO_LOW
);
}
if (s.maxEpochLength < epochLength) {
revert IPublicVault.InvalidState(
IPublicVault.InvalidStates.EPOCH_TOO_HIGH
);
}
return
_newVault(
s,
epochLength,
delegate,
vaultFee,
Expand Down Expand Up @@ -598,6 +619,7 @@ contract AstariaRouter is Auth, ERC4626Router, Pausable, IAstariaRouter {
* @return vaultAddr The address for the new Vault.
*/
function _newVault(
RouterStorage storage s,
uint256 epochLength,
address delegate,
uint256 vaultFee,
Expand All @@ -607,11 +629,7 @@ contract AstariaRouter is Auth, ERC4626Router, Pausable, IAstariaRouter {
) internal returns (address vaultAddr) {
uint8 vaultType;

RouterStorage storage s = _loadRouterSlot();
if (epochLength > uint256(0)) {
if (s.minEpochLength > epochLength || epochLength > s.maxEpochLength) {
revert InvalidEpochLength(epochLength);
}
vaultType = uint8(ImplementationType.PublicVault);
} else {
vaultType = uint8(ImplementationType.PrivateVault);
Expand Down
39 changes: 24 additions & 15 deletions src/LienToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ contract LienToken is ERC721, ILienToken, Auth {
modifier validateStack(uint256 collateralId, Stack[] memory stack) {
LienStorage storage s = _loadLienStorageSlot();
bytes32 stateHash = s.collateralStateHash[collateralId];
if (stateHash != bytes32(0)) {
require(keccak256(abi.encode(stack)) == stateHash, "invalid hash");
if (stateHash != bytes32(0) && keccak256(abi.encode(stack)) != stateHash) {
revert InvalidState(InvalidStates.INVALID_HASH);
}
_;
}
Expand Down Expand Up @@ -410,28 +410,37 @@ contract LienToken is ERC721, ILienToken, Auth {
LienStorage storage s,
Stack[] memory stack,
Stack memory newSlot
) internal view returns (Stack[] memory newStack) {
uint256 n = stack.length;
newStack = new Stack[](n + 1);

uint256 maxPotentialDebt = 0;
for (uint256 i; i < n; ) {
if (block.timestamp > stack[i].point.end) {
) internal returns (Stack[] memory newStack) {
newStack = new Stack[](stack.length + 1);
newStack[stack.length] = newSlot;

uint256 potentialDebt = _getOwed(newSlot, newSlot.point.end);
for (uint256 i = stack.length; i > 0; ) {
uint256 j = i - 1;
newStack[j] = stack[j];
if (block.timestamp > newStack[j].point.end) {
revert InvalidState(InvalidStates.EXPIRED_LIEN);
}
newStack[i] = stack[i];
unchecked {
maxPotentialDebt += _getOwed(stack[i], stack[i].point.end);
++i;
potentialDebt += _getOwed(newStack[j], newStack[j].point.end);
}
if (potentialDebt > newStack[j].lien.details.liquidationInitialAsk) {
revert InvalidState(InvalidStates.INITIAL_ASK_EXCEEDED);
}
}

if (maxPotentialDebt > newSlot.lien.details.maxPotentialDebt) {
unchecked {
--i;
}
}
if (
stack.length > 0 && potentialDebt > newSlot.lien.details.maxPotentialDebt
) {
revert InvalidState(InvalidStates.DEBT_LIMIT);
}
newStack[n] = newSlot;
}

event log_named_uint(string name, uint256 value);

function payDebtViaClearingHouse(uint256 collateralId, uint256 payment)
external
{
Expand Down
5 changes: 3 additions & 2 deletions src/interfaces/ILienToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,9 @@ interface ILienToken is IERC721 {
LIEN_NO_DEBT,
EXPIRED_LIEN,
DEBT_LIMIT,
MAX_LIENS,
INVALID_LIQUIDATION_INITIAL_ASK
MAX_LIENS,
INVALID_HASH,
INITIAL_ASK_EXCEEDED
}

error InvalidState(InvalidStates);
Expand Down
Loading

0 comments on commit 76ac3c8

Please sign in to comment.