Skip to content

Commit

Permalink
Getting close
Browse files Browse the repository at this point in the history
  • Loading branch information
mehranhydary committed Sep 17, 2024
1 parent c4e4255 commit 957a169
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
4 changes: 3 additions & 1 deletion contracts/src/MiladyPoolOrderManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {BalanceDelta} from "v4-core/types/BalanceDelta.sol";
import {BeforeSwapDelta, toBeforeSwapDelta} from "v4-core/types/BeforeSwapDelta.sol";
import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol";
import {StateLibrary} from "v4-core/libraries/StateLibrary.sol";
import {CurrencyLibrary, Currency} from "v4-core/types/Currency.sol";
// Eigenlayer
import "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol";
Expand Down Expand Up @@ -42,6 +43,7 @@ contract MiladyPoolOrderManager is
using BN254 for BN254.G1Point;
using PoolIdLibrary for PoolKey;
using StateLibrary for IPoolManager;
using CurrencyLibrary for Currency;

constructor(
IRegistryCoordinator _registryCoordinator,
Expand Down Expand Up @@ -80,7 +82,7 @@ contract MiladyPoolOrderManager is
function afterSwap(
address,
PoolKey calldata key,
IPoolManager.SwapParams calldata params,
IPoolManager.SwapParams calldata,
BalanceDelta,
bytes calldata
) external override onlyByPoolManager returns (bytes4, int128) {
Expand Down
78 changes: 60 additions & 18 deletions contracts/src/MiladyPoolRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {PoolId, PoolIdLibrary} from "v4-core/types/PoolId.sol";
import {StateLibrary} from "v4-core/libraries/StateLibrary.sol";
import {TransientStateLibrary} from "v4-core/libraries/TransientStateLibrary.sol";
import {CurrencyLibrary, Currency} from "v4-core/types/Currency.sol";
import {CurrencySettler} from "@uniswap/v4-core/test/utils/CurrencySettler.sol";

import {MiladyPoolMath} from "./libraries/MiladyPoolMath.sol";
import {BalanceDelta} from "v4-core/types/BalanceDelta.sol";
Expand All @@ -26,6 +27,7 @@ contract MiladyPoolRouter is WyvernInspired {
using TransientStateLibrary for IPoolManager;
using PoolIdLibrary for PoolKey;
using CurrencyLibrary for Currency;
using CurrencySettler for Currency;

// TODO: Hardcoded for now, should update so that we pass it in
address constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
Expand Down Expand Up @@ -155,27 +157,67 @@ contract MiladyPoolRouter is WyvernInspired {
permit2Signature
);

_settle(
data.params.zeroForOne // Gets the token that is swapped in
? Currency(data.key.currency0)
: Currency(data.key.currency1),
uint128(amountIn)
);

// Can skip the entire amount to swap here if we just do the swap here
// At this point the first token is already in the pool so we need to call _take
_take(
data.params.zeroForOne // Gets the token that is swapped out
? Currency(data.key.currency1)
: Currency(data.key.currency0),
uint128(amountOut),
walletAddress
);
console.log("amountIn: %d", amountIn);
console.log("amountOut: %d", amountOut);
console.log("zeroForOne: %s", data.params.zeroForOne);

// Call this with new data params
BalanceDelta delta = manager.swap(data.key, data.params, data.hookData);
BalanceDelta delta = manager.swap(data.key, data.params, "");
int256 deltaAfter0 = delta.amount0();
int256 deltaAfter1 = delta.amount1();
// _settle(
// data.params.zeroForOne // Gets the token that is swapped in
// ? Currency(data.key.currency0)
// : Currency(data.key.currency1),
// uint128(delta.amount0())
// );

// // Can skip the entire amount to swap here if we just do the swap here
// // At this point the first token is already in the pool so we need to call _take
// _take(
// data.params.zeroForOne // Gets the token that is swapped out
// ? Currency(data.key.currency1)
// : Currency(data.key.currency0),
// uint128(delta.amount1()),
// walletAddress
// );

if (deltaAfter0 < 0) {
data.key.currency0.settle(
manager,
walletAddress,
uint256(-deltaAfter0),
true
);
}
if (deltaAfter1 < 0) {
data.key.currency1.settle(
manager,
walletAddress,
uint256(-deltaAfter1),
true
);
}
if (deltaAfter0 > 0) {
data.key.currency0.take(
manager,
walletAddress,
uint256(deltaAfter0),
false
);
}
if (deltaAfter1 > 0) {
data.key.currency1.take(
manager,
walletAddress,
uint256(deltaAfter1),
false
);
}

return abi.encode(delta);

return abi.encode(0);
// return abi.encode(toBeforeSwapDelta(0, 0));
}

// TODO: Update so that the PoolKey sqrtPriceCurrentX96, liquidity are what you need
Expand Down
7 changes: 5 additions & 2 deletions contracts/test/MiladyPoolOrderManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,12 @@ contract MiladyPoolOrderManagerTest is MiladyPoolDeployer, Deployers {
hooksUseable
);

// Supporting exact input, zero for one
IPoolManager.SwapParams memory swapParams = IPoolManager.SwapParams({
zeroForOne: true,
amountSpecified: 100 * 10 ** 18,
zeroForOne: Currency.unwrap(token0) < Currency.unwrap(token1)
? true
: false,
amountSpecified: -100 * 10 ** 18,
sqrtPriceLimitX96: TickMath.MIN_SQRT_PRICE + 1
});

Expand Down

0 comments on commit 957a169

Please sign in to comment.