From 02af6439eca3d5a4b720cd62cadd608a78a70ba9 Mon Sep 17 00:00:00 2001 From: 0xCrumbs <97379465+0xcrumb@users.noreply.github.com> Date: Mon, 2 Sep 2024 15:22:35 +0100 Subject: [PATCH] Add gTrade v9.2 partial increase/decrease events --- dexs/gains-network/index.ts | 86 ++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 25 deletions(-) diff --git a/dexs/gains-network/index.ts b/dexs/gains-network/index.ts index 83d9067af6..5484fa2930 100644 --- a/dexs/gains-network/index.ts +++ b/dexs/gains-network/index.ts @@ -25,6 +25,10 @@ const topic0_market_ex = [ "0xca42b0e44cd853d207b87e8f8914eaefef9c9463a8c77ca33754aa62f6904f00", // v7 V8_MARKET_TOPIC0, // v8 ]; +const topic0_partials = [ + "0xf09a9c949c4bd4cbe75b424bea11c683c3ae55e7cdb8321c3ec37e01af72c8d5", // PositionSizeIncreaseExecuted + "0xe74b50af866d7f8e3577bc959bf73a2690841f0abce22ab0cfb1b1c84122a7d7", // PositionSizeDecreaseExecuted +]; const precisionException: { [a: string | number]: number } = { "0x2ac6749d0affd42c8d61ef25e433f92e375a1aef": 1e6, @@ -32,43 +36,60 @@ const precisionException: { [a: string | number]: number } = { 3: 1e6, // v8 USDC }; +const diamonds = { + [CHAIN.POLYGON]: "0x209a9a01980377916851af2ca075c2b170452018", + [CHAIN.ARBITRUM]: "0xff162c694eaa571f685030649814282ea457f169", +}; + const contract_addresses: IAddresses = { [CHAIN.POLYGON]: [ "0x82e59334da8c667797009bbe82473b55c7a6b311", // DAI TradingCallbacks "0x0bbed2eac3237ba128643670b7cf3be475933755", // ETH TradingCallbacks "0x2ac6749d0affd42c8d61ef25e433f92e375a1aef", // USDC TradingCallbacks - "0x209a9a01980377916851af2ca075c2b170452018", // v8 Diamond + diamonds[CHAIN.POLYGON], // v8 Diamond ], [CHAIN.ARBITRUM]: [ "0x298a695906e16aea0a184a2815a76ead1a0b7522", // DAI TradingCallbacks "0x62a9f50c92a57c719ff741133caa55c7a81ce019", // ETH TradingCallbacks "0x4542256c583bcad66a19a525b57203773a6485bf", // USDC TradingCallbacks - "0xff162c694eaa571f685030649814282ea457f169", // v8 Diamond + diamonds[CHAIN.ARBITRUM], // v8 Diamond ], }; -const fetch: any = async (timestamp: number, _, { getLogs, createBalances, chain }): Promise => { - const limitLogs: ILog[] = ( - (await Promise.all( - topic0_limit_ex.map(async (topic0) => - getLogs({ - targets: contract_addresses[chain], - topics: [topic0], - }) - ) - )) as ILog[][] - ).flat(); - - const marketLogs: ILog[] = ( - (await Promise.all( - topic0_market_ex.map(async (topic0) => - getLogs({ - targets: contract_addresses[chain], - topics: [topic0], - }) - ) - )) as ILog[][] - ).flat(); +const fetch: any = async (timestamp: number, _, { getLogs, chain }): Promise => { + const [limitLogs, marketLogs, partialsLogs] = ( + await Promise.all([ + // Limit Executed logs + (await Promise.all( + topic0_limit_ex.map(async (topic0) => + getLogs({ + targets: contract_addresses[chain], + topics: [topic0], + }) + ) + )) as ILog[][], + + // Market Executed logs + (await Promise.all( + topic0_market_ex.map(async (topic0) => + getLogs({ + targets: contract_addresses[chain], + topics: [topic0], + }) + ) + )) as ILog[][], + + // Partial Increase/Decrease logs + (await Promise.all( + topic0_partials.map(async (topic0) => + getLogs({ + targets: [diamonds[chain]], + topics: [topic0], + }) + ) + )) as ILog[][], + ]) + ).map((logs: ILog[][]) => logs.flat()); const limit_volume = limitLogs .map((e: ILog) => { @@ -111,7 +132,22 @@ const fetch: any = async (timestamp: number, _, { getLogs, createBalances, chain }) .reduce((a: number, b: number) => a + b, 0); - const dailyVolume = limit_volume + market_volume; + const partials_volume = partialsLogs + .map((e: ILog) => { + const data = e.data.replace("0x", ""); + const cancelReason = Number("0x" + data.slice(128, 192)); + + if (cancelReason > 0) return 0; + + const collateralPrecision = precisionException[Number(e.topics[1])] ?? 1e18; + const collateralPriceUsd = Number("0x" + data.slice(384, 448)) / 1e8; + const positionSizeDelta = Number("0x" + data.slice(576, 640)) / collateralPrecision; + + return positionSizeDelta * collateralPriceUsd; + }) + .reduce((a: number, b: number) => a + b, 0); + + const dailyVolume = limit_volume + market_volume + partials_volume; return { dailyVolume, timestamp }; };