Skip to content

Commit

Permalink
Trying to figure out MinPriorityQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJem committed Jan 26, 2024
1 parent 1c6206a commit 54269ee
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
66 changes: 34 additions & 32 deletions test/modules/auctions/LSBBA/MinPriorityQueue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ contract MinPriorityQueueTest is Test {
MinPriorityQueue.initialize(queue);
}

// Sorted by Bid.amountIn / Bid.minAmountOut

// [X] initial values
// [X] when a single bid is added
// [X] when a larger bid is added
Expand All @@ -36,7 +38,7 @@ contract MinPriorityQueueTest is Test {
}

function test_singleBid() public {
queue.insert(0, 1, 1);
queue.insert(0, 1, 1); // Price = 1/1

// get bid
QueueBid memory bid = queue.getBid(_INITIAL_INDEX);
Expand Down Expand Up @@ -66,28 +68,28 @@ contract MinPriorityQueueTest is Test {

function test_addLargerBid() public {
// Add the first bid
queue.insert(0, 1, 1);
queue.insert(0, 1, 1); // Price = 1/1

// Add a second bid that is larger
queue.insert(1, 2, 2);
queue.insert(1, 4, 2); // Price = 4/2 = 2

// get first sorted bid (bid id = 0)
// get first sorted bid (bid id = 1)
QueueBid memory bid = queue.getBid(_INITIAL_INDEX);
assertEq(bid.queueId, 1, "1: queueId mismatch");
assertEq(bid.bidId, 0);
assertEq(bid.amountIn, 1);
assertEq(bid.minAmountOut, 1);
assertEq(bid.queueId, 2, "1: queueId mismatch");
assertEq(bid.bidId, 1, "1: bidId mismatch");
assertEq(bid.amountIn, 4, "1: amountIn mismatch");
assertEq(bid.minAmountOut, 2, "1: minAmountOut mismatch");

// get second sorted bid (bid id = 1)
// get second sorted bid (bid id = 0)
bid = queue.getBid(_INITIAL_INDEX + 1);
assertEq(bid.queueId, 2, "2: queueId mismatch");
assertEq(bid.bidId, 1);
assertEq(bid.amountIn, 2);
assertEq(bid.minAmountOut, 2);
assertEq(bid.queueId, 1, "2: queueId mismatch");
assertEq(bid.bidId, 0, "2: bidId mismatch");
assertEq(bid.amountIn, 1, "2: amountIn mismatch");
assertEq(bid.minAmountOut, 1, "2: minAmountOut mismatch");

// get min bid (bid id = 0)
bid = queue.getMin();
assertEq(bid.queueId, 1);
assertEq(bid.queueId, 1, "min: queueId mismatch");
assertEq(bid.bidId, 0);
assertEq(bid.amountIn, 1);
assertEq(bid.minAmountOut, 1);
Expand All @@ -101,40 +103,40 @@ contract MinPriorityQueueTest is Test {
// queueIdList
assertEq(queue.queueIdList.length, 3);
assertEq(queue.queueIdList[0], 0);
assertEq(queue.queueIdList[1], 1);
assertEq(queue.queueIdList[2], 2);
assertEq(queue.queueIdList[1], 2);
assertEq(queue.queueIdList[2], 1);
}

function test_addSmallerBid() public {
// Add the first bid
queue.insert(0, 1, 1); // queueId = 1
queue.insert(0, 1, 1); // queueId = 1, price = 1/1

// Add a second bid that is larger
queue.insert(1, 2, 2); // queueId = 2
queue.insert(1, 4, 2); // queueId = 2, price = 4/2 = 2

// Add a third bid that is smaller than the second bid
queue.insert(2, 1, 2); // queueId = 3
queue.insert(2, 3, 2); // queueId = 3, price = 3/2 = 1.5

// get first sorted bid (bid id = 0)
// get first sorted bid (bid id = 1)
QueueBid memory bid = queue.getBid(_INITIAL_INDEX);
assertEq(bid.queueId, 1, "index 1: queueId mismatch");
assertEq(bid.bidId, 0, "index 1: bidId mismatch");
assertEq(bid.amountIn, 1, "index 1: amountIn mismatch");
assertEq(bid.minAmountOut, 1, "index 1: minAmountOut mismatch");
assertEq(bid.queueId, 2, "index 1: queueId mismatch");
assertEq(bid.bidId, 1, "index 1: bidId mismatch");
assertEq(bid.amountIn, 4, "index 1: amountIn mismatch");
assertEq(bid.minAmountOut, 2, "index 1: minAmountOut mismatch");

// get second sorted bid (bid id = 2)
bid = queue.getBid(_INITIAL_INDEX + 1);
assertEq(bid.queueId, 3, "index 2: queueId mismatch");
assertEq(bid.bidId, 2, "index 2: bidId mismatch");
assertEq(bid.amountIn, 1, "index 2: amountIn mismatch");
assertEq(bid.amountIn, 3, "index 2: amountIn mismatch");
assertEq(bid.minAmountOut, 2, "index 2: minAmountOut mismatch");

// get third sorted bid (bid id = 1)
// get third sorted bid (bid id = 0)
bid = queue.getBid(_INITIAL_INDEX + 2);
assertEq(bid.queueId, 2, "index 3: queueId mismatch");
assertEq(bid.bidId, 1, "index 3: bidId mismatch");
assertEq(bid.amountIn, 2, "index 3: amountIn mismatch");
assertEq(bid.minAmountOut, 2, "index 3: minAmountOut mismatch");
assertEq(bid.queueId, 1, "index 3: queueId mismatch");
assertEq(bid.bidId, 0, "index 3: bidId mismatch");
assertEq(bid.amountIn, 1, "index 3: amountIn mismatch");
assertEq(bid.minAmountOut, 1, "index 3: minAmountOut mismatch");

// get min bid (bid id = 0)
bid = queue.getMin();
Expand All @@ -152,8 +154,8 @@ contract MinPriorityQueueTest is Test {
// queueIdList
assertEq(queue.queueIdList.length, 4);
assertEq(queue.queueIdList[0], 0);
assertEq(queue.queueIdList[1], 1);
assertEq(queue.queueIdList[1], 2);
assertEq(queue.queueIdList[2], 3);
assertEq(queue.queueIdList[3], 2);
assertEq(queue.queueIdList[3], 1);
}
}
8 changes: 4 additions & 4 deletions test/modules/auctions/LSBBA/decryptAndSortBids.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ contract LSBBADecryptAndSortBidsTest is Test, Permit2User {
bytes32(0x1AFCC05BD15602738CBE9BD75B76403AB2C9409F2CC0C189B4551DEE8B576AD3)
);

// bidThree > bidOne > bidTwo
// bidTwo > bidOne > bidThree
uint256 internal bidSeed = 1e9;
uint96 internal bidOne;
uint256 internal bidOneAmount = 1e18;
uint256 internal bidOneAmountOut = 3e18;
uint256 internal bidOneAmountOut = 3e18; // Price = 1/3
LocalSealedBidBatchAuction.Decrypt internal decryptedBidOne;
uint96 internal bidTwo;
uint256 internal bidTwoAmount = 1e18;
uint256 internal bidTwoAmountOut = 2e18;
uint256 internal bidTwoAmountOut = 2e18; // Price = 1/2
LocalSealedBidBatchAuction.Decrypt internal decryptedBidTwo;
uint96 internal bidThree;
uint256 internal bidThreeAmount = 1e18;
uint256 internal bidThreeAmountOut = 7e18;
uint256 internal bidThreeAmountOut = 7e18; // Price = 1/7
LocalSealedBidBatchAuction.Decrypt internal decryptedBidThree;
LocalSealedBidBatchAuction.Decrypt[] internal decrypts;

Expand Down
7 changes: 4 additions & 3 deletions test/modules/auctions/LSBBA/settle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ contract LSBBASettleTest is Test, Permit2User {
bytes32(0x1AFCC05BD15602738CBE9BD75B76403AB2C9409F2CC0C189B4551DEE8B576AD3)
);

// TODO adjust these
uint256 internal bidSeed = 1e9;
uint96 internal bidOne;
uint256 internal bidOneAmount = 2e18;
uint256 internal bidOneAmountOut = 2e18;
uint256 internal bidOneAmountOut = 2e18; // Price = 1
LocalSealedBidBatchAuction.Decrypt internal decryptedBidOne;
uint96 internal bidTwo;
uint256 internal bidTwoAmount = 3e18;
uint256 internal bidTwoAmountOut = 3e18;
uint256 internal bidTwoAmountOut = 3e18; // Price = 1
LocalSealedBidBatchAuction.Decrypt internal decryptedBidTwo;
uint96 internal bidThree;
uint256 internal bidThreeAmount = 7e18;
uint256 internal bidThreeAmountOut = 7e18;
uint256 internal bidThreeAmountOut = 7e18; // Price = 1
LocalSealedBidBatchAuction.Decrypt internal decryptedBidThree;
uint96 internal bidFour;
uint256 internal bidFourAmount = 2e18;
Expand Down

0 comments on commit 54269ee

Please sign in to comment.