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 f65a429 commit b56b7a7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
7 changes: 5 additions & 2 deletions markets/perps-market/contracts/modules/LimitOrderModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {DecimalMath} from "@synthetixio/core-contracts/contracts/utils/DecimalMa
import {SafeCastI256} from "@synthetixio/core-contracts/contracts/utils/SafeCast.sol";
import {SafeCastU256} from "@synthetixio/core-contracts/contracts/utils/SafeCast.sol";
import {FeatureFlag} from "@synthetixio/core-modules/contracts/storage/FeatureFlag.sol";
// import {ERC2771Context} from "@synthetixio/core-contracts/contracts/utils/ERC2771Context.sol";
import {ILimitOrderModule} from "../interfaces/ILimitOrderModule.sol";
import {IMarketEvents} from "../interfaces/IMarketEvents.sol";
import {IAccountEvents} from "../interfaces/IAccountEvents.sol";
Expand All @@ -23,6 +22,7 @@ import {PerpsAccount, SNX_USD_MARKET_ID} from "../storage/PerpsAccount.sol";
import {PerpsMarketConfiguration} from "../storage/PerpsMarketConfiguration.sol";
import {MathUtil} from "../utils/MathUtil.sol";
import {Flags} from "../utils/Flags.sol";
import "hardhat/console.sol";

/**
* @title Module for settling signed P2P limit orders
Expand Down Expand Up @@ -79,7 +79,8 @@ contract LimitOrderModule is ILimitOrderModule, IMarketEvents, IAccountEvents {
LimitOrder.SignedOrderRequest calldata order,
LimitOrder.Signature calldata sig
) external {
// TODO consider adding feature flag here
FeatureFlag.ensureAccessToFeature(Flags.PERPS_SYSTEM);
FeatureFlag.ensureAccessToFeature(Flags.LIMIT_ORDER);
checkSigPermission(order, sig);
LimitOrder.Data storage limitOrderData = LimitOrder.load();

Expand Down Expand Up @@ -118,6 +119,8 @@ contract LimitOrderModule is ILimitOrderModule, IMarketEvents, IAccountEvents {
PerpsMarketConfiguration.Data storage marketConfig = PerpsMarketConfiguration.load(
shortOrder.marketId
);
console.log("maxMarketSize", marketConfig.maxMarketSize);
console.log("maxMarketValue", marketConfig.maxMarketValue);
perpsMarketData.validateLimitOrderSize(
marketConfig.maxMarketSize,
marketConfig.maxMarketValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import assertRevert from '@synthetixio/core-utils/utils/assertions/assert-revert
// import assert from 'assert';
// import { getTxTime } from '@synthetixio/core-utils/src/utils/hardhat/rpc';

describe('Settle Offchain Limit Order tests', () => {
describe.only('Settle Offchain Limit Order tests', () => {
const { systems, perpsMarkets, synthMarkets, provider, trader1, trader2, signers, owner } =
bootstrapMarkets({
synthMarkets: [
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Settle Offchain Limit Order tests', () => {

const restoreToSnapshot = snapshotCheckpoint(provider);

it('settles the orders and emits the proper events', async () => {
it.only('settles the orders and emits the proper events', async () => {
const signedShortOrder = await signOrder(
shortOrder,
trader1() as ethers.Wallet,
Expand Down Expand Up @@ -269,6 +269,52 @@ describe('Settle Offchain Limit Order tests', () => {
);
});

it.only('fails to cancel an already completed limit order', async () => {
const signedShortOrder = await signOrder(
shortOrder,
trader1() as ethers.Wallet,
systems().PerpsMarket.address
);
await assertRevert(
systems().PerpsMarket.connect(owner()).cancelLimitOrder(shortOrder, signedShortOrder),
`LimitOrderAlreadyUsed(${shortOrder.accountId}, ${shortOrder.nonce}, ${shortOrder.price}, ${shortOrder.amount})`
);
});

it.only('successfully cancels a new limit order', async () => {
const newNonceShortOrder = { ...shortOrder, nonce: 197889234 };
const signedNewNonceShortOrder = await signOrder(
newNonceShortOrder,
trader1() as ethers.Wallet,
systems().PerpsMarket.address
);
const successTx = await systems()
.PerpsMarket.connect(owner())
.cancelLimitOrder(newNonceShortOrder, signedNewNonceShortOrder);

await assertEvent(
successTx,
`LimitOrderCancelled(${newNonceShortOrder.accountId}, ${newNonceShortOrder.nonce}, ${newNonceShortOrder.price}, ${newNonceShortOrder.amount})`,
systems().PerpsMarket
);
});

it.only('fails to cancel a new limit order that is already settled', async () => {
const newNonceShortOrder = { ...shortOrder, nonce: 197889234 };
const signedNewNonceShortOrder = await signOrder(
newNonceShortOrder,
trader1() as ethers.Wallet,
systems().PerpsMarket.address
);
await assertRevert(
systems()
.PerpsMarket.connect(owner())
.cancelLimitOrder(newNonceShortOrder, signedNewNonceShortOrder),
`LimitOrderAlreadyUsed(${newNonceShortOrder.accountId}, ${newNonceShortOrder.nonce}, ${newNonceShortOrder.price}, ${newNonceShortOrder.amount})`
);
});

// TODO add the other transaction here and call rest
it('fails when the relayers are different for each order', async () => {
const badLongOrder = { ...longOrder, relayer: await trader1().getAddress() };
const signedShortOrder = await signOrder(
Expand Down
3 changes: 0 additions & 3 deletions protocol/synthetix/contracts/storage/AccountRBAC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pragma solidity >=0.8.11 <0.9.0;

import "@synthetixio/core-contracts/contracts/utils/SetUtil.sol";
import "@synthetixio/core-contracts/contracts/errors/AddressError.sol";
import "hardhat/console.sol";

/**
* @title Object for tracking an accounts permissions (role based access control).
Expand Down Expand Up @@ -139,8 +138,6 @@ library AccountRBAC {
bytes32 permission,
address target
) internal view returns (bool) {
console.log("signing address aka target", target);
console.log("account owner self.owner", self.owner);
return ((target == self.owner) ||
hasPermission(self, _ADMIN_PERMISSION, target) ||
hasPermission(self, permission, target));
Expand Down

0 comments on commit b56b7a7

Please sign in to comment.