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

optimize: token info fetching #477

Merged
merged 1 commit into from
Dec 28, 2022
Merged
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
57 changes: 42 additions & 15 deletions src/hooks/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SafeAppProvider } from "@gnosis.pm/safe-apps-provider";
import { useSafeAppsSDK } from "@gnosis.pm/safe-apps-react-sdk";
import { SafeBalanceResponse } from "@gnosis.pm/safe-react-gateway-sdk";
import { ethers, utils } from "ethers";
import xdaiTokens from "honeyswap-default-token-list";
import { useState, useEffect, useMemo } from "react";
Expand Down Expand Up @@ -90,33 +91,59 @@ export interface TokenInfoProvider {
export const useTokenInfoProvider: () => TokenInfoProvider = () => {
const { safe, sdk } = useSafeAppsSDK();
const web3Provider = useMemo(() => new ethers.providers.Web3Provider(new SafeAppProvider(safe, sdk)), [sdk, safe]);
const [balances, setBalances] = useState<SafeBalanceResponse>({
fiatTotal: "0",
items: [],
});
useEffect(() => {
let isMounted = true;
sdk.safe
.experimental_getBalances()
.then((balances) => {
isMounted && setBalances(balances);
})
.catch(() => console.error("Error fetching balances from safe apps sdk"));
return () => {
isMounted = false;
};
}, [sdk.safe]);
const { tokenList } = useTokenList();

return useMemo(
() => ({
getTokenInfo: async (tokenAddress: string) => {
if (tokenList?.has(tokenAddress)) {
return tokenList.get(tokenAddress);
} else {
const tokenContract = erc20Instance(tokenAddress, web3Provider);
const decimals = await tokenContract.decimals().catch((reason) => undefined);
const symbol = await tokenContract.symbol().catch((reason) => undefined);
}

const tokenInfoFromBalances = balances.items.find((item) => item.tokenInfo.address === tokenAddress);
if (tokenInfoFromBalances) {
return {
decimals: tokenInfoFromBalances.tokenInfo.decimals,
symbol: tokenInfoFromBalances.tokenInfo.symbol,
address: tokenAddress,
logoURI: tokenInfoFromBalances.tokenInfo.logoUri,
};
}

if (typeof decimals !== "undefined") {
tokenList?.set(tokenAddress, {
decimals,
symbol,
address: tokenAddress,
});
return { decimals, symbol, address: tokenAddress };
} else {
return undefined;
}
const tokenContract = erc20Instance(tokenAddress, web3Provider);
const decimals = await tokenContract.decimals().catch((reason) => undefined);
const symbol = await tokenContract.symbol().catch((reason) => undefined);

if (typeof decimals !== "undefined") {
tokenList?.set(tokenAddress, {
decimals,
symbol,
address: tokenAddress,
});
return { decimals, symbol, address: tokenAddress };
} else {
return undefined;
}
},
getNativeTokenSymbol: () => networkInfo.get(safe.chainId)?.currencySymbol ?? "ETH",
getSelectedNetworkShortname: () => networkInfo.get(safe.chainId)?.shortName,
}),
[safe.chainId, tokenList, web3Provider],
[balances.items, safe.chainId, tokenList, web3Provider],
);
};