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

chore: update low gas alert logic #277

Merged
merged 2 commits into from
Aug 20, 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
14 changes: 6 additions & 8 deletions frontend/components/Main/MainGasBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useEffect, useMemo, useState } from 'react';
import styled from 'styled-components';

import { COLOR } from '@/constants/colors';
import { LOW_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useStore } from '@/hooks/useStore';
Expand Down Expand Up @@ -34,7 +33,7 @@ const FineDot = styled(Dot)`
`;

const BalanceStatus = () => {
const { isBalanceLoaded, safeBalance } = useBalance();
const { isBalanceLoaded, isLowBalance } = useBalance();
const { storeState } = useStore();
const { showNotification } = useElectronApi();

Expand All @@ -44,35 +43,34 @@ const BalanceStatus = () => {
// show notification if balance is too low
useEffect(() => {
if (!isBalanceLoaded) return;
if (!safeBalance) return;
if (!showNotification) return;
if (!storeState?.isInitialFunded) return;

if (safeBalance.ETH < LOW_BALANCE && !isLowBalanceNotificationShown) {
if (isLowBalance && !isLowBalanceNotificationShown) {
showNotification('Trading balance is too low.');
setIsLowBalanceNotificationShown(true);
}

// If it has already been shown and the balance has increased,
// should show the notification again if it goes below the threshold.
if (safeBalance.ETH >= LOW_BALANCE && isLowBalanceNotificationShown) {
if (!isLowBalance && isLowBalanceNotificationShown) {
setIsLowBalanceNotificationShown(false);
}
}, [
isBalanceLoaded,
isLowBalanceNotificationShown,
safeBalance,
isLowBalance,
showNotification,
storeState?.isInitialFunded,
]);

const status = useMemo(() => {
if (!safeBalance || safeBalance.ETH < LOW_BALANCE) {
if (isLowBalance) {
return { statusName: 'Too low', StatusComponent: EmptyDot };
}

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

const { statusName, StatusComponent } = status;
return (
Expand Down
6 changes: 3 additions & 3 deletions frontend/components/Main/MainHeader/AgentButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { useCallback, useMemo } from 'react';

import { Chain, DeploymentStatus } from '@/client';
import { COLOR } from '@/constants/colors';
import { LOW_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useServices } from '@/hooks/useServices';
Expand Down Expand Up @@ -101,6 +100,7 @@ const AgentNotRunningButton = () => {
const {
setIsPaused: setIsBalancePollingPaused,
safeBalance,
isLowBalance,
totalOlasStakedBalance,
totalEthBalance,
} = useBalance();
Expand Down Expand Up @@ -194,7 +194,7 @@ const AgentNotRunningButton = () => {
const isServiceInactive =
serviceStatus === DeploymentStatus.BUILT ||
serviceStatus === DeploymentStatus.STOPPED;
if (isServiceInactive && safeBalance && safeBalance.ETH < LOW_BALANCE) {
if (isServiceInactive && isLowBalance) {
return false;
}

Expand Down Expand Up @@ -224,7 +224,7 @@ const AgentNotRunningButton = () => {
serviceStatus,
storeState?.isInitialFunded,
totalEthBalance,
safeBalance,
isLowBalance,
]);

const buttonProps: ButtonProps = {
Expand Down
7 changes: 3 additions & 4 deletions frontend/components/Main/MainHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Flex } from 'antd';
import { useCallback, useEffect, useState } from 'react';

import { DeploymentStatus } from '@/client';
import { LOW_BALANCE } from '@/constants/thresholds';
import { useBalance } from '@/hooks/useBalance';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useServices } from '@/hooks/useServices';
Expand All @@ -12,12 +11,12 @@ import { AgentHead } from './AgentHead';
import { FirstRunModal } from './FirstRunModal';

const useSetupTrayIcon = () => {
const { safeBalance } = useBalance();
const { isLowBalance } = useBalance();
const { serviceStatus } = useServices();
const { setTrayIcon } = useElectronApi();

useEffect(() => {
if (safeBalance && safeBalance.ETH < LOW_BALANCE) {
if (isLowBalance) {
setTrayIcon?.('low-gas');
} else if (serviceStatus === DeploymentStatus.DEPLOYED) {
setTrayIcon?.('running');
Expand All @@ -26,7 +25,7 @@ const useSetupTrayIcon = () => {
} else if (serviceStatus === DeploymentStatus.BUILT) {
setTrayIcon?.('logged-out');
}
}, [safeBalance, serviceStatus, setTrayIcon]);
}, [isLowBalance, serviceStatus, setTrayIcon]);

return null;
};
Expand Down
5 changes: 2 additions & 3 deletions frontend/components/Main/MainOlasBalance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ const MainOlasBalanceAlert = styled.div`
`;

const LowTradingBalanceAlert = () => {
const { isBalanceLoaded, safeBalance } = useBalance();
const { isBalanceLoaded, isLowBalance } = useBalance();
const { storeState } = useStore();

if (!isBalanceLoaded) return null;
if (!safeBalance) return null;
if (!storeState?.isInitialFunded) return;
if (safeBalance.ETH >= LOW_BALANCE) return null;
if (!isLowBalance) return null;

return (
<MainOlasBalanceAlert>
Expand Down
1 change: 1 addition & 0 deletions frontend/constants/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export const MIN_ETH_BALANCE_THRESHOLDS = {
},
};

export const LOW_AGENT_BALANCE = 0.5;
export const LOW_BALANCE = 2;
Tanya-atatakai marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions frontend/context/BalanceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useInterval } from 'usehooks-ts';

import { Wallet } from '@/client';
import { FIVE_SECONDS_INTERVAL } from '@/constants/intervals';
import { LOW_AGENT_BALANCE, LOW_BALANCE } from '@/constants/thresholds';
import { TOKENS } from '@/constants/tokens';
import { ServiceRegistryL2ServiceState } from '@/enums/ServiceRegistryL2ServiceState';
import { Token } from '@/enums/Token';
Expand Down Expand Up @@ -43,6 +44,7 @@ export const BalanceContext = createContext<{
safeBalance?: ValueOf<WalletAddressNumberRecord>;
totalEthBalance?: number;
totalOlasBalance?: number;
isLowBalance: boolean;
wallets?: Wallet[];
walletBalances: WalletAddressNumberRecord;
updateBalances: () => Promise<void>;
Expand All @@ -58,6 +60,7 @@ export const BalanceContext = createContext<{
safeBalance: undefined,
totalEthBalance: undefined,
totalOlasBalance: undefined,
isLowBalance: false,
wallets: undefined,
walletBalances: {},
updateBalances: async () => {},
Expand Down Expand Up @@ -195,6 +198,22 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
() => masterSafeAddress && walletBalances[masterSafeAddress],
[masterSafeAddress, walletBalances],
);
const agentSafeBalance = useMemo(
() =>
services?.[0]?.chain_data?.multisig &&
walletBalances[services[0].chain_data.multisig],
[services, walletBalances],
);
const isLowBalance = useMemo(() => {
if (!safeBalance || !agentSafeBalance) return false;
mohandast52 marked this conversation as resolved.
Show resolved Hide resolved
if (
safeBalance.ETH < LOW_BALANCE &&
// Need to check agentSafe balance as well, because it's auto-funded from safeBalance
agentSafeBalance.ETH < LOW_AGENT_BALANCE
)
return true;
return false;
}, [safeBalance, agentSafeBalance]);

useInterval(
() => {
Expand All @@ -215,6 +234,7 @@ export const BalanceProvider = ({ children }: PropsWithChildren) => {
safeBalance,
totalEthBalance,
totalOlasBalance,
isLowBalance,
wallets,
walletBalances,
updateBalances,
Expand Down
Loading