diff --git a/frontend/components/MainPage/index.tsx b/frontend/components/MainPage/index.tsx index 0e2be2e5..893010af 100644 --- a/frontend/components/MainPage/index.tsx +++ b/frontend/components/MainPage/index.tsx @@ -19,25 +19,6 @@ export const Main = () => { const isStakingContractSectionEnabled = useFeatureFlag( 'staking-contract-section', ); - // const { refetch: updateServicesState } = useServices(); - // const { - // updateBalances, - // isLoaded: isBalanceLoaded, - // setIsLoaded: setIsBalanceLoaded, - // } = useBalanceContext(); - - // TODO: reintroduce later, non critical - // useEffect(() => { - // if (!isBalanceLoaded) { - // updateServicesState?.().then(() => updateBalances()); - // setIsBalanceLoaded(true); - // } - // }, [ - // isBalanceLoaded, - // setIsBalanceLoaded, - // updateBalances, - // updateServicesState, - // ]); return ( { - const { selectedService } = useServices(); + const { selectedService, selectedAgentConfig } = useServices(); const { isLoaded: isBalanceLoaded } = useBalanceContext(); const { masterWalletBalances } = useMasterBalances(); const { serviceStakedBalances, serviceWalletBalances } = useServiceBalances( @@ -36,10 +36,13 @@ export const MainOlasBalance = () => { const isBalanceBreakdownEnabled = useFeatureFlag('manage-wallet'); const displayedBalance = useMemo(() => { - // olas across master wallets, safes and eoa + // olas across master wallet -- safes and eoa -- on relevant chains for agent const masterWalletOlasBalance = masterWalletBalances?.reduce( - (acc, { symbol, balance }) => { - if (symbol === TokenSymbol.OLAS) { + (acc, { symbol, balance, evmChainId }) => { + if ( + symbol === TokenSymbol.OLAS && + selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId) + ) { return acc + Number(balance); } return acc; @@ -47,10 +50,13 @@ export const MainOlasBalance = () => { 0, ); - // olas across all service wallets + // olas across all wallets owned by selected service const serviceWalletOlasBalance = serviceWalletBalances?.reduce( - (acc, { symbol, balance }) => { - if (symbol === TokenSymbol.OLAS) { + (acc, { symbol, balance, evmChainId }) => { + if ( + symbol === TokenSymbol.OLAS && + selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId) + ) { return acc + Number(balance); } return acc; @@ -58,9 +64,12 @@ export const MainOlasBalance = () => { 0, ); - // olas staked across all services + // olas staked across services on relevant chains for agent const serviceStakedOlasBalance = serviceStakedBalances?.reduce( - (acc, { olasBondBalance, olasDepositBalance }) => { + (acc, { olasBondBalance, olasDepositBalance, evmChainId }) => { + if (!selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId)) { + return acc; + } return acc + Number(olasBondBalance) + Number(olasDepositBalance); }, 0, @@ -73,7 +82,12 @@ export const MainOlasBalance = () => { ]); return balanceFormat(totalOlasBalance, 2); - }, [masterWalletBalances, serviceStakedBalances, serviceWalletBalances]); + }, [ + masterWalletBalances, + selectedAgentConfig.requiresAgentSafesOn, + serviceStakedBalances, + serviceWalletBalances, + ]); return ( { + const masterSafeBalanceOnHomeChain = useMemo(() => { if (!isBalanceLoaded) return; if (isNil(masterSafeBalances) || isEmpty(masterSafeBalances)) return; return masterSafeBalances.reduce( @@ -61,14 +59,16 @@ const AlertInsufficientMigrationFunds = ({ if (!isAllStakingContractDetailsRecordLoaded) return null; if (isNil(requiredStakedOlas)) return null; - if (isNil(safeBalance?.[TokenSymbol.OLAS])) return null; + if (isNil(masterSafeBalanceOnHomeChain?.[TokenSymbol.OLAS])) return null; - if (isNil(totalStakedOlasBalance)) return null; + if (isNil(totalStakedOlasBalanceOnHomeChain)) return null; - const requiredOlasDeposit = isServiceStaked - ? requiredStakedOlas - - (totalStakedOlasBalance + safeBalance[TokenSymbol.OLAS]) // when staked - : requiredStakedOlas - safeBalance[TokenSymbol.OLAS]; // when not staked + const requiredOlasDeposit = + requiredStakedOlas - + sum([ + masterSafeBalanceOnHomeChain[TokenSymbol.OLAS], + totalStakedOlasBalanceOnHomeChain, + ]); const homeChainId = selectedAgentConfig.evmHomeChainId; const nativeTokenSymbol = getNativeTokenSymbol(homeChainId); @@ -76,9 +76,9 @@ const AlertInsufficientMigrationFunds = ({ ? selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ WalletType.Safe ][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] - - (safeBalance[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance + (masterSafeBalanceOnHomeChain[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance : (serviceFundRequirements[homeChainId]?.[nativeTokenSymbol] || 0) - - (safeBalance[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements + (masterSafeBalanceOnHomeChain[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements return ( ( () => - (service && getServiceTemplate(service.hash)) ?? SERVICE_TEMPLATES.find( (template) => template.agentType === selectedAgentType, ), - [selectedAgentType, service], + [selectedAgentType], ); const { setIsPaused: setIsBalancePollingPaused } = useBalanceContext(); diff --git a/frontend/components/YourWalletPage/index.tsx b/frontend/components/YourWalletPage/index.tsx index c386aab4..45b724a2 100644 --- a/frontend/components/YourWalletPage/index.tsx +++ b/frontend/components/YourWalletPage/index.tsx @@ -72,14 +72,18 @@ const Address = () => { }; const OlasBalance = () => { + const { selectedAgentConfig } = useServices(); const { totalStakedOlasBalance } = useBalanceContext(); const { masterWalletBalances } = useMasterBalances(); - const { middlewareChain, evmHomeChainId } = useYourWallet(); + const { middlewareChain } = useYourWallet(); const masterSafeOlasBalance = masterWalletBalances ?.filter( - ({ symbol, evmChainId }) => - symbol === TokenSymbol.OLAS && evmChainId === evmHomeChainId, + (walletBalance) => + walletBalance.symbol === TokenSymbol.OLAS && + selectedAgentConfig.requiresMasterSafesOn.includes( + walletBalance.evmChainId, + ), ) .reduce((acc, balance) => acc + balance.balance, 0);