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: fetch chain configs from config service #596

Merged
merged 2 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react-svg": "^16.1.31",
"react-virtualized-auto-sizer": "^1.0.6",
"react-window": "^1.8.9",
"swr": "^2.2.5",
"tslib": "^2.6.1",
"typescript": "~5.0.4"
},
Expand Down
11 changes: 5 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import { AssetTransfer, CollectibleTransfer, useCsvParser } from "./hooks/useCsv
import { useEnsResolver } from "./hooks/useEnsResolver";
import CheckIcon from "./static/check.svg";
import AppIcon from "./static/logo.svg";
import { useGetAssetBalanceQuery, useGetAllNFTsQuery } from "./stores/api/balanceApi";
import { setupParserListener } from "./stores/middleware/parseListener";
import { setSafeInfo } from "./stores/slices/safeInfoSlice";
import { RootState, startAppListening } from "./stores/store";
import { RootState, selectIsLoading, startAppListening, useAppSelector } from "./stores/store";
import { buildAssetTransfers, buildCollectibleTransfers } from "./transfers/transfers";

import "./styles/globals.css";
Expand All @@ -28,8 +27,6 @@ const App: React.FC = () => {
const theme = useTheme();
const { isLoading } = useTokenList();
const { sdk, safe } = useSafeAppsSDK();
const assetBalanceQuery = useGetAssetBalanceQuery();
const nftBalanceQuery = useGetAllNFTsQuery();

const { messages } = useSelector((state: RootState) => state.messages);
const { transfers, parsing } = useSelector((state: RootState) => state.csvEditor);
Expand All @@ -39,6 +36,8 @@ const App: React.FC = () => {
const ensResolver = useEnsResolver();
const dispatch = useDispatch();

const isDataLoading = useAppSelector(selectIsLoading);

useEffect(() => {
dispatch(setSafeInfo(safe));
const subscriptions: Unsubscribe[] = [setupParserListener(startAppListening, parseCsv, ensResolver)];
Expand Down Expand Up @@ -78,7 +77,7 @@ const App: React.FC = () => {
<Box display="flex" flexDirection="column" justifyContent="left">
{
<>
{isLoading || assetBalanceQuery.isLoading || nftBalanceQuery.isLoading ? (
{isLoading || isDataLoading ? (
<Loading />
) : (
<Box display="flex" flexDirection="column" gap={2}>
Expand All @@ -91,7 +90,7 @@ const App: React.FC = () => {
<FAQModal />
</Box>
</Grid>
<Grid item xs display="flex" direction="row" alignItems="center" gap={2}>
<Grid item xs display="flex" alignItems="center" gap={2}>
<img
src={CheckIcon}
alt="check"
Expand Down
12 changes: 12 additions & 0 deletions src/AppInitializer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk";

import { useLoadAssets, useLoadCollectibles } from "./hooks/useBalances";
import { useLoadChains } from "./hooks/useChains";

export const AppInitializer = () => {
const { safe } = useSafeAppsSDK();
useLoadChains();
useLoadAssets(safe);
useLoadCollectibles(safe);
return <></>;
};
2 changes: 2 additions & 0 deletions src/AppWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SafeThemeProvider } from "@safe-global/safe-react-components";
import { Provider as ReduxProvider } from "react-redux";

import App from "./App";
import { AppInitializer } from "./AppInitializer";
import { useDarkMode } from "./hooks/useDarkMode";
import errorIcon from "./static/error-icon.svg";
import { store } from "./stores/store";
Expand Down Expand Up @@ -42,6 +43,7 @@ export const AppWrapper = () => {
}
>
<ReduxProvider store={store}>
<AppInitializer />
<App />
</ReduxProvider>
</SafeProvider>
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/tokenList.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from "ethers";

import { fetchTokenList } from "../hooks/token";
import { networkInfo } from "../networks";
import { staticNetworkInfo } from "../networks";

beforeEach(() => {
jest.spyOn(window, "fetch").mockImplementation(() => {
Expand Down Expand Up @@ -68,8 +68,8 @@ describe("Mainnet tokenlist", () => {
});

describe("Fetch should resolve for all networks", () => {
for (const chainId of networkInfo.keys()) {
it(`fetches tokens for ${networkInfo.get(chainId)?.name} network`, () => {
for (const chainId of staticNetworkInfo.keys()) {
it(`fetches tokens for ${staticNetworkInfo.get(chainId)?.name} network`, () => {
expect(() => fetchTokenList(chainId)).not.toThrow();
});
}
Expand Down
10 changes: 4 additions & 6 deletions src/components/DonateDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import {
TextField,
Typography,
} from "@mui/material";
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk";
import { BigNumber, ethers } from "ethers";
import { useEffect, useState } from "react";
import { useCsvContent } from "src/hooks/useCsvContent";
import { useCurrentChain } from "src/hooks/useCurrentChain";
import { useDarkMode } from "src/hooks/useDarkMode";
import { networkInfo } from "src/networks";
import { AssetBalance } from "src/stores/api/balanceApi";
import { AssetBalance } from "src/stores/slices/assetBalanceSlice";
import { updateCsvContent } from "src/stores/slices/csvEditorSlice";
import { useAppDispatch } from "src/stores/store";
import { DONATION_ADDRESS } from "src/utils";
Expand All @@ -36,12 +35,11 @@ export const DonateDialog = ({
onClose: () => void;
assetBalance: AssetBalance;
}) => {
const { safe } = useSafeAppsSDK();
const dispatch = useAppDispatch();
const csvContent = useCsvContent();
const darkMode = useDarkMode();

const nativeSymbol = networkInfo.get(safe.chainId)?.currencySymbol || "ETH";
const chainConfig = useCurrentChain();
const nativeSymbol = chainConfig?.currencySymbol || "ETH";

const items = assetBalance?.map((asset) => ({
id: asset.tokenAddress || "0x0",
Expand Down
14 changes: 6 additions & 8 deletions src/components/DrainSafeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import {
TextField,
Typography,
} from "@mui/material";
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk";
import BigNumber from "bignumber.js";
import { utils } from "ethers";
import { useState } from "react";
import { useCurrentChain } from "src/hooks/useCurrentChain";
import { useEnsResolver } from "src/hooks/useEnsResolver";
import { networkInfo } from "src/networks";
import { AssetBalance, NFTBalance } from "src/stores/api/balanceApi";
import { AssetBalance } from "src/stores/slices/assetBalanceSlice";
import { NFTBalance } from "src/stores/slices/collectiblesSlice";
import { updateCsvContent } from "src/stores/slices/csvEditorSlice";
import { useAppDispatch } from "src/stores/store";
import { fromWei } from "src/utils";
Expand All @@ -33,18 +33,16 @@ export const DrainSafeDialog = ({
isOpen: boolean;
onClose: () => void;
assetBalance: AssetBalance;
nftBalance: NFTBalance;
nftBalance: NFTBalance["results"];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand this notation. Is the balance type the declared as whatever type the results field is?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It refers to the type of the results field of the NFTBalance type.

}) => {
const [drainAddress, setDrainAddress] = useState("");
const [resolvedAddress, setResolvedAddress] = useState("");

const [resolving, setResolving] = useState(false);
const { safe } = useSafeAppsSDK();

const dispatch = useAppDispatch();

const selectedNetworkInfo = networkInfo.get(safe.chainId);

const selectedNetworkInfo = useCurrentChain();
const ensResolver = useEnsResolver();

const invalidNetworkError = resolvedAddress.includes(":")
Expand Down Expand Up @@ -75,7 +73,7 @@ export const DrainSafeDialog = ({
}
});

nftBalance?.results.forEach((collectible) => {
nftBalance.forEach((collectible) => {
drainCSV += `\nnft,${collectible.address},${drainAddress},,${collectible.id}`;
});
}
Expand Down
11 changes: 5 additions & 6 deletions src/components/GenerateTransfersMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { Box, Button, Tooltip } from "@mui/material";
import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk";
import { useState } from "react";
import { useGetAssetBalanceQuery, useGetAllNFTsQuery } from "src/stores/api/balanceApi";
import { selectAssetBalances } from "src/stores/slices/assetBalanceSlice";
import { selectCollectibles } from "src/stores/slices/collectiblesSlice";
import { useAppSelector } from "src/stores/store";

import { NETWORKS_WITH_DONATIONS_DEPLOYED } from "../networks";

import { DonateDialog } from "./DonateDialog";
import { DrainSafeDialog } from "./DrainSafeDialog";

export const GenerateTransfersMenu = () => {
const assetBalanceQuery = useGetAssetBalanceQuery();
const nftBalanceQuery = useGetAllNFTsQuery();

const assetBalance = assetBalanceQuery.currentData;
const nftBalance = nftBalanceQuery.currentData;
const assetBalance = useAppSelector(selectAssetBalances);
const nftBalance = useAppSelector(selectCollectibles);

const [isDrainModalOpen, setIsDrainModalOpen] = useState(false);
const [isDonateModalOpen, setIsDonateModalOpen] = useState(false);
Expand Down
11 changes: 6 additions & 5 deletions src/hooks/collectibleTokenInfoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { useSafeAppsSDK } from "@safe-global/safe-apps-react-sdk";
import BigNumber from "bignumber.js";
import { ethers } from "ethers";
import { useCallback, useMemo } from "react";
import { useGetAllNFTsQuery } from "src/stores/api/balanceApi";
import { selectCollectibles } from "src/stores/slices/collectiblesSlice";
import { useAppSelector } from "src/stores/store";
import { resolveIpfsUri } from "src/utils";

import { erc1155Instance } from "../transfers/erc1155";
Expand Down Expand Up @@ -35,9 +36,9 @@ export interface CollectibleTokenInfoProvider {

export const useCollectibleTokenInfoProvider: () => CollectibleTokenInfoProvider = () => {
const { safe, sdk } = useSafeAppsSDK();
const currentNftBalance = useAppSelector(selectCollectibles);

const web3Provider = useMemo(() => new ethers.providers.Web3Provider(new SafeAppProvider(safe, sdk)), [sdk, safe]);
const nftBalanceQuery = useGetAllNFTsQuery();
const currentNftBalance = nftBalanceQuery.currentData;

const collectibleContractCache = useMemo(() => new Map<string, CollectibleTokenInfo | undefined>(), []);

Expand All @@ -49,7 +50,7 @@ export const useCollectibleTokenInfoProvider: () => CollectibleTokenInfoProvider
return contractInterfaceCache.get(tokenAddress) ?? [undefined];
}
if (currentNftBalance) {
const tokenInfo = currentNftBalance.results.find((nftEntry) => nftEntry.address === tokenAddress);
const tokenInfo = currentNftBalance.find((nftEntry) => nftEntry.address === tokenAddress);
if (tokenInfo) {
return Promise.resolve(["erc721"]);
}
Expand Down Expand Up @@ -111,7 +112,7 @@ export const useCollectibleTokenInfoProvider: () => CollectibleTokenInfoProvider
async (tokenAddress: string, id: BigNumber, token_type: "erc1155" | "erc721") => {
if (token_type === "erc721") {
if (currentNftBalance) {
const tokenInfo = currentNftBalance.results.find(
const tokenInfo = currentNftBalance.find(
(nftEntry) => nftEntry.address === tokenAddress && nftEntry.id === id.toFixed(),
);
if (tokenInfo && tokenInfo.imageUri && tokenInfo.name) {
Expand Down
21 changes: 9 additions & 12 deletions src/hooks/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { ethers, utils } from "ethers";
import xdaiTokens from "honeyswap-default-token-list";
import { useState, useEffect, useMemo } from "react";

import { networkInfo } from "../networks";
import rinkeby from "../static/rinkebyTokens.json";
import { erc20Instance } from "../transfers/erc20";
import { TokenInfo } from "../utils";

import { useCurrentChain } from "./useCurrentChain";

export type TokenMap = Map<string | null, MinimalTokenInfo>;

function tokenMap(tokenList: TokenInfo[]): TokenMap {
Expand All @@ -32,17 +32,11 @@ export const fetchTokenList = async (chainId: number): Promise<TokenMap> => {
.then((response) => response.tokens)
.catch(() => []);
break;
case 4:
// Hardcoded this because the list provided at
// https://github.com/Uniswap/default-token-list/blob/master/src/tokens/rinkeby.json
// Doesn't have GNO or OWL and/or many others.
tokens = rinkeby;
break;
case 100:
tokens = xdaiTokens.tokens;
break;
default:
console.warn(`Unimplemented token list for ${networkInfo.get(chainId)?.name} network`);
console.warn(`Unimplemented token list for chainId ${chainId}`);
tokens = [];
}
return tokenMap(tokens);
Expand All @@ -59,6 +53,7 @@ export function useTokenList(): {
const { safe } = useSafeAppsSDK();
const [tokenList, setTokenList] = useState<TokenMap>(new Map());
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
let isMounted = true;
setIsLoading(true);
Expand Down Expand Up @@ -109,6 +104,8 @@ export const useTokenInfoProvider: () => TokenInfoProvider = () => {
}, [sdk.safe]);
const { tokenList } = useTokenList();

const chainConfig = useCurrentChain();

return useMemo(
() => ({
getTokenInfo: async (tokenAddress: string) => {
Expand Down Expand Up @@ -141,9 +138,9 @@ export const useTokenInfoProvider: () => TokenInfoProvider = () => {
return undefined;
}
},
getNativeTokenSymbol: () => networkInfo.get(safe.chainId)?.currencySymbol ?? "ETH",
getSelectedNetworkShortname: () => networkInfo.get(safe.chainId)?.shortName,
getNativeTokenSymbol: () => chainConfig?.currencySymbol ?? "ETH",
getSelectedNetworkShortname: () => chainConfig?.shortName,
}),
[balances.items, safe.chainId, tokenList, web3Provider],
[balances.items, tokenList, web3Provider, chainConfig],
);
};
Loading
Loading