Skip to content

Commit

Permalink
Add {Risk,Protocol,Market}Parameter, Local, Order, and Position lib t…
Browse files Browse the repository at this point in the history
…ests (#26)

* Add 'Local' lib tests

* Add 'MarketParameter' lib tests

* Add 'ProtocolParameter' lib tests

* Add 'RiskParameter' lib tests

* Add 'Position' lib tests

* Add 'Order' lib tests

* [RiskParameterTest] Use different values for structs
  • Loading branch information
arjun-io authored Jul 19, 2023
1 parent c931370 commit bb0fe47
Show file tree
Hide file tree
Showing 14 changed files with 4,177 additions and 2 deletions.
49 changes: 49 additions & 0 deletions packages/perennial/contracts/test/LocalTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import "../types/Local.sol";

contract LocalTester {
LocalStorage public local;

function read() external view returns (Local memory) {
return local.read();
}

function store(Local memory newLocal) external {
return local.store(newLocal);
}

function update(Fixed6 collateral) external {
Local memory newLocal = local.read();
newLocal.update(collateral);
local.store(newLocal);
}

function accumulate(
Position memory fromPosition,
Position memory toPosition,
Version memory fromVersion,
Version memory toVersion
) external returns (LocalAccumulationResult memory values) {
Local memory newLocal = local.read();
values = newLocal.accumulate(fromPosition, toPosition, fromVersion, toVersion);
local.store(newLocal);
}

function protect(
Position memory latestPosition,
uint256 currentTimestamp,
bool tryProtect
) external returns (bool protection) {
Local memory newLocal = local.read();
protection = newLocal.protect(latestPosition, currentTimestamp, tryProtect);
local.store(newLocal);
}

function clearReward() external {
Local memory newLocal = local.read();
newLocal.clearReward();
local.store(newLocal);
}
}
16 changes: 16 additions & 0 deletions packages/perennial/contracts/test/MarketParameterTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import "../types/MarketParameter.sol";

contract MarketParameterTester {
MarketParameterStorage public marketParameter;

function read() external view returns (MarketParameter memory) {
return marketParameter.read();
}

function store(MarketParameter memory newMarketParameter) external {
return marketParameter.store(newMarketParameter);
}
}
40 changes: 40 additions & 0 deletions packages/perennial/contracts/test/OrderTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import "../types/Order.sol";

contract OrderTester {
function registerFee(
Order memory order,
OracleVersion memory latestVersion,
MarketParameter memory marketParameter,
RiskParameter memory riskParameter
) public pure returns (Order memory) {
order.registerFee(latestVersion, marketParameter, riskParameter);

return order;
}

function increasesPosition(Order memory order) public pure returns (bool) {
return order.increasesPosition();
}

function increasesTaker(Order memory order) public pure returns (bool) {
return order.increasesTaker();
}

function decreasesLiquidity(Order memory order) public pure returns (bool) {
return order.decreasesLiquidity();
}

function liquidityCheckApplicable(
Order memory order,
MarketParameter memory marketParameter
) public pure returns (bool) {
return order.liquidityCheckApplicable(marketParameter);
}

function isEmpty(Order memory order) public pure returns (bool) {
return order.isEmpty();
}
}
160 changes: 160 additions & 0 deletions packages/perennial/contracts/test/PositionTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import "../types/Position.sol";

abstract contract PositionTester {
function read() public view virtual returns (Position memory);

function store(Position memory newPosition) public virtual;

function ready(OracleVersion memory latestVersion) external view returns (bool result) {
return read().ready(latestVersion);
}

function update(Position memory newPosition) external {
Position memory _position = read();
_position.update(newPosition);
store(_position);
}

function update(
uint256 currentId,
uint256 currentTimestamp,
UFixed6 newMaker,
UFixed6 newLong,
UFixed6 newShort
) external returns (Order memory newOrder) {
Position memory _position = read();
newOrder = _position.update(currentId, currentTimestamp, newMaker, newLong, newShort);
store(_position);
}

function update(
uint256 currentId,
uint256 currentTimestamp,
Order memory order,
RiskParameter memory riskParameter
) external returns (Order memory updatedOrder) {
Position memory _position = read();
_position.update(currentId, currentTimestamp, order, riskParameter);
store(_position);
return order;
}

function update(Fixed6 collateralAmount) external {
Position memory _position = read();
_position.update(collateralAmount);
store(_position);
}

function invalidate(Position memory latestPosition) external {
Position memory _position = read();
_position.invalidate(latestPosition);
store(_position);
}

function registerFee(Order memory order) external {
Position memory _position = read();
_position.registerFee(order);
store(_position);
}

function magnitude() external view returns (UFixed6) {
return read().magnitude();
}

function major() external view returns (UFixed6) {
return read().major();
}

function minor() external view returns (UFixed6) {
return read().minor();
}

function net() external view returns (UFixed6) {
return read().net();
}

function skew() external view returns (Fixed6) {
return read().skew();
}

function virtualSkew(RiskParameter memory riskParameter) external view returns (Fixed6) {
return read().virtualSkew(riskParameter);
}

function utilization() external view returns (UFixed6) {
return read().utilization();
}

function longSocialized() external view returns (UFixed6) {
return read().longSocialized();
}

function shortSocialized() external view returns (UFixed6) {
return read().shortSocialized();
}

function takerSocialized() external view returns (UFixed6) {
return read().takerSocialized();
}

function efficiency() external view returns (UFixed6) {
return read().efficiency();
}

function socialized() external view returns (bool) {
return read().socialized();
}

function singleSided() external view returns (bool) {
return read().singleSided();
}

function maintenance(
OracleVersion memory latestVersion,
RiskParameter memory riskParameter
) external view returns (UFixed6) {
return read().maintenance(latestVersion, riskParameter);
}

function collateralized(
OracleVersion memory currentOracleVersion,
RiskParameter memory riskParameter,
Fixed6 collateral
) external view returns (bool) {
return read().collateralized(currentOracleVersion, riskParameter, collateral);
}

function liquidationFee(
OracleVersion memory currentOracleVersion,
RiskParameter memory riskParameter
) external view returns (UFixed6) {
return read().liquidationFee(currentOracleVersion, riskParameter);
}
}

contract PositionGlobalTester is PositionTester {
PositionStorageGlobal public position;

function read() public view override returns (Position memory) {
return position.read();
}

function store(Position memory newPosition) public override {
position.store(newPosition);
}
}

contract PositionLocalTester is PositionTester {
PositionStorageLocal public position;

function read() public view override returns (Position memory) {
return position.read();
}

function store(Position memory newPosition) public override {
position.store(newPosition);
}
}
16 changes: 16 additions & 0 deletions packages/perennial/contracts/test/ProtocolParameterTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import "../types/ProtocolParameter.sol";

contract ProtocolParameterTester {
ProtocolParameterStorage public protocolParameter;

function read() external view returns (ProtocolParameter memory) {
return protocolParameter.read();
}

function store(ProtocolParameter memory newProtocolParameter) external {
return protocolParameter.store(newProtocolParameter);
}
}
16 changes: 16 additions & 0 deletions packages/perennial/contracts/test/RiskParameterTester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.13;

import "../types/RiskParameter.sol";

contract RiskParameterTester {
RiskParameterStorage public riskParameter;

function read() external view returns (RiskParameter memory) {
return riskParameter.read();
}

function store(RiskParameter memory newRiskParameter) external {
return riskParameter.store(newRiskParameter);
}
}
2 changes: 1 addition & 1 deletion packages/perennial/contracts/types/Order.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ library OrderLib {
}

function isEmpty(Order memory self) internal pure returns (bool) {
return self.maker.add(self.long).add(self.short).isZero();
return self.maker.abs().add(self.long.abs()).add(self.short.abs()).isZero();
}
}
2 changes: 1 addition & 1 deletion packages/perennial/contracts/types/RiskParameter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ library RiskParameterStorageLib {
if (newValue.utilizationCurve.minRate.gt(UFixed6.wrap(type(uint32).max))) revert RiskParameterStorageInvalidError();
if (newValue.utilizationCurve.maxRate.gt(UFixed6.wrap(type(uint32).max))) revert RiskParameterStorageInvalidError();
if (newValue.utilizationCurve.targetRate.gt(UFixed6.wrap(type(uint32).max))) revert RiskParameterStorageInvalidError();
if (newValue.utilizationCurve.targetUtilization.gt(UFixed6.wrap(type(uint32).max))) revert RiskParameterStorageInvalidError();
if (newValue.utilizationCurve.targetUtilization.gt(UFixed6.wrap(type(uint24).max))) revert RiskParameterStorageInvalidError();
if (newValue.pController.k.gt(UFixed6.wrap(type(uint40).max))) revert RiskParameterStorageInvalidError();
if (newValue.pController.max.gt(UFixed6.wrap(type(uint32).max))) revert RiskParameterStorageInvalidError();
if (newValue.minMaintenance.gt(UFixed6.wrap(type(uint48).max))) revert RiskParameterStorageInvalidError();
Expand Down
Loading

0 comments on commit bb0fe47

Please sign in to comment.