Skip to content

Commit

Permalink
More variable renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSamWitch committed Feb 3, 2024
1 parent d2b56d4 commit 3182e5d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
60 changes: 30 additions & 30 deletions contracts/SamWitchOrderBook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ 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
mapping(uint tokenId => mapping(uint price => bytes32[] segments)) private asksAtPrice;
// 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() {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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_);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down
16 changes: 8 additions & 8 deletions contracts/interfaces/ISamWitchOrderBook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ISamWitchOrderBook is IERC1155Receiver {
Sell
}

struct TokenInfo {
struct TokenIdInfo {
uint128 tick;
uint128 minQuantity;
}
Expand All @@ -28,9 +28,9 @@ interface ISamWitchOrderBook is IERC1155Receiver {
uint72 price;
}

struct OrderInfo {
struct ClaimableTokenInfo {
address maker;
uint80 claimable;
uint80 amount;
}

struct Order {
Expand All @@ -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();
Expand All @@ -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;

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions scripts/deployTestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -43,7 +43,7 @@ async function main() {
],
);
await tx.wait();
console.log("orderBook.setTokenInfos");
console.log("orderBook.setTokenIdInfos");

tx = await orderBook.limitOrders([
{
Expand Down
23 changes: 13 additions & 10 deletions test/SamWitchOrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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 () {
Expand Down Expand Up @@ -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([
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 3182e5d

Please sign in to comment.