Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fredsnax committed Aug 16, 2024
1 parent 862ea42 commit f65a429
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ interface ILimitOrderModule {
int256 amount
);

/**
* @notice Thrown when attempting to use two makers or two takers
* @param shortIsMaker is the short a maker?
* @param longIsMaker is the long a maker?
*/
error MismatchingMakerTakerLimitOrder(bool shortIsMaker, bool longIsMaker);

/**
* @notice Thrown when attempting to use an invalid relayer
* @param relayer address of the relayer submitted with a limit order
Expand Down
6 changes: 6 additions & 0 deletions markets/perps-market/contracts/modules/LimitOrderModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ contract LimitOrderModule is ILimitOrderModule, IMarketEvents, IAccountEvents {
LimitOrder.SignedOrderRequest calldata shortOrder,
LimitOrder.SignedOrderRequest calldata longOrder
) internal view {
if (shortOrder.limitOrderMaker == longOrder.limitOrderMaker) {
revert MismatchingMakerTakerLimitOrder(
shortOrder.limitOrderMaker,
longOrder.limitOrderMaker
);
}
if (shortOrder.relayer != longOrder.relayer) {
revert LimitOrderDifferentRelayer(shortOrder.relayer, longOrder.relayer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ describe('Settle Offchain Limit Order tests', () => {
let longOrder: Order;
const price = bn(999.9995);
const amount = bn(1);
const nonZeroLimitOrderMakerFee = bn(0.0002); // 2bps
const nonZeroLimitOrderTakerFee = bn(0.0006); // 6bps
let relayer: ethers.Signer;
const relayerRatio = wei(0.3); // 30%

Expand All @@ -61,8 +63,6 @@ describe('Settle Offchain Limit Order tests', () => {
await systems()
.PerpsMarket.connect(owner())
.updateRelayerShare(await relayer.getAddress(), relayerRatio.toBN()); // 30%
const nonZeroLimitOrderMakerFee = bn(0.0002); // 2bps
const nonZeroLimitOrderTakerFee = bn(0.0006); // 6bps
await systems()
.PerpsMarket.connect(owner())
.setLimitOrderFees(ethMarketId, nonZeroLimitOrderMakerFee, nonZeroLimitOrderTakerFee);
Expand Down Expand Up @@ -163,12 +163,23 @@ describe('Settle Offchain Limit Order tests', () => {
accruedFundingLong = 0;
const newPositionSizeShort = 0,
newPositionSizeLong = 0;
const limitOrderFeesShort = 599999700000000000,
limitOrderFeesLong = 599999700000000000;
const limitOrderFeesShort = amount
.mul(price)
.div(bn(1))
.mul(nonZeroLimitOrderMakerFee)
.div(bn(1))
.toString(),
limitOrderFeesLong = amount
.mul(price)
.div(bn(1))
.mul(nonZeroLimitOrderTakerFee)
.div(bn(1))
.toString();
const relayerFees = 0;
const feeCollectorFees = 0;
const chargedInterestShort = 0,
chargedInterestLong = 0;

const orderSettledEventsArgs = {
trader1: [
`${ethMarketId}`,
Expand All @@ -195,7 +206,7 @@ describe('Settle Offchain Limit Order tests', () => {
`${limitOrderFeesLong}`,
`${relayerFees}`,
`${feeCollectorFees}`,
`"${longOrder.trackingCode.toString()}"`,
`"${longOrder.trackingCode}"`,
`${chargedInterestLong}`,
].join(', '),
};
Expand Down Expand Up @@ -318,6 +329,26 @@ describe('Settle Offchain Limit Order tests', () => {
);
});

it('fails when the orders are both makers', async () => {
const badLongOrder = { ...longOrder, limitOrderMaker: true };
const signedShortOrder = await signOrder(
shortOrder,
trader1() as ethers.Wallet,
systems().PerpsMarket.address
);
const badSignedLongOrder = await signOrder(
badLongOrder,
trader2() as ethers.Wallet,
systems().PerpsMarket.address
);
await assertRevert(
systems()
.PerpsMarket.connect(owner())
.settleLimitOrder(shortOrder, signedShortOrder, badLongOrder, badSignedLongOrder),
`MismatchingMakerTakerLimitOrder(${shortOrder.limitOrderMaker}, ${badLongOrder.limitOrderMaker})`
);
});

it('fails with an invalid relayer', async () => {
const badLongOrder = { ...longOrder, relayer: await trader1().getAddress() };
const badShortOrder = { ...shortOrder, relayer: await trader1().getAddress() };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export function createMatchingLimitOrders(orderArgs: OrderCreationArgs): {
...orderArgs,
isShort: true,
accountId: orderArgs.accountId - 1,
isMaker: !orderArgs.isMaker,
});
return {
shortOrder: oppositeOrder,
Expand Down

0 comments on commit f65a429

Please sign in to comment.