From 3182e5ddf532c1e082e0fd1284d54e955419e703 Mon Sep 17 00:00:00 2001 From: 0xSamWitch Date: Sat, 3 Feb 2024 01:07:32 +0000 Subject: [PATCH] More variable renaming --- contracts/SamWitchOrderBook.sol | 60 ++++++++++----------- contracts/interfaces/ISamWitchOrderBook.sol | 16 +++--- scripts/deployTestData.ts | 4 +- test/SamWitchOrderBook.ts | 23 ++++---- 4 files changed, 53 insertions(+), 50 deletions(-) diff --git a/contracts/SamWitchOrderBook.sol b/contracts/SamWitchOrderBook.sol index 5cb5c00..0aec59f 100644 --- a/contracts/SamWitchOrderBook.sol +++ b/contracts/SamWitchOrderBook.sol @@ -54,7 +54,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable address private royaltyRecipient; // mappings - mapping(uint tokenId => TokenInfo tokenInfo) private tokenInfoById; + mapping(uint tokenId => TokenIdInfo tokenIdInfo) private tokenIdInfo; mapping(uint tokenId => BokkyPooBahsRedBlackTreeLibrary.Tree) private asks; mapping(uint tokenId => BokkyPooBahsRedBlackTreeLibrary.Tree) private bids; // token id => price => ask(quantity (uint24), id (uint40)) x 4 @@ -62,9 +62,9 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable // token id => price => bid(quantity (uint24), id (uint40)) x 4 mapping(uint tokenId => mapping(uint price => bytes32[] segments)) private bidsAtPrice; // token id => order id => amount claimable, 3 per slot - mapping(uint tokenId => uint80[MAX_ORDER_ID]) private amountClaimableForToken; - // order id => (maker, claimable) - OrderInfo[MAX_ORDER_ID] private orderInfoById; + mapping(uint tokenId => uint80[MAX_ORDER_ID]) private amountClaimableForTokenId; + // order id => (maker, amount claimable) + ClaimableTokenInfo[MAX_ORDER_ID] private tokenClaimable; /// @custom:oz-upgrades-unsafe-allow constructor constructor() { @@ -261,17 +261,17 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable uint amount; for (uint i = 0; i < _orderIds.length; ++i) { uint40 orderId = uint40(_orderIds[i]); - OrderInfo storage orderInfo = orderInfoById[orderId]; - uint80 claimableAmount = orderInfo.claimable; + ClaimableTokenInfo storage claimableTokenInfo = tokenClaimable[orderId]; + uint80 claimableAmount = claimableTokenInfo.amount; if (claimableAmount == 0) { revert NothingToClaim(); } - if (orderInfo.maker != _msgSender()) { + if (claimableTokenInfo.maker != _msgSender()) { revert NotMaker(); } - orderInfo.claimable = 0; + claimableTokenInfo.amount = 0; amount += claimableAmount; } @@ -294,12 +294,12 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable for (uint i = 0; i < _tokenIds.length; ++i) { uint40 orderId = uint40(_orderIds[i]); uint tokenId = _tokenIds[i]; - uint amount = amountClaimableForToken[tokenId][orderId]; + uint amount = amountClaimableForTokenId[tokenId][orderId]; if (amount == 0) { revert NothingToClaim(); } nftAmountsFromUs[i] = amount; - amountClaimableForToken[tokenId][orderId] = 0; + amountClaimableForTokenId[tokenId][orderId] = 0; } _safeBatchTransferNFTsFromUs(_msgSender(), _tokenIds, nftAmountsFromUs); @@ -321,14 +321,14 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable } /// @notice Get the amount of tokens claimable for these orders - /// @param _orderIds The order IDs to get the claimable tokens for + /// @param _orderIds The order IDs of which to find the claimable tokens for /// @param _takeAwayFees Whether to take away the fees from the claimable amount function tokensClaimable( uint40[] calldata _orderIds, bool _takeAwayFees ) external view override returns (uint amount_) { for (uint i = 0; i < _orderIds.length; ++i) { - amount_ += orderInfoById[_orderIds[i]].claimable; + amount_ += tokenClaimable[_orderIds[i]].amount; } if (_takeAwayFees) { (uint royalty, uint dev, uint burn) = _calcFees(amount_); @@ -345,18 +345,18 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable ) external view override returns (uint[] memory amounts_) { amounts_ = new uint[](_orderIds.length); for (uint i = 0; i < _orderIds.length; ++i) { - amounts_[i] = amountClaimableForToken[_tokenIds[i]][_orderIds[i]]; + amounts_[i] = amountClaimableForTokenId[_tokenIds[i]][_orderIds[i]]; } } /// @notice Get the token ID info for a specific token ID /// @param _tokenId The token ID to get the info for - function getTokenInfo(uint _tokenId) external view override returns (TokenInfo memory) { - return tokenInfoById[_tokenId]; + function getTokenIdInfo(uint _tokenId) external view override returns (TokenIdInfo memory) { + return tokenIdInfo[_tokenId]; } - function getOrderInfo(uint40 _orderId) external view override returns (OrderInfo memory) { - return orderInfoById[_orderId]; + function getClaimableTokenInfo(uint40 _orderId) external view override returns (ClaimableTokenInfo memory) { + return tokenClaimable[_orderId]; } /// @notice Get the highest bid for a specific token ID @@ -440,17 +440,17 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable /// @notice Set constraints like minimum quantity of an order that is allowed to be /// placed and the minimum of specific tokenIds in this nft collection. /// @param _tokenIds Array of token IDs for which to set TokenInfo - /// @param _tokenInfos Array of TokenInfo to be set - function setTokenInfos(uint[] calldata _tokenIds, TokenInfo[] calldata _tokenInfos) external payable onlyOwner { - if (_tokenIds.length != _tokenInfos.length) { + /// @param _tokenIdInfos Array of TokenInfo to be set + function setTokenIdInfos(uint[] calldata _tokenIds, TokenIdInfo[] calldata _tokenIdInfos) external payable onlyOwner { + if (_tokenIds.length != _tokenIdInfos.length) { revert LengthMismatch(); } for (uint i = 0; i < _tokenIds.length; ++i) { - tokenInfoById[_tokenIds[i]] = _tokenInfos[i]; + tokenIdInfo[_tokenIds[i]] = _tokenIdInfos[i]; } - emit SetTokenInfos(_tokenIds, _tokenInfos); + emit SetTokenInfos(_tokenIds, _tokenIdInfos); } /// @notice Set the fees for the contract @@ -549,9 +549,9 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable cost_ += quantityNFTClaimable * bestPrice; if (isTakingFromBuy) { - amountClaimableForToken[_tokenId][orderId] += uint80(quantityNFTClaimable); + amountClaimableForTokenId[_tokenId][orderId] += uint80(quantityNFTClaimable); } else { - orderInfoById[orderId].claimable += uint80(quantityNFTClaimable * bestPrice); + tokenClaimable[orderId].amount += uint80(quantityNFTClaimable * bestPrice); } _orderIdsPool[numberOfOrders] = orderId; @@ -685,7 +685,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable uint40 id = uint40(segment >> offset.mul(64)); if (id != 0) { uint24 quantity = uint24(segment >> offset.mul(64).add(40)); - orders_[numberOfEntries] = Order(orderInfoById[id].maker, quantity, id); + orders_[numberOfEntries] = Order(tokenClaimable[id].maker, quantity, id); numberOfEntries = numberOfEntries.inc(); } } @@ -731,8 +731,8 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable revert PriceZero(); } - TokenInfo storage tokenInfo = tokenInfoById[_limitOrder.tokenId]; - uint128 tick = tokenInfo.tick; + TokenIdInfo storage tokenIdInfo = tokenIdInfo[_limitOrder.tokenId]; + uint128 tick = tokenIdInfo.tick; if (tick == 0) { revert TokenDoesntExist(_limitOrder.tokenId); @@ -753,7 +753,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable ); // Add the rest to the order book if has the minimum required, in order to keep order books healthy - if (quantityRemaining >= tokenInfo.minQuantity) { + if (quantityRemaining >= tokenIdInfo.minQuantity) { quantityAddedToBook_ = quantityRemaining; _addToBook(_newOrderId, tick, _limitOrder.side, _limitOrder.tokenId, _limitOrder.price, quantityAddedToBook_); } else if (quantityRemaining != 0) { @@ -840,7 +840,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable uint72 _price, uint24 _quantity ) private { - orderInfoById[_newOrderId] = OrderInfo(_msgSender(), 0); + tokenClaimable[_newOrderId] = ClaimableTokenInfo(_msgSender(), 0); uint72 price; // Price can update if the price level is at capacity if (_side == OrderSide.Buy) { @@ -914,7 +914,7 @@ contract SamWitchOrderBook is ISamWitchOrderBook, ERC1155Holder, UUPSUpgradeable bytes32 segment = _segments[_index]; uint40 orderId = uint40(uint(segment) >> _offset.mul(64)); - if (orderInfoById[orderId].maker != _msgSender()) { + if (tokenClaimable[orderId].maker != _msgSender()) { revert NotMaker(); } diff --git a/contracts/interfaces/ISamWitchOrderBook.sol b/contracts/interfaces/ISamWitchOrderBook.sol index 34ea2dd..8743225 100644 --- a/contracts/interfaces/ISamWitchOrderBook.sol +++ b/contracts/interfaces/ISamWitchOrderBook.sol @@ -10,7 +10,7 @@ interface ISamWitchOrderBook is IERC1155Receiver { Sell } - struct TokenInfo { + struct TokenIdInfo { uint128 tick; uint128 minQuantity; } @@ -28,9 +28,9 @@ interface ISamWitchOrderBook is IERC1155Receiver { uint72 price; } - struct OrderInfo { + struct ClaimableTokenInfo { address maker; - uint80 claimable; + uint80 amount; } struct Order { @@ -45,8 +45,8 @@ interface ISamWitchOrderBook is IERC1155Receiver { event FailedToAddToBook(address maker, OrderSide side, uint tokenId, uint price, uint quantity); event ClaimedTokens(address user, uint[] orderIds, uint amount, uint fees); event ClaimedNFTs(address user, uint[] orderIds, uint[] tokenIds, uint[] amounts); - event SetTokenInfos(uint[] tokenIds, TokenInfo[] tokenInfos); - event SetMaxOrdersPerPriceLevel(uint maxOrdersPerPrice); + event SetTokenInfos(uint[] tokenIds, TokenIdInfo[] tokenInfos); + event SetMaxOrdersPerPriceLevel(uint maxOrdesrsPerPrice); event SetFees(address devAddr, uint devFee, uint burntFee); error ZeroAddress(); @@ -71,7 +71,7 @@ interface ISamWitchOrderBook is IERC1155Receiver { function limitOrders(LimitOrder[] calldata orders) external; - function cancelOrders(uint[] calldata orderIds, CancelOrder[] calldata cancelOrderInfos) external; + function cancelOrders(uint[] calldata orderIds, CancelOrder[] calldata cancelClaimableTokenInfos) external; function claimTokens(uint[] calldata _orderIds) external; @@ -98,9 +98,9 @@ interface ISamWitchOrderBook is IERC1155Receiver { function nodeExists(OrderSide side, uint tokenId, uint72 price) external view returns (bool); - function getTokenInfo(uint tokenId) external view returns (TokenInfo memory); + function getTokenIdInfo(uint tokenId) external view returns (TokenIdInfo memory); - function getOrderInfo(uint40 _orderId) external view returns (OrderInfo memory); + function getClaimableTokenInfo(uint40 _orderId) external view returns (ClaimableTokenInfo memory); function allOrdersAtPrice( OrderSide side, diff --git a/scripts/deployTestData.ts b/scripts/deployTestData.ts index 633d2e7..f7332ae 100644 --- a/scripts/deployTestData.ts +++ b/scripts/deployTestData.ts @@ -34,7 +34,7 @@ async function main() { await tx.wait(); console.log("erc1155.setApprovalForAll"); - tx = await orderBook.setTokenInfos( + tx = await orderBook.setTokenIdInfos( [tokenId, tokenId + 1, tokenId + 2], [ {tick, minQuantity: 3}, @@ -43,7 +43,7 @@ async function main() { ], ); await tx.wait(); - console.log("orderBook.setTokenInfos"); + console.log("orderBook.setTokenIdInfos"); tx = await orderBook.limitOrders([ { diff --git a/test/SamWitchOrderBook.ts b/test/SamWitchOrderBook.ts index 11cd594..34363c5 100644 --- a/test/SamWitchOrderBook.ts +++ b/test/SamWitchOrderBook.ts @@ -42,7 +42,7 @@ describe("SamWitchOrderBook", function () { const tick = 1; const minQuantity = 1; - await orderBook.setTokenInfos([tokenId], [{tick, minQuantity}]); + await orderBook.setTokenIdInfos([tokenId], [{tick, minQuantity}]); return { orderBook, @@ -143,7 +143,7 @@ describe("SamWitchOrderBook", function () { it("Get token id info", async function () { const {orderBook, tokenId} = await loadFixture(deployContractsFixture); - const info = await orderBook.getTokenInfo(tokenId); + const info = await orderBook.getTokenIdInfo(tokenId); expect(info.tick).to.equal(1); expect(info.minQuantity).to.equal(1); }); @@ -169,9 +169,9 @@ describe("SamWitchOrderBook", function () { ]; let orderId = 1; await orderBook.limitOrders(limitOrders); - let order = await orderBook.getOrderInfo(orderId); + let order = await orderBook.getClaimableTokenInfo(orderId); expect(order.maker).to.equal(await owner.getAddress()); - expect(order.claimable).to.equal(0); + expect(order.amount).to.equal(0); }); it("Add to order book", async function () { @@ -464,7 +464,7 @@ describe("SamWitchOrderBook", function () { quantity: quantity - 3, }, ]); - await orderBook.setTokenInfos([tokenId], [{tick: 1, minQuantity: 20}]); + await orderBook.setTokenIdInfos([tokenId], [{tick: 1, minQuantity: 20}]); await expect( orderBook.limitOrders([ { @@ -1463,7 +1463,7 @@ describe("SamWitchOrderBook", function () { it("Price must be modulus of tick quantity must be > min quantity, sell", async function () { const {orderBook, erc1155, tokenId} = await loadFixture(deployContractsFixture); - await orderBook.setTokenInfos([tokenId], [{tick: 10, minQuantity: 20}]); + await orderBook.setTokenIdInfos([tokenId], [{tick: 10, minQuantity: 20}]); let price = 101; let quantity = 20; @@ -1510,7 +1510,7 @@ describe("SamWitchOrderBook", function () { it("Price must be modulus of tick quantity must be > min quantity, buy", async function () { const {orderBook, brush, tokenId} = await loadFixture(deployContractsFixture); - await orderBook.setTokenInfos([tokenId], [{tick: 10, minQuantity: 20}]); + await orderBook.setTokenIdInfos([tokenId], [{tick: 10, minQuantity: 20}]); let price = 101; let quantity = 20; @@ -1558,16 +1558,19 @@ describe("SamWitchOrderBook", function () { const {orderBook, tokenId, alice} = await loadFixture(deployContractsFixture); await expect( - orderBook.connect(alice).setTokenInfos([tokenId], [{tick: 10, minQuantity: 20}]), + orderBook.connect(alice).setTokenIdInfos([tokenId], [{tick: 10, minQuantity: 20}]), ).to.be.revertedWithCustomError(orderBook, "OwnableUnauthorizedAccount"); - await expect(orderBook.setTokenInfos([tokenId], [{tick: 10, minQuantity: 20}])).to.emit(orderBook, "SetTokenInfos"); + await expect(orderBook.setTokenIdInfos([tokenId], [{tick: 10, minQuantity: 20}])).to.emit( + orderBook, + "SetTokenInfos", + ); }); it("Set tokenId infos argument length mismatch", async function () { const {orderBook, tokenId} = await loadFixture(deployContractsFixture); - await expect(orderBook.setTokenInfos([tokenId], [])).to.be.revertedWithCustomError(orderBook, "LengthMismatch"); + await expect(orderBook.setTokenIdInfos([tokenId], [])).to.be.revertedWithCustomError(orderBook, "LengthMismatch"); }); it("Set max orders per price can only be called by the owner", async function () {