Skip to content

Commit

Permalink
univ2
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed May 14, 2024
1 parent 4ad0261 commit ce47a82
Showing 1 changed file with 64 additions and 8 deletions.
72 changes: 64 additions & 8 deletions protocols/uniswap/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ADDRESSES from '../../helpers/coreAssets.json'
import { Chain } from "@defillama/sdk/build/general";
import { BreakdownAdapter, FetchResultGeneric, BaseAdapter } from "../../adapters/types";
import { BreakdownAdapter, FetchResultGeneric, BaseAdapter, FetchOptions } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getStartTimestamp } from "../../helpers/getStartTimestamp";
import * as sdk from "@defillama/sdk";
Expand All @@ -11,6 +11,7 @@ import {
DEFAULT_DAILY_VOLUME_FACTORY,
DEFAULT_TOTAL_VOLUME_FIELD,
} from "../../helpers/getUniSubgraph"
import request, { gql } from 'graphql-request';

const v1Endpoints = {
[CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/ianlapham/uniswap",
Expand Down Expand Up @@ -170,6 +171,61 @@ const v2Deployments = {
zora: '0x0F797dC7efaEA995bB916f268D919d0a1950eE3C'
}

const chainv2mapping: any = {
[CHAIN.ARBITRUM]: "ARBITRUM",
[CHAIN.ETHEREUM]: "ETHEREUM",
[CHAIN.OPTIMISM]: "OPTIMISM",
[CHAIN.POLYGON]: "POLYGON",
[CHAIN.BASE]: "BASE",
[CHAIN.BSC]: "BNB",
}

const fetchV2 = async (options: FetchOptions) => {
interface IGraphResponse {
v2HistoricalProtocolVolume: Array<{
id: string
timestamp: number
value: string
__typename: string
}>
}
const url = 'https://interface.gateway.uniswap.org/v1/graphql';
const query = gql`query getVolume($chain: Chain!, $duration: HistoryDuration!) {
v2HistoricalProtocolVolume: historicalProtocolVolume(
chain: $chain
version: V2
duration: $duration
) {
id
timestamp
value
__typename
}
}`;
try {
const response:IGraphResponse = await request(url, query, { chain: chainv2mapping[options.chain], duration: "MONTH" }, {
'accept': '*/*',
'accept-language': 'th,en-US;q=0.9,en;q=0.8',
'cache-control': 'no-cache',
'content-type': 'application/json',
'origin': 'https://app.uniswap.org',
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': 'https://app.uniswap.org/',
'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'sec-ch-ua-mobile': '?0',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
});
const dailyVolume = response.v2HistoricalProtocolVolume.find((item) => item.timestamp === options.startOfDay)?.value;
return {dailyVolume: dailyVolume}
} catch (e) {
console.error(e)
return {
dailyVolume: "0"
}
}
}


const adapter: BreakdownAdapter = {
version: 2,
Expand Down Expand Up @@ -222,13 +278,13 @@ const adapter: BreakdownAdapter = {
methodology
},
},
// ...Object.keys(v2Deployments).reduce((acc, chain) => {
// acc[chain] = {
// fetch: getUniV2LogAdapter({factory: v2Deployments[chain]}),
// start: 0,
// }
// return acc
// }, {})
...Object.keys(chainv2mapping).reduce((acc, chain) => {
acc[chain] = {
fetch: fetchV2,
start: 0,
}
return acc
}, {})
},
v3: Object.keys(v3Endpoints).reduce((acc, chain) => {
acc[chain] = {
Expand Down

0 comments on commit ce47a82

Please sign in to comment.