Skip to content

Commit

Permalink
Merge pull request #2150 from GainsNetwork-org/master
Browse files Browse the repository at this point in the history
[gTrade | Gains Network] Add Apechain deployment
  • Loading branch information
dtmkeng authored Nov 28, 2024
2 parents ea6d70d + 0e7f592 commit b92b95b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
50 changes: 38 additions & 12 deletions dexs/gains-network/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<FetchResultVolume> => {
const fetch: any = async (timestamp: number, _: ChainBlocks, options: FetchOptions): Promise<FetchResultVolume> => {
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<FetchResultVolume> => {
// 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,
};
Expand Down
38 changes: 35 additions & 3 deletions fees/gains-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,51 @@ const fetch = async (timestamp: number, _: ChainBlocks, options: FetchOptions):
};
};

const fetchApechain = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs }: FetchOptions): Promise<FetchResultFees> => {
// 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,
Expand Down

0 comments on commit b92b95b

Please sign in to comment.