Skip to content

Commit

Permalink
Merge branch 'development' of https://github.com/madfish-solutions/te…
Browse files Browse the repository at this point in the history
…mplewallet-extension into TW-1104-integrate-slise-xyz-to-temple-wallet-extension
  • Loading branch information
keshan3262 committed Oct 25, 2023
2 parents 4b71c24 + b3c892e commit 20daf32
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 44 deletions.
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)
.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;
}
}, [address, tezos]);

Expand Down

0 comments on commit 20daf32

Please sign in to comment.