Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 219 additions & 91 deletions .gas-snapshot

Large diffs are not rendered by default.

268 changes: 268 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
# Testing Status Report for SwapVM Instructions

## Invariant Tests with TODO Comments

### 1. **ConcentrateXYCDecayFeesInvariants.t.sol**

#### Test: `test_Order1_GrowPriceRange2D`
- Instructions used: `Balances`, `Decay`, `Fee` (progressive), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_Order1_GrowPriceRangeXD`
- Instructions used: `Balances`, `Decay`, `Fee` (flat + progressive), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_Order2_GrowPriceRange2D`
- Instructions used: `Balances`, `Fee` (progressive), `Decay`, `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_Order3_GrowPriceRangeXD`
- Instructions used: `Balances`, `Decay`, `Concentrate` (GrowPriceRange), `Fee` (progressive), `XYC`
- TODO: "why it didn't fail?"

#### Test: `test_Order4_GrowPriceRange2D`
- Instructions used: `Balances`, `Fee` (flat + progressive), `Concentrate` (GrowPriceRange), `Decay`, `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_Order5_GrowPriceRangeXD`
- Instructions used: `Balances`, `Concentrate` (GrowPriceRange), `Decay`, `Fee` (progressive), `XYC`
- TODO: "why it didn't fail?"

#### Test: `test_Order6_GrowPriceRange2D`
- Instructions used: `Balances`, `Concentrate` (GrowPriceRange), `Fee` (protocol + progressive), `Decay`, `XYC`
- TODO: "why it didn't fail?"

#### All tests in this file
- TODO: "need to research behavior" (in `_testInvariantsWithTolerance` method)
- Always skips additivity test

### 2. **DecayXYCFeesInvariants.t.sol**

#### Test: `test_DecayXYCFlatFeeOut`
- Instructions used: `Balances`, `Fee` (flat output), `Decay`, `XYC`
- TODO: "State-dependent due to decay"
- Skips: additivity test

#### Test: `test_DecayXYCProgressiveFeeIn`
- Instructions used: `Balances`, `Fee` (progressive input), `Decay`, `XYC`
- TODO: "Progressive fees violate additivity by design"
- Skips: additivity test

#### Test: `test_DecayXYCProgressiveFeeOut`
- Instructions used: `Balances`, `Fee` (progressive output), `Decay`, `XYC`
- TODO: "Progressive fees violate additivity by design"
- Skips: additivity test

#### Test: `test_DecayXYCProtocolFee`
- Instructions used: `Balances`, `Fee` (protocol), `Decay`, `XYC`
- TODO: "State-dependent due to decay + protocol fees"
- Skips: additivity test

#### Test: `test_DecayXYCMultipleFees`
- Instructions used: `Balances`, `Fee` (flat + progressive), `Decay`, `XYC`
- TODO: "due to progressive fees"
- Skips: additivity test

### 3. **TWAPLimitSwapInvariants.t.sol**

#### Test: `test_TWAPLimitSwapSymmetry`
- Instructions used: `Balances`, `TWAP`, `LimitSwap`
- TODO: "TWAP violates standard invariants due to time and state dependencies"
- Skips: symmetry test

#### Test: `test_TWAPLimitSwapAdditivity`
- Instructions used: `Balances`, `TWAP`, `LimitSwap`
- TODO: "TWAP violates standard invariants due to time and state dependencies"
- Skips: additivity test

#### Test: `test_TWAPLimitSwapMonotonicity`
- Instructions used: `Balances`, `TWAP`, `LimitSwap`
- TODO: "TWAP violates standard invariants due to time and state dependencies"
- Skips: symmetry test

#### Test: `test_TWAPLimitSwapFlatFeeAdditivity`
- Instructions used: `Balances`, `TWAP`, `Fee` (flat), `LimitSwap`
- TODO: "TWAP violates standard invariants due to time and state dependencies"
- Skips: additivity test

#### Test: `test_TWAPLimitSwapProgressiveFeeAdditivity`
- Instructions used: `Balances`, `TWAP`, `Fee` (progressive), `LimitSwap`
- TODO: "TWAP violates standard invariants due to time and state dependencies"
- Skips: additivity test

#### Test: `test_TWAPLimitSwapProtocolFeeAdditivity`
- Instructions used: `Balances`, `TWAP`, `Fee` (protocol), `LimitSwap`
- TODO: "TWAP violates standard invariants due to time and state dependencies"
- Skips: additivity test

### 4. **DutchAuctionLimitSwapFeesInvariants.t.sol**

#### Test: `test_DutchAuctionLimitSwapProgressiveFee`
- Instructions used: `Balances`, `DutchAuction`, `Fee` (progressive), `LimitSwap`
- TODO: "Fix additivity and monotonicity for progressive fees with dutch auction"
- Skips: additivity and monotonicity tests

#### Test: `test_DutchAuctionLimitSwapMultipleFees`
- Instructions used: `Balances`, `DutchAuction`, `Fee` (flat + progressive), `LimitSwap`
- TODO: "Fix additivity for progressive fees with dutch auction"
- Skips: additivity test

#### Test: `test_DutchAuctionLimitSwapProtocolFee`
- Instructions used: `Balances`, `DutchAuction`, `Fee` (protocol), `LimitSwap`
- TODO: "Fix additivity for protocol fees with dutch auction"
- Skips: additivity test

#### Test: `test_DutchAuctionLimitSwapAllFeeTypes`
- Instructions used: `Balances`, `DutchAuction`, `Fee` (flat + progressive + protocol), `LimitSwap`
- TODO: "Fix additivity for progressive fees with dutch auction"
- Skips: additivity test

### 5. **BaseFeeAdjusterFeesInvariants.t.sol**

#### Test: `test_BaseFeeAdjuster_ModerateGas_FlatFee`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (flat), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjuster_ModerateGas_ProgressiveFee`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (progressive), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjuster_ModerateGas_ProtocolFee`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (protocol), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjuster_ModerateGas_MultipleFees`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (flat + progressive), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjuster_ModerateGas_ProtocolAndProgressiveFees`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (protocol + progressive), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjuster_ModerateGas_AllFeeTypes`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (flat + progressive + protocol), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjusterWithDutchAuction_ModerateGas_Fees`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `DutchAuction`, `Fee` (progressive), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjusterWithETH_ModerateGas_Fees`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `DutchAuction`, `Fee` (progressive), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Test: `test_BaseFeeAdjuster_GasSpike_Fees`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `Fee` (flat), `LimitSwap`
- TODO: "BaseFeeAdjuster breaks symmetry and additivity due to asymmetric gas adjustments"
- Skips: symmetry and additivity tests

#### Helper method: `_testInvariantsWithConfigForFees`
- TODO: "Research if additivity can be preserved for gas-adjusted orders with fees"
- TODO: "Research if symmetry can be restored despite asymmetric gas adjustments and fees"
- TODO: "Research monotonicity behavior with progressive fees and gas adjustment"

### 6. **ConcentrateXYCFeesInvariants.t.sol**

#### Test: `test_GrowLiquidity_2D_FlatFeeIn`
- Instructions used: `Balances`, `Fee` (flat input), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "need to research behavior - state-dependent due to scale"
- Skips: additivity test

#### Test: `test_GrowLiquidity_2D_ProgressiveFeeIn`
- Instructions used: `Balances`, `Fee` (progressive input), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "Progressive fees violate additivity by design"
- Skips: additivity test

#### Test: `test_GrowPriceRange_2D_FlatFeeIn`
- Instructions used: `Balances`, `Fee` (flat input), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_GrowLiquidity_XD_FlatFeeOut`
- Instructions used: `Balances`, `Fee` (flat output), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "need to research behavior - state-dependent due to scale"
- Skips: additivity test

#### Test: `test_GrowLiquidity_XD_ProgressiveFeeOut`
- Instructions used: `Balances`, `Fee` (progressive output), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "Progressive fees violate additivity by design"
- Skips: additivity test

#### Test: `test_GrowPriceRange_XD_FlatFeeOut`
- Instructions used: `Balances`, `Fee` (flat output), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_ComplexFeeStructure_GrowLiquidity`
- Instructions used: `Balances`, `Fee` (multiple types), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "Complex fee interactions affect additivity"
- Skips: additivity test

#### Test: `test_GrowLiquidity_2D_ProtocolFee`
- Instructions used: `Balances`, `Fee` (protocol), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "need to research behavior - state-dependent due to scale"
- Skips: additivity test

#### Test: `test_GrowLiquidity_XD_ProgressiveFeeIn`
- Instructions used: `Balances`, `Fee` (progressive input), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "Progressive fees violate additivity by design"
- Skips: additivity test

#### Test: `test_GrowPriceRange_XD_ProgressiveFeeIn`
- Instructions used: `Balances`, `Fee` (progressive input), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_GrowLiquidity_XD_ProtocolFee`
- Instructions used: `Balances`, `Fee` (protocol), `Concentrate` (GrowLiquidity), `XYC`
- TODO: "need to research behavior - state-dependent due to scale"
- Skips: additivity test

#### Test: `test_GrowPriceRange_2D_ProgressiveFeeOut`
- Instructions used: `Balances`, `Fee` (progressive output), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "Progressive fees violate additivity by design"
- Skips: additivity test

#### Test: `test_GrowPriceRange_2D_ProtocolFee`
- Instructions used: `Balances`, `Fee` (protocol), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior"
- Skips: symmetry test

#### Test: `test_ComplexFeeStructure_GrowPriceRange`
- Instructions used: `Balances`, `Fee` (multiple types), `Concentrate` (GrowPriceRange), `XYC`
- TODO: "need to research behavior - state-dependent due to scale"
- Skips: additivity test

### 7. **BaseFeeAdjusterInvariants.t.sol**

#### Test: `test_BaseFeeAdjuster_ModerateGasPrice`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `LimitSwap`
- TODO: "research invariant behavior at moderate gas prices"

#### Test: `test_BaseFeeAdjuster_HighGasPrice`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `LimitSwap`
- TODO: "research invariant behavior at high gas prices"

#### Test: `test_BaseFeeAdjusterWithETH_VariousPrices`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `LimitSwap`
- TODO: "Analyze invariant behavior across different ETH prices"

#### Test: `test_BaseFeeAdjusterWithDutchAuction_ModerateGas`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `DutchAuction`, `LimitSwap`
- TODO: "Analyze invariant behavior with DutchAuction and gas adjustment"

#### Test: `test_BaseFeeAdjusterWithDutchAuctionOutput_ModerateGas`
- Instructions used: `Balances`, `BaseFeeAdjuster`, `DutchAuction` (output), `LimitSwap`
- TODO: "Analyze invariant behavior with DutchAuction output and gas adjustment"
12 changes: 6 additions & 6 deletions src/instructions/DutchAuction.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ library DutchAuctionArgsBuilder {
function build(
uint40 startTime,
uint16 duration,
uint32 decayFactor
uint64 decayFactor
) internal pure returns (bytes memory) {
require(decayFactor < 1e18, DutchAuctionDecayFactorShouldBeLessThanOneE18(decayFactor));
return abi.encodePacked(
Expand All @@ -34,11 +34,11 @@ library DutchAuctionArgsBuilder {
function parse(bytes calldata args) internal pure returns (
uint40 startTime,
uint16 duration,
uint32 decayFactor
uint64 decayFactor
) {
startTime = uint40(bytes5(args.slice(0, 5, DutchAuctionMissingStartTime.selector)));
duration = uint16(bytes2(args.slice(5, 7, DutchAuctionMissingDuration.selector)));
decayFactor = uint32(bytes4(args.slice(7, 11, DutchAuctionMissingDecayFactor.selector)));
decayFactor = uint64(bytes8(args.slice(7, 15, DutchAuctionMissingDecayFactor.selector)));
}
}

Expand Down Expand Up @@ -70,12 +70,12 @@ contract DutchAuction {
using Math for uint256;
using Power for uint256;

error DutchAuctionShouldBeAppliedAfterSwap();
error DutchAuctionShouldBeAppliedBeforeSwapAmountsComputed(uint256 amountIn, uint256 amountOut);
error DutchAuctionExpired(uint256 currentTime, uint256 deadline);

/// @notice Apply Dutch auction decay to shrink the amount in by shrinking the balance in
function _dutchAuctionBalanceIn1D(Context memory ctx, bytes calldata args) internal view {
require(ctx.swap.amountIn > 0 && ctx.swap.amountOut > 0, DutchAuctionShouldBeAppliedAfterSwap());
require(ctx.swap.amountIn == 0 || ctx.swap.amountOut == 0, DutchAuctionShouldBeAppliedBeforeSwapAmountsComputed(ctx.swap.amountIn, ctx.swap.amountOut));

(uint256 startTime, uint256 duration, uint256 decayFactor) = DutchAuctionArgsBuilder.parse(args);
require(block.timestamp <= startTime + duration, DutchAuctionExpired(block.timestamp, startTime + duration));
Expand All @@ -86,7 +86,7 @@ contract DutchAuction {

/// @notice Apply Dutch auction decay to increase the amount out by increasing the balance out
function _dutchAuctionBalanceOut1D(Context memory ctx, bytes calldata args) internal view {
require(ctx.swap.amountIn > 0 && ctx.swap.amountOut > 0, DutchAuctionShouldBeAppliedAfterSwap());
require(ctx.swap.amountIn == 0 || ctx.swap.amountOut == 0, DutchAuctionShouldBeAppliedBeforeSwapAmountsComputed(ctx.swap.amountIn, ctx.swap.amountOut));

(uint256 startTime, uint256 duration, uint256 decayFactor) = DutchAuctionArgsBuilder.parse(args);
require(block.timestamp <= startTime + duration, DutchAuctionExpired(block.timestamp, startTime + duration));
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/AquaOpcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract AquaOpcodes is
function _notInstruction(Context memory /* ctx */, bytes calldata /* args */) internal view {}

function _opcodes() internal pure virtual returns (function(Context memory, bytes calldata) internal[] memory result) {
function(Context memory, bytes calldata) internal[29] memory instructions = [
function(Context memory, bytes calldata) internal[31] memory instructions = [
_notInstruction,
// Debug - reserved for debugging utilities (core infrastructure)
_notInstruction,
Expand All @@ -52,6 +52,8 @@ contract AquaOpcodes is
// XYCConcentrate - liquidity concentration (common AMM feature)
XYCConcentrate._xycConcentrateGrowLiquidityXD,
XYCConcentrate._xycConcentrateGrowLiquidity2D,
XYCConcentrate._xycConcentrateGrowPriceRangeXD,
XYCConcentrate._xycConcentrateGrowPriceRange2D,
// Decay - Decay AMM (specific AMM)
Decay._decayXD,
// NOTE: Add new instructions here to maintain backward compatibility
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/Opcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract Opcodes is
function _notInstruction(Context memory /* ctx */, bytes calldata /* args */) internal view {}

function _opcodes() internal pure virtual returns (function(Context memory, bytes calldata) internal[] memory result) {
function(Context memory, bytes calldata) internal[43] memory instructions = [
function(Context memory, bytes calldata) internal[45] memory instructions = [
_notInstruction,
// Debug - reserved for debugging utilities (core infrastructure)
_notInstruction,
Expand Down Expand Up @@ -75,6 +75,8 @@ contract Opcodes is
// XYCConcentrate - liquidity concentration (common AMM feature)
XYCConcentrate._xycConcentrateGrowLiquidityXD,
XYCConcentrate._xycConcentrateGrowLiquidity2D,
XYCConcentrate._xycConcentrateGrowPriceRangeXD,
XYCConcentrate._xycConcentrateGrowPriceRange2D,
// Decay - Decay AMM (specific AMM)
Decay._decayXD,
// LimitSwap - limit orders (specific trading type)
Expand Down
Loading