Skip to content

Commit

Permalink
feat, test: round gas adjustments in favor of swapper
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhwu committed Oct 1, 2024
1 parent b0cd19a commit 8dce531
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/reactors/V3DutchOrderReactor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,19 @@ contract V3DutchOrderReactor is BaseReactor {
// positive means an increase in gas
int256 gasDeltaGwei = block.basefee.sub(order.startingBaseFee);

// Gas increase should increase input
int256 inputDelta = int256(order.baseInput.adjustmentPerGweiBaseFee) * gasDeltaGwei / 1 gwei;
// Gas increase should increase input (round up when positive, down when negative)
int256 inputDelta = gasDeltaGwei >= 0
? (int256(order.baseInput.adjustmentPerGweiBaseFee) * gasDeltaGwei + 1 gwei - 1) / 1 gwei
: (int256(order.baseInput.adjustmentPerGweiBaseFee) * gasDeltaGwei) / 1 gwei;
order.baseInput.startAmount = order.baseInput.startAmount.boundedAdd(inputDelta, 0, order.baseInput.maxAmount);

// Gas increase should decrease output
// Gas increase should decrease output (round down when positive, up when negative)
uint256 outputsLength = order.baseOutputs.length;
for (uint256 i = 0; i < outputsLength; i++) {
V3DutchOutput memory output = order.baseOutputs[i];
int256 outputDelta = int256(output.adjustmentPerGweiBaseFee) * gasDeltaGwei / 1 gwei;
int256 outputDelta = gasDeltaGwei >= 0
? (int256(output.adjustmentPerGweiBaseFee) * gasDeltaGwei) / 1 gwei
: (int256(output.adjustmentPerGweiBaseFee) * gasDeltaGwei - 1 gwei + 1) / 1 gwei;
output.startAmount = output.startAmount.boundedSub(outputDelta, output.minAmount, type(uint256).max);
}
}
Expand Down
48 changes: 48 additions & 0 deletions test/reactors/V3DutchOrderReactor.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,54 @@ contract V3DutchOrderTest is PermitSignature, DeployPermit2, BaseReactorTest {
assertEq(resolvedOrder.input.amount, 0);
}

function testV3GasAdjustmentRounding() public {
uint256 currentBlock = 21212121;
vm.roll(currentBlock);
vm.fee(1 gwei);

// Order with 0.8 gwei gas adjustments
SignedOrder memory order = generateOrder(
TestDutchOrderSpec({
currentBlock: currentBlock,
startBlock: currentBlock,
deadline: currentBlock + 21,
input: V3DutchInput(tokenIn, 100 ether, CurveBuilder.emptyCurve(), 101 ether, 0.8 gwei),
outputs: OutputsBuilder.singleV3Dutch(
V3DutchOutput(address(tokenOut), 100 ether, CurveBuilder.emptyCurve(), address(0), 99 ether, 0.8 gwei)
)
})
);

// Test gas increase
vm.fee(2 gwei);
ResolvedOrder memory resolvedOrder = quoter.quote(order.order, order.sig);
assertEq(resolvedOrder.input.amount, 100 ether + 0.8 gwei, "Input should round up for positive gas change");
assertEq(resolvedOrder.outputs[0].amount, 100 ether - 0.8 gwei, "Output should round down for positive gas change");

// Test gas decrease
vm.fee(0.5 gwei);
resolvedOrder = quoter.quote(order.order, order.sig);
assertEq(resolvedOrder.input.amount, 100 ether - 0.4 gwei, "Input should round down for negative gas change");
assertEq(resolvedOrder.outputs[0].amount, 100 ether + 0.4 gwei, "Output should round up for negative gas change");

// Test gas half
vm.fee(0.5 gwei);
resolvedOrder = quoter.quote(order.order, order.sig);
assertEq(resolvedOrder.input.amount, 100 ether - 0.4 gwei, "Input should round down for gas halving change");
assertEq(resolvedOrder.outputs[0].amount, 100 ether + 0.4 gwei, "Output should round up for exact gas halving change");

// Test smaller gas changes
vm.fee(1.1 gwei);
resolvedOrder = quoter.quote(order.order, order.sig);
assertEq(resolvedOrder.input.amount, 100 ether + 0.08 gwei, "Input should handle small positive gas changes");
assertEq(resolvedOrder.outputs[0].amount, 100 ether - 0.08 gwei, "Output should handle small positive gas changes");

vm.fee(0.9 gwei);
resolvedOrder = quoter.quote(order.order, order.sig);
assertEq(resolvedOrder.input.amount, 100 ether - 0.08 gwei, "Input should handle small negative gas changes");
assertEq(resolvedOrder.outputs[0].amount, 100 ether + 0.08 gwei, "Output should handle small negative gas changes");
}

/* Test helpers */

struct TestDutchOrderSpec {
Expand Down

0 comments on commit 8dce531

Please sign in to comment.