Skip to content

Instantly share code, notes, and snippets.

@togosh
Last active June 25, 2023 09:16
Show Gist options
  • Save togosh/724a9e01f9eda1c3213152de10b388a6 to your computer and use it in GitHub Desktop.
Save togosh/724a9e01f9eda1c3213152de10b388a6 to your computer and use it in GitHub Desktop.
Price of HEX on Uniswap V2 & V3
// 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(){
var priceUV2 = await getUniswapV2HEXDailyPrice();
var priceUV3 = await getUniswapV3HEXDailyPrice();
console.log("HEX Price Uniswap V2: " + Number(priceUV2).toLocaleString(undefined, {minimumFractionDigits:4, maximumFractionDigits:4}));
console.log("HEX Price Uniswap V3: " + Number(priceUV3).toLocaleString(undefined, {minimumFractionDigits:4, maximumFractionDigits:4}));
}
async function getUniswapV2HEXDailyPrice(){
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `
query {
tokenDayDatas (
first: 1,
orderBy: date,
orderDirection: desc,
where: {
token: "0x2b591e99afe9f32eaa6214f7b7629768c40eeb39"
})
{
date
token { symbol }
priceUSD
}
}`
}),
})
.then(res => res.json())
.then(res => {
var tokenDayData = res.data.tokenDayDatas[0];
return parseFloat(tokenDayData.priceUSD);
});
}
async function getUniswapV3HEXDailyPrice(){
return await fetch('https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: `
query {
tokenDayDatas (
first: 1,
orderBy: date,
orderDirection: desc,
where: {
token: "0x2b591e99afe9f32eaa6214f7b7629768c40eeb39"
})
{
date
token { symbol }
priceUSD
}
}`
}),
})
.then(res => res.json())
.then(res => {
var tokenDayData = res.data.tokenDayDatas[0];
return parseFloat(tokenDayData.priceUSD);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment