From 0e7f59239068ace9f7ad276d60d0f75e1ecf8a98 Mon Sep 17 00:00:00 2001 From: 0xCrumbs <97379465+0xcrumb@users.noreply.github.com> Date: Thu, 28 Nov 2024 12:42:06 +0000 Subject: [PATCH] add ApeChain fees and volume stats for gTrade --- dexs/gains-network/index.ts | 50 ++++++++++++++++++++++++++++--------- fees/gains-network.ts | 38 +++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/dexs/gains-network/index.ts b/dexs/gains-network/index.ts index ea396d1e09..0e57ce1968 100644 --- a/dexs/gains-network/index.ts +++ b/dexs/gains-network/index.ts @@ -9,8 +9,7 @@ interface IStats { daily_volume: number; } -const requests: any = {} - +const requests: any = {}; export async function fetchURLWithRetry(url: string, options: FetchOptions) { const start = options.startTimestamp; @@ -20,26 +19,53 @@ export async function fetchURLWithRetry(url: string, options: FetchOptions) { requests[key] = queryDune("4192496", { start: start, end: end, - }) - return requests[key] + }); + return requests[key]; } -const fetch: any = async ( - timestamp: number, - _: ChainBlocks, - options: FetchOptions -): Promise => { +const fetch: any = async (timestamp: number, _: ChainBlocks, options: FetchOptions): Promise => { const stats: IStats[] = await fetchURLWithRetry("4192496", options); const chainStat = stats.find((stat) => stat.unix_ts === options.startOfDay && stat.blockchain === options.chain); return { timestamp, dailyVolume: chainStat?.daily_volume || 0 }; }; +const fetchApechain: any = async (timestamp: number, _: ChainBlocks, { getLogs }: FetchOptions): Promise => { + // Apechain currently not supported on Dune, must fetch from Events + const DIAMOND = "0x2BE5D7058AdBa14Bc38E4A83E94A81f7491b0163"; + const [limitLogs, marketLogs, partialIncreaseLogs, partialDecreaseLogs] = await Promise.all( + [ + "event LimitExecuted((address user, uint32 index) orderId, address indexed user, uint32 indexed index, uint32 indexed limitIndex, (address user, uint32 index, uint16 pairIndex, uint24 leverage, bool long, bool isOpen, uint8 collateralIndex, uint8 tradeType, uint120 collateralAmount, uint64 openPrice, uint64 tp, uint64 sl, uint192 __placeholder) t, address triggerCaller, uint8 orderType, uint256 oraclePrice, uint256 marketPrice, uint256 liqPrice, uint256 priceImpactP, int256 percentProfit, uint256 amountSentToTrader, uint256 collateralPriceUsd, bool exactExecution)", + "event MarketExecuted((address user, uint32 index) orderId, address indexed user, uint32 indexed index, (address user, uint32 index, uint16 pairIndex, uint24 leverage, bool long, bool isOpen, uint8 collateralIndex, uint8 tradeType, uint120 collateralAmount, uint64 openPrice, uint64 tp, uint64 sl, uint192 __placeholder) t, bool open, uint256 oraclePrice, uint256 marketPrice, uint256 liqPrice, uint256 priceImpactP, int256 percentProfit, uint256 amountSentToTrader, uint256 collateralPriceUsd)", + "event PositionSizeIncreaseExecuted((address user, uint32 index) orderId, uint8 cancelReason, uint8 indexed collateralIndex, address indexed trader, uint256 pairIndex, uint256 indexed index, bool long, uint256 oraclePrice, uint256 collateralPriceUsd, uint256 collateralDelta, uint256 leverageDelta, (uint256 positionSizeCollateralDelta, uint256 existingPositionSizeCollateral, uint256 newPositionSizeCollateral, uint256 newCollateralAmount, uint256 newLeverage, uint256 priceAfterImpact, int256 existingPnlCollateral, uint256 oldPosSizePlusPnlCollateral, uint256 newOpenPrice, uint256 borrowingFeeCollateral, uint256 openingFeesCollateral, uint256 existingLiqPrice, uint256 newLiqPrice) vals)", + "event PositionSizeDecreaseExecuted((address user, uint32 index) orderId, uint8 cancelReason, uint8 indexed collateralIndex, address indexed trader, uint256 pairIndex, uint256 indexed index, bool long, uint256 oraclePrice, uint256 collateralPriceUsd, uint256 collateralDelta, uint256 leverageDelta, (uint256 positionSizeCollateralDelta, uint256 existingPositionSizeCollateral, uint256 existingLiqPrice, uint256 priceAfterImpact, int256 existingPnlCollateral, uint256 borrowingFeeCollateral, uint256 closingFeeCollateral, int256 availableCollateralInDiamond, int256 collateralSentToTrader, uint120 newCollateralAmount, uint24 newLeverage) vals)", + ].map((eventAbi) => getLogs({ target: DIAMOND, eventAbi })) + ); + + const [BI_1e3, BI_1e18, BI_1e8] = [1000n, BigInt(1e18), BigInt(1e8)]; + + const volumeLimitsAndMarkets = limitLogs + .concat(marketLogs) + .map((e: any) => Number((e.t.collateralAmount * e.t.leverage * e.collateralPriceUsd) / BI_1e18 / BI_1e3 / BI_1e8)) + .reduce((a: number, b: number) => a + b, 0); + + const volumePartials = partialIncreaseLogs + .concat(partialDecreaseLogs) + .map((e: any) => Number((e.vals.positionSizeCollateralDelta * e.collateralPriceUsd) / BI_1e18 / BI_1e8)) + .reduce((a: number, b: number) => a + b, 0); + + return { dailyVolume: volumeLimitsAndMarkets + volumePartials, timestamp }; +}; + const adapter: SimpleAdapter = { adapter: { - [CHAIN.ARBITRUM]: { fetch, start: '2023-05-25' }, - [CHAIN.POLYGON]: { fetch, start: '2023-05-25' }, - [CHAIN.BASE]: { fetch, start: '2024-09-26' }, + [CHAIN.ARBITRUM]: { fetch, start: "2023-05-25" }, + [CHAIN.POLYGON]: { fetch, start: "2023-05-25" }, + [CHAIN.BASE]: { fetch, start: "2024-09-26" }, + [CHAIN.APECHAIN]: { + fetch: fetchApechain, + start: "2024-11-19", + }, }, isExpensiveAdapter: true, }; diff --git a/fees/gains-network.ts b/fees/gains-network.ts index 5162c4329c..cfdeefb53d 100644 --- a/fees/gains-network.ts +++ b/fees/gains-network.ts @@ -63,19 +63,51 @@ const fetch = async (timestamp: number, _: ChainBlocks, options: FetchOptions): }; }; +const fetchApechain = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs }: FetchOptions): Promise => { + // Dune does not currently support Apechain. Using events until support is added. + const dailyFees = createBalances(); + const dailyRevenue = createBalances(); + const dailyHoldersRevenue = createBalances(); + const dailySupplySideRevenue = createBalances(); + const DIAMOND = "0x2BE5D7058AdBa14Bc38E4A83E94A81f7491b0163"; + const APE = "0x48b62137edfa95a428d35c09e44256a739f6b557"; // wAPE + + const [govFee, referralFee, triggerFee, stakingFee, gTokenFee, borrowingFee]: any = await Promise.all( + [ + "event GovFeeCharged(address indexed trader, uint8 indexed collateralIndex, uint256 amountCollateral)", + "event ReferralFeeCharged(address indexed trader, uint8 indexed collateralIndex, uint256 amountCollateral)", + "event TriggerFeeCharged(address indexed trader, uint8 indexed collateralIndex, uint256 amountCollateral)", + "event GnsOtcFeeCharged(address indexed trader, uint8 indexed collateralIndex, uint256 amountCollateral)", + "event GTokenFeeCharged(address indexed trader, uint8 indexed collateralIndex, uint256 amountCollateral)", + "event BorrowingFeeCharged(address indexed trader, uint32 indexed index, uint8 indexed collateralIndex, uint256 amountCollateral)", + ].map((eventAbi) => getLogs({ target: DIAMOND, eventAbi })) + ); + + [govFee, referralFee, triggerFee, stakingFee, gTokenFee, borrowingFee].flat().forEach((i: any) => dailyFees.add(APE, i.amountCollateral)); + [govFee, stakingFee].flat().forEach((i: any) => dailyRevenue.add(APE, i.amountCollateral)); + stakingFee.forEach((i: any) => dailyHoldersRevenue.add(APE, i.amountCollateral)); + gTokenFee.forEach((i: any) => dailySupplySideRevenue.add(APE, i.amountCollateral)); + + return { timestamp, dailyFees, dailyRevenue, dailyHoldersRevenue, dailySupplySideRevenue }; +}; + const adapter: Adapter = { adapter: { [CHAIN.POLYGON]: { fetch: fetch, - start: '2022-06-03', + start: "2022-06-03", }, [CHAIN.ARBITRUM]: { fetch: fetch, - start: '2022-12-30', + start: "2022-12-30", }, [CHAIN.BASE]: { fetch: fetch, - start: '2024-09-26', + start: "2024-09-26", + }, + [CHAIN.APECHAIN]: { + fetch: fetchApechain, + start: "2024-11-19", }, }, isExpensiveAdapter: true,