Skip to content

Commit

Permalink
Perps V1: Switch to testnet subgraph (#1852)
Browse files Browse the repository at this point in the history
* switch to testnet subgraph

* remove import

* fix 24h price
  • Loading branch information
Tburm authored Jan 10, 2023
1 parent a96397b commit 37accff
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 31 deletions.
4 changes: 2 additions & 2 deletions components/TVChart/DataFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const fetchCombinedCandles = async (
const baseCurrencyIsSUSD = base === 'sUSD';
const quoteCurrencyIsSUSD = quote === 'sUSD';
const baseDataPromise = requestCandlesticks(
base,
getDisplayAsset(base),
from,
to,
resolutionToSeconds(resolution),
Expand Down Expand Up @@ -88,7 +88,7 @@ const fetchLastCandle = async (
const from = 0;

const baseDataPromise = requestCandlesticks(
base,
getDisplayAsset(base),
from,
to,
resolutionToSeconds(resolution),
Expand Down
2 changes: 1 addition & 1 deletion queries/rates/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { chain } from 'wagmi';
export const RATES_ENDPOINT_MAIN = 'https://api.thegraph.com/subgraphs/name/kwenta/mainnet-main';

export const RATES_ENDPOINT_OP_MAINNET =
'https://api.thegraph.com/subgraphs/name/kwenta/optimism-main';
'https://api.thegraph.com/subgraphs/name/kwenta/optimism-goerli-main';

export const RATES_ENDPOINT_OP_GOERLI =
'https://api.thegraph.com/subgraphs/name/kwenta/optimism-goerli-main';
Expand Down
2 changes: 1 addition & 1 deletion queries/rates/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type Candle = {
export type Candles = Candle[];

export type LatestRate = {
id: string;
synth: string;
rate: Wei;
};

Expand Down
50 changes: 27 additions & 23 deletions queries/rates/useLaggedDailyPrice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NetworkId } from '@synthetixio/contracts-interface';
import EthDater from 'ethereum-block-by-date';
import request, { gql } from 'graphql-request';
import { values } from 'lodash';
import { useQuery, UseQueryOptions } from 'react-query';
Expand All @@ -8,21 +7,22 @@ import { useSetRecoilState } from 'recoil';
import QUERY_KEYS from 'constants/queryKeys';
import ROUTES from 'constants/routes';
import Connector from 'containers/Connector';
import { getDisplayAsset } from 'sdk/utils/futures';
import { selectMarketAssets } from 'state/futures/selectors';
import { useAppSelector } from 'state/hooks';
import { pastRatesState } from 'store/futures';
import logError from 'utils/logError';

import { RATES_ENDPOINT_OP_MAINNET } from './constants';
import { Price } from './types';
import { LatestRate, Price } from './types';
import { getRatesEndpoint, mapLaggedDailyPrices } from './utils';

const useLaggedDailyPrice = (options?: UseQueryOptions<Price[] | null>) => {
const { provider, network, synthsMap } = Connector.useContainer();
const { network, synthsMap } = Connector.useContainer();
const marketAssets = useAppSelector(selectMarketAssets);
const setPastRates = useSetRecoilState(pastRatesState);

const minTimestamp = Math.floor(Date.now()) - 60 * 60 * 24 * 1000;
const minTimestamp = Math.floor((Date.now() - 60 * 60 * 24 * 1000) / 1000);
const synths = [...marketAssets, ...values(synthsMap).map(({ name }) => name)];

const ratesEndpoint =
Expand All @@ -33,32 +33,36 @@ const useLaggedDailyPrice = (options?: UseQueryOptions<Price[] | null>) => {
return useQuery<Price[] | null>(
QUERY_KEYS.Rates.PastRates(network?.id as NetworkId, synths),
async () => {
if (!provider) return null;
const dater = new EthDater(provider);

const block = await dater.getDate(minTimestamp, true, false);

try {
const rateUpdateQueries = synths.map((synth) => {
return gql`
# last before timestamp
${synth}: rateUpdates(
first: 1
where: { synth: "${getDisplayAsset(synth) ?? synth}", timestamp_gte: $minTimestamp }
orderBy: timestamp
orderDirection: asc
) {
synth
rate
}
`;
});

const response = await request(
ratesEndpoint,
gql`
query latestRates($synths: [String!]!) {
latestRates(
where: {
id_in: $synths
}
block: { number: ${block.block} }
) {
id
rate
}
}
`,
query rateUpdates($minTimestamp: BigInt!) {
${rateUpdateQueries.reduce((acc: string, curr: string) => {
return acc + curr;
})}
}`,
{
synths: synths,
minTimestamp: minTimestamp,
}
);
const pastRates = response ? mapLaggedDailyPrices(response.latestRates) : [];
const latestRates = (response ? Object.values(response).flat() : []) as LatestRate[];
const pastRates = mapLaggedDailyPrices(latestRates);

setPastRates(pastRates);
return pastRates;
Expand Down
4 changes: 2 additions & 2 deletions queries/rates/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export const getRatesEndpoint = (networkId: NetworkId): string => {
export const mapLaggedDailyPrices = (rates: LatestRate[]): Prices => {
return rates.map((rate) => {
return {
synth: rate.id,
synth: rate.synth,
price:
rate.id === 'DebtRatio'
rate.synth === 'DebtRatio'
? wei(rate.rate).div(DEBT_RATIO_UNIT).toNumber()
: wei(rate.rate).toNumber(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { DEFAULT_CRYPTO_DECIMALS } from 'constants/defaults';
import ROUTES from 'constants/routes';
import Connector from 'containers/Connector';
import { FundingRateResponse } from 'sdk/types/futures';
import { getDisplayAsset } from 'sdk/utils/futures';
import {
selectAverageFundingRates,
selectFuturesType,
Expand All @@ -40,7 +41,7 @@ const FuturesMarketsTable: FC = () => {
return futuresMarkets.map((market) => {
const description = getSynthDescription(market.asset, synthsMap, t);
const volume = futuresVolumes[market.assetHex]?.volume;
const pastPrice = pastRates.find((price) => price.synth === market.asset);
const pastPrice = pastRates.find((price) => price.synth === getDisplayAsset(market.asset));
const fundingRate = fundingRates.find(
(funding) => (funding as FundingRateResponse)?.asset === MarketKeyByAsset[market.asset]
);
Expand Down
3 changes: 2 additions & 1 deletion sections/futures/MarketDetails/useGetMarketData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DEFAULT_CRYPTO_DECIMALS } from 'constants/defaults';
import { NO_VALUE } from 'constants/placeholder';
import useSelectedPriceCurrency from 'hooks/useSelectedPriceCurrency';
import useExternalPriceQuery from 'queries/rates/useExternalPriceQuery';
import { getDisplayAsset } from 'sdk/utils/futures';
import {
selectFundingRate,
selectMarketAsset,
Expand Down Expand Up @@ -44,7 +45,7 @@ const useGetMarketData = (mobile?: boolean) => {
? DEFAULT_CRYPTO_DECIMALS
: undefined;

const pastPrice = pastRates.find((price) => price.synth === marketAsset);
const pastPrice = pastRates.find((price) => price.synth === getDisplayAsset(marketAsset));

const fundingTitle = useMemo(
() => `${fundingRate?.fundingTitle ?? t('futures.market.info.hourly-funding')}`,
Expand Down

0 comments on commit 37accff

Please sign in to comment.