From 2ed24d121cbc0c8b0c898aee9f2768eff2222b44 Mon Sep 17 00:00:00 2001 From: Atatakai Date: Mon, 23 Dec 2024 18:40:07 +0400 Subject: [PATCH 1/4] fix: check agent safe in low funds, update thresholds --- .../LowFunds/LowOperatingBalanceAlert.tsx | 37 +++++++++++++---- .../MainPage/sections/GasBalanceSection.tsx | 40 +++++++++++++++++-- .../components/YourWalletPage/YourAgent.tsx | 2 +- frontend/components/YourWalletPage/index.tsx | 4 +- frontend/config/agents.ts | 15 ++++++- frontend/constants/serviceTemplates.ts | 12 +++--- 6 files changed, 89 insertions(+), 21 deletions(-) diff --git a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx index 57546fc93..8849d84a4 100644 --- a/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx +++ b/frontend/components/MainPage/sections/AlertSections/LowFunds/LowOperatingBalanceAlert.tsx @@ -3,7 +3,10 @@ import { useMemo } from 'react'; import { CustomAlert } from '@/components/Alert'; import { WalletType } from '@/enums/Wallet'; -import { useMasterBalances } from '@/hooks/useBalanceContext'; +import { + useMasterBalances, + useServiceBalances, +} from '@/hooks/useBalanceContext'; import { useServices } from '@/hooks/useServices'; import { useStore } from '@/hooks/useStore'; @@ -17,22 +20,42 @@ const { Text, Title } = Typography; */ export const LowOperatingBalanceAlert = () => { const { storeState } = useStore(); - const { selectedAgentType } = useServices(); - const { isLoaded: isBalanceLoaded, masterSafeNativeGasBalance } = + const { selectedAgentType, selectedService, selectedAgentConfig } = + useServices(); + const { isLoaded: isBalanceLoaded, isMasterSafeLowOnNativeGas } = useMasterBalances(); + const { serviceSafeBalances } = useServiceBalances( + selectedService?.service_config_id, + ); const { chainName, tokenSymbol, masterSafeAddress, masterThresholds } = useLowFundsDetails(); + const serviceSafeNativeBalance = useMemo( + () => + serviceSafeBalances?.find( + ({ isNative, evmChainId }) => + isNative && evmChainId === selectedAgentConfig.evmHomeChainId, + ), + [serviceSafeBalances, selectedAgentConfig], + ); + const isLowBalance = useMemo(() => { - if (!masterSafeNativeGasBalance) return false; if (!masterThresholds) return false; + if (!serviceSafeNativeBalance) return false; + // Check both master and agent safes return ( - masterSafeNativeGasBalance < - masterThresholds[WalletType.Safe][tokenSymbol] + isMasterSafeLowOnNativeGas && + serviceSafeNativeBalance.balance < + masterThresholds[WalletType.Safe][tokenSymbol] ); - }, [masterSafeNativeGasBalance, masterThresholds, tokenSymbol]); + }, [ + isMasterSafeLowOnNativeGas, + masterThresholds, + serviceSafeNativeBalance, + tokenSymbol, + ]); if (!isBalanceLoaded) return null; if (!masterThresholds) return null; diff --git a/frontend/components/MainPage/sections/GasBalanceSection.tsx b/frontend/components/MainPage/sections/GasBalanceSection.tsx index c3270571c..6ac98fd34 100644 --- a/frontend/components/MainPage/sections/GasBalanceSection.tsx +++ b/frontend/components/MainPage/sections/GasBalanceSection.tsx @@ -6,9 +6,11 @@ import styled from 'styled-components'; import { COLOR } from '@/constants/colors'; import { EXPLORER_URL_BY_MIDDLEWARE_CHAIN } from '@/constants/urls'; +import { WalletOwnerType, WalletType } from '@/enums/Wallet'; import { useBalanceContext, useMasterBalances, + useServiceBalances, } from '@/hooks/useBalanceContext'; import { useElectronApi } from '@/hooks/useElectronApi'; import { useServices } from '@/hooks/useServices'; @@ -41,23 +43,53 @@ const FineDot = styled(Dot)` const BalanceStatus = () => { const { isLoaded: isBalanceLoaded } = useBalanceContext(); - const { isFetched: isServicesLoaded, selectedAgentType } = useServices(); + const { + isFetched: isServicesLoaded, + selectedAgentType, + selectedAgentConfig, + selectedService, + } = useServices(); const { storeState } = useStore(); const { showNotification } = useElectronApi(); const { isMasterSafeLowOnNativeGas } = useMasterBalances(); + const { serviceSafeBalances } = useServiceBalances( + selectedService?.service_config_id, + ); const [isLowBalanceNotificationShown, setIsLowBalanceNotificationShown] = useState(false); + const serviceSafeNativeBalance = useMemo( + () => + serviceSafeBalances?.find( + ({ isNative, evmChainId }) => + isNative && evmChainId === selectedAgentConfig.evmHomeChainId, + ), + [serviceSafeBalances, selectedAgentConfig], + ); + + const isLowFunds = useMemo(() => { + if (!serviceSafeNativeBalance) return false; + return ( + isMasterSafeLowOnNativeGas && + serviceSafeNativeBalance.balance < + selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][ + WalletType.Safe + ][serviceSafeNativeBalance.symbol] + ); + }, []); + // show notification if balance is too low useEffect(() => { if (!isBalanceLoaded || !isServicesLoaded) return; if (!showNotification) return; if (!storeState?.[selectedAgentType]?.isInitialFunded) return; + if (!serviceSafeNativeBalance) return; - if (isMasterSafeLowOnNativeGas && !isLowBalanceNotificationShown) { + // Check both master and agent safes + if (isLowFunds && !isLowBalanceNotificationShown) { showNotification('Operating balance is too low.'); setIsLowBalanceNotificationShown(true); } @@ -82,12 +114,12 @@ const BalanceStatus = () => { return { statusName: 'Loading...', StatusComponent: EmptyDot }; } - if (isMasterSafeLowOnNativeGas) { + if (isLowFunds) { return { statusName: 'Too low', StatusComponent: EmptyDot }; } return { statusName: 'Fine', StatusComponent: FineDot }; - }, [isBalanceLoaded, isMasterSafeLowOnNativeGas, isServicesLoaded]); + }, [isBalanceLoaded, isLowFunds, isServicesLoaded]); const { statusName, StatusComponent } = status; return ( diff --git a/frontend/components/YourWalletPage/YourAgent.tsx b/frontend/components/YourWalletPage/YourAgent.tsx index c92027d24..7dcd307fd 100644 --- a/frontend/components/YourWalletPage/YourAgent.tsx +++ b/frontend/components/YourWalletPage/YourAgent.tsx @@ -261,7 +261,7 @@ const YourAgentWalletBreakdown = () => { list={serviceSafeNativeBalances.map((balance) => ({ left: {balance.symbol}, leftClassName: 'text-sm', - right: `${balanceFormat(balance.balance, 2)} ${balance.symbol}`, + right: `${balanceFormat(balance.balance, 4)} ${balance.symbol}`, }))} parentStyle={infoBreakdownParentStyle} /> diff --git a/frontend/components/YourWalletPage/index.tsx b/frontend/components/YourWalletPage/index.tsx index db2dec913..c386aab47 100644 --- a/frontend/components/YourWalletPage/index.tsx +++ b/frontend/components/YourWalletPage/index.tsx @@ -148,7 +148,7 @@ const MasterSafeNativeBalance = () => { ), leftClassName: 'text-light', - right: `${balanceFormat(masterSafeNativeBalance, 2)} ${nativeTokenSymbol}`, + right: `${balanceFormat(masterSafeNativeBalance, 4)} ${nativeTokenSymbol}`, }, ]} parentStyle={infoBreakdownParentStyle} @@ -191,7 +191,7 @@ const MasterEoaSignerNativeBalance = () => { /> ), leftClassName: 'text-light', - right: `${balanceFormat(masterEoaBalance, 3)} ${nativeTokenSymbol}`, + right: `${balanceFormat(masterEoaBalance, 4)} ${nativeTokenSymbol}`, }, ]} parentStyle={infoBreakdownParentStyle} diff --git a/frontend/config/agents.ts b/frontend/config/agents.ts index e6dde164a..f89b868f1 100644 --- a/frontend/config/agents.ts +++ b/frontend/config/agents.ts @@ -1,4 +1,5 @@ import { MiddlewareChain } from '@/client'; +import { SERVICE_TEMPLATES } from '@/constants/serviceTemplates'; import { AgentType } from '@/enums/Agent'; import { EvmChainId } from '@/enums/Chain'; import { TokenSymbol } from '@/enums/Token'; @@ -7,10 +8,15 @@ import { MemeooorBaseService } from '@/service/agents/Memeooor'; import { PredictTraderService } from '@/service/agents/PredictTrader'; // import { OptimusService } from '@/service/agents/Optimus'; import { AgentConfig } from '@/types/Agent'; +import { formatEther } from '@/utils/numberFormatters'; // TODO: complete this config // TODO: add funding requirements +const traderFundRequirements = SERVICE_TEMPLATES.find( + (template) => template.agentType === AgentType.PredictTrader, +)?.configurations[MiddlewareChain.GNOSIS].fund_requirements; + export const AGENT_CONFIG: { [key in AgentType]: AgentConfig; } = { @@ -28,7 +34,14 @@ export const AGENT_CONFIG: { [TokenSymbol.XDAI]: 0.1, }, [WalletType.Safe]: { - [TokenSymbol.XDAI]: 2, + [TokenSymbol.XDAI]: Number( + formatEther( + `${ + (traderFundRequirements?.agent ?? 0) + + (traderFundRequirements?.safe ?? 0) + }`, + ), + ), }, }, [WalletOwnerType.Agent]: { diff --git a/frontend/constants/serviceTemplates.ts b/frontend/constants/serviceTemplates.ts index 3e001ff97..9d3bf7891 100644 --- a/frontend/constants/serviceTemplates.ts +++ b/frontend/constants/serviceTemplates.ts @@ -23,11 +23,11 @@ export const SERVICE_TEMPLATES: ServiceTemplate[] = [ use_staking: true, use_mech_marketplace: false, // TODO: pull fund requirements from staking program config - cost_of_bond: 10000000000000000, - monthly_gas_estimate: 10000000000000000000, + cost_of_bond: +parseEther(0.001), + monthly_gas_estimate: +parseEther(10), fund_requirements: { - agent: 100000000000000000, - safe: 5000000000000000000, + agent: +parseEther(0.1), + safe: +parseEther(5), }, }, }, @@ -174,10 +174,10 @@ export const SERVICE_TEMPLATES: ServiceTemplate[] = [ threshold: 1, use_staking: true, cost_of_bond: +parseEther(50), - monthly_gas_estimate: +parseEther(0.03), + monthly_gas_estimate: +parseEther(0.045), fund_requirements: { agent: +parseEther(0.001), - safe: +parseEther(0.001), + safe: +parseEther(0.0125), }, }, }, From c684b251bf29fef6bc9abd13e667830e29f0e805 Mon Sep 17 00:00:00 2001 From: Atatakai Date: Mon, 23 Dec 2024 18:42:05 +0400 Subject: [PATCH 2/4] release: 0.2.0-rc53 --- package.json | 2 +- pyproject.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f65496eea..c837ffe1a 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,7 @@ "start:frontend": "cd frontend && yarn start", "test:frontend": "cd frontend && yarn test" }, - "version": "0.2.0-rc52", + "version": "0.2.0-rc53", "engine": { "node": ">=20", "yarn": ">=1.22.0", diff --git a/pyproject.toml b/pyproject.toml index 577585811..fb31f0da5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "olas-operate-middleware" -version = "0.2.0-rc52" +version = "0.2.0-rc53" description = "" authors = ["David Vilela ", "Viraj Patel "] readme = "README.md" From 6a5c89ae06563ca3ec58814342d234630fa6c78b Mon Sep 17 00:00:00 2001 From: Atatakai Date: Mon, 23 Dec 2024 18:59:25 +0400 Subject: [PATCH 3/4] fix: linter --- frontend/components/MainPage/sections/GasBalanceSection.tsx | 6 ++++-- frontend/types/ElectronApi.ts | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frontend/components/MainPage/sections/GasBalanceSection.tsx b/frontend/components/MainPage/sections/GasBalanceSection.tsx index 6ac98fd34..199a728b6 100644 --- a/frontend/components/MainPage/sections/GasBalanceSection.tsx +++ b/frontend/components/MainPage/sections/GasBalanceSection.tsx @@ -101,12 +101,14 @@ const BalanceStatus = () => { } }, [ isBalanceLoaded, - isServicesLoaded, isLowBalanceNotificationShown, + isLowFunds, isMasterSafeLowOnNativeGas, + isServicesLoaded, + selectedAgentType, + serviceSafeNativeBalance, showNotification, storeState, - selectedAgentType, ]); const status = useMemo(() => { diff --git a/frontend/types/ElectronApi.ts b/frontend/types/ElectronApi.ts index dcb6b01fa..e181890c4 100644 --- a/frontend/types/ElectronApi.ts +++ b/frontend/types/ElectronApi.ts @@ -15,8 +15,8 @@ export type ElectronStore = { agentEvictionAlertShown?: boolean; // Each agent has its own settings - trader: AgentSettings; - memeooorr: AgentSettings; + trader?: AgentSettings; + memeooorr?: AgentSettings; }; export type ElectronTrayIconStatus = From cc845b466f166fc78ed99c58d527b72e0cfb0312 Mon Sep 17 00:00:00 2001 From: Atatakai Date: Mon, 23 Dec 2024 19:45:53 +0400 Subject: [PATCH 4/4] fix: check correct value --- .../components/MainPage/sections/GasBalanceSection.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frontend/components/MainPage/sections/GasBalanceSection.tsx b/frontend/components/MainPage/sections/GasBalanceSection.tsx index 199a728b6..9bc6a72d1 100644 --- a/frontend/components/MainPage/sections/GasBalanceSection.tsx +++ b/frontend/components/MainPage/sections/GasBalanceSection.tsx @@ -79,7 +79,11 @@ const BalanceStatus = () => { WalletType.Safe ][serviceSafeNativeBalance.symbol] ); - }, []); + }, [ + isMasterSafeLowOnNativeGas, + selectedAgentConfig.operatingThresholds, + serviceSafeNativeBalance, + ]); // show notification if balance is too low useEffect(() => { @@ -96,14 +100,13 @@ const BalanceStatus = () => { // If it has already been shown and the balance has increased, // should show the notification again if it goes below the threshold. - if (!isMasterSafeLowOnNativeGas && isLowBalanceNotificationShown) { + if (!isLowFunds && isLowBalanceNotificationShown) { setIsLowBalanceNotificationShown(false); } }, [ isBalanceLoaded, isLowBalanceNotificationShown, isLowFunds, - isMasterSafeLowOnNativeGas, isServicesLoaded, selectedAgentType, serviceSafeNativeBalance,