Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: “Operating balance it too low” is shown on Trader agent, without being true. User gets 2 warning messages #601

Merged
merged 5 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const AgentNotRunningButton = () => {
updateBalances,
} = useBalanceContext();

const { serviceStakedBalances } = useServiceBalances(
const { serviceStakedBalances, serviceSafeBalances } = useServiceBalances(
selectedService?.service_config_id,
);

Expand Down Expand Up @@ -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;
}, [
Expand All @@ -162,6 +170,7 @@ export const AgentNotRunningButton = () => {
selectedAgentConfig.evmHomeChainId,
serviceTotalStakedOlas,
serviceSafeOlasWithStaked,
serviceSafeBalances,
]);

const pauseAllPolling = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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]
mohandast52 marked this conversation as resolved.
Show resolved Hide resolved
);
}, [
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;

Expand All @@ -52,12 +52,7 @@ export const LowOperatingBalanceAlert = () => {
<Text>
To run your agent, add at least
<Text strong>{` ${
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][
CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken
.symbol
]
masterThresholds[WalletType.Safe][tokenSymbol]
} ${tokenSymbol} `}</Text>
on {chainName} chain to your safe.
</Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -33,5 +34,7 @@ export const useLowFundsDetails = () => {
tokenSymbol: nativeToken.symbol,
masterSafeAddress: selectedMasterSafe?.address,
masterEoaAddress: masterEoa?.address,
masterThresholds:
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master],
};
};
2 changes: 1 addition & 1 deletion frontend/config/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading