Skip to content

Commit

Permalink
Add cancelAndMakeLimitOrders which allows editing orders but also bat…
Browse files Browse the repository at this point in the history
…ching up cancels + adds into 1 transaction
  • Loading branch information
0xSamWitch committed Feb 14, 2024
1 parent ce32261 commit 229b9bd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
17 changes: 15 additions & 2 deletions contracts/SamWitchOrderBook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable

/// @notice Place multiple limit orders in the order book
/// @param _orders Array of limit orders to be placed
function limitOrders(LimitOrder[] calldata _orders) external override {
function limitOrders(LimitOrder[] calldata _orders) public override {
uint royalty;
uint dev;
uint burn;
Expand Down Expand Up @@ -209,7 +209,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable
/// @notice Cancel multiple orders in the order book
/// @param _orderIds Array of order IDs to be cancelled
/// @param _orders Information about the orders so that they can be found in the order book
function cancelOrders(uint[] calldata _orderIds, CancelOrder[] calldata _orders) external override {
function cancelOrders(uint[] calldata _orderIds, CancelOrder[] calldata _orders) public override {
if (_orderIds.length != _orders.length) {
revert LengthMismatch();
}
Expand Down Expand Up @@ -256,6 +256,19 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable
}
}

/// @notice Cancel multiple orders and place multiple limit orders in the order book. Can be used to replace orders
/// @param _orderIds Array of order IDs to be cancelled
/// @param _orders Information about the orders so that they can be found in the order book
/// @param _newOrders Array of limit orders to be placed
function cancelAndMakeLimitOrders(
uint[] calldata _orderIds,
CancelOrder[] calldata _orders,
LimitOrder[] calldata _newOrders
) external override {
cancelOrders(_orderIds, _orders);
limitOrders(_newOrders);
}

/// @notice Claim tokens associated with filled or partially filled orders.
/// Must be the maker of these orders.
/// @param _orderIds Array of order IDs from which to claim NFTs
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/ISamWitchOrderBook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ interface ISamWitchOrderBook is IERC1155Receiver {

function cancelOrders(uint[] calldata orderIds, CancelOrder[] calldata cancelClaimableTokenInfos) external;

function cancelAndMakeLimitOrders(
uint[] calldata orderIds,
CancelOrder[] calldata orders,
LimitOrder[] calldata newOrders
) external;

function claimTokens(uint[] calldata _orderIds) external;

function claimNFTs(uint[] calldata orderIds, uint[] calldata tokenIds) external;
Expand Down
38 changes: 37 additions & 1 deletion test/SamWitchOrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2216,7 +2216,7 @@ describe("SamWitchOrderBook", function () {

it("Claiming no tokens, empty order id array argument", async function () {
const {orderBook} = await loadFixture(deployContractsFixture);
await expect(orderBook.claimTokens([1])).to.be.revertedWithCustomError(orderBook, "NothingToClaim");
await expect(orderBook.claimTokens([])).to.be.revertedWithCustomError(orderBook, "NothingToClaim");
});

it("Claim NFTs", async function () {
Expand Down Expand Up @@ -2465,6 +2465,42 @@ describe("SamWitchOrderBook", function () {
});
});

describe("Edit orders", function () {
it("Cancels and makes a new order", async function () {
const {orderBook, tokenId, tick, owner} = await loadFixture(deployContractsFixture);

// Set up order book
const price = 100;
const quantity = 100;
await orderBook.limitOrders([
{
side: OrderSide.Buy,
tokenId,
price,
quantity,
},
]);

const newOrder = {side: OrderSide.Buy, tokenId, price: price + 1 * tick, quantity: quantity + 2};
console.log(newOrder);
await orderBook.cancelAndMakeLimitOrders([1], [{side: OrderSide.Buy, tokenId, price}], [newOrder]);

const nextOrderIdSlot = 2;
let packedSlot = await ethers.provider.getStorage(orderBook, nextOrderIdSlot);
let nextOrderId = parseInt(packedSlot.slice(2, 12), 16);
expect(nextOrderId).to.eq(3);

expect((await orderBook.allOrdersAtPrice(OrderSide.Buy, tokenId, price)).length).to.eq(0);
expect((await orderBook.allOrdersAtPrice(OrderSide.Buy, tokenId, price + 1 * tick)).length).to.eq(1);
const orderId = 2;
expect((await orderBook.allOrdersAtPrice(OrderSide.Buy, tokenId, price + 1 * tick))[0]).to.deep.eq([
owner.address,
quantity + 2,
orderId,
]);
});
});

it("Max brush price", async function () {
const {orderBook, tokenId} = await loadFixture(deployContractsFixture);

Expand Down

0 comments on commit 229b9bd

Please sign in to comment.