Skip to content

Commit

Permalink
Refactor MASBalance component to use balance from account store and s…
Browse files Browse the repository at this point in the history
…treamline balance fetching logic
  • Loading branch information
Ben-Rey committed Jan 20, 2025
1 parent c8ef05c commit 4678475
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
17 changes: 2 additions & 15 deletions src/lib/ConnectMassaWallets/components/MASBalance.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import React from 'react';

import { useEffect, useState } from 'react';
import React, { useEffect } from 'react';

import Intl from '../i18n';
import { useAccountStore } from '../store';
Expand All @@ -11,18 +9,7 @@ import { massaToken } from '../../massa-react/utils/const';
import { formatAmount } from '../../util/parseAmount';

export function MASBalance() {
const [balance, setBalance] = useState<bigint>();

const { connectedAccount, currentWallet, network } = useAccountStore();

useEffect(() => {
if (!connectedAccount) return;
const fetchBalance = async () => {
const balance = await connectedAccount.balance(false);
setBalance(balance);
};
fetchBalance();
}, [connectedAccount, setBalance, currentWallet, network]);
const { balance } = useAccountStore();

const formattedBalance = formatAmount(balance?.toString() || '0', 9).full;

Expand Down
21 changes: 19 additions & 2 deletions src/lib/ConnectMassaWallets/store/accountStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Wallet, WalletName } from '@massalabs/wallet-provider';

export interface AccountStoreState {
connectedAccount?: Provider;
balance?: bigint;
accounts?: Provider[];
currentWallet?: Wallet;
wallets: Wallet[];
Expand All @@ -20,12 +21,15 @@ export interface AccountStoreState {
setWallets: (wallets: Wallet[]) => void;
setConnectedAccount: (account?: Provider) => void;
setCurrentNetwork: (network: Network) => void;
fetchBalance: (provider: Provider, final: boolean) => Promise<void>;
refreshBalance: (final: boolean) => void;
}

export const useAccountStore = create<AccountStoreState>((set, get) => ({
accounts: undefined,
network: undefined,
connectedAccount: undefined,
balance: undefined,
accountObserver: undefined,
networkObserver: undefined,
currentWallet: undefined,
Expand Down Expand Up @@ -78,7 +82,7 @@ export const useAccountStore = create<AccountStoreState>((set, get) => ({
setAccounts: async (wallet: Wallet, account?: Provider) => {
const accounts = await wallet.accounts();
set({ accounts });
set({ connectedAccount: account || accounts[0] });
get().setConnectedAccount(account || accounts[0]);
},

setWallets: (wallets: Wallet[]) => {
Expand All @@ -88,14 +92,27 @@ export const useAccountStore = create<AccountStoreState>((set, get) => ({
}
},

setConnectedAccount: (connectedAccount?: Provider) => {
setConnectedAccount: async (connectedAccount?: Provider) => {
set({ connectedAccount });
if (!connectedAccount) return;
get().fetchBalance(connectedAccount, false);
},

setCurrentNetwork: (network: Network) => {
if (network === get().network) return;
set({ network });
},

fetchBalance: async (provider: Provider) => {
const balance = await provider.balance(false);
set({ balance });
},

refreshBalance: async (final: boolean) => {
const { connectedAccount } = get();
if (!connectedAccount) return;
get().fetchBalance(connectedAccount, final);
},
}));

function resetObservers(
Expand Down

0 comments on commit 4678475

Please sign in to comment.