From cadd63231bce48ad7dd81239a96cc81d00b0704c Mon Sep 17 00:00:00 2001 From: Rana Sadam Date: Tue, 31 Dec 2024 13:26:06 +0500 Subject: [PATCH 1/2] - Add Swap events to new implementations - Modify deployment script to deploy new contract structure --- script/DeployAllContract.s.sol | 115 ++++++++++-------- src/PoolLogic.sol | 38 ++++++ .../pool-logic/IPoolLogicEvents.sol | 32 +++++ 3 files changed, 136 insertions(+), 49 deletions(-) diff --git a/script/DeployAllContract.s.sol b/script/DeployAllContract.s.sol index c736008..472d239 100644 --- a/script/DeployAllContract.s.sol +++ b/script/DeployAllContract.s.sol @@ -1,49 +1,66 @@ -// // SPDX-License-Identifier: MIT -// pragma solidity ^0.8.0; - -// import "forge-std/Script.sol"; -// import "../src/Router.sol"; -// import "../src/Pool.sol"; -// import "../src/PoolLogic.sol"; - -// contract DeployAllContract is Script { -// function run() external { -// address ZERO_ADDRESS = address(0); -// // Fetch the OWNER_ADDRESS from the environment variables -// address ownerAddress = vm.envAddress("OWNER_ADDRESS"); -// console.log("Owner address:", ownerAddress); - -// // Fetch the VAULT_ADDRESS from the environment variables -// address vaultAddress = vm.envAddress("VAULT_ADDRESS"); -// console.log("Vault address:", vaultAddress); - -// // Fetch the PRIVATE_KEY from the environment variables -// uint256 privateKey = vm.envUint("PRIVATE_KEY"); - -// // Start broadcasting transactions -// vm.startBroadcast(privateKey); - -// // Step 1: Deploy Pool contract -// Pool pool = new Pool(vaultAddress, ZERO_ADDRESS, ZERO_ADDRESS); -// console.log("Pool deployed to:", address(pool)); - -// // Step 2: Deploy PoolLogic contract with Pool address -// PoolLogic poolLogic = new PoolLogic(ownerAddress, address(pool)); -// console.log("PoolLogic deployed to:", address(poolLogic)); - -// // Step 3: Deploy Router contract with Pool address -// Router router = new Router(ownerAddress, address(pool)); -// console.log("Router deployed to:", address(router)); - -// // Step 4: Update Pool contract with Router address -// pool.updateRouterAddress(address(router)); -// console.log("Router address updated in Pool:", address(router)); - -// // Step 5: Update Pool contract with PoolLogic address -// pool.updatePoolLogicAddress(address(poolLogic)); -// console.log("PoolLogic address updated in Pool:", address(poolLogic)); - -// // Stop broadcasting transactions -// vm.stopBroadcast(); -// } -// } +// SPDX - License - Identifier: MIT +pragma solidity 0.8.28; + +import "forge-std/Script.sol"; +import "../src/Router.sol"; +import "../src/Pool.sol"; +import "../src/PoolLogic.sol"; +import "../src/LiquidityLogic.sol"; + +contract DeployAllContract is Script { + function run() external { + address ZERO_ADDRESS = address(0); + // Fetch the OWNER_ADDRESS from the environment variables + address ownerAddress = vm.envAddress("OWNER_ADDRESS"); + console.log("Owner address:", ownerAddress); + + // Fetch the VAULT_ADDRESS from the environment variables + address vaultAddress = vm.envAddress("VAULT_ADDRESS"); + console.log("Vault address:", vaultAddress); + + // Fetch the PRIVATE_KEY from the environment variables + uint256 privateKey = vm.envUint("PRIVATE_KEY"); + + // Start broadcasting transactions + vm.startBroadcast(privateKey); + + // Step 1: Deploy PoolLogic contract + PoolLogic poolLogic = new PoolLogic(ownerAddress, ZERO_ADDRESS, ZERO_ADDRESS); + console.log("PoolLogic deployed to:", address(poolLogic)); + + // Step 2: Deploy PoolLogic contract + LiquidityLogic liquidityLogic = new LiquidityLogic(ownerAddress, ZERO_ADDRESS, ZERO_ADDRESS); + console.log("LiquidityLogic deployed to:", address(liquidityLogic)); + + // Step 3: Deploy Pool contract + Pool pool = new Pool(vaultAddress, ZERO_ADDRESS, address(poolLogic), address(liquidityLogic)); + console.log("Pool deployed to:", address(pool)); + + // Step 4: Deploy Router contract with Pool address + Router router = new Router(ownerAddress, address(pool)); + console.log("Router deployed to:", address(router)); + + // Step 5: Update Pool contract with Router address + pool.updateRouterAddress(address(router)); + console.log("Router address updated in Pool:", address(router)); + + // Step 6: Update Pool logic contract with Pool address + poolLogic.updatePoolAddress(address(pool)); + console.log("Pool address updated in PoolLogic:", address(pool)); + + // Step 7: Update Pool logic contract with LiquidityLogic address + poolLogic.updateLiquidityLogicAddress(address(liquidityLogic)); + console.log("LiquidityLogic address updated in PoolLogic:", address(liquidityLogic)); + + // Step 8: Update LiquidityLogic contract with Pool address + liquidityLogic.updatePoolAddress(address(pool)); + console.log("Pool address updated in LiquidityLogic:", address(pool)); + + // Step 9: Update LiquidityLogic contract with PoolLogic address + liquidityLogic.updatePoolLogicAddress(address(poolLogic)); + console.log("PoolLogic address updated in LiquidityLogic:", address(poolLogic)); + + // Stop broadcasting transactions + vm.stopBroadcast(); + } +} diff --git a/src/PoolLogic.sol b/src/PoolLogic.sol index 593e5f5..64bd195 100644 --- a/src/PoolLogic.sol +++ b/src/PoolLogic.sol @@ -168,6 +168,8 @@ contract PoolLogic is IPoolLogic { currentSwap.tokenOut, currentSwap.user, currentSwap.amountOut ); } + + emitSwapUpdated(currentSwap.swapID, currentSwap.streamsRemaining, currentSwap.swapPerStream); } else if (currentSwap.typeOfOrder == 1 && currentSwap.executionPrice == currentExecPrice) { currentSwap = _executeStreamAgainstPool(currentSwap); currentSwap.typeOfOrder++; @@ -194,6 +196,8 @@ contract PoolLogic is IPoolLogic { currentSwap.tokenOut, currentSwap.user, currentSwap.amountOut ); } + + emitSwapUpdated(currentSwap.swapID, currentSwap.streamsRemaining, currentSwap.swapPerStream); } unchecked { @@ -713,6 +717,8 @@ contract PoolLogic is IPoolLogic { } currentSwap.amountOut += amountOut; + emitSwapAgainstPool(currentSwap.swapID, swapAmountIn, amountOut); + return currentSwap; } @@ -897,4 +903,36 @@ contract PoolLogic is IPoolLogic { function STREAM_COUNT_PRECISION() external view override returns (uint256) { return liquidityLogic.STREAM_COUNT_PRECISION(); } + + function emitSwapEntered( + uint256 swapID, + bytes32 pairId, + uint256 swapAmount, + uint256 executionPrice, + address user, + uint8 typeOfOrder + ) + internal + { + emit SwapEntered(swapID, pairId, swapAmount, executionPrice, user, typeOfOrder); + } + + function emitSwapAgainstOrderBook( + uint256 swapIdIncoming, + uint256 swapIdOpposite, + uint256 settledAmountIn, + uint256 settledAmountOut + ) + internal + { + emit SwapAgainstOrderBook(swapIdIncoming, swapIdOpposite, settledAmountIn, settledAmountOut); + } + + function emitSwapUpdated(uint256 swapId, uint256 streamCountRemaining, uint256 swapPerStream) internal { + emit SwapUpdated(swapId, streamCountRemaining, swapPerStream); + } + + function emitSwapAgainstPool(uint256 swapId, uint256 amountIn, uint256 amountOut) internal { + emit SwapAgainstPool(swapId, amountIn, amountOut); + } } diff --git a/src/interfaces/pool-logic/IPoolLogicEvents.sol b/src/interfaces/pool-logic/IPoolLogicEvents.sol index 3f2a5b3..5334f32 100644 --- a/src/interfaces/pool-logic/IPoolLogicEvents.sol +++ b/src/interfaces/pool-logic/IPoolLogicEvents.sol @@ -6,4 +6,36 @@ interface IPoolLogicEvents { event PoolAddressUpdated(address, address); event LiquidityLogicAddressUpdated(address, address); event OwnerUpdated(address, address); + + // Event 1: SwapEntered called every time when a swap is started + event SwapEntered( + uint256 indexed swapID, // Unique identifier for the swap + bytes32 indexed pairId, // Token pair identifier (e.g., token AB) + uint256 indexed swapAmount, // Total amount involved in the swap (tokenIn) + uint256 executionPrice, // Current execution price (D/token during swap) + address user, // Address of the user initiating the swap + uint8 typeOfOrder // 1 = TRIGGER, 2 = MARKET, 3 = LIMIT + ); + + // Event 2: SwapAgainstOrderBook called every time when a swap is settled against opposite swap + event SwapAgainstOrderBook( + uint256 indexed swapIdIncoming, // Identifier linking the stream to its parent swap + uint256 indexed swapIdOpposite, // Identifier of the opposite swap + uint256 settledAmountIn, // (tokenIn) Amount involved in the stream + uint256 settledAmountOut // (tokenOut) Amount involved in the stream + ); + + // Event 3: SwapUpdated Called every time stream count is recalculated + event SwapUpdated( + uint256 indexed swapId, // Swap ID + uint256 indexed streamCountRemaining, // 0 as false if there is no opposite swap + uint256 indexed swapPerStream // Amount per stream + ); + + // Event 4: SwapAgainstPool called every time when a swap is settled against pool + event SwapAgainstPool( + uint256 indexed swapId, // Identifier linking the stream to its parent swap + uint256 amountIn, // Input amount + uint256 amountOut // Output amount + ); } From f6b33a40a6903caa72f94646e9bb601ca7af4a94 Mon Sep 17 00:00:00 2001 From: Rana Sadam Date: Fri, 10 Jan 2025 18:16:50 +0500 Subject: [PATCH 2/2] Add pool events --- src/LiquidityLogic.sol | 51 +++++++++++++++++++ .../liquidity-logic/ILiquidityLogicEvents.sol | 44 ++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/LiquidityLogic.sol b/src/LiquidityLogic.sol index 012c9b3..b8a1095 100644 --- a/src/LiquidityLogic.sol +++ b/src/LiquidityLogic.sol @@ -655,4 +655,55 @@ contract LiquidityLogic is ILiquidityLogic { } } } + + // Methods to emit events + function emitAddLiquidityEntered( + uint256 id, + address token, + address user, + uint256 amount, + uint256 streamCount + ) internal { + emit AddLiquidityEntered(id, token, user, amount, streamCount); + } + + function emitRemoveLiquidityEntered( + uint256 id, + address token, + address user, + uint256 amount, + uint256 streamCount + ) internal { + emit RemoveLiquidityEntered(id, token, user, amount, streamCount); + } + + function emitAddLiquidityStreamExecuted( + uint256 id + ) internal { + emit AddLiquidityStreamExecuted(id); + } + + function emitRemoveLiquidityStreamExecuted( + uint256 id + ) internal { + emit RemoveLiquidityStreamExecuted(id); + } + + function emitGenesisPoolInitialized( + address token, + address user, + uint256 tokenAmount, + uint256 initialDToMint + ) internal { + emit GenesisPoolInitialized(token, user, tokenAmount, initialDToMint); + } + + function emitPoolInitialized( + bytes32 pairId, + address user, + uint256 tokenAmount, + uint256 dTokenAmount + ) internal { + emit PoolInitialized(pairId, user, tokenAmount, dTokenAmount); + } } diff --git a/src/interfaces/liquidity-logic/ILiquidityLogicEvents.sol b/src/interfaces/liquidity-logic/ILiquidityLogicEvents.sol index 4bf058e..6cec6e7 100644 --- a/src/interfaces/liquidity-logic/ILiquidityLogicEvents.sol +++ b/src/interfaces/liquidity-logic/ILiquidityLogicEvents.sol @@ -5,4 +5,48 @@ interface ILiquidityLogicEvents { event PoolAddressUpdated(address, address); event PoolLogicAddressUpdated(address, address); event OwnerUpdated(address, address); + + // Event 1: Add Liquidity Entered + event AddLiquidityEntered( + uint256 indexed id, + address token, + address user, + uint256 amount, + uint256 streamCount + ); + + // Event 2: Remove Liquidity Entered + event RemoveLiquidityEntered( + uint256 indexed id, + address token, + address user, + uint256 amount, + uint256 streamCount + ); + + // Event 3: Add Liquidity Stream Executed + event AddLiquidityStreamExecuted( + uint256 indexed id + ); + + // Event 4: Remove Liquidity Stream Executed + event RemoveLiquidityStreamExecuted( + uint256 indexed id + ); + + // Event 5: Genesis Pool Initialized + event GenesisPoolInitialized( + address indexed token, + address indexed user, + uint256 tokenAmount, + uint256 initialDToMint + ); + + // Event 6: Pool Initialized + event PoolInitialized( + bytes32 indexed pairId, + address indexed user, + uint256 tokenAmount, + uint256 dTokenAmount + ); }