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(() => { diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowFunds.tsx index a534b8fd..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,39 +24,27 @@ 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; - // Funds are transferred from master EOA to master Safe, no need to display - // low safe signer balance alert if EOA has funds - if ( - masterSafeNativeGasBalance >= - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] - ) - return false; - return ( - masterEoaNativeGasBalance < - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.EOA - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] + masterEoaNativeGasBalance < masterThresholds[WalletType.EOA][tokenSymbol] ); }, [ isBalanceLoaded, 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 76747ef6..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,29 +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; + if (!masterThresholds) return false; + return ( masterSafeNativeGasBalance < - selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ - WalletType.Safe - ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] + 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; @@ -52,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], }; }; 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,