diff --git a/dexs/primex-finance/index.ts b/dexs/primex-finance/index.ts index 19d3ea29ec..12e1d37db2 100644 --- a/dexs/primex-finance/index.ts +++ b/dexs/primex-finance/index.ts @@ -1,37 +1,69 @@ -import { ChainBlocks, FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types" -import { config, } from "./utils"; +import { + ChainBlocks, + FetchOptions, + FetchResultVolume, + SimpleAdapter, +} from "../../adapters/types"; +import { config, topics } from "./utils"; const abi = { - "SpotSwap": "event SpotSwap(address indexed trader, address indexed receiver, address tokenA, address tokenB, uint256 amountSold, uint256 amountBought)", - "OpenPosition": "event OpenPosition(uint256 indexed positionId, address indexed trader, address indexed openedBy, (uint256 id, uint256 scaledDebtAmount, address bucket, address soldAsset, uint256 depositAmountInSoldAsset, address positionAsset, uint256 positionAmount, address trader, uint256 openBorrowIndex, uint256 createdAt, uint256 updatedConditionsAt, bytes extraParams) position, address feeToken, uint256 protocolFee, uint256 entryPrice, uint256 leverage, (uint256 managerType, bytes params)[] closeConditions)", - "ClosePosition": "event ClosePosition(uint256 indexed positionId, address indexed trader, address indexed closedBy, address bucketAddress, address soldAsset, address positionAsset, uint256 decreasePositionAmount, int256 profit, uint256 positionDebt, uint256 amountOut, uint8 reason)", - "PartialClosePosition": "event PartialClosePosition(uint256 indexed positionId, address indexed trader, address bucketAddress, address soldAsset, address positionAsset, uint256 decreasePositionAmount, uint256 depositedAmount, uint256 scaledDebtAmount, int256 profit, uint256 positionDebt, uint256 amountOut)" -} + SpotSwap: + "event SpotSwap(address indexed trader, address indexed receiver, address tokenA, address tokenB, uint256 amountSold, uint256 amountBought)", + OpenPosition: + "event OpenPosition(uint256 indexed positionId, address indexed trader, address indexed openedBy, (uint256 id, uint256 scaledDebtAmount, address bucket, address soldAsset, uint256 depositAmountInSoldAsset, address positionAsset, uint256 positionAmount, address trader, uint256 openBorrowIndex, uint256 createdAt, uint256 updatedConditionsAt, bytes extraParams) position, address feeToken, uint256 protocolFee, uint256 entryPrice, uint256 leverage, (uint256 managerType, bytes params)[] closeConditions)", + ClosePosition: + "event ClosePosition(uint256 indexed positionId, address indexed trader, address indexed closedBy, address bucketAddress, address soldAsset, address positionAsset, uint256 decreasePositionAmount, int256 profit, uint256 positionDebt, uint256 amountOut, uint8 reason)", + PartialClosePosition: + "event PartialClosePosition(uint256 indexed positionId, address indexed trader, address bucketAddress, address soldAsset, address positionAsset, uint256 decreasePositionAmount, uint256 depositedAmount, uint256 scaledDebtAmount, int256 profit, uint256 positionDebt, uint256 amountOut)", +}; -const fetch = (chain: string) => async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, }: FetchOptions): Promise => { - const { swapManager, positionManager, batchManager } = config[chain]; - const dailyVolume = createBalances() +const fetch = + (chain: string) => + async ( + timestamp: number, + _: ChainBlocks, + { createBalances, getLogs }: FetchOptions + ): Promise => { + const { swapManager, positionManager, batchManager } = config[chain]; + const dailyVolume = createBalances(); - const logsConfig = [ - { targets: swapManager, eventAbi: abi.SpotSwap }, - { targets: positionManager, eventAbi: abi.OpenPosition }, - { targets: positionManager, eventAbi: abi.ClosePosition }, - { targets: positionManager, eventAbi: abi.PartialClosePosition }, - { targets: batchManager, eventAbi: abi.ClosePosition }, - ] + const logsConfig = [ + { targets: swapManager, eventAbi: abi.SpotSwap }, + { + targets: positionManager, + eventAbi: abi.OpenPosition, + topic: topics.openPosition, + }, + { targets: positionManager, eventAbi: abi.ClosePosition }, + { targets: positionManager, eventAbi: abi.PartialClosePosition }, + { targets: batchManager, eventAbi: abi.ClosePosition }, + ]; - const [swapLogs, openPositionLogs, closePositionLogs, partiallyClosePositionLogs, closePositionBatchLogs] = await Promise.all(logsConfig.map(async (config) => getLogs(config))) + const [ + swapLogs, + openPositionLogs, + closePositionLogs, + partiallyClosePositionLogs, + closePositionBatchLogs, + ] = await Promise.all(logsConfig.map(async (config) => getLogs(config))); - swapLogs.forEach((e: any) => dailyVolume.add(e.tokenA, e.amountSold)) - openPositionLogs.forEach((e: any) => dailyVolume.add(e.position.soldAsset, e.position.amountSold * e.leverage)) - closePositionLogs.forEach((e: any) => dailyVolume.add(e.soldAsset, e.amountOut)) - partiallyClosePositionLogs.forEach((e: any) => dailyVolume.add(e.soldAsset, e.amountOut)) - closePositionBatchLogs.forEach((e: any) => dailyVolume.add(e.soldAsset, e.amountOut)) + swapLogs.forEach((e: any) => dailyVolume.add(e.tokenA, e.amountSold)); + openPositionLogs.forEach((e: any) => + dailyVolume.add(e.position.positionAsset, e.position.positionAmount) + ); + closePositionLogs.forEach((e: any) => + dailyVolume.add(e.soldAsset, e.amountOut) + ); + partiallyClosePositionLogs.forEach((e: any) => + dailyVolume.add(e.soldAsset, e.amountOut) + ); + closePositionBatchLogs.forEach((e: any) => + dailyVolume.add(e.soldAsset, e.amountOut) + ); - return { dailyVolume: dailyVolume, timestamp, } + return { dailyVolume: dailyVolume, timestamp }; + }; - -} const adapters: SimpleAdapter = { adapter: Object.keys(config).reduce((acc, chain) => { return { @@ -39,8 +71,8 @@ const adapters: SimpleAdapter = { [chain]: { fetch: fetch(chain), start: config[chain].start, - } - } - }, {}) -} + }, + }; + }, {}), +}; export default adapters; diff --git a/dexs/primex-finance/utils.ts b/dexs/primex-finance/utils.ts index 1f0c98e769..359eea20f4 100644 --- a/dexs/primex-finance/utils.ts +++ b/dexs/primex-finance/utils.ts @@ -9,357 +9,39 @@ interface ChainConfig { const config: { [chain: string]: ChainConfig } = { [CHAIN.POLYGON]: { - swapManager: ['0x0AaDC2Eae6963ED983d85cbF088b0c294f4c26ff', '0xA0069a14Df3ECd19a38c509757eBc2C2Aaa44992'], - positionManager: ['0x02bcaA4633E466d151b34112608f60A82a4F6035'], - batchManager: ['0xC6B1AF3dEb9E379ccADF2Fa21263a50E91F4776C', '0xc10771D8f5B6Ba702E3a44EC76969f07578F08b7'], + swapManager: [ + "0x0AaDC2Eae6963ED983d85cbF088b0c294f4c26ff", + "0xA0069a14Df3ECd19a38c509757eBc2C2Aaa44992", + ], + positionManager: ["0x02bcaA4633E466d151b34112608f60A82a4F6035"], + batchManager: [ + "0xC6B1AF3dEb9E379ccADF2Fa21263a50E91F4776C", + "0xc10771D8f5B6Ba702E3a44EC76969f07578F08b7", + ], start: 1697587200, }, [CHAIN.ARBITRUM]: { - swapManager: ['0xbE3de856EB22bf6EFA03DD55e65DF22bA212e6Db'], - positionManager: ['0x86890E30cE9E1e13Db5560BbEb435c55567Af1cd'], - batchManager: ['0xF2225a8f90311DaF9e989db1AfFd47617bb69E96'], + swapManager: ["0xbE3de856EB22bf6EFA03DD55e65DF22bA212e6Db"], + positionManager: ["0x86890E30cE9E1e13Db5560BbEb435c55567Af1cd"], + batchManager: ["0xF2225a8f90311DaF9e989db1AfFd47617bb69E96"], start: 1700611200, }, [CHAIN.ETHEREUM]: { - swapManager: ['0xa6d76535e265357187653d4AAd9b362404D42EA8'], - positionManager: ['0x99d63fEA4b3Ef6ca77941df3C5740dAd1586f0B8'], - batchManager: ['0x1da9c104C517C7b4465c8Eef458Da0a6c61835Fe'], + swapManager: ["0xa6d76535e265357187653d4AAd9b362404D42EA8"], + positionManager: ["0x99d63fEA4b3Ef6ca77941df3C5740dAd1586f0B8"], + batchManager: ["0x1da9c104C517C7b4465c8Eef458Da0a6c61835Fe"], start: 1702771200, }, -} +}; const topics = { - swap: '0x5fcf6637f014854f918b233372226c5492e6a5157e517674a8588675550c40c6', - openPosition: '0x3f505465ce78d219c28bcf9bed881a651c4800d1161454b0d5c93225196e7b8e', - partiallyClosePosition: '0xda47f84a849dfb28125ae28a0bf305b75e72bff27796fc4bca36e2f848b0a0e6', - closePosition: '0x4a06c6510972c5a49ff5582d7d8e59f20228038c8cb9ea05d78f02ac7ee40662' -} - -const abi = [ - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "address", - "name": "trader", - "type": "address" - }, - { - "indexed": true, - "internalType": "address", - "name": "receiver", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "tokenA", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "tokenB", - "type": "address" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "amountSold", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "amountBought", - "type": "uint256" - } - ], - "name": "SpotSwap", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "uint256", - "name": "positionId", - "type": "uint256" - }, - { - "indexed": true, - "internalType": "address", - "name": "trader", - "type": "address" - }, - { - "indexed": true, - "internalType": "address", - "name": "openedBy", - "type": "address" - }, - { - "components": [ - { - "internalType": "uint256", - "name": "id", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "scaledDebtAmount", - "type": "uint256" - }, - { - "internalType": "contract IBucket", - "name": "bucket", - "type": "address" - }, - { - "internalType": "address", - "name": "soldAsset", - "type": "address" - }, - { - "internalType": "uint256", - "name": "depositAmountInSoldAsset", - "type": "uint256" - }, - { - "internalType": "address", - "name": "positionAsset", - "type": "address" - }, - { - "internalType": "uint256", - "name": "positionAmount", - "type": "uint256" - }, - { - "internalType": "address", - "name": "trader", - "type": "address" - }, - { - "internalType": "uint256", - "name": "openBorrowIndex", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "createdAt", - "type": "uint256" - }, - { - "internalType": "uint256", - "name": "updatedConditionsAt", - "type": "uint256" - }, - { - "internalType": "bytes", - "name": "extraParams", - "type": "bytes" - } - ], - "indexed": false, - "internalType": "struct PositionLibrary.Position", - "name": "position", - "type": "tuple" - }, - { - "indexed": false, - "internalType": "address", - "name": "feeToken", - "type": "address" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "protocolFee", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "entryPrice", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "leverage", - "type": "uint256" - }, - { - "components": [ - { - "internalType": "uint256", - "name": "managerType", - "type": "uint256" - }, - { - "internalType": "bytes", - "name": "params", - "type": "bytes" - } - ], - "indexed": false, - "internalType": "struct LimitOrderLibrary.Condition[]", - "name": "closeConditions", - "type": "tuple[]" - } - ], - "name": "OpenPosition", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "uint256", - "name": "positionId", - "type": "uint256" - }, - { - "indexed": true, - "internalType": "address", - "name": "trader", - "type": "address" - }, - { - "indexed": true, - "internalType": "address", - "name": "closedBy", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "bucketAddress", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "soldAsset", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "positionAsset", - "type": "address" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "decreasePositionAmount", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "int256", - "name": "profit", - "type": "int256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "positionDebt", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "amountOut", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "enum PositionLibrary.CloseReason", - "name": "reason", - "type": "uint8" - } - ], - "name": "ClosePosition", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "internalType": "uint256", - "name": "positionId", - "type": "uint256" - }, - { - "indexed": true, - "internalType": "address", - "name": "trader", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "bucketAddress", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "soldAsset", - "type": "address" - }, - { - "indexed": false, - "internalType": "address", - "name": "positionAsset", - "type": "address" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "decreasePositionAmount", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "depositedAmount", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "scaledDebtAmount", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "int256", - "name": "profit", - "type": "int256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "positionDebt", - "type": "uint256" - }, - { - "indexed": false, - "internalType": "uint256", - "name": "amountOut", - "type": "uint256" - } - ], - "name": "PartialClosePosition", - "type": "event" - } -] + swap: "0x5fcf6637f014854f918b233372226c5492e6a5157e517674a8588675550c40c6", + openPosition: + "0x3f505465ce78d219c28bcf9bed881a651c4800d1161454b0d5c93225196e7b8e", + partiallyClosePosition: + "0xda47f84a849dfb28125ae28a0bf305b75e72bff27796fc4bca36e2f848b0a0e6", + closePosition: + "0x4a06c6510972c5a49ff5582d7d8e59f20228038c8cb9ea05d78f02ac7ee40662", +}; -export { config, topics, abi } \ No newline at end of file +export { config, topics };