Skip to content

Commit

Permalink
Merge pull request #165 from dappradar/Ethereum_Uniswap_Subgraph
Browse files Browse the repository at this point in the history
Ethereum uniswap subgraph
  • Loading branch information
Sonmezturk authored Jul 7, 2023
2 parents 23fac58 + f7b4d47 commit ee39eb3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'path';
import * as dotenv from 'dotenv';
dotenv.config();
dotenv.config({ path: `.env.${process.env.APP_ENV || 'dev'}`, override: true });
dotenv.config({ path: `.env.${process.env.APP_ENV || 'dev'}` });

const {
HOST = '127.0.0.1',
Expand Down
2 changes: 1 addition & 1 deletion src/factory/providers/arbitrum/sushiswapv3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
});
requestResult.pools.forEach((pool) => {
poolBalances[pool.id] = {
tokens: [pool.token0, pool.token1],
tokens: [pool.token0.id, pool.token1.id],
balances: [
BigNumber(pool.totalValueLockedToken0)
.shiftedBy(Number(pool.token0.decimals))
Expand Down
109 changes: 82 additions & 27 deletions src/factory/providers/ethereum/uniswap/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,97 @@
import BigNumber from 'bignumber.js';
import { ITvlParams, ITvlReturn } from '../../../../interfaces/ITvl';
import uniswapV2 from '../../../../util/calculators/uniswapV2';
import { request } from 'graphql-request';
import formatter from '../../../../util/formatter';

const START_BLOCK = 10000835;
const V2_FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f';

const QUERY_SIZE = 1000;
const MAX_LIMIT = 5000;
const SUBGRAPH_ENDPOINT =
'https://api.thegraph.com/subgraphs/name/ianlapham/uniswap-v2-dev';
const WETH = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
const GET_PAIRS = `
query getPairs($block: Int!, $skip: Int!) {
pairs(
block: { number: $block }
orderBy: txCount
orderDirection: desc
first: ${QUERY_SIZE}
skip: $skip
where: {
reserveUSD_not: "0"
}
) {
token0 {
id
name
decimals
}
token1 {
id
name
decimals
}
reserve0
reserve1
reserveUSD
reserveETH
txCount
volumeUSD
}
}
`;
async function tvl(params: ITvlParams): Promise<Partial<ITvlReturn>> {
const { block, chain, provider, web3 } = params;

if (block < START_BLOCK) {
return {};
}

const v2 = await uniswapV2.getTvl(
V2_FACTORY_ADDRESS,
block,
chain,
provider,
web3,
);

const balances = {};
for (const token in v2.balances) {
const address = token.toLowerCase();
if (!balances[address]) {
balances[address] = BigNumber(0);
}
balances[address] = balances[address].plus(v2.balances[token]);
}

for (const token in balances) {
if (balances[token].isLessThan(10_000_000_000)) {
delete balances[token];
} else {
balances[token] = balances[token].toFixed();
}
const promises = [];
let skip = 0;
while (MAX_LIMIT >= skip) {
promises.push(
request(SUBGRAPH_ENDPOINT, GET_PAIRS, {
block: block,
skip: skip,
}),
);
skip += QUERY_SIZE;
}

const requestResult = await Promise.all(promises);
const balances = {};
requestResult.forEach((result, index) => {
result.pairs.forEach((pair) => {
if (Number(pair.txCount) < 100000 || Number(pair.volumeUSD) < 100000) {
balances[WETH] = BigNumber(pair.reserveETH)
.shiftedBy(18)
.plus(balances[WETH] || '0')
.toFixed();
} else {
if (balances[pair.token0.id]) {
balances[pair.token0.id] = BigNumber(pair.reserve0)
.shiftedBy(Number(pair.token0.decimals))
.plus(balances[pair.token0.id])
.toFixed();
} else {
balances[pair.token0.id] = BigNumber(pair.reserve0)
.shiftedBy(Number(pair.token0.decimals))
.toFixed();
}
if (balances[pair.token1.id]) {
balances[pair.token1.id] = BigNumber(pair.reserve1)
.shiftedBy(Number(pair.token1.decimals))
.plus(balances[pair.token1.id])
.toFixed();
} else {
balances[pair.token1.id] = BigNumber(pair.reserve1)
.shiftedBy(Number(pair.token1.decimals))
.toFixed();
}
}
});
});
formatter.convertBalancesToFixed(balances);
return { balances };
}

Expand Down

0 comments on commit ee39eb3

Please sign in to comment.