Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pausable #303

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion contracts/LimitOrderProtocol.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pragma solidity 0.8.23;

import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "./OrderMixin.sol";

/**
Expand All @@ -28,14 +30,30 @@ import "./OrderMixin.sol";
*/
contract LimitOrderProtocol is
EIP712("1inch Limit Order Protocol", "4"),
Ownable,
Pausable,
OrderMixin
{
// solhint-disable-next-line no-empty-blocks
constructor(IWETH _weth) OrderMixin(_weth) {}
constructor(IWETH _weth) OrderMixin(_weth) Ownable(msg.sender) {}

/// @dev Returns the domain separator for the current chain (EIP-712)
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns(bytes32) {
return _domainSeparatorV4();
}

/**
* @notice Pauses all the trading functionality in the contract.
*/
function pause() external onlyOwner {
_pause();
}

/**
* @notice Unpauses all the trading functionality in the contract.
*/
function unpause() external onlyOwner {
_unpause();
}
}
6 changes: 4 additions & 2 deletions contracts/OrderMixin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pragma solidity 0.8.23;
import "@openzeppelin/contracts/utils/math/Math.sol";
import { EIP712 } from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";

import "@1inch/solidity-utils/contracts/interfaces/IWETH.sol";
import "@1inch/solidity-utils/contracts/libraries/SafeERC20.sol";
import "@1inch/solidity-utils/contracts/OnlyWethReceiver.sol";
Expand All @@ -23,7 +25,7 @@ import "./libraries/RemainingInvalidatorLib.sol";
import "./OrderLib.sol";

/// @title Limit Order mixin
abstract contract OrderMixin is IOrderMixin, EIP712, OnlyWethReceiver, PredicateHelper, SeriesEpochManager, PermitAndCall {
abstract contract OrderMixin is IOrderMixin, EIP712, PredicateHelper, SeriesEpochManager, Pausable, OnlyWethReceiver, PermitAndCall {
using SafeERC20 for IERC20;
using SafeERC20 for IWETH;
using OrderLib for IOrderMixin.Order;
Expand Down Expand Up @@ -267,7 +269,7 @@ abstract contract OrderMixin is IOrderMixin, EIP712, OnlyWethReceiver, Predicate
address target,
bytes calldata extension,
bytes calldata interaction
) private returns(uint256 makingAmount, uint256 takingAmount) {
) private whenNotPaused() returns(uint256 makingAmount, uint256 takingAmount) {
// Validate order
{
(bool valid, bytes4 validationResult) = order.isValidExtension(extension);
Expand Down
37 changes: 35 additions & 2 deletions test/LimitOrderProtocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe('LimitOrderProtocol', function () {
if (hre.__SOLIDITY_COVERAGE_RUNNING === undefined) {
const trace = findTrace(tracer, 'CALL', await swap.getAddress());
const opcodes = trace.children.map(item => item.opcode);
expect(countAllItems(opcodes)).to.deep.equal({ STATICCALL: 1, CALL: 2, SLOAD: 1, SSTORE: 1, LOG1: 1, MSTORE: 29, MLOAD: 10, SHA3: 5 });
expect(countAllItems(opcodes)).to.deep.equal({ STATICCALL: 1, CALL: 2, SLOAD: 2, SSTORE: 1, LOG1: 1, MSTORE: 29, MLOAD: 10, SHA3: 5 });
}
}
});
Expand All @@ -278,7 +278,7 @@ describe('LimitOrderProtocol', function () {
if (hre.__SOLIDITY_COVERAGE_RUNNING === undefined) {
const trace = findTrace(tracer, 'CALL', await swap.getAddress());
const opcodes = trace.children.map(item => item.opcode);
expect(countAllItems(opcodes)).to.deep.equal({ STATICCALL: 1, CALL: 2, SLOAD: 1, SSTORE: 1, LOG1: 1, MSTORE: 31, MLOAD: 10, SHA3: 6 });
expect(countAllItems(opcodes)).to.deep.equal({ STATICCALL: 1, CALL: 2, SLOAD: 2, SSTORE: 1, LOG1: 1, MSTORE: 31, MLOAD: 10, SHA3: 6 });
}
});

Expand Down Expand Up @@ -2018,4 +2018,37 @@ describe('LimitOrderProtocol', function () {
)).to.be.revertedWithCustomError(swap, 'ETHTransferFailed');
});
});

describe('Pause', function () {
it('Paused contract should not work', async function () {
const { tokens: { dai, weth }, contracts: { swap }, chainId } = await loadFixture(deployContractsAndInit);

await swap.pause();

const order = buildOrder({
makerAsset: await dai.getAddress(),
takerAsset: await weth.getAddress(),
makingAmount: 1,
takingAmount: 1,
maker: addr1.address,
makerTraits: buildMakerTraits(),
});

const { r, yParityAndS: vs } = ethers.Signature.from(await signOrder(order, chainId, await swap.getAddress(), addr1));
await expect(swap.fillOrder(order, r, vs, 1, fillWithMakingAmount(1))).to.be.revertedWithCustomError(swap, 'EnforcedPause');
});

it('pause and unpause can only be called by owner', async function () {
const { contracts: { swap } } = await loadFixture(deployContractsAndInit);
await expect(swap.connect(addr2).pause()).to.be.revertedWithCustomError(swap, 'OwnableUnauthorizedAccount', addr2.address);
await expect(swap.connect(addr2).unpause()).to.be.revertedWithCustomError(swap, 'OwnableUnauthorizedAccount', addr2.address);
});

it('unpause should work', async function () {
const { contracts: { swap } } = await loadFixture(deployContractsAndInit);
await swap.pause();
await swap.unpause();
expect(await swap.paused()).to.be.false;
});
});
});
Loading