Skip to content

Commit

Permalink
Merge pull request #627 from valory-xyz/tanya/low-balance-fixes
Browse files Browse the repository at this point in the history
fix: check agent safe in low funds, update thresholds
  • Loading branch information
Tanya-atatakai authored Dec 24, 2024
2 parents 1f152fb + 9233ec7 commit 449b58e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
Expand Down
53 changes: 45 additions & 8 deletions frontend/components/MainPage/sections/GasBalanceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -41,53 +43,88 @@ 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]
);
}, [
isMasterSafeLowOnNativeGas,
selectedAgentConfig.operatingThresholds,
serviceSafeNativeBalance,
]);

// 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);
}

// 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,
isServicesLoaded,
isLowBalanceNotificationShown,
isMasterSafeLowOnNativeGas,
isLowFunds,
isServicesLoaded,
selectedAgentType,
serviceSafeNativeBalance,
showNotification,
storeState,
selectedAgentType,
]);

const status = useMemo(() => {
if (!isBalanceLoaded || !isServicesLoaded) {
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 (
Expand Down
2 changes: 1 addition & 1 deletion frontend/components/YourWalletPage/YourAgent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ const YourAgentWalletBreakdown = () => {
list={serviceSafeNativeBalances.map((balance) => ({
left: <strong>{balance.symbol}</strong>,
leftClassName: 'text-sm',
right: `${balanceFormat(balance.balance, 2)} ${balance.symbol}`,
right: `${balanceFormat(balance.balance, 4)} ${balance.symbol}`,
}))}
parentStyle={infoBreakdownParentStyle}
/>
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/YourWalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const MasterSafeNativeBalance = () => {
</Text>
),
leftClassName: 'text-light',
right: `${balanceFormat(masterSafeNativeBalance, 2)} ${nativeTokenSymbol}`,
right: `${balanceFormat(masterSafeNativeBalance, 4)} ${nativeTokenSymbol}`,
},
]}
parentStyle={infoBreakdownParentStyle}
Expand Down Expand Up @@ -191,7 +191,7 @@ const MasterEoaSignerNativeBalance = () => {
/>
),
leftClassName: 'text-light',
right: `${balanceFormat(masterEoaBalance, 3)} ${nativeTokenSymbol}`,
right: `${balanceFormat(masterEoaBalance, 4)} ${nativeTokenSymbol}`,
},
]}
parentStyle={infoBreakdownParentStyle}
Expand Down
15 changes: 14 additions & 1 deletion frontend/config/agents.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
} = {
Expand All @@ -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]: {
Expand Down
12 changes: 6 additions & 6 deletions frontend/constants/serviceTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
},
Expand Down Expand Up @@ -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),
},
},
},
Expand Down
4 changes: 2 additions & 2 deletions frontend/types/ElectronApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 449b58e

Please sign in to comment.