Skip to content

Commit

Permalink
Fix balances getters
Browse files Browse the repository at this point in the history
  • Loading branch information
Atatakai authored and Atatakai committed May 24, 2024
1 parent bfc348d commit f929b24
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 61 deletions.
8 changes: 4 additions & 4 deletions frontend/components/Main/MainGasBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ const LowDot = styled(Dot)`
`;

const BalanceStatus = () => {
const { totalEthBalance } = useBalance();
const { safeBalance } = useBalance();

const status = useMemo(() => {
if (!totalEthBalance || totalEthBalance === 0) {
if (!safeBalance || safeBalance.ETH === 0) {
return { statusName: 'Empty', StatusComponent: EmptyDot };
}

if (totalEthBalance < LOW_BALANCE) {
if (safeBalance.ETH < LOW_BALANCE) {
return { statusName: 'Low', StatusComponent: LowDot };
}

return { statusName: 'Fine', StatusComponent: FineDot };
}, [totalEthBalance]);
}, [safeBalance]);

const { statusName, StatusComponent } = status;
return (
Expand Down
20 changes: 12 additions & 8 deletions frontend/components/Main/MainHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const MainHeader = () => {
const { getServiceTemplates } = useServiceTemplates();
const { wallets, masterSafeAddress } = useWallet();
const {
safeBalance,
totalOlasBalance,
totalEthBalance,
isBalanceLoaded,
setIsPaused: setIsBalancePollingPaused,
} = useBalance();

Expand All @@ -44,14 +46,14 @@ export const MainHeader = () => {
);

useEffect(() => {
if (totalEthBalance && totalEthBalance < LOW_BALANCE) {
if (safeBalance && safeBalance.ETH < LOW_BALANCE) {
setTrayIcon?.('low-gas');
} else if (serviceStatus === DeploymentStatus.DEPLOYED) {
setTrayIcon?.('running');
} else if (serviceStatus === DeploymentStatus.STOPPED) {
setTrayIcon?.('paused');
}
}, [totalEthBalance, serviceStatus, setTrayIcon]);
}, [safeBalance, serviceStatus, setTrayIcon]);

const agentHead = useMemo(() => {
if (
Expand Down Expand Up @@ -185,7 +187,7 @@ export const MainHeader = () => {
);
}

if (totalOlasBalance === undefined || totalEthBalance === undefined) {
if (!isBalanceLoaded) {
return (
<Button type="primary" size="large" disabled>
Start agent
Expand Down Expand Up @@ -213,8 +215,9 @@ export const MainHeader = () => {
);

if (
totalOlasBalance < olasCostOfBond + olasRequiredToStake ||
totalEthBalance < monthlyGasEstimate
(totalOlasBalance &&
totalOlasBalance < olasCostOfBond + olasRequiredToStake) ||
(totalEthBalance && totalEthBalance < monthlyGasEstimate)
) {
return (
<Button type="default" size="large" disabled>
Expand All @@ -229,12 +232,13 @@ export const MainHeader = () => {
</Button>
);
}, [
handlePause,
handleStart,
isBalanceLoaded,
serviceButtonState,
serviceStatus,
totalOlasBalance,
totalEthBalance,
handleStart,
handlePause,
totalOlasBalance,
]);

return (
Expand Down
30 changes: 8 additions & 22 deletions frontend/components/Main/MainNeedsFunds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const COVER_PREV_BLOCK_BORDER_STYLE = { marginTop: '-1px' };
const useNeedsFunds = () => {
const serviceTemplate = SERVICE_TEMPLATES[0];
const { storeState } = useStore();
const { totalEthBalance, totalOlasBalance } = useBalance();
const { safeBalance } = useBalance();

const isInitialFunded = storeState?.isInitialFunded as boolean | undefined;

Expand Down Expand Up @@ -45,13 +45,13 @@ const useNeedsFunds = () => {
]);

const hasEnoughEth = useMemo(
() => (totalEthBalance || 0) >= (serviceFundRequirements?.eth || 0),
[serviceFundRequirements?.eth, totalEthBalance],
() => (safeBalance?.ETH || 0) >= (serviceFundRequirements?.eth || 0),
[serviceFundRequirements?.eth, safeBalance],
);

const hasEnoughOlas = useMemo(
() => (totalOlasBalance || 0) >= (serviceFundRequirements?.olas || 0),
[serviceFundRequirements?.olas, totalOlasBalance],
() => (safeBalance?.OLAS || 0) >= (serviceFundRequirements?.olas || 0),
[serviceFundRequirements?.olas, safeBalance],
);

return {
Expand All @@ -63,7 +63,7 @@ const useNeedsFunds = () => {
};

export const MainNeedsFunds = () => {
const { isBalanceLoaded, totalEthBalance, totalOlasBalance } = useBalance();
const { isBalanceLoaded } = useBalance();
const {
hasEnoughEth,
hasEnoughOlas,
Expand All @@ -75,23 +75,10 @@ export const MainNeedsFunds = () => {

const isVisible: boolean = useMemo(() => {
if (isInitialFunded) return false;
if (
[totalEthBalance, totalOlasBalance].some(
(balance) => balance === undefined,
)
) {
return false;
}

if (!isBalanceLoaded) return false;
if (hasEnoughEth && hasEnoughOlas) return false;
return true;
}, [
hasEnoughEth,
hasEnoughOlas,
isInitialFunded,
totalEthBalance,
totalOlasBalance,
]);
}, [hasEnoughEth, hasEnoughOlas, isBalanceLoaded, isInitialFunded]);

const message: ReactNode = useMemo(
() => (
Expand Down Expand Up @@ -130,7 +117,6 @@ export const MainNeedsFunds = () => {
}, [electronApi.store, hasEnoughEth, hasEnoughOlas, isInitialFunded]);

if (!isVisible) return null;
if (!isBalanceLoaded) return null;

return (
<CardSection style={COVER_PREV_BLOCK_BORDER_STYLE}>
Expand Down
8 changes: 4 additions & 4 deletions frontend/components/Settings/SettingsAddBackupWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import { CardTitle } from '../common/CardTitle';
import { CardFlex } from '../styled/CardFlex';

export const SettingsAddBackupWallet = () => {
const { totalEthBalance } = useBalance();
const { eoaBalance } = useBalance();
const { goto } = useSettings();

const [form] = Form.useForm();

const isFunded = useMemo<boolean>(() => {
if (!totalEthBalance) return false;
if (!eoaBalance) return false;
return (
totalEthBalance >= MIN_ETH_BALANCE_THRESHOLDS[Chain.GNOSIS].safeAddSigner
eoaBalance.ETH >= MIN_ETH_BALANCE_THRESHOLDS[Chain.GNOSIS].safeAddSigner
);
}, [totalEthBalance]);
}, [eoaBalance]);

return (
<CardFlex
Expand Down
34 changes: 12 additions & 22 deletions frontend/components/Setup/Create/SetupEoaFunding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ import { UNICODE_SYMBOLS } from '@/constants/unicode';
import { SetupScreen } from '@/enums';
import { useBalance, useSetup } from '@/hooks';
import { useWallet } from '@/hooks/useWallet';
import { Address } from '@/types';

import { SetupCreateHeader } from './SetupCreateHeader';

export const SetupEoaFunding = () => {
const { masterEoaAddress } = useWallet();
const { walletBalances } = useBalance();
const { eoaBalance } = useBalance();
const { goto } = useSetup();

const masterEaoEthBalance =
masterEoaAddress && walletBalances?.[masterEoaAddress]?.ETH;

const isFundedMasterEoa =
masterEaoEthBalance &&
masterEaoEthBalance >=
MIN_ETH_BALANCE_THRESHOLDS[Chain.GNOSIS].safeCreation;
eoaBalance?.ETH &&
eoaBalance.ETH >= MIN_ETH_BALANCE_THRESHOLDS[Chain.GNOSIS].safeCreation;

const statusMessage = useMemo(() => {
if (isFundedMasterEoa) {
Expand Down Expand Up @@ -80,18 +74,14 @@ export const SetupEoaFunding = () => {
Status: {statusMessage}
</Typography.Text>
</CardSection>
{!isFundedMasterEoa && (
<SetupEoaFundingWaiting masterEoa={masterEoaAddress} />
)}
{!isFundedMasterEoa && <SetupEoaFundingWaiting />}
</CardFlex>
);
};

const SetupEoaFundingWaiting = ({
masterEoa,
}: {
masterEoa: Address | undefined;
}) => {
const SetupEoaFundingWaiting = () => {
const { masterEoaAddress } = useWallet();

return (
<>
<CardSection>
Expand Down Expand Up @@ -121,21 +111,21 @@ const SetupEoaFundingWaiting = ({
<CopyOutlined
style={ICON_STYLE}
onClick={() =>
masterEoa &&
copyToClipboard(masterEoa).then(() =>
masterEoaAddress &&
copyToClipboard(masterEoaAddress).then(() =>
message.success('Address copied!'),
)
}
/>
</Tooltip>

{/* {masterEoa && (
{/* {masterEoaAddress && (
<Popover
title="Scan QR code"
content={
<QRCode
size={250}
value={`https://metamask.app.link/send/${masterEoa}@${100}`}
value={`https://metamask.app.link/send/${masterEoaAddress}@${100}`}
/>
}
>
Expand All @@ -146,7 +136,7 @@ const SetupEoaFundingWaiting = ({
</Flex>

<span className="can-select-text break-word">
{`GNO: ${masterEoa}`}
{`GNO: ${masterEoaAddress}`}
</span>
<Alert
type="info"
Expand Down
16 changes: 16 additions & 0 deletions frontend/context/BalanceProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { message } from 'antd';
import { isAddress } from 'ethers/lib/utils';
import { isNumber } from 'lodash';
import { ValueOf } from 'next/dist/shared/lib/constants';
import {
createContext,
Dispatch,
Expand Down Expand Up @@ -36,6 +37,8 @@ export const BalanceContext = createContext<{
isBalanceLoaded: boolean;
olasBondBalance?: number;
olasDepositBalance?: number;
eoaBalance?: ValueOf<WalletAddressNumberRecord>;
safeBalance?: ValueOf<WalletAddressNumberRecord>;
totalEthBalance?: number;
totalOlasBalance?: number;
wallets?: Wallet[];
Expand All @@ -49,6 +52,8 @@ export const BalanceContext = createContext<{
isBalanceLoaded: false,
olasBondBalance: undefined,
olasDepositBalance: undefined,
eoaBalance: undefined,
safeBalance: undefined,
totalEthBalance: undefined,
totalOlasBalance: undefined,
wallets: undefined,
Expand Down Expand Up @@ -179,6 +184,15 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
}
}, [masterEoaAddress, masterSafeAddress, serviceAddresses, services]);

const eoaBalance = useMemo(
() => masterEoaAddress && walletBalances[masterEoaAddress],
[masterEoaAddress, walletBalances],
);
const safeBalance = useMemo(
() => masterSafeAddress && walletBalances[masterSafeAddress],
[masterSafeAddress, walletBalances],
);

useInterval(
() => {
updateBalances();
Expand All @@ -194,6 +208,8 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
isBalanceLoaded,
olasBondBalance,
olasDepositBalance,
eoaBalance,
safeBalance,
totalEthBalance,
totalOlasBalance,
wallets,
Expand Down
4 changes: 4 additions & 0 deletions frontend/hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const useBalance = () => {
isLoaded,
setIsLoaded,
isBalanceLoaded,
eoaBalance,
safeBalance,
totalEthBalance,
totalOlasBalance,
wallets,
Expand All @@ -20,6 +22,8 @@ export const useBalance = () => {
isLoaded,
setIsLoaded,
isBalanceLoaded,
eoaBalance,
safeBalance,
totalEthBalance,
totalOlasBalance,
wallets,
Expand Down
7 changes: 6 additions & 1 deletion frontend/types/Records.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Token } from '@/enums';

import { Address } from '.';

export type AddressNumberRecord = Record<Address, number>;
export type AddressBooleanRecord = Record<Address, boolean>;

// defines token balances in a wallet by token name
export type WalletAddressNumberRecord = Record<Address, Record<string, number>>;
export type WalletAddressNumberRecord = Record<
Address,
Record<Token.ETH | Token.OLAS, number>
>;

0 comments on commit f929b24

Please sign in to comment.