Skip to content

Commit

Permalink
test: swaps with/without fees
Browse files Browse the repository at this point in the history
  • Loading branch information
Autoparallel committed Feb 21, 2024
1 parent 7e8fc22 commit 4699489
Showing 1 changed file with 137 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/test/G3M/G3MTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,150 @@ contract G3MTest is Test {
_;
}

/// @dev Initializes a basic pool in dfmm with 0 swapFee.
modifier basic_feeless() {
vm.warp(0);
G3M.G3MParams memory params = G3M.G3MParams({
wX: 0.5 ether,
wY: 0.5 ether,
swapFee: 0,
controller: address(0)
});
uint256 init_p = 1 ether;
uint256 init_x = 1 ether;
bytes memory initData =
solver.getInitialPoolData(init_x, init_p, params);

IDFMM.InitParams memory initParams = IDFMM.InitParams({
strategy: address(g3m),
tokenX: tokenX,
tokenY: tokenY,
data: initData
});

dfmm.init(initParams);
_;
}

function test_g3m_swap() public basic {
uint256 amountIn = 10 ether;
uint256 poolId = dfmm.nonce() - 1;
(bool valid, uint256 amountOut, uint256 price, bytes memory swapData) =
solver.simulateSwap(poolId, true, amountIn);
}

function test_g3m_swap_x_in_no_fee() public basic_feeless {
bool xIn = true;
uint256 amountIn = 0.1 ether;
uint256 poolId = dfmm.nonce() - 1;
(bool valid, uint256 amountOut, uint256 price, bytes memory swapData) =
solver.simulateSwap(poolId, xIn, amountIn);

console2.log("Valid: ", valid);
assert(valid);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 90_909_090_909_090_907);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 1.1 ether);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 909_090_909_090_909_093);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1 ether);

dfmm.swap(poolId, swapData);
}

function test_g3m_swap_y_in_no_fee() public basic_feeless {
bool xIn = false;
uint256 amountIn = 0.1 ether;
uint256 poolId = dfmm.nonce() - 1;
(bool valid, uint256 amountOut, uint256 price, bytes memory swapData) =
solver.simulateSwap(poolId, xIn, amountIn);

console2.log("Valid: ", valid);
assert(valid);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 90_909_090_909_090_907);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 909_090_909_090_909_093);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 1.1 ether);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1 ether);

dfmm.swap(poolId, swapData);
}

function test_g3m_swap_x_in_with_fee() public basic {
bool xIn = true;
uint256 amountIn = 0.1 ether;
uint256 poolId = dfmm.nonce() - 1;
(bool valid, uint256 amountOut, uint256 price, bytes memory swapData) =
solver.simulateSwap(poolId, xIn, amountIn);

console2.log("Valid: ", valid);
assert(valid);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 90_636_343_181_818_178);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 1.1 ether);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 909_363_656_818_181_822);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1.00015 ether);

dfmm.swap(poolId, swapData);
}

function test_g3m_swap_y_in_with_fee() public basic {
bool xIn = false;
uint256 amountIn = 0.1 ether;
uint256 poolId = dfmm.nonce() - 1;
(bool valid, uint256 amountOut, uint256 price, bytes memory swapData) =
solver.simulateSwap(poolId, xIn, amountIn);

console2.log("Valid: ", valid);
assert(valid);

console2.log("AmountOut: ", amountOut);
assertEq(amountOut, 90_636_343_181_818_178);

(uint256 endReservesRx, uint256 endReservesRy, uint256 endReservesL) =
abi.decode(swapData, (uint256, uint256, uint256));

console2.log("endReservesRx: ", endReservesRx);
assertEq(endReservesRx, 909_363_656_818_181_822);

console2.log("endReservesRy: ", endReservesRy);
assertEq(endReservesRy, 1.1 ether);

console2.log("endReservesL: ", endReservesL);
assertEq(endReservesL, 1.00015 ether);

dfmm.swap(poolId, swapData);
}

function test_diff_lower() public basic {
uint256 poolId = dfmm.nonce() - 1;
int256 diffLowered =
Expand Down

0 comments on commit 4699489

Please sign in to comment.