Last active
April 12, 2023 01:25
-
-
Save togosh/4e3dfc23d2c88a8dcda9526137cd0d3e to your computer and use it in GitHub Desktop.
Liquidity of HEX, USDC, ETH, DAI trading pairs - Uniswap V2 & V3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://codeakk.medium.com/hex-development-data-a1b1822446fa | |
// https://togosh.medium.com/hex-developer-guide-3b018a943a55 | |
// https://thegraph.com/legacy-explorer/subgraph/uniswap/uniswap-v2 | |
// https://thegraph.com/legacy-explorer/subgraph/uniswap/uniswap-v3 | |
test(); | |
async function test(){ | |
console.log("=== Uniswap V2"); | |
var { liquidityUV2_HEXUSDC, liquidityUV2_USDC } = await getUniswapV2HEXUSDC_Polling(); await sleep(1000); | |
var { liquidityUV2_HEXETH, liquidityUV2_ETH } = await getUniswapV2HEXETH(); await sleep(1000); | |
console.log("=== Uniswap V3"); | |
var { liquidityUV3_HEX, liquidityUV3_USDC, liquidityUV3_ETH, liquidityUV3_DAI } = await getUniswapV3(); await sleep(1000); | |
var liquidityUV2UV3_HEX = parseInt(liquidityUV2_HEXUSDC + liquidityUV2_HEXETH + liquidityUV3_HEX); | |
var liquidityUV2UV3_USDC = parseInt(liquidityUV2_USDC + liquidityUV3_USDC); | |
var liquidityUV2UV3_ETH = parseInt(liquidityUV2_ETH + liquidityUV3_ETH); | |
var liquidityUV2V3_DAI = parseInt(liquidityUV3_DAI); | |
console.log("=== Total"); | |
console.log("Liquidity HEX: " + Number(liquidityUV2UV3_HEX).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0})); | |
console.log("Liquidity USDC: " + Number(liquidityUV2UV3_USDC).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0})); | |
console.log("Liquidity ETH: " + Number(liquidityUV2UV3_ETH).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0})); | |
console.log("Liquidity DAI: " + Number(liquidityUV2V3_DAI).toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:0})); | |
} | |
async function getUniswapV2HEXUSDC_Polling(){ | |
var count = 0; | |
while (true){ | |
var { liquidityUV2_HEXUSDC, liquidityUV2_USDC } = await getUniswapV2HEXUSDC(); | |
if ( liquidityUV2_HEXUSDC != 0 && liquidityUV2_USDC != 0){ | |
break; | |
} | |
count += 1; | |
if (count > 3) { | |
break; | |
} | |
console.log("getUniswapV2HEXUSDC_Polling() --- RETRY " + count); | |
await sleep(1000); | |
} | |
return { liquidityUV2_HEXUSDC, liquidityUV2_USDC }; | |
} | |
async function getUniswapV2HEXUSDC(){ | |
const UNISWAP_V2_HEXUSDC = "0xf6dcdce0ac3001b2f67f750bc64ea5beb37b5824"; | |
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ query: ` | |
query { | |
pairDayDatas ( | |
orderBy: id, orderDirection: desc, first: 1, | |
where: {pairAddress: "` + UNISWAP_V2_HEXUSDC + `"}){ | |
token0 { | |
symbol | |
} | |
token1 { | |
symbol | |
} | |
pairAddress | |
reserve0 | |
reserve1 | |
} | |
}` | |
}), | |
}) | |
.then(res => res.json()) | |
.then(res => { | |
try { | |
var pairDayData = res.data.pairDayDatas[0]; | |
console.log(UNISWAP_V2_HEXUSDC + " - " + "HEX" + " - " + parseInt(pairDayData.reserve0) + " / " + "USDC" + " - " + parseInt(pairDayData.reserve1)); | |
return { | |
liquidityUV2_HEXUSDC: parseInt(pairDayData.reserve0), | |
liquidityUV2_USDC: parseInt(pairDayData.reserve1), | |
} | |
} catch (error){ | |
return { | |
liquidityUV2_HEXUSDC: 0, | |
liquidityUV2_USDC: 0, | |
} | |
} | |
}); | |
} | |
async function getUniswapV2HEXETH(){ | |
const UNISWAP_V2_HEXETH = "0x55d5c232d921b9eaa6b37b5845e439acd04b4dba"; | |
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ query: ` | |
query { | |
pairDayDatas ( | |
orderBy: id, orderDirection: desc, first: 1, | |
where: {pairAddress: "` + UNISWAP_V2_HEXETH + `"}){ | |
token0 { | |
symbol | |
} | |
token1 { | |
symbol | |
} | |
pairAddress | |
reserve0 | |
reserve1 | |
} | |
}` | |
}), | |
}) | |
.then(res => res.json()) | |
.then(res => { | |
try { | |
var pairDayData = res.data.pairDayDatas[0]; | |
console.log(UNISWAP_V2_HEXETH + " - " + "HEX" + " - " + parseInt(pairDayData.reserve0) + " / " + "ETH" + " - " + parseInt(pairDayData.reserve1)); | |
return { | |
liquidityUV2_HEXETH: parseInt(pairDayData.reserve0), | |
liquidityUV2_ETH: parseInt(pairDayData.reserve1), | |
} | |
} catch (error){ | |
return { | |
liquidityUV2_HEXETH: 0, | |
liquidityUV2_ETH: 0, | |
} | |
} | |
}); | |
} | |
async function getUniswapV3Pools() { | |
const HEX_CONTRACT_ADDRESS = "0x2b591e99afe9f32eaa6214f7b7629768c40eeb39"; | |
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ query: ` | |
query { | |
token (id: "` + HEX_CONTRACT_ADDRESS + `") { | |
id | |
symbol | |
name | |
whitelistPools { | |
id | |
} | |
} | |
}` | |
}), | |
}) | |
.then(res => res.json()) | |
.then(res => { | |
pools = res.data.token.whitelistPools.map(function (obj) { | |
return obj.id; | |
}); | |
return pools; | |
}); | |
} | |
async function getUniswapV3() { | |
var pools = await getUniswapV3Pools(); await sleep(300); | |
if (pools == undefined) {return;} | |
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ query: ` | |
query { | |
pools ( | |
orderBy: volumeUSD, orderDirection: desc, | |
where: {id_in: ` + JSON.stringify(pools) + `} | |
){ | |
id | |
feeTier | |
token0 { | |
name | |
derivedETH | |
} | |
token1 { | |
name | |
derivedETH | |
} | |
liquidity | |
volumeToken0 | |
volumeToken1 | |
totalValueLockedToken0 | |
totalValueLockedToken1 | |
totalValueLockedUSD | |
volumeUSD | |
} | |
}` | |
}), | |
}) | |
.then(res => res.json()) | |
.then(res => { | |
var liquidityUV3_USDC = 0; | |
var liquidityUV3_ETH = 0; | |
var liquidityUV3_HEX = 0; | |
var liquidityUV3_DAI = 0; | |
for(var i = 0; i < res.data.pools.length; i++) { | |
var current = res.data.pools[i]; | |
const id = current.id | |
const token0Name = current.token0.name; | |
const token1Name = current.token1.name; | |
const feePercent = parseFloat(current.feeTier) / 10000 / 100; | |
const tvlAdjust0 = current.volumeToken0 ? (parseFloat(current.volumeToken0) * feePercent) / 2 : 0; | |
const tvlAdjust1 = current.volumeToken1 ? (parseFloat(current.volumeToken1) * feePercent) / 2 : 0; | |
const tvlToken0 = current ? parseFloat(current.totalValueLockedToken0) - tvlAdjust0 : 0; | |
const tvlToken1 = current ? parseFloat(current.totalValueLockedToken1) - tvlAdjust1 : 0; | |
console.log(id + " - " + token0Name + " - " + tvlToken0 + " / " + token1Name + " - " + tvlToken1); | |
if (token0Name == "HEX" && token1Name == "USD Coin") { | |
liquidityUV3_HEX += parseInt(tvlToken0); | |
liquidityUV3_USDC += parseInt(tvlToken1); | |
} | |
if (token0Name == "HEX" && token1Name == "Wrapped Ether") { | |
liquidityUV3_HEX += parseInt(tvlToken0); | |
liquidityUV3_ETH += parseInt(tvlToken1); | |
} | |
if (token0Name == "HEX" && token1Name == "Dai Stablecoin") { | |
liquidityUV3_HEX += parseInt(tvlToken0); | |
liquidityUV3_DAI += parseInt(tvlToken1); | |
} | |
} | |
return { | |
liquidityUV3_HEX: liquidityUV3_HEX, | |
liquidityUV3_USDC: liquidityUV3_USDC, | |
liquidityUV3_ETH: liquidityUV3_ETH, | |
liquidityUV3_DAI: liquidityUV3_DAI, | |
} | |
}); | |
} | |
function sleep(ms) { | |
return new Promise(resolve => setTimeout(resolve, ms)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment