From 7dc3093fb8a694fcaaa04d14e829fa0c3256373a Mon Sep 17 00:00:00 2001 From: mohandast52 Date: Tue, 17 Dec 2024 16:49:22 +0530 Subject: [PATCH 1/5] refactor: streamline low funds balance checks by using master thresholds --- .../AlertSections/LowFunds/LowFunds.tsx | 19 ++++++++++--------- .../LowFunds/LowOperatingBalanceAlert.tsx | 10 +++++++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx index a534b8fd..68a3eae7 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx @@ -34,21 +34,22 @@ export const LowFunds = () => { if (!masterSafeNativeGasBalance) return false; if (!isInitialFunded) return false; + const masterThresholds = + selectedAgentConfig.operatingThresholds[WalletOwnerType.Master]; + const tokenSymbol = + CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol; + // Funds are transferred from master EOA to master Safe, no need to display - // low safe signer balance alert if EOA has funds + // low safe signer balance alert if "Safe" has funds if ( masterSafeNativeGasBalance >= - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] - ) + masterThresholds[WalletType.Safe][tokenSymbol] + ) { return false; + } return ( - masterEoaNativeGasBalance < - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.EOA - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] + masterEoaNativeGasBalance < masterThresholds[WalletType.EOA][tokenSymbol] ); }, [ isBalanceLoaded, diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx index 76747ef6..df2d0bbc 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx @@ -23,11 +23,15 @@ export const LowOperatingBalanceAlert = () => { const isLowBalance = useMemo(() => { if (!masterSafeNativeGasBalance) return false; + + const masterThresholds = + selectedAgentConfig.operatingThresholds[WalletOwnerType.Master]; + const tokenSymbol = + CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol; + return ( masterSafeNativeGasBalance < - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] + masterThresholds[WalletType.Safe][tokenSymbol] ); }, [ masterSafeNativeGasBalance, From 31544a65d328493fb88bd9a4fef17b2fbb3f9c0a Mon Sep 17 00:00:00 2001 From: mohandast52 Date: Tue, 17 Dec 2024 16:51:29 +0530 Subject: [PATCH 2/5] fix: adjust XDAI operating threshold for Master EOA wallet --- frontend/config/agents.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/config/agents.ts b/frontend/config/agents.ts index 547f081e..5927933e 100644 --- a/frontend/config/agents.ts +++ b/frontend/config/agents.ts @@ -25,7 +25,7 @@ export const AGENT_CONFIG: { operatingThresholds: { [WalletOwnerType.Master]: { [WalletType.EOA]: { - [TokenSymbol.XDAI]: 1.5, + [TokenSymbol.XDAI]: 0.1, }, [WalletType.Safe]: { [TokenSymbol.XDAI]: 2, From 83e1407fc88b8cddde7f9448bbb683b3f03c6af3 Mon Sep 17 00:00:00 2001 From: mohandast52 Date: Tue, 17 Dec 2024 16:52:57 +0530 Subject: [PATCH 3/5] refactor: remove unnecessary low funds alert check for Safe balance and should show alert regardless of what master safe hs --- .../sections/AlertSections/LowFunds/LowFunds.tsx | 9 --------- 1 file changed, 9 deletions(-) diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx index 68a3eae7..5f4d68d2 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx @@ -39,15 +39,6 @@ export const LowFunds = () => { const tokenSymbol = CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol; - // Funds are transferred from master EOA to master Safe, no need to display - // low safe signer balance alert if "Safe" has funds - if ( - masterSafeNativeGasBalance >= - masterThresholds[WalletType.Safe][tokenSymbol] - ) { - return false; - } - return ( masterEoaNativeGasBalance < masterThresholds[WalletType.EOA][tokenSymbol] ); From d4d632d3b87fab4977c4763f5b015aa4845de568 Mon Sep 17 00:00:00 2001 From: mohandast52 Date: Tue, 17 Dec 2024 17:14:03 +0530 Subject: [PATCH 4/5] refactor: enhance low funds alerts by centralizing threshold logic and improving balance checks --- .../AlertSections/LowFunds/LowFunds.tsx | 16 ++++------ .../LowFunds/LowOperatingBalanceAlert.tsx | 31 +++++++------------ .../LowFunds/LowSafeSignerBalanceAlert.tsx | 3 ++ .../AlertSections/LowFunds/useLowFunds.ts | 3 ++ 4 files changed, 23 insertions(+), 30 deletions(-) diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx index 5f4d68d2..d0fb2525 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx @@ -1,8 +1,7 @@ import { round } from 'lodash'; import { useMemo } from 'react'; -import { CHAIN_CONFIG } from '@/config/chains'; -import { WalletOwnerType, WalletType } from '@/enums/Wallet'; +import { WalletType } from '@/enums/Wallet'; import { useMasterBalances } from '@/hooks/useBalanceContext'; import { useNeedsFunds } from '@/hooks/useNeedsFunds'; import { useServices } from '@/hooks/useServices'; @@ -12,6 +11,7 @@ import { EmptyFunds } from './EmptyFunds'; import { LowOperatingBalanceAlert } from './LowOperatingBalanceAlert'; import { LowSafeSignerBalanceAlert } from './LowSafeSignerBalanceAlert'; import { MainNeedsFunds } from './MainNeedsFunds'; +import { useLowFundsDetails } from './useLowFunds'; export const LowFunds = () => { const { selectedAgentConfig } = useServices(); @@ -24,21 +24,17 @@ export const LowFunds = () => { const { nativeBalancesByChain, olasBalancesByChain, isInitialFunded } = useNeedsFunds(selectedStakingProgramId); + const { tokenSymbol, masterThresholds } = useLowFundsDetails(); const chainId = selectedAgentConfig.evmHomeChainId; - // Check if the safe signer balance is low + // Check if the safe signer (EOA) balance is low const isSafeSignerBalanceLow = useMemo(() => { if (!isBalanceLoaded) return false; if (!masterEoaNativeGasBalance) return false; if (!masterSafeNativeGasBalance) return false; if (!isInitialFunded) return false; - const masterThresholds = - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master]; - const tokenSymbol = - CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol; - return ( masterEoaNativeGasBalance < masterThresholds[WalletType.EOA][tokenSymbol] ); @@ -47,8 +43,8 @@ export const LowFunds = () => { isInitialFunded, masterEoaNativeGasBalance, masterSafeNativeGasBalance, - selectedAgentConfig.evmHomeChainId, - selectedAgentConfig.operatingThresholds, + masterThresholds, + tokenSymbol, ]); // Show the empty funds alert if the agent is not funded diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx index df2d0bbc..ed8f350e 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx @@ -2,8 +2,7 @@ import { Flex, Typography } from 'antd'; import { useMemo } from 'react'; import { CustomAlert } from '@/components/Alert'; -import { CHAIN_CONFIG } from '@/config/chains'; -import { WalletOwnerType, WalletType } from '@/enums/Wallet'; +import { WalletType } from '@/enums/Wallet'; import { useMasterBalances } from '@/hooks/useBalanceContext'; import { useServices } from '@/hooks/useServices'; import { useStore } from '@/hooks/useStore'; @@ -13,33 +12,30 @@ import { useLowFundsDetails } from './useLowFunds'; const { Text, Title } = Typography; +/** + * Alert for low operating (safe) balance + */ export const LowOperatingBalanceAlert = () => { const { storeState } = useStore(); - const { selectedAgentConfig, selectedAgentType } = useServices(); + const { selectedAgentType } = useServices(); const { isLoaded: isBalanceLoaded, masterSafeNativeGasBalance } = useMasterBalances(); - const { chainName, tokenSymbol, masterSafeAddress } = useLowFundsDetails(); + const { chainName, tokenSymbol, masterSafeAddress, masterThresholds } = + useLowFundsDetails(); const isLowBalance = useMemo(() => { if (!masterSafeNativeGasBalance) return false; - - const masterThresholds = - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master]; - const tokenSymbol = - CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol; + if (!masterThresholds) return false; return ( masterSafeNativeGasBalance < masterThresholds[WalletType.Safe][tokenSymbol] ); - }, [ - masterSafeNativeGasBalance, - selectedAgentConfig.evmHomeChainId, - selectedAgentConfig.operatingThresholds, - ]); + }, [masterSafeNativeGasBalance, masterThresholds, tokenSymbol]); if (!isBalanceLoaded) return null; + if (!masterThresholds) return null; if (!storeState?.[`isInitialFunded_${selectedAgentType}`]) return; if (!isLowBalance) return null; @@ -56,12 +52,7 @@ export const LowOperatingBalanceAlert = () => { To run your agent, add at least {` ${ - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][ - CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken - .symbol - ] + masterThresholds[WalletType.Safe][tokenSymbol] } ${tokenSymbol} `} on {chainName} chain to your safe. diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowSafeSignerBalanceAlert.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowSafeSignerBalanceAlert.tsx index 96a5122a..061cd7dd 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowSafeSignerBalanceAlert.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowSafeSignerBalanceAlert.tsx @@ -9,6 +9,9 @@ import { useLowFundsDetails } from './useLowFunds'; const { Text, Title } = Typography; +/** + * Alert for low safe signer (EOA) balance + */ export const LowSafeSignerBalanceAlert = () => { const { chainName, tokenSymbol, masterEoaAddress } = useLowFundsDetails(); const { selectedAgentConfig } = useServices(); diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/useLowFunds.ts b/frontend/components/MainPage/sections/AlertSections/LowFunds/useLowFunds.ts index b97c8f5d..0da3d62d 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/useLowFunds.ts +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/useLowFunds.ts @@ -1,6 +1,7 @@ import { useMemo } from 'react'; import { CHAIN_CONFIG } from '@/config/chains'; +import { WalletOwnerType } from '@/enums/Wallet'; import { useServices } from '@/hooks/useServices'; import { useMasterWalletContext } from '@/hooks/useWallet'; @@ -33,5 +34,7 @@ export const useLowFundsDetails = () => { tokenSymbol: nativeToken.symbol, masterSafeAddress: selectedMasterSafe?.address, masterEoaAddress: masterEoa?.address, + masterThresholds: + selectedAgentConfig.operatingThresholds[WalletOwnerType.Master], }; }; From 828dc185b94a59be26f8d8dd6b0be7f38a0b1801 Mon Sep 17 00:00:00 2001 From: mohandast52 Date: Tue, 17 Dec 2024 18:39:26 +0530 Subject: [PATCH 5/5] refactor: enhance AgentNotRunningButton by adding serviceSafeBalances for improved threshold checks --- .../AgentButton/AgentNotRunningButton.tsx | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx b/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx index 47a68cf7..98f7749f 100644 --- a/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx +++ b/frontend/components/MainPage/header/AgentButton/AgentNotRunningButton.tsx @@ -54,7 +54,7 @@ export const AgentNotRunningButton = () => { updateBalances, } = useBalanceContext(); - const { serviceStakedBalances } = useServiceBalances( + const { serviceStakedBalances, serviceSafeBalances } = useServiceBalances( selectedService?.service_config_id, ); @@ -122,26 +122,34 @@ export const AgentNotRunningButton = () => { return (serviceTotalStakedOlas ?? 0) >= requiredStakedOlas; } - // If was evicted, but can restake - unlock the button + // If was evicted, but can re-stake - unlock the button if (isAgentEvicted && isEligibleForStaking) return true; + // threshold check + const masterThresholds = + selectedAgentConfig.operatingThresholds[WalletOwnerType.Master]; + const tokenSymbol = + CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol; + const agentSafeNativeBalance = serviceSafeBalances?.find( + ({ symbol }) => symbol === tokenSymbol, + )?.balance; + const safeThreshold = masterThresholds[WalletType.Safe][tokenSymbol]; + if (isServiceStaked) { const hasEnoughOlas = (serviceSafeOlasWithStaked ?? 0) >= requiredStakedOlas; + + // @note: Funds are transferred to the agent safe from the master safe. + // Hence, if the agent safe has enough funds, it is considered as enough. const hasEnoughNativeGas = - (masterSafeNativeGasBalance ?? 0) > - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol]; + (masterSafeNativeGasBalance ?? 0) > safeThreshold || + (agentSafeNativeBalance ?? 0) > safeThreshold; return hasEnoughOlas && hasEnoughNativeGas; } const hasEnoughForInitialDeployment = (masterSafeOlasBalance ?? 0) >= requiredStakedOlas && - (masterSafeNativeGasBalance ?? 0) >= - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol]; + (masterSafeNativeGasBalance ?? 0) >= safeThreshold; return hasEnoughForInitialDeployment; }, [ @@ -162,6 +170,7 @@ export const AgentNotRunningButton = () => { selectedAgentConfig.evmHomeChainId, serviceTotalStakedOlas, serviceSafeOlasWithStaked, + serviceSafeBalances, ]); const pauseAllPolling = useCallback(() => {