refactor(with-ethers): handles multi-account balance representation

This commit is contained in:
Ben Kremer 2022-02-22 11:53:39 +01:00
parent e7e8638229
commit 97bdc4aa59
3 changed files with 31 additions and 17 deletions

View File

@ -8,7 +8,13 @@ import Column from "./Column";
import Loader from "./Loader";
import { getChainMetadata } from "../chains";
import { AccountAction, ellipseAddress, ChainMetadata, ChainNamespaces } from "../helpers";
import {
AccountAction,
ellipseAddress,
ChainMetadata,
ChainNamespaces,
AccountBalances,
} from "../helpers";
import { fonts } from "../styles";
interface AccountStyleProps {
@ -82,7 +88,7 @@ interface BlockchainProps {
chainId: string;
address?: string;
onClick?: (chain: string) => void;
balances?: any[];
balances?: AccountBalances;
actions?: AccountAction[];
}
@ -141,13 +147,11 @@ const Blockchain: FC<PropsWithChildren<BlockchainProps>> = (
</Column>
) : (
<>
{!!balances && balances.length ? (
{!!address && !!balances && balances[address] ? (
<SFullWidthContainer>
<h6>Balances</h6>
<Column center>
{balances.map(balance => (
<Asset key={balance.symbol} asset={balance} />
))}
<Asset key={balances[address].symbol} asset={balances[address]} />
</Column>
</SFullWidthContainer>
) : null}

View File

@ -18,7 +18,7 @@ import {
DEFAULT_RELAY_URL,
} from "../constants";
import { providers, utils } from "ethers";
import { ChainNamespaces, getAllChainNamespaces } from "../helpers";
import { AccountBalances, ChainNamespaces, getAllChainNamespaces } from "../helpers";
import { apiGetChainNamespace, ChainsMap } from "caip-api";
/**
@ -32,7 +32,7 @@ interface IContext {
chain: string;
pairings: string[];
accounts: string[];
balances: { symbol: string; balance: string }[];
balances: AccountBalances;
isFetchingBalances: boolean;
chainData: ChainNamespaces;
onEnable: (chainId: string) => Promise<void>;
@ -59,7 +59,7 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
const [isInitializing, setIsInitializing] = useState(false);
const [hasCheckedPersistedSession, setHasCheckedPersistedSession] = useState(false);
const [balances, setBalances] = useState<{ symbol: string; balance: string }[]>([]);
const [balances, setBalances] = useState<AccountBalances>({});
const [accounts, setAccounts] = useState<string[]>([]);
const [chainData, setChainData] = useState<ChainNamespaces>({});
const [chain, setChain] = useState<string>("");
@ -67,7 +67,7 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
const resetApp = () => {
setPairings([]);
setSession(undefined);
setBalances([]);
setBalances({});
setAccounts([]);
setChain("");
};
@ -186,10 +186,21 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
const _balances = await Promise.all(
_accounts.map(async account => {
const balance = await web3Provider.getBalance(account);
return { symbol: "ETH", balance: utils.formatEther(balance) };
return {
account,
symbol: "ETH",
balance: utils.formatEther(balance),
contractAddress: "",
};
}),
);
setBalances(_balances);
const balancesByAccount = _balances.reduce((obj, balance) => {
obj[balance.account] = balance;
return obj;
}, {} as AccountBalances);
setBalances(balancesByAccount);
} catch (error: any) {
throw new Error(error);
} finally {

View File

@ -1,11 +1,10 @@
import { ChainsMap } from "caip-api";
export interface AssetData {
account: string;
symbol: string;
name: string;
decimals: string;
contractAddress: string;
balance?: string;
balance: string;
contractAddress?: string;
}
export interface ChainData {
@ -155,5 +154,5 @@ export interface AccountAction {
}
export interface AccountBalances {
[account: string]: AssetData[];
[account: string]: AssetData;
}