Skip to content

Commit

Permalink
✅ Test co fees and eth deposits/withdraws
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders committed Oct 13, 2023
1 parent 0e272d9 commit ba51478
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 44 deletions.
6 changes: 3 additions & 3 deletions test/AsyncOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract CommitOrder is AsyncOrderTest {
_perpsMarketId: SETH_PERPS_MARKET_ID,
_accountId: accountId,
_sizeDelta: 1 ether,
_settlementStrategyId: 0,
_settlementStrategyId: SETTLEMENT_STRATEGY_ID,
_acceptablePrice: type(uint256).max,
_trackingCode: TRACKING_CODE,
_referrer: REFERRER
Expand Down Expand Up @@ -65,7 +65,7 @@ contract CommitOrder is AsyncOrderTest {
_perpsMarketId: INVALID_PERPS_MARKET_ID,
_accountId: accountId,
_sizeDelta: 1 ether,
_settlementStrategyId: 0,
_settlementStrategyId: SETTLEMENT_STRATEGY_ID,
_acceptablePrice: type(uint256).max,
_trackingCode: TRACKING_CODE,
_referrer: REFERRER
Expand Down Expand Up @@ -95,7 +95,7 @@ contract CommitOrder is AsyncOrderTest {
_perpsMarketId: SETH_PERPS_MARKET_ID,
_accountId: accountId,
_sizeDelta: sizeDelta,
_settlementStrategyId: 0,
_settlementStrategyId: SETTLEMENT_STRATEGY_ID,
_acceptablePrice: type(uint256).max,
_trackingCode: TRACKING_CODE,
_referrer: REFERRER
Expand Down
248 changes: 211 additions & 37 deletions test/ConditionalOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ contract CanExecute is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -91,9 +91,9 @@ contract CanExecute is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -127,9 +127,9 @@ contract CanExecute is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -165,7 +165,7 @@ contract VerifySigner is ConditionalOrderTest {
marketId: 0,
accountId: accountId,
sizeDelta: 0,
settlementStrategyId: 0,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: 0,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
Expand All @@ -192,7 +192,7 @@ contract VerifySigner is ConditionalOrderTest {
marketId: 0,
accountId: accountId,
sizeDelta: 0,
settlementStrategyId: 0,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: 0,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
Expand Down Expand Up @@ -221,7 +221,7 @@ contract VerifySignature is ConditionalOrderTest {
marketId: 0,
accountId: 0,
sizeDelta: 0,
settlementStrategyId: 0,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: 0,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
Expand Down Expand Up @@ -254,7 +254,7 @@ contract VerifySignature is ConditionalOrderTest {
marketId: 0,
accountId: 0,
sizeDelta: 0,
settlementStrategyId: 0,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: 0,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
Expand Down Expand Up @@ -424,9 +424,9 @@ contract Execute is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -465,25 +465,91 @@ contract Execute is ConditionalOrderTest {
assertTrue(fees != 0);
}

function test_execute_leverage_exceeded() public {
/// @custom:todo test
function test_execute_CannotExecuteOrder_too_leveraged() public {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: INVALID_SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: signer,
nonce: 0,
requireVerified: false,
trustedExecutor: address(this),
maxExecutorFee: type(uint256).max,
conditions: new bytes[](0)
});

bytes memory signature = getConditionalOrderSignature({
co: co,
privateKey: signerPrivateKey,
domainSeparator: engine.DOMAIN_SEPARATOR()
});

try engine.execute(co, signature, ZERO_CO_FEE) {}
catch (bytes memory reason) {
assertEq(bytes4(reason), InsufficientMargin.selector);
}
}

function test_execute_CannotExecuteOrder_invalid_acceptablePrice() public {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: INVALID_ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: signer,
nonce: 0,
requireVerified: false,
trustedExecutor: address(this),
maxExecutorFee: type(uint256).max,
conditions: new bytes[](0)
});

bytes memory signature = getConditionalOrderSignature({
co: co,
privateKey: signerPrivateKey,
domainSeparator: engine.DOMAIN_SEPARATOR()
});

try engine.execute(co, signature, ZERO_CO_FEE) {}
catch (bytes memory reason) {
assertEq(bytes4(reason), AcceptablePriceExceeded.selector);
}
}

function test_execute_CannotExecuteOrder() public {
function test_execute_CannotExecuteOrder_invalid_settlementStrategyId()
public
{
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 50 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: INVALID_SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: address(0),
signer: signer,
nonce: 0,
requireVerified: false,
trustedExecutor: address(this),
Expand All @@ -498,7 +564,10 @@ contract Execute is ConditionalOrderTest {
});

vm.expectRevert(
abi.encodeWithSelector(IEngine.CannotExecuteOrder.selector)
abi.encodeWithSelector(
InvalidSettlementStrategy.selector,
INVALID_SETTLEMENT_STRATEGY_ID
)
);

engine.execute(co, signature, ZERO_CO_FEE);
Expand All @@ -507,7 +576,112 @@ contract Execute is ConditionalOrderTest {

contract Fee is ConditionalOrderTest {
function test_fee_imposed() public {
/// @custom:todo test
engine.depositEth{value: 1 ether}(accountId);

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: signer,
nonce: 0,
requireVerified: false,
trustedExecutor: address(this),
maxExecutorFee: type(uint256).max,
conditions: new bytes[](0)
});

bytes memory signature = getConditionalOrderSignature({
co: co,
privateKey: signerPrivateKey,
domainSeparator: engine.DOMAIN_SEPARATOR()
});

uint256 preExecutorBalance = address(this).balance;

engine.execute(co, signature, CO_FEE);

uint256 postExecutorBalance = address(this).balance;

assertEq(preExecutorBalance + CO_FEE, postExecutorBalance);
}

function test_fee_exceeds_account_credit() public {
engine.depositEth{value: CO_FEE - 1}(accountId);

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: signer,
nonce: 0,
requireVerified: false,
trustedExecutor: address(this),
maxExecutorFee: type(uint256).max,
conditions: new bytes[](0)
});

bytes memory signature = getConditionalOrderSignature({
co: co,
privateKey: signerPrivateKey,
domainSeparator: engine.DOMAIN_SEPARATOR()
});

vm.expectRevert(IEngine.CannotExecuteOrder.selector);

engine.execute(co, signature, CO_FEE);
}

function test_fee_exceeds_maxExecutorFee() public {
engine.depositEth{value: 1 ether}(accountId);

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: false,
trackingCode: TRACKING_CODE,
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: signer,
nonce: 0,
requireVerified: false,
trustedExecutor: address(this),
maxExecutorFee: 0, // 0 max fee (i.e. any non-zero fee is too high)
conditions: new bytes[](0)
});

bytes memory signature = getConditionalOrderSignature({
co: co,
privateKey: signerPrivateKey,
domainSeparator: engine.DOMAIN_SEPARATOR()
});

vm.expectRevert(IEngine.CannotExecuteOrder.selector);

engine.execute(co, signature, CO_FEE);
}
}

Expand All @@ -520,9 +694,9 @@ contract ReduceOnly is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: true,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -554,9 +728,9 @@ contract ReduceOnly is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: true,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -591,9 +765,9 @@ contract ReduceOnly is ConditionalOrderTest {
IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: 1 ether,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
sizeDelta: SIZE_DELTA,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: true,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -629,8 +803,8 @@ contract ReduceOnly is ConditionalOrderTest {
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: type(int128).max,
settlementStrategyId: 0,
acceptablePrice: type(uint256).max,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: ACCEPTABLE_PRICE,
isReduceOnly: true,
trackingCode: TRACKING_CODE,
referrer: REFERRER
Expand Down Expand Up @@ -668,7 +842,7 @@ contract ReduceOnly is ConditionalOrderTest {
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: -type(int128).max,
settlementStrategyId: 0,
settlementStrategyId: SETTLEMENT_STRATEGY_ID,
acceptablePrice: 0,
isReduceOnly: true,
trackingCode: TRACKING_CODE,
Expand Down
Loading

0 comments on commit ba51478

Please sign in to comment.