Skip to content

Commit

Permalink
add OrderRegistrator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ZumZoom committed Jun 25, 2024
1 parent 92115b7 commit 6cde1ec
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contracts/helpers/OrderRegistrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract OrderRegistrator is IOrderRegistrator {
}

// Validate signature
if(!ECDSA.recoverOrIsValidSignature(order.maker.get(), _LIMIT_ORDER_PROTOCOL.hashOrder(order), signature)) revert BadSignature();
if(!ECDSA.recoverOrIsValidSignature(order.maker.get(), _LIMIT_ORDER_PROTOCOL.hashOrder(order), signature)) revert IOrderMixin.BadSignature();

emit OrderRegistered(order, extension, signature);
}
Expand Down
2 changes: 0 additions & 2 deletions contracts/interfaces/IOrderRegistrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ pragma solidity 0.8.23;
import { IOrderMixin } from "./IOrderMixin.sol";

interface IOrderRegistrator {
error BadSignature();

event OrderRegistered(IOrderMixin.Order order, bytes extension, bytes signature);

function registerOrder(IOrderMixin.Order calldata order, bytes calldata extension, bytes calldata signature) external;
Expand Down
76 changes: 76 additions & 0 deletions test/OrderRegistrator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { expect } = require('@1inch/solidity-utils');
const { signOrder, buildOrder } = require('./helpers/orderUtils');
const { ethers } = require('hardhat');
const { deploySwap, deployUSDC, deployUSDT } = require('./helpers/fixtures');

describe('OrderRegistrator', function () {
let addr;

before(async function () {
[addr] = await ethers.getSigners();
});

async function deployAndInit () {
const { swap } = await deploySwap();
const { usdc } = await deployUSDC();
const { usdt } = await deployUSDT();
const OrderRegistrator = await ethers.getContractFactory('OrderRegistrator');
const registrator = await OrderRegistrator.deploy(swap);
await registrator.waitForDeployment();
const chainId = (await ethers.provider.getNetwork()).chainId;
return { swap, usdc, usdt, registrator, chainId };
};

it('should emit OrderRegistered event', async function () {
const { usdc, usdt, swap, registrator, chainId } = await loadFixture(deployAndInit);

const order = buildOrder({
makerAsset: await usdc.getAddress(),
takerAsset: await usdt.getAddress(),
makingAmount: 1,
takingAmount: 2,
maker: addr.address,
});

const orderTuple = [order.salt, order.maker, order.receiver, order.makerAsset, order.takerAsset, order.makingAmount, order.takingAmount, order.makerTraits];

const signature = ethers.Signature.from(await signOrder(order, chainId, await swap.getAddress(), addr)).compactSerialized;

const tx = registrator.registerOrder(order, order.extension, signature);
await expect(tx).to.emit(registrator, 'OrderRegistered').withArgs(orderTuple, order.extension, signature);
});

it('should revert with wrong signature', async function () {
const { usdc, usdt, swap, registrator, chainId } = await loadFixture(deployAndInit);

const order = buildOrder({
makerAsset: await usdc.getAddress(),
takerAsset: await usdt.getAddress(),
makingAmount: 1,
takingAmount: 2,
maker: addr.address,
});
const signature = ethers.Signature.from(await signOrder(order, chainId + 1n, await swap.getAddress(), addr)).compactSerialized;

const tx = registrator.registerOrder(order, order.extension, signature);
await expect(tx).to.be.revertedWithCustomError(swap, 'BadSignature');
});

it('should revert with wrong extension', async function () {
const { usdc, usdt, swap, registrator, chainId } = await loadFixture(deployAndInit);

const order = buildOrder({
makerAsset: await usdc.getAddress(),
takerAsset: await usdt.getAddress(),
makingAmount: 1,
takingAmount: 2,
maker: addr.address,
});
const orderLibFactory = await ethers.getContractFactory('OrderLib');

const signature = ethers.Signature.from(await signOrder(order, chainId, await swap.getAddress(), addr)).compactSerialized;
const tx = registrator.registerOrder(order, order.extension + '00', signature);
await expect(tx).to.be.revertedWithCustomError(orderLibFactory, 'UnexpectedOrderExtension');
});
});

0 comments on commit 6cde1ec

Please sign in to comment.