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

TW-1149: Ghostnet / Modaynet errors #1007

Merged
merged 3 commits into from
Oct 24, 2023
Merged
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
40 changes: 23 additions & 17 deletions src/app/hooks/use-load-tokens-apy.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,45 @@ import { useEffect, useState } from 'react';
import { useDispatch } from 'react-redux';
import { forkJoin } from 'rxjs';

import { useTezos } from '../../lib/temple/front';
import { useUsdToTokenRatesSelector } from '../store/currency/selectors';
import { loadTokensApyActions } from '../store/d-apps/actions';
import { useUsdToTokenRatesSelector } from 'app/store/currency/selectors';
import { loadTokensApyActions } from 'app/store/d-apps/actions';
import {
fetchKUSDApy$,
fetchTzBtcApy$,
fetchUBTCApr$,
fetchUSDTApy$,
fetchUUSDCApr$,
fetchYOUApr$
} from '../store/d-apps/utils';
} from 'app/store/d-apps/utils';
import { useChainId, useTezos } from 'lib/temple/front';
import { TempleChainId } from 'lib/temple/types';

export const useTokensApyLoading = () => {
const dispatch = useDispatch();
const tezos = useTezos();
const chainId = useChainId(true)!;
const usdToTokenRates = useUsdToTokenRatesSelector();

const [tokensApy, setTokensApy] = useState({});

useEffect(() => {
const subscription = forkJoin([
fetchTzBtcApy$(),
fetchKUSDApy$(),
fetchUSDTApy$(),
fetchUUSDCApr$(tezos),
fetchUBTCApr$(tezos),
fetchYOUApr$(tezos, usdToTokenRates)
]).subscribe(responses => {
setTokensApy(Object.assign({}, ...responses));
});

return () => subscription.unsubscribe();
}, [usdToTokenRates]);
if (chainId === TempleChainId.Mainnet) {
const subscription = forkJoin([
fetchTzBtcApy$(),
fetchKUSDApy$(),
fetchUSDTApy$(),
fetchUUSDCApr$(tezos),
fetchUBTCApr$(tezos),
fetchYOUApr$(tezos, usdToTokenRates)
]).subscribe(responses => {
setTokensApy(Object.assign({}, ...responses));
});

return () => subscription.unsubscribe();
}

return;
}, [chainId, usdToTokenRates, tezos]);

useEffect(() => {
dispatch(loadTokensApyActions.success(tokensApy));
Expand Down
13 changes: 8 additions & 5 deletions src/app/hooks/use-metadata-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { useDispatch } from 'react-redux';

import { refreshTokensMetadataAction } from 'app/store/tokens-metadata/actions';
import { useTokensMetadataSelector } from 'app/store/tokens-metadata/selectors';
import { fetchTokensMetadata } from 'lib/apis/temple';
import { TokenMetadata } from 'lib/metadata';
import { fetchTokensMetadata } from 'lib/metadata/fetch';
import { buildTokenMetadataFromFetched } from 'lib/metadata/utils';
import { useChainId, useTezos } from 'lib/temple/front';
import { useChainId } from 'lib/temple/front';
import { TempleChainId } from 'lib/temple/types';
import { useLocalStorage } from 'lib/ui/local-storage';

const STORAGE_KEY = 'METADATA_REFRESH';
Expand All @@ -17,7 +18,6 @@ type RefreshRecords = Record<string, number>;
const REFRESH_VERSION = 1;

export const useMetadataRefresh = () => {
const tezos = useTezos();
const chainId = useChainId()!;
const dispatch = useDispatch();

Expand All @@ -38,8 +38,10 @@ export const useMetadataRefresh = () => {
return;
}

if (needToSetVersion)
fetchTokensMetadata(tezos.rpc.getRpcUrl(), slugsOnAppLoad)
if (!needToSetVersion) return;

if (chainId === TempleChainId.Mainnet) {
fetchTokensMetadata(chainId, slugsOnAppLoad)
lendihop marked this conversation as resolved.
Show resolved Hide resolved
.then(data =>
data.reduce<TokenMetadata[]>((acc, token, index) => {
const slug = slugsOnAppLoad[index]!;
Expand All @@ -57,5 +59,6 @@ export const useMetadataRefresh = () => {
},
error => console.error(error)
);
}
}, [chainId]);
};
5 changes: 1 addition & 4 deletions src/lib/metadata/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ export const fetchOneTokenMetadata = async (
return chainTokenMetadataToBase(metadataOnChain) || undefined;
};

export const fetchTokensMetadata = async (
rpcUrl: string,
slugs: string[]
): Promise<(TokenMetadataResponse | null)[]> => {
const fetchTokensMetadata = async (rpcUrl: string, slugs: string[]): Promise<(TokenMetadataResponse | null)[]> => {
if (slugs.length === 0) return [];

const tezos = new TezosToolkit(rpcUrl);
Expand Down
20 changes: 2 additions & 18 deletions src/lib/temple/front/baking.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useCallback, useMemo } from 'react';

import { HttpResponseError, HttpRequestFailed } from '@taquito/http-utils';
import BigNumber from 'bignumber.js';

import {
Expand All @@ -20,23 +19,8 @@ export function useDelegate(address: string, suspense = true) {
const getDelegate = useCallback(async () => {
try {
return await tezos.rpc.getDelegate(address);
} catch (error: unknown) {
if (error instanceof HttpResponseError) {
if (error.status === 404) return null;
}
if (error instanceof HttpRequestFailed) {
/*
`@taquito/http-utils` package uses `@vespaiach/axios-fetch-adapter`,
which throws a SyntaxError in case of 404 response.
See: https://github.com/vespaiach/axios-fetch-adapter/issues/25
*/
if (/SyntaxError(.*)JSON/.test(error.message)) {
console.error('Presumably, `@vespaiach/axios-fetch-adapter` SyntaxError (taken as 404):', { error });
return null;
}
}

throw error;
} catch {
return null;
lendihop marked this conversation as resolved.
Show resolved Hide resolved
}
}, [address, tezos]);

Expand Down
Loading