Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to new exchange subgraphs #903

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ export function useLegacyLiquidityPositionsBalances({ account, chainId }: Positi
const pair = pairs.find((pair: any) => pair.id === position.pair.id)
const pair1w = pairs1w?.find((pair: any) => pair.id === position.pair.id) ?? pair

// console.log(pair.volumeUSD - pair1w.volumeUSD, pairs1w, pair.reserveUSD)

return {
id: position.id,
assets: [pair.token0, pair.token1].map(
Expand All @@ -95,8 +93,8 @@ export function useLegacyLiquidityPositionsBalances({ account, chainId }: Positi
type: PoolType.ConstantProduct,
swapFeePercent: 0.3,
twapEnabled: true,
value: (position.liquidityTokenBalance / pair.totalSupply) * pair.reserveUSD,
apy: getApy({ volume: pair.volumeUSD - pair1w.volumeUSD, liquidity: pair.reserveUSD, days: 7 }),
value: (position.balance / pair.liquidity) * pair.liquidityUSD,
apy: getApy({ volume: pair.volumeUSD - pair1w.volumeUSD, liquidity: pair.liquidityUSD, days: 7 }),
legacy: true,
}
})
Expand Down
11 changes: 6 additions & 5 deletions src/features/analytics/bentobox/getAnalyticsBentobox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export default async function getAnalyticsBentobox({ chainId }: getAnalyticsBent
})

// Creating map to easily reference TokenId -> Token
const tokenIdToPrice: Map<string, { derivedETH: number; volumeUSD: number; dayData: Array<{ priceUSD: number }> }> =
new Map(tokens?.map((token: any) => [token.id, token]))
const tokenIdToPrice: Map<string, { price: { derivedNative: number }; volumeUSD: number }> = new Map(
tokens?.map((token: any) => [token.id, token])
)

const bentoBoxTvl: number = bentoBoxTokens
.map(({ id, rebase }: any) => {
const token = tokenIdToPrice.get(id)
return (token?.derivedETH ?? 0) * nativePrice * rebase.elastic
return (token?.price.derivedNative ?? 0) * nativePrice * rebase.elastic
})
.filter(Boolean)
.reduce((previousValue: any, currentValue: any) => previousValue + currentValue, 0)
Expand All @@ -45,8 +46,8 @@ export default async function getAnalyticsBentobox({ chainId }: getAnalyticsBent
.map(({ id, rebase, decimals, symbol, name }: any) => {
const token = tokenIdToPrice.get(id)
const supply = rebase.elastic
const tokenDerivedETH = token?.derivedETH
const price = (tokenDerivedETH ?? 0) * nativePrice
const tokenDerivedNative = token?.price.derivedNative
const price = (tokenDerivedNative ?? 0) * nativePrice
const tvl = price * supply

const strategy = strategies?.find((strategy) => strategy.token === id)
Expand Down
17 changes: 9 additions & 8 deletions src/features/analytics/hooks/useFarmRewardHistories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function useFarmRewardHistories({
masterChefV1TotalAllocPoint,
masterChefV1SushiPerBlock,
sushiPrice,
ethPrice,
nativePrice,
maticPrice,
gnoPrice,
onePrice,
Expand Down Expand Up @@ -73,7 +73,7 @@ export default function useFarmRewardHistories({
masterChefV1SushiPerBlock,
masterChefV1TotalAllocPoint,
sushiPrice,
ethPrice,
nativePrice,
maticPrice,
gnoPrice,
onePrice,
Expand All @@ -90,16 +90,17 @@ export default function useFarmRewardHistories({
: poolHistory.balance / 10 ** kashiPair.token0.decimals

const tvl = swapPair
? (balance / Number(swapPair.totalSupply)) * Number(swapPair.reserveUSD)
: balance * kashiPair.token0.derivedETH * ethPrice
? (balance / Number(swapPair.liquidity)) * Number(swapPair.liquidityUSD)
: balance * kashiPair.token0.price.derivedNative * nativePrice

const tvlHistory = swapPair
? (balanceHistory / Number(swapPair.totalSupply)) * Number(swapPair.reserveUSD)
: balanceHistory * kashiPair.token0.derivedETH * ethPrice
? (balanceHistory / Number(swapPair.liquidity)) * Number(swapPair.liquidityUSD)
: balanceHistory * kashiPair.token0.price.derivedNative * nativePrice

const feeApyPerYear =
swapPair && swapPair1d
? aprToApy((((pair?.volumeUSD - swapPair1d?.volumeUSD) * 0.0025 * 365) / pair?.reserveUSD) * 100, 3650) / 100
? aprToApy((((pair?.volumeUSD - swapPair1d?.volumeUSD) * 0.0025 * 365) / pair?.liquidityUSD) * 100, 3650) /
100
: 0

const feeApyPerMonth = feeApyPerYear / 12
Expand Down Expand Up @@ -163,7 +164,6 @@ export default function useFarmRewardHistories({
blocksPerDay,
celoPrice,
chainId,
ethPrice,
fantomPrice,
fusePrice,
glimmerPrice,
Expand All @@ -173,6 +173,7 @@ export default function useFarmRewardHistories({
masterChefV1TotalAllocPoint,
maticPrice,
movrPrice,
nativePrice,
onePrice,
positions,
sushiPrice,
Expand Down
33 changes: 17 additions & 16 deletions src/features/analytics/hooks/useFarmRewardsWithUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { useAllTokens } from 'app/hooks/Tokens'
import {
useAverageBlockTime,
useCeloPrice,
useEthPrice,
useFantomPrice,
useFarmsWithUsers,
useFusePrice,
Expand All @@ -21,6 +20,7 @@ import {
useMasterChefV1TotalAllocPoint,
useMaticPrice,
useMovrPrice,
useNativePrice,
useOneDayBlock,
useOnePrice,
useSushiPairs,
Expand All @@ -38,7 +38,7 @@ export function getRewards({
masterChefV1SushiPerBlock,
masterChefV1TotalAllocPoint,
sushiPrice,
ethPrice,
nativePrice,
maticPrice,
gnoPrice,
onePrice,
Expand All @@ -57,7 +57,7 @@ export function getRewards({
masterChefV1SushiPerBlock: any
masterChefV1TotalAllocPoint: any
sushiPrice: any
ethPrice: any
nativePrice: any
maticPrice: any
gnoPrice: any
onePrice: any
Expand Down Expand Up @@ -102,7 +102,7 @@ export function getRewards({
pool.rewarder.rewardToken = '0xba8a621b4a54e61c442f5ec623687e2a942225ef'
pool.rewardToken.id = '0xba8a621b4a54e61c442f5ec623687e2a942225ef'
pool.rewardToken.symbol = 'vestedQUARTZ'
pool.rewardToken.derivedETH = pair.token1.derivedETH
pool.rewardToken.price.derivedNative = pair.token1.price.derivedNative
pool.rewardToken.decimals = 18
}

Expand All @@ -123,7 +123,7 @@ export function getRewards({
? (0 / decimals) * averageBlockTime * blocksPerDay
: (pool.rewarder.rewardPerSecond / decimals) * averageBlockTime * blocksPerDay

const rewardPrice = pool.rewardToken.derivedETH * ethPrice
const rewardPrice = pool.rewardToken.price.derivedNative * nativePrice

const address = getAddress(pool.rewardToken.id)

Expand Down Expand Up @@ -230,7 +230,7 @@ export function getRewards({
: ((pool.allocPoint / pool.rewarder.totalAllocPoint) * pool.rewarder.rewardPerSecond) / 1e18
const rewardPerBlock = rewardPerSecond * averageBlockTime
const rewardPerDay = rewardPerBlock * blocksPerDay
const rewardPrice = pool.rewardToken.derivedETH * ethPrice
const rewardPrice = pool.rewardToken.price.derivedNative * nativePrice

const address = getAddress(pool.rewardToken.id)

Expand Down Expand Up @@ -292,7 +292,7 @@ export function useRewardCalculationData(chainId: number, farmAddresses: any) {
const { data: masterChefV1SushiPerBlock } = useMasterChefV1SushiPerBlock()

const { data: sushiPrice } = useSushiPrice()
const { data: ethPrice } = useEthPrice()
const { data: nativePrice } = useNativePrice({ chainId })
const { data: maticPrice } = useMaticPrice()
const { data: gnoPrice } = useGnoPrice()
const { data: onePrice } = useOnePrice()
Expand All @@ -314,7 +314,7 @@ export function useRewardCalculationData(chainId: number, farmAddresses: any) {
masterChefV1TotalAllocPoint,
masterChefV1SushiPerBlock,
sushiPrice,
ethPrice,
nativePrice,
maticPrice,
gnoPrice,
onePrice,
Expand Down Expand Up @@ -346,7 +346,7 @@ export default function useFarmRewardsWithUsers({
masterChefV1TotalAllocPoint,
masterChefV1SushiPerBlock,
sushiPrice,
ethPrice,
nativePrice,
maticPrice,
gnoPrice,
onePrice,
Expand Down Expand Up @@ -389,7 +389,7 @@ export default function useFarmRewardsWithUsers({
masterChefV1SushiPerBlock,
masterChefV1TotalAllocPoint,
sushiPrice,
ethPrice,
nativePrice,
maticPrice,
gnoPrice,
onePrice,
Expand All @@ -403,12 +403,13 @@ export default function useFarmRewardsWithUsers({
const balance = swapPair ? Number(pool.balance / 1e18) : pool.balance / 10 ** kashiPair.token0.decimals

const tvl = swapPair
? (balance / Number(swapPair.totalSupply)) * Number(swapPair.reserveUSD)
: balance * kashiPair.token0.derivedETH * ethPrice
? (balance / Number(swapPair.totalSupply)) * Number(swapPair.liquidityUSD)
: balance * kashiPair.token0.price.derivedNative * nativePrice

const feeApyPerYear =
swapPair && swapPair1d
? aprToApy((((pair?.volumeUSD - swapPair1d?.volumeUSD) * 0.0025 * 365) / pair?.reserveUSD) * 100, 3650) / 100
? aprToApy((((pair?.volumeUSD - swapPair1d?.volumeUSD) * 0.0025 * 365) / pair?.liquidityUSD) * 100, 3650) /
100
: 0

const feeApyPerMonth = feeApyPerYear / 12
Expand Down Expand Up @@ -440,8 +441,8 @@ export default function useFarmRewardsWithUsers({
share: (amount * 100) / balance,
amount,
amountUSD: swapPair
? (amount / Number(swapPair.totalSupply)) * Number(swapPair.reserveUSD)
: amount * kashiPair.token0.derivedETH * ethPrice,
? (amount / Number(swapPair.liquidity)) * Number(swapPair.liquidityUSD)
: amount * kashiPair.token0.price.derivedNative * nativePrice,
}
})

Expand Down Expand Up @@ -478,7 +479,6 @@ export default function useFarmRewardsWithUsers({
blocksPerDay,
celoPrice,
chainId,
ethPrice,
fantomPrice,
fusePrice,
glimmerPrice,
Expand All @@ -488,6 +488,7 @@ export default function useFarmRewardsWithUsers({
masterChefV1TotalAllocPoint,
maticPrice,
movrPrice,
nativePrice,
onePrice,
positions,
sushiPrice,
Expand Down
15 changes: 6 additions & 9 deletions src/features/analytics/hooks/useTokensAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,15 @@ export default function useTokensAnalytics({ chainId = ChainId.ETHEREUM }) {
name: token.name,
decimals: token.decimals,
},
liquidity: token.liquidity * token.derivedETH * nativePrice,
liquidity: token.liquidityUSD,
volume1d: token.volumeUSD - token1d.volumeUSD,
volume1w: token.volumeUSD - token1w.volumeUSD,
price: token.derivedETH * nativePrice,
price: token.price.derivedNative * nativePrice,
strategy,
change1d: ((token.derivedETH * nativePrice) / (token1d.derivedETH * nativePrice1d)) * 100 - 100,
change1w: ((token.derivedETH * nativePrice) / (token1w.derivedETH * nativePrice1w)) * 100 - 100,
graph: token.dayData
.slice(0)
.reverse()
// @ts-ignore TYPE NEEDS FIXING
.map((day, i) => ({ x: i, y: Number(day.priceUSD) })),
change1d:
((token.price.derivedNative * nativePrice) / (token1d.price.derivedNative * nativePrice1d)) * 100 - 100,
change1w:
((token.price.derivedNative * nativePrice) / (token1w.price.derivedNative * nativePrice1w)) * 100 - 100,
}
}),
[
Expand Down
22 changes: 11 additions & 11 deletions src/features/analytics/pools/getAnalyticsPair.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainId } from '@sushiswap/core-sdk'
import { getBlockDaysAgo, getNativePrice, getPairDayData, getPairs } from 'app/services/graph'
import { getBlockDaysAgo, getNativePrice, getPairDaySnapshots, getPairs } from 'app/services/graph'

export type AnalyticsPair = Awaited<ReturnType<typeof getAnalyticsPair>>

Expand All @@ -10,12 +10,12 @@ export default async function getAnalyticsPair(chainId: ChainId, id: string) {
getPairs(chainId, { where: { id: id.toLowerCase() } }),
getPairs(chainId, { where: { id: id.toLowerCase() }, block: block1d }),
getPairs(chainId, { where: { id: id.toLowerCase() }, block: block2d }),
getPairDayData(chainId, { where: { pair: id.toLowerCase() } }),
getPairDaySnapshots(chainId, { where: { pair: id.toLowerCase() } }),
getNativePrice(chainId),
])

const liquidityUSD = pair.reserveUSD
const liquidityUSD1dChange = (pair.reserveUSD / pair1d.reserveUSD) * 100 - 100
const liquidityUSD = pair.liquidityUSD
const liquidityUSD1dChange = (pair.liquidityUSD / pair1d.liquidityUSD) * 100 - 100

const volumeUSD1d = pair.volumeUSD - pair1d.volumeUSD
const volumeUSD2d = pair1d.volumeUSD - pair2d.volumeUSD
Expand All @@ -29,14 +29,14 @@ export default async function getAnalyticsPair(chainId: ChainId, id: string) {
const avgTrade2d = volumeUSD2d / tx2d
const avgTrade1dChange = (avgTrade1d / avgTrade2d) * 100 - 100

const utilisation1d = (volumeUSD1d / pair.reserveUSD) * 100
const utilisation2d = (volumeUSD2d / pair1d.reserveUSD) * 100
const utilisation1d = (volumeUSD1d / pair.liquidityUSD) * 100
const utilisation2d = (volumeUSD2d / pair1d.liquidityUSD) * 100
const utilisation1dChange = (utilisation1d / utilisation2d) * 100 - 100

const chartData = {
liquidity: pairDayData
.sort((a: any, b: any) => a.date - b.date)
.map((day: any) => ({ x: Number(day.date), y: Number(day.reserveUSD) })),
.map((day: any) => ({ x: Number(day.date), y: Number(day.liquidityUSD) })),
volume: pairDayData
.sort((a: any, b: any) => a.date - b.date)
.map((day: any) => ({ x: Number(day.date), y: Number(day.volumeUSD) })),
Expand All @@ -46,14 +46,14 @@ export default async function getAnalyticsPair(chainId: ChainId, id: string) {
token0: {
...pair.token0,
price: pair.token1Price,
reserve: pair.reserve0,
derivedUSD: pair.token0.derivedETH * nativePrice,
reserve: pair.reserve0 / 10 ** pair.token0.decimals,
derivedUSD: pair.token0.price.derivedNative * nativePrice,
},
token1: {
...pair.token1,
price: pair.token0Price,
reserve: pair.reserve1,
derivedUSD: pair.token1.derivedETH * nativePrice,
reserve: pair.reserve1 / 10 ** pair.token1.decimals,
derivedUSD: pair.token1.price.derivedNative * nativePrice,
},
liquidityUSD,
liquidityUSD1dChange,
Expand Down
12 changes: 6 additions & 6 deletions src/features/analytics/pools/getAnalyticsPairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ export default async function getAnalyticsPairs({ chainId, first }: getAnalytics
const fees1d = volume1d * 0.003
const fees1w = volume1w * 0.003

const liquidity = pair.reserveUSD
const liquidity1d = pair1d.reserveUSD
const liquidity = pair.liquidityUSD
const liquidity1d = pair1d.liquidityUSD

const liquidity1dChange = pair.reserveUSD - pair1d.reserveUSD
const liquidity1dChangePercent = (pair.reserveUSD / pair1d.reserveUSD) * 100 - 100 ?? 0
const liquidity1dChange = pair.liquidityUSD - pair1d.liquidityUSD
const liquidity1dChangePercent = (pair.liquidityUSD / pair1d.liquidityUSD) * 100 - 100 ?? 0

const utilisation1d = ((pair.volumeUSD - pair1d.volumeUSD) / pair?.reserveUSD) * 100 ?? 0
const utilisation2d = ((pair1d.volumeUSD - pair2d.volumeUSD) / pair1d?.reserveUSD) * 100 ?? 0
const utilisation1d = ((pair.volumeUSD - pair1d.volumeUSD) / pair?.liquidityUSD) * 100 ?? 0
const utilisation2d = ((pair1d.volumeUSD - pair2d.volumeUSD) / pair1d?.liquidityUSD) * 100 ?? 0
const utilisation1dChange = (utilisation1d / utilisation2d) * 100 - 100 ?? 0

const tx1d = pair.txCount - pair1d.txCount
Expand Down
15 changes: 6 additions & 9 deletions src/features/analytics/tokens/getAnalyticsTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,15 @@ export default async function getAnalyticsTokens({ chainId, first }: getAnalytic
name: token.name,
decimals: token.decimals,
},
liquidity: token.liquidity * token.derivedETH * nativePrice,
liquidity: token.liquidityUSD,
volume1d: token.volumeUSD - token1d.volumeUSD,
volume1w: token.volumeUSD - token1w.volumeUSD,
price: token.derivedETH * nativePrice,
price: token.price.derivedNative * nativePrice,
strategy,
change1d: ((token.derivedETH * nativePrice) / (token1d.derivedETH * nativePrice1d)) * 100 - 100,
change1w: ((token.derivedETH * nativePrice) / (token1w.derivedETH * nativePrice1w)) * 100 - 100,
graph: token.dayData
.slice(0)
.reverse()
// @ts-ignore TYPE NEEDS FIXING
.map((day, i) => ({ x: i, y: Number(day.priceUSD) })),
change1d:
((token.price.derivedNative * nativePrice) / (token1d.price.derivedNative * nativePrice1d)) * 100 - 100,
change1w:
((token.price.derivedNative * nativePrice) / (token1w.price.derivedNative * nativePrice1w)) * 100 - 100,
}
})
)
Expand Down
6 changes: 3 additions & 3 deletions src/features/analytics/xsushi/getAnalyticsXsushi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default async function getAnalyticsXsushi() {
getBlockDaysAgo(ChainId.ETHEREUM, 365),
])

const [ethPrice, [xSushi], bar, bar1m, bar3m, bar6m, bar1y, barHistory] = await Promise.all([
const [nativePrice, [xSushi], bar, bar1m, bar3m, bar6m, bar1y, barHistory] = await Promise.all([
getNativePrice(ChainId.ETHEREUM),
getTokens(ChainId.ETHEREUM, { first: 1, where: { id: XSUSHI.address.toLowerCase() } }),
getBar(),
Expand All @@ -33,8 +33,8 @@ export default async function getAnalyticsXsushi() {
const apy1y = (bar.ratio / bar1y.ratio - 1) * 100

const [xSushiPrice, xSushiMarketcap] = [
xSushi?.derivedETH * ethPrice,
xSushi?.derivedETH * ethPrice * bar?.totalSupply,
xSushi?.price.derivedNative * nativePrice,
xSushi?.price.derivedNative * nativePrice * bar?.totalSupply,
]

const chart = barHistory.map((barDay: any, i: number) => {
Expand Down
Loading