Skip to content

Commit

Permalink
Retry failed order creation on next block (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedgiac authored Feb 13, 2024
1 parent 9a15237 commit 048c78e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/ConstantProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,6 @@ contract ConstantProduct is IConditionalOrderGenerator {
* bucket.
*/
function revertPollAtNextBucket(string memory message) internal view {
revert IWatchtowerCustomErrors.PollTryAtEpoch(Utils.validToBucket(MAX_ORDER_DURATION) + 1, message);
revert IWatchtowerCustomErrors.PollTryAtBlock(block.number + 1, message);
}
}
4 changes: 2 additions & 2 deletions src/interfaces/IWatchtowerCustomErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pragma solidity >=0.8.0 <0.9.0;
contract IWatchtowerCustomErrors {
/**
* No order is currently available for trading, but the watchtower should
* try again at the specified timestamp.
* try again at the specified block.
*/
error PollTryAtEpoch(uint256 timestamp, string message);
error PollTryAtBlock(uint256 blockNumber, string message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
setUpDefaultReserves(orderOwner);
setUpDefaultReferencePairReserves(42, 1337);

uint256 nextBucket = moveTimeToMidFutureBucket();
uint256 currentBlock = 1337;
vm.roll(currentBlock);

GPv2Order.Data memory order = getTradeableOrderWrapper(orderOwner, defaultData);
require(order.sellToken == defaultData.token0, "test was design for token0 to be the sell token");
Expand All @@ -54,7 +55,7 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
defaultData.minTradedToken0 = order.sellAmount + 1;
vm.expectRevert(
abi.encodeWithSelector(
IWatchtowerCustomErrors.PollTryAtEpoch.selector, nextBucket + 1, "traded amount too small"
IWatchtowerCustomErrors.PollTryAtBlock.selector, currentBlock + 1, "traded amount too small"
)
);
getTradeableOrderUncheckedWrapper(orderOwner, defaultData);
Expand All @@ -65,7 +66,8 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
setUpDefaultReserves(orderOwner);
setUpDefaultReferencePairReserves(1337, 42);

uint256 nextBucket = moveTimeToMidFutureBucket();
uint256 currentBlock = 1337;
vm.roll(currentBlock);

GPv2Order.Data memory order = getTradeableOrderWrapper(orderOwner, defaultData);
require(order.buyToken == defaultData.token0, "test was design for token0 to be the buy token");
Expand All @@ -74,7 +76,7 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
defaultData.minTradedToken0 = order.buyAmount + 1;
vm.expectRevert(
abi.encodeWithSelector(
IWatchtowerCustomErrors.PollTryAtEpoch.selector, nextBucket + 1, "traded amount too small"
IWatchtowerCustomErrors.PollTryAtBlock.selector, currentBlock + 1, "traded amount too small"
)
);
getTradeableOrderUncheckedWrapper(orderOwner, defaultData);
Expand All @@ -87,25 +89,14 @@ abstract contract ValidateOrderParametersTest is ConstantProductTestHarness {
setUpDefaultWithReserves(orderOwner, selfReserve0, selfReserve1);
setUpDefaultReferencePairReserves(uniswapReserve0, uniswapReserve1);

uint256 nextBucket = moveTimeToMidFutureBucket();
uint256 currentBlock = 1337;
vm.roll(currentBlock);

vm.expectRevert(
abi.encodeWithSelector(
IWatchtowerCustomErrors.PollTryAtEpoch.selector, nextBucket + 1, "subtraction underflow"
IWatchtowerCustomErrors.PollTryAtBlock.selector, currentBlock + 1, "subtraction underflow"
)
);
getTradeableOrderUncheckedWrapper(orderOwner, defaultData);
}

function moveTimeToMidFutureBucket() internal returns (uint256 nextBucketStart) {
uint256 smallOffset = 42;
require(smallOffset < constantProduct.MAX_ORDER_DURATION());
uint256 nextTimestamp = 1337 * constantProduct.MAX_ORDER_DURATION() + smallOffset;
nextBucketStart = 1338 * constantProduct.MAX_ORDER_DURATION();
require(
nextTimestamp % constantProduct.MAX_ORDER_DURATION() != 0,
"test was designed so that the timestamp doesn't fall exactly at the start of a bucket, please change the offset"
);
vm.warp(nextTimestamp);
}
}

0 comments on commit 048c78e

Please sign in to comment.