diff --git a/aggregators/paraswap/index.ts b/aggregators/paraswap/index.ts index ba2a4f9f3c..0e0a3a2fab 100644 --- a/aggregators/paraswap/index.ts +++ b/aggregators/paraswap/index.ts @@ -1,38 +1,3 @@ -// https://developers.paraswap.network/smart-contracts -const chains = [ - "ethereum", - "arbitrum", - "avax", - "bsc", - "fantom", - "optimism", - "polygon", - "base", - "polygon_zkevm", -]; +import { getParaswapAdapter } from "./paraswapApi"; -import { ChainBlocks, FetchOptions } from "../../adapters/types"; - -const abis = { - "SwappedDirect": "event SwappedDirect(bytes16 uuid, address partner, uint256 feePercent, address initiator, uint8 kind, address indexed beneficiary, address indexed srcToken, address indexed destToken, uint256 srcAmount, uint256 receivedAmount, uint256 expectedAmount)", - "SwappedV3": "event SwappedV3(bytes16 uuid, address partner, uint256 feePercent, address initiator, address indexed beneficiary, address indexed srcToken, address indexed destToken, uint256 srcAmount, uint256 receivedAmount, uint256 expectedAmount)", -} - -const defaultSwapper = '0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57' -const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, chain, }: FetchOptions) => { - const dailyVolume = createBalances() - let target = defaultSwapper - if (chain === "polygon_zkevm") target = '0xb83b554730d29ce4cb55bb42206c3e2c03e4a40a' - const swappedDirectLogs = await getLogs({ target, eventAbi: abis.SwappedDirect, }) - const swappedV3Logs = await getLogs({ target, eventAbi: abis.SwappedV3, }); - [swappedDirectLogs, swappedV3Logs].flat().forEach((log: any) => { - dailyVolume.add(log.destToken, log.receivedAmount) - }) - return { timestamp, dailyVolume } -}; - -const adapter: any = { adapter: {}, }; - -chains.forEach((chain) => adapter.adapter[chain] = { fetch, start: 1676592000, }); - -export default adapter; +export default getParaswapAdapter("volume"); diff --git a/aggregators/paraswap/paraswapApi.ts b/aggregators/paraswap/paraswapApi.ts new file mode 100644 index 0000000000..ebc5b52535 --- /dev/null +++ b/aggregators/paraswap/paraswapApi.ts @@ -0,0 +1,77 @@ +import { Adapter, FetchResultFees, FetchResultVolume } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; +import { getTimestampAtStartOfDayUTC } from "../../utils/date"; +import { Chain } from "@defillama/sdk/build/general"; +import fetchURL from "../../utils/fetchURL"; + + +const feesMMURL = "https://api.paraswap.io/stk/volume-stats/breakdown-by-chain"; +type TChainId = { + [l: string | Chain]: string; +} +const mapChainId: TChainId = { + [CHAIN.ETHEREUM]: '1', + [CHAIN.POLYGON]: '137', + [CHAIN.BSC]: '56', + [CHAIN.AVAX]: '43114', + [CHAIN.FANTOM]: '250', + [CHAIN.ARBITRUM]: '42161', + [CHAIN.OPTIMISM]: '10', + [CHAIN.BASE]: '8453', + [CHAIN.POLYGON_ZKEVM]: '1101' +} + +interface IResponse { + daily: any[]; + allTime: any; +} + +export function getParaswapAdapter(type:"fees"|"volume"){ +const fetch = (chain: Chain) => { + return async (timestamp: number): Promise => { + const timestampToday = getTimestampAtStartOfDayUTC(timestamp) + const response: IResponse = (await fetchURL(feesMMURL)); + const dailyResultFees: any[] = response.daily; + const [totalVolume,totalPartnerRevenue, totalProtocolRevenue]: number[] = response.allTime[mapChainId[chain]]; + const [dailyVolume, partnerRevenue, protocolRevenue]: number[] = dailyResultFees.filter(([time]: any) => time === timestampToday) + .map(([_, data]: any) => data[mapChainId[chain]]).flat() + const otherFees = partnerRevenue + protocolRevenue; + const otherProtocolReveune = protocolRevenue; + + const dailyFees = otherFees; + if (dailyFees > 1_000_000) { + return {} as FetchResultFees; + } + const dailyRevenue = otherProtocolReveune; + const totalFees = totalPartnerRevenue + totalProtocolRevenue; + const totalRevenue = totalProtocolRevenue; + if(type === "fees"){ + return { + dailyFees: dailyFees ? dailyFees.toString() : undefined, + dailyRevenue: dailyRevenue ? dailyRevenue.toString() : undefined, + totalRevenue: totalRevenue ? totalRevenue.toString() : undefined, + totalFees: totalFees ? totalFees.toString(): undefined, + timestamp + } + } else { + return { + dailyVolume: dailyVolume.toString(), + totalVolume: totalVolume.toString(), + timestamp + } + } + } +} + +const adapter: Adapter = { + adapter: Object.keys(mapChainId).reduce((all, chain)=>({ + ...all, + [chain]:{ + fetch: fetch(chain), + start: 1647907200, + } + }), {} as any) +} + +return adapter +} diff --git a/fees/paraswap.ts b/fees/paraswap.ts index 9989ddfef0..63766e3543 100644 --- a/fees/paraswap.ts +++ b/fees/paraswap.ts @@ -1,88 +1,3 @@ -import { Adapter, FetchResultFees } from "../adapters/types"; -import { CHAIN } from "../helpers/chains"; -import { getTimestampAtStartOfDayUTC } from "../utils/date"; -import { Chain } from "@defillama/sdk/build/general"; -import fetchURL from "../utils/fetchURL"; +import {getParaswapAdapter} from "../aggregators/paraswap/paraswapApi" - -const feesMMURL = "https://api.paraswap.io/stk/volume-stats/breakdown-by-chain"; -type TChainId = { - [l: string | Chain]: string; -} -const mapChainId: TChainId = { - [CHAIN.ETHEREUM]: '1', - [CHAIN.POLYGON]: '137', - [CHAIN.BSC]: '56', - [CHAIN.AVAX]: '43114', - [CHAIN.FANTOM]: '250', - [CHAIN.ARBITRUM]: '42161', - [CHAIN.OPTIMISM]: '10', -} - -interface IResponse { - daily: any[]; - allTime: any; -} - -const fetch = (chain: Chain) => { - return async (timestamp: number): Promise => { - const timestampToday = getTimestampAtStartOfDayUTC(timestamp) - const response: IResponse = (await fetchURL(feesMMURL)); - const dailyResultFees: any[] = response.daily; - const [__,totalPartnerRevenue, totalProtocolRevenue]: number[] = response.allTime[mapChainId[chain]]; - const [_, partnerRevenue, protocolRevenue]: number[] = dailyResultFees.filter(([time]: any) => time === timestampToday) - .map(([_, data]: any) => data[mapChainId[chain]]).flat() - const otherFees = partnerRevenue + protocolRevenue; - const otherProtocolReveune = protocolRevenue; - - const dailyFees = otherFees; - if (dailyFees > 1_000_000) { - return {} as FetchResultFees; - } - const dailyRevenue = otherProtocolReveune; - const totalFees = totalPartnerRevenue + totalProtocolRevenue; - const totalRevenue = totalProtocolRevenue; - return { - dailyFees: dailyFees ? dailyFees.toString() : undefined, - dailyRevenue: dailyRevenue ? dailyRevenue.toString() : undefined, - totalRevenue: totalRevenue ? totalRevenue.toString() : undefined, - totalFees: totalFees ? totalFees.toString(): undefined, - timestamp - } - } -} - -const adapter: Adapter = { - adapter: { - [CHAIN.ETHEREUM]: { - fetch: fetch(CHAIN.ETHEREUM), - start: 1647907200, - }, - [CHAIN.POLYGON]: { - fetch: fetch(CHAIN.POLYGON), - start: 1647907200, - }, - [CHAIN.BSC]: { - fetch: fetch(CHAIN.BSC), - start: 1647907200, - }, - [CHAIN.AVAX]: { - fetch: fetch(CHAIN.AVAX), - start: 1647907200, - }, - [CHAIN.FANTOM]: { - fetch: fetch(CHAIN.FANTOM), - start: 1647907200, - }, - [CHAIN.ARBITRUM]: { - fetch: fetch(CHAIN.ARBITRUM), - start: 1647907200, - }, - [CHAIN.OPTIMISM]: { - fetch: fetch(CHAIN.OPTIMISM), - start: 1647907200, - } - } -} - -export default adapter; +export default getParaswapAdapter("fees"); \ No newline at end of file