-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add {Risk,Protocol,Market}Parameter, Local, Order, and Position lib t…
…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
Showing
14 changed files
with
4,177 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -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
16
packages/perennial/contracts/test/MarketParameterTester.sol
This file contains 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 |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -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
16
packages/perennial/contracts/test/ProtocolParameterTester.sol
This file contains 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 |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.