From 1349dfefa2f5637a1687d85af1691fafc4dc38a6 Mon Sep 17 00:00:00 2001 From: Alexander Burkut Date: Mon, 16 Dec 2024 13:36:29 +0300 Subject: [PATCH] add fixes --- src/abi/fluid-dex/fluid-dex.abi.json | 606 +++++++++++++++++- src/dex/fluid-dex/fluid-dex-events.test.ts | 50 +- .../fluid-dex/fluid-dex-liquidity-proxy.ts | 34 - src/dex/fluid-dex/fluid-dex-pool.ts | 5 +- 4 files changed, 628 insertions(+), 67 deletions(-) diff --git a/src/abi/fluid-dex/fluid-dex.abi.json b/src/abi/fluid-dex/fluid-dex.abi.json index 14ada197a..cdd5e0f1a 100644 --- a/src/abi/fluid-dex/fluid-dex.abi.json +++ b/src/abi/fluid-dex/fluid-dex.abi.json @@ -1,9 +1,119 @@ [ + { + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "dexId", + "type": "uint256" + }, + { + "internalType": "address", + "name": "liquidity", + "type": "address" + }, + { + "internalType": "address", + "name": "factory", + "type": "address" + }, + { + "components": [ + { + "internalType": "address", + "name": "shift", + "type": "address" + }, + { + "internalType": "address", + "name": "admin", + "type": "address" + }, + { + "internalType": "address", + "name": "colOperations", + "type": "address" + }, + { + "internalType": "address", + "name": "debtOperations", + "type": "address" + }, + { + "internalType": "address", + "name": "perfectOperationsAndSwapOut", + "type": "address" + } + ], + "internalType": "struct Structs.Implementations", + "name": "implementations", + "type": "tuple" + }, + { + "internalType": "address", + "name": "deployerContract", + "type": "address" + }, + { + "internalType": "address", + "name": "token0", + "type": "address" + }, + { + "internalType": "address", + "name": "token1", + "type": "address" + }, + { + "internalType": "bytes32", + "name": "supplyToken0Slot", + "type": "bytes32" + }, + { + "internalType": "bytes32", + "name": "borrowToken0Slot", + "type": "bytes32" + }, + { + "internalType": "bytes32", + "name": "supplyToken1Slot", + "type": "bytes32" + }, + { + "internalType": "bytes32", + "name": "borrowToken1Slot", + "type": "bytes32" + }, + { + "internalType": "bytes32", + "name": "exchangePriceToken0Slot", + "type": "bytes32" + }, + { + "internalType": "bytes32", + "name": "exchangePriceToken1Slot", + "type": "bytes32" + }, + { + "internalType": "uint256", + "name": "oracleMapping", + "type": "uint256" + } + ], + "internalType": "struct Structs.ConstantViews", + "name": "constantViews_", + "type": "tuple" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, { "inputs": [ { "internalType": "uint256", - "name": "errorId", + "name": "errorId_", "type": "uint256" } ], @@ -14,7 +124,18 @@ "inputs": [ { "internalType": "uint256", - "name": "shares", + "name": "errorId", + "type": "uint256" + } + ], + "name": "FluidDexFactoryError", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "shares_", "type": "uint256" } ], @@ -87,7 +208,7 @@ "type": "uint256" } ], - "internalType": "struct IFluidDexT1.PricesAndExchangePrice", + "internalType": "struct Structs.PricesAndExchangePrice", "name": "pex_", "type": "tuple" } @@ -117,6 +238,346 @@ "name": "FluidDexSwapResult", "type": "error" }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "errorId_", + "type": "uint256" + } + ], + "name": "FluidLiquidityCalcsError", + "type": "error" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "errorId_", + "type": "uint256" + } + ], + "name": "FluidSafeTransferError", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "int256", + "name": "routing", + "type": "int256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amtOut", + "type": "uint256" + } + ], + "name": "LogArbitrage", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + } + ], + "name": "LogBorrowDebtLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token0Amt", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token1Amt", + "type": "uint256" + } + ], + "name": "LogBorrowPerfectDebtLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + } + ], + "name": "LogDepositColLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token0Amt", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token1Amt", + "type": "uint256" + } + ], + "name": "LogDepositPerfectColLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + ], + "name": "LogPauseSwapAndArbitrage", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + ], + "name": "LogUnpauseSwapAndArbitrage", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token0Amt", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token1Amt", + "type": "uint256" + } + ], + "name": "LogPaybackDebtInOneToken", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + } + ], + "name": "LogPaybackDebtLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token0Amt", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token1Amt", + "type": "uint256" + } + ], + "name": "LogPaybackPerfectDebtLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token0Amt", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token1Amt", + "type": "uint256" + } + ], + "name": "LogWithdrawColInOneToken", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + } + ], + "name": "LogWithdrawColLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint256", + "name": "shares", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token0Amt", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "token1Amt", + "type": "uint256" + } + ], + "name": "LogWithdrawPerfectColLiquidity", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "bool", + "name": "swap0to1", + "type": "bool" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amountIn", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amountOut", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "address", + "name": "to", + "type": "address" + } + ], + "name": "Swap", + "type": "event" + }, + { + "stateMutability": "payable", + "type": "fallback" + }, { "inputs": [], "name": "DEX_ID", @@ -148,9 +609,9 @@ "type": "uint256" }, { - "internalType": "bool", - "name": "estimate_", - "type": "bool" + "internalType": "address", + "name": "to_", + "type": "address" } ], "name": "borrow", @@ -182,9 +643,9 @@ "type": "uint256" }, { - "internalType": "bool", - "name": "estimate_", - "type": "bool" + "internalType": "address", + "name": "to_", + "type": "address" } ], "name": "borrowPerfect", @@ -248,11 +709,11 @@ }, { "internalType": "address", - "name": "perfectOperationsAndOracle", + "name": "perfectOperationsAndSwapOut", "type": "address" } ], - "internalType": "struct IFluidDexT1.Implementations", + "internalType": "struct Structs.Implementations", "name": "implementations", "type": "tuple" }, @@ -307,7 +768,7 @@ "type": "uint256" } ], - "internalType": "struct IFluidDexT1.ConstantViews", + "internalType": "struct Structs.ConstantViews", "name": "constantsView_", "type": "tuple" } @@ -342,7 +803,7 @@ "type": "uint256" } ], - "internalType": "struct IFluidDexT1.ConstantViews2", + "internalType": "struct Structs.ConstantViews2", "name": "constantsView2_", "type": "tuple" } @@ -476,7 +937,7 @@ "type": "uint256" } ], - "internalType": "struct IFluidDexT1.CollateralReserves", + "internalType": "struct Structs.CollateralReserves", "name": "c_", "type": "tuple" } @@ -547,7 +1008,7 @@ "type": "uint256" } ], - "internalType": "struct IFluidDexT1.DebtReserves", + "internalType": "struct Structs.DebtReserves", "name": "d_", "type": "tuple" } @@ -562,6 +1023,29 @@ "stateMutability": "nonpayable", "type": "function" }, + { + "inputs": [ + { + "internalType": "address", + "name": "token_", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount_", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "data_", + "type": "bytes" + } + ], + "name": "liquidityCallback", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, { "inputs": [ { @@ -605,7 +1089,7 @@ "type": "uint256" } ], - "internalType": "struct IFluidDexT1.Oracle[]", + "internalType": "struct Structs.Oracle[]", "name": "twaps_", "type": "tuple[]" }, @@ -778,6 +1262,40 @@ "stateMutability": "payable", "type": "function" }, + { + "inputs": [ + { + "internalType": "bool", + "name": "swap0to1_", + "type": "bool" + }, + { + "internalType": "uint256", + "name": "amountIn_", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "amountOutMin_", + "type": "uint256" + }, + { + "internalType": "address", + "name": "to_", + "type": "address" + } + ], + "name": "swapInWithCallback", + "outputs": [ + { + "internalType": "uint256", + "name": "amountOut_", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, { "inputs": [ { @@ -812,6 +1330,40 @@ "stateMutability": "payable", "type": "function" }, + { + "inputs": [ + { + "internalType": "bool", + "name": "swap0to1_", + "type": "bool" + }, + { + "internalType": "uint256", + "name": "amountOut_", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "amountInMax_", + "type": "uint256" + }, + { + "internalType": "address", + "name": "to_", + "type": "address" + } + ], + "name": "swapOutWithCallback", + "outputs": [ + { + "internalType": "uint256", + "name": "amountIn_", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, { "inputs": [ { @@ -830,9 +1382,9 @@ "type": "uint256" }, { - "internalType": "bool", - "name": "estimate_", - "type": "bool" + "internalType": "address", + "name": "to_", + "type": "address" } ], "name": "withdraw", @@ -864,9 +1416,9 @@ "type": "uint256" }, { - "internalType": "bool", - "name": "estimate_", - "type": "bool" + "internalType": "address", + "name": "to_", + "type": "address" } ], "name": "withdrawPerfect", @@ -903,9 +1455,9 @@ "type": "uint256" }, { - "internalType": "bool", - "name": "estimate_", - "type": "bool" + "internalType": "address", + "name": "to_", + "type": "address" } ], "name": "withdrawPerfectInOneToken", @@ -918,5 +1470,9 @@ ], "stateMutability": "nonpayable", "type": "function" + }, + { + "stateMutability": "payable", + "type": "receive" } ] diff --git a/src/dex/fluid-dex/fluid-dex-events.test.ts b/src/dex/fluid-dex/fluid-dex-events.test.ts index 974e8a506..f5bad501d 100644 --- a/src/dex/fluid-dex/fluid-dex-events.test.ts +++ b/src/dex/fluid-dex/fluid-dex-events.test.ts @@ -11,14 +11,15 @@ import { FluidDexConfig } from './config'; import { FluidDexLiquidityProxy } from './fluid-dex-liquidity-proxy'; import { FluidDexFactory } from './fluid-dex-factory'; import { FluidDexEventPool } from './fluid-dex-pool'; +import { StatefulEventSubscriber } from '../../stateful-event-subscriber'; jest.setTimeout(50 * 1000); -async function fetchLiquidityProxyState( - liquidityProxy: FluidDexLiquidityProxy, +async function fetchState( + statefulEventSubscriber: StatefulEventSubscriber, blockNumber: number, -): Promise { - return liquidityProxy.generateState(blockNumber); +): Promise { + return statefulEventSubscriber.generateState(blockNumber); } // eventName -> blockNumbers @@ -65,7 +66,7 @@ describe('FluidDex EventPool Mainnet', function () { liquidityProxy, liquidityProxy.addressesSubscribed, (_blockNumber: number) => - fetchLiquidityProxyState(liquidityProxy, _blockNumber), + fetchState(liquidityProxy, _blockNumber), blockNumber, `${dexKey}_${poolAddress}`, dexHelper.provider, @@ -134,8 +135,45 @@ describe('FluidDex EventPool Mainnet', function () { const eventsToTest: Record = { '0x8710039D5de6840EdE452A85672B32270a709aE2': { LogPauseSwapAndArbitrage: [21337128], - LogUnpauseSwapAndArbitrage: [], + }, + '0x2886a01a0645390872a9eb99dae1283664b0c524': { + LogPauseSwapAndArbitrage: [21374547], }, }; + + Object.entries(eventsToTest).forEach( + ([poolAddress, events]: [string, EventMappings]) => { + describe(`Events for ${poolAddress}`, () => { + beforeEach(() => { + dexPool = new FluidDexEventPool( + dexKey, + poolAddress, + network, + dexHelper, + logger, + ); + }); + Object.entries(events).forEach( + ([eventName, blockNumbers]: [string, number[]]) => { + describe(`${eventName}`, () => { + blockNumbers.forEach((blockNumber: number) => { + it(`State after ${blockNumber}`, async function () { + await testEventSubscriber( + dexPool, + dexPool.addressesSubscribed, + (_blockNumber: number) => + fetchState(dexPool, _blockNumber), + blockNumber, + `${dexKey}_${poolAddress}`, + dexHelper.provider, + ); + }); + }); + }); + }, + ); + }); + }, + ); }); }); diff --git a/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts b/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts index 036eb43d1..63276bfb1 100644 --- a/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts +++ b/src/dex/fluid-dex/fluid-dex-liquidity-proxy.ts @@ -15,12 +15,6 @@ import { } from './types'; import { Address } from '../../types'; import { Contract, ethers } from 'ethers'; -import { uint256ToBigInt } from '../../lib/decoders'; -import { DecodedStateMultiCallResultWithRelativeBitmaps } from '../uniswap-v3/types'; - -const { - utils: { hexlify, hexZeroPad }, -} = ethers; export class FluidDexLiquidityProxy extends StatefulEventSubscriber { handlers: { @@ -132,34 +126,6 @@ export class FluidDexLiquidityProxy extends StatefulEventSubscriber ({ - // target: pool.pool, - // callData: this.poolIface.encodeFunctionData('readFromStorage', [ - // hexZeroPad(hexlify(1), 32), - // ]), - // decodeFunction: uint256ToBigInt, - // })); - // - // const storageResults = await this.dexHelper.multiWrapper.tryAggregate< - // bigint | DecodedStateMultiCallResultWithRelativeBitmaps - // >( - // false, - // multicallData, - // blockNumber, - // this.dexHelper.multiWrapper.defaultBatchSize, - // false, - // ); - // - // const poolsReserves = convertedResult.poolsReserves.map( - // (poolReserve, index) => { - // const isSwapAndArbitragePaused = - // BigInt(storageResults[index].returnData.toString()) >> BigInt(255) === - // BigInt(1); - // return { ...poolReserve, isSwapAndArbitragePaused }; - // }, - // ); - // this.logger.info(`${this.parentName}: ${this.name}: generating state...`); return convertedResult; diff --git a/src/dex/fluid-dex/fluid-dex-pool.ts b/src/dex/fluid-dex/fluid-dex-pool.ts index 2fbfa64c7..02fbf3177 100644 --- a/src/dex/fluid-dex/fluid-dex-pool.ts +++ b/src/dex/fluid-dex/fluid-dex-pool.ts @@ -69,6 +69,7 @@ export class FluidDexEventPool extends StatefulEventSubscriber { } catch (e) { return null; } + if (event.name in this.handlers) { return this.handlers[event.name](event, state, log); } @@ -79,11 +80,11 @@ export class FluidDexEventPool extends StatefulEventSubscriber { return null; } - handleLogPauseSwapAndArbitrage() { + handleLogPauseSwapAndArbitrage(): PoolState { return { isSwapAndArbitragePaused: true }; } - handleLogUnpauseSwapAndArbitrage() { + handleLogUnpauseSwapAndArbitrage(): PoolState { return { isSwapAndArbitragePaused: false }; }