From 5d3e67f5298c24ae57e5a3ce63ec94543feb94c8 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Sat, 17 Feb 2024 23:00:23 +0100 Subject: [PATCH] refactor pancakeswap-perp to remove dependency on dune and count only opening positions as volume --- dexs/panacakeswap-perp/index.ts | 84 ++++++++++++--------------------- 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/dexs/panacakeswap-perp/index.ts b/dexs/panacakeswap-perp/index.ts index 500f3f88cd..9340360a2f 100644 --- a/dexs/panacakeswap-perp/index.ts +++ b/dexs/panacakeswap-perp/index.ts @@ -1,64 +1,42 @@ -import { Chain } from "@defillama/sdk/build/general"; -import { SimpleAdapter } from "../../adapters/types"; -import { CHAIN } from "../../helpers/chains"; -import { getBlock } from "../../helpers/getBlock"; -import * as sdk from "@defillama/sdk"; -import { queryDune } from "../../helpers/dune"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -type TID = { - [key: string | Chain]: string; +// https://api.dodoex.io/dodo-contract/list +const config = { + arbitrum: { contract: '0xb3879e95a4b8e3ee570c232b19d520821f540e48', }, + bsc: { contract: '0x1b6f2d3844c6ae7d56ceb3c3643b9060ba28feb0', }, } -type TBrokerID = { - [s: string | Chain]: number[]; -} -const brokerID: TBrokerID = { - [CHAIN.BSC]: [2], - [CHAIN.ARBITRUM]: [1, 2] -} -const contract_address: TID = { - [CHAIN.BSC]: '2826941', - [CHAIN.ARBITRUM]: '2982352' -} -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} -const topic_0_open = '0xa858fcdefab65cbd1997932d8ac8aa1a9a8c46c90b20947575525d9a2a437f8c' -interface IData { - dt: string; - volume: number; - fees: number; +import { ChainBlocks, FetchOptions } from "../../adapters/types"; + +const abis = { + "OpenMarketTrade": "event OpenMarketTrade(address indexed user, bytes32 indexed tradeHash, (address user, uint32 userOpenTradeIndex, uint64 entryPrice, address pairBase, address tokenIn, uint96 margin, uint64 stopLoss, uint64 takeProfit, uint24 broker, bool isLong, uint96 openFee, int256 longAccFundingFeePerShare, uint96 executionFee, uint40 timestamp, uint80 qty, uint40 holdingFeeRate, uint256 openBlock) ot)", } -const fetchVolume = (chain: Chain) => { - return async (timestamp: number) => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); - const dateString = new Date(dayTimestamp * 1000).toISOString().split("T")[0]; - const query: IData[] = (await queryDune(contract_address[chain])) - // const query: IData[] = require(`./${chain}.json`); - const dailyVolume = query.find((e: IData) => e.dt.split(' ')[0] === dateString)?.volume; +const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, chain, api }: FetchOptions) => { + const dailyVolume = createBalances() + const target = config[chain].contract + + const openLogs = await getLogs({ target, eventAbi: abis.OpenMarketTrade, topics:['0xa858fcdefab65cbd1997932d8ac8aa1a9a8c46c90b20947575525d9a2a437f8c'], skipCacheRead: true}) + let tokens = new Set() + openLogs.forEach(({ ot }: any) => { + tokens.add(ot.tokenIn.toLowerCase()) + }) + const tokensArray = Array.from(tokens) + const decimals = await api.multiCall({ abi: 'erc20:decimals', calls: tokensArray as any}) + const decimalMapping: any = {} + decimals.forEach((d: any, idx) => { + decimalMapping[tokensArray[idx] as any] = d + }) - return { - dailyVolume: `${dailyVolume}`, - timestamp, - }; - }; + openLogs.forEach(({ ot }: any) => { + dailyVolume.addCGToken('tether', Number(ot.entryPrice) * Number(ot.qty) * 10 **(-18)) + }) + return { timestamp, dailyVolume } }; -const adapter: SimpleAdapter = { - adapter: { - [CHAIN.BSC]: { - fetch: fetchVolume(CHAIN.BSC), - start: 1682035200, - }, - [CHAIN.ARBITRUM]: { - fetch: fetchVolume(CHAIN.ARBITRUM), - start: 1692662400, - }, - }, +const adapter: any = { + adapter: {}, }; +Object.keys(config).forEach((chain) => adapter.adapter[chain] = { fetch, start: 1690848000, }); + export default adapter;