Skip to content

Commit

Permalink
feat: implement operating thresholds for wallet types in agent config…
Browse files Browse the repository at this point in the history
…uration
  • Loading branch information
truemiller committed Dec 13, 2024
1 parent 922a1f6 commit a80a22c
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { isNil, sum } from 'lodash';
import { useCallback, useMemo } from 'react';

import { MiddlewareDeploymentStatus } from '@/client';
import { CHAIN_CONFIG } from '@/config/chains';
import { MechType } from '@/config/mechs';
import { STAKING_PROGRAMS } from '@/config/stakingPrograms';
import { SERVICE_TEMPLATES } from '@/constants/serviceTemplates';
import { LOW_MASTER_SAFE_BALANCE } from '@/constants/thresholds';
import { TokenSymbol } from '@/enums/Token';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
import {
useBalanceContext,
useMasterBalances,
Expand Down Expand Up @@ -123,13 +124,19 @@ export const AgentNotRunningButton = () => {
const hasEnoughOlas =
(serviceSafeOlasWithStaked ?? 0) >= requiredStakedOlas;
const hasEnoughNativeGas =
(masterSafeNativeGasBalance ?? 0) > LOW_MASTER_SAFE_BALANCE;
(masterSafeNativeGasBalance ?? 0) >
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol];
return hasEnoughOlas && hasEnoughNativeGas;
}

const hasEnoughForInitialDeployment =
(masterSafeOlasBalance ?? 0) >= requiredStakedOlas &&
(masterSafeNativeGasBalance ?? 0) >= LOW_MASTER_SAFE_BALANCE;
(masterSafeNativeGasBalance ?? 0) >=
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][TokenSymbol.XDAI];

return hasEnoughForInitialDeployment;
}, [
Expand All @@ -145,6 +152,7 @@ export const AgentNotRunningButton = () => {
isEligibleForStaking,
isAgentEvicted,
masterSafeNativeGasBalance,
selectedAgentConfig.operatingThresholds,
selectedAgentConfig.evmHomeChainId,
serviceTotalStakedOlas,
serviceSafeOlasWithStaked,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Divider, Flex, Typography } from 'antd';

import { CustomAlert } from '@/components/Alert';
import { COLOR } from '@/constants/colors';
import { LOW_AGENT_SAFE_BALANCE } from '@/constants/thresholds';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
import { useServices } from '@/hooks/useServices';

import { FundsToActivate } from './FundsToActivate';
import { InlineBanner } from './InlineBanner';
Expand All @@ -16,6 +17,7 @@ const PurpleDivider = () => (

export const EmptyFunds = () => {
const { chainName, tokenSymbol, masterEoaAddress } = useLowFundsDetails();
const { selectedAgentConfig } = useServices();

return (
<CustomAlert
Expand All @@ -29,7 +31,11 @@ export const EmptyFunds = () => {

<Text>
To keep your agent operational, add
<Text strong>{` ${LOW_AGENT_SAFE_BALANCE} ${tokenSymbol} `}</Text>
<Text strong>{` ${
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.EOA
][tokenSymbol]
} ${tokenSymbol} `}</Text>
on {chainName} chain to the safe signer.
</Text>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { round } from 'lodash';
import { useMemo } from 'react';

import { LOW_AGENT_SAFE_BALANCE } from '@/constants/thresholds';
import { CHAIN_CONFIG } from '@/config/chains';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
import { useMasterBalances } from '@/hooks/useBalanceContext';
import { useNeedsFunds } from '@/hooks/useNeedsFunds';
import { useServices } from '@/hooks/useServices';
Expand Down Expand Up @@ -32,8 +33,19 @@ export const LowFunds = () => {
if (!masterEoaNativeGasBalance) return false;
if (!storeState?.isInitialFunded) return false;

return masterEoaNativeGasBalance < LOW_AGENT_SAFE_BALANCE;
}, [isBalanceLoaded, masterEoaNativeGasBalance, storeState]);
return (
masterEoaNativeGasBalance <
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.EOA
][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol]
);
}, [
isBalanceLoaded,
masterEoaNativeGasBalance,
selectedAgentConfig.evmHomeChainId,
selectedAgentConfig.operatingThresholds,
storeState?.isInitialFunded,
]);

// Show the empty funds alert if the agent is not funded
const isEmptyFundsVisible = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Flex, Typography } from 'antd';
import { useMemo } from 'react';

import { CustomAlert } from '@/components/Alert';
import { LOW_MASTER_SAFE_BALANCE } from '@/constants/thresholds';
import { CHAIN_CONFIG } from '@/config/chains';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
import { useMasterBalances } from '@/hooks/useBalanceContext';
import { useServices } from '@/hooks/useServices';
import { useStore } from '@/hooks/useStore';

import { InlineBanner } from './InlineBanner';
Expand All @@ -13,15 +15,25 @@ const { Text, Title } = Typography;

export const LowOperatingBalanceAlert = () => {
const { storeState } = useStore();
const { selectedAgentConfig } = useServices();
const { isLoaded: isBalanceLoaded, masterSafeNativeGasBalance } =
useMasterBalances();

const { chainName, tokenSymbol, masterSafeAddress } = useLowFundsDetails();

const isLowBalance = useMemo(() => {
if (!masterSafeNativeGasBalance) return false;
return masterSafeNativeGasBalance < LOW_MASTER_SAFE_BALANCE;
}, [masterSafeNativeGasBalance]);
return (
masterSafeNativeGasBalance <
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol]
);
}, [
masterSafeNativeGasBalance,
selectedAgentConfig.evmHomeChainId,
selectedAgentConfig.operatingThresholds,
]);

if (!isBalanceLoaded) return null;
if (!storeState?.isInitialFunded) return;
Expand All @@ -39,7 +51,14 @@ export const LowOperatingBalanceAlert = () => {
</Title>
<Text>
To run your agent, add at least
<Text strong>{` ${LOW_MASTER_SAFE_BALANCE} ${tokenSymbol} `}</Text>
<Text strong>{` ${
selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][
CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken
.symbol
]
} ${tokenSymbol} `}</Text>
on {chainName} chain to your safe.
</Text>
<Text>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Flex, Typography } from 'antd';

import { CustomAlert } from '@/components/Alert';
import { LOW_AGENT_SAFE_BALANCE } from '@/constants/thresholds';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
import { useServices } from '@/hooks/useServices';

import { InlineBanner } from './InlineBanner';
import { useLowFundsDetails } from './useLowFunds';
Expand All @@ -10,6 +11,7 @@ const { Text, Title } = Typography;

export const LowSafeSignerBalanceAlert = () => {
const { chainName, tokenSymbol, masterEoaAddress } = useLowFundsDetails();
const { selectedAgentConfig } = useServices();

return (
<CustomAlert
Expand All @@ -23,7 +25,9 @@ export const LowSafeSignerBalanceAlert = () => {
</Title>
<Text>
To keep your agent operational, add
<Text strong>{` ${LOW_AGENT_SAFE_BALANCE} ${tokenSymbol} `}</Text>
<Text
strong
>{` ${selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][WalletType.EOA][tokenSymbol]} ${tokenSymbol} `}</Text>
on {chainName} chain to the safe signer.
</Text>
<Text>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { isEmpty, isNil } from 'lodash';
import { useMemo } from 'react';

import { CustomAlert } from '@/components/Alert';
import { CHAIN_CONFIG } from '@/config/chains';
import { getNativeTokenSymbol } from '@/config/tokens';
import { LOW_MASTER_SAFE_BALANCE } from '@/constants/thresholds';
import { StakingProgramId } from '@/enums/StakingProgram';
import { TokenSymbol } from '@/enums/Token';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
import {
useBalanceContext,
useMasterBalances,
Expand Down Expand Up @@ -72,7 +73,10 @@ const AlertInsufficientMigrationFunds = ({
const homeChainId = selectedAgentConfig.evmHomeChainId;
const nativeTokenSymbol = getNativeTokenSymbol(homeChainId);
const requiredNativeTokenDeposit = isInitialFunded
? LOW_MASTER_SAFE_BALANCE - (safeBalance[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance
? selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] -
(safeBalance[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance
: (serviceFundRequirements[homeChainId]?.[nativeTokenSymbol] || 0) -
(safeBalance[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements

Expand Down
43 changes: 41 additions & 2 deletions frontend/config/agents.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { MiddlewareChain } from '@/client';
import { AgentType } from '@/enums/Agent';
import { EvmChainId } from '@/enums/Chain';
import { TokenSymbol } from '@/enums/Token';
import { WalletOwnerType, WalletType } from '@/enums/Wallet';
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
Expand All @@ -18,7 +21,25 @@ export const AGENT_CONFIG: {
middlewareHomeChainId: MiddlewareChain.GNOSIS,
requiresAgentSafesOn: [EvmChainId.Gnosis],
agentSafeFundingRequirements: {
[EvmChainId.Gnosis]: 100000000000000000,
[EvmChainId.Gnosis]: +formatEther(0.1),
},
operatingThresholds: {
[WalletOwnerType.Master]: {
[WalletType.EOA]: {
[TokenSymbol.XDAI]: +formatEther(1.5),
},
[WalletType.Safe]: {
[TokenSymbol.XDAI]: +formatEther(2),
},
},
[WalletOwnerType.Agent]: {
[WalletType.EOA]: {
[TokenSymbol.XDAI]: +formatEther(0.1),
},
[WalletType.Safe]: {
[TokenSymbol.XDAI]: +formatEther(0.1),
},
},
},
requiresMasterSafesOn: [EvmChainId.Gnosis],
serviceApi: PredictTraderService,
Expand All @@ -44,7 +65,25 @@ export const AGENT_CONFIG: {
middlewareHomeChainId: MiddlewareChain.BASE,
requiresAgentSafesOn: [EvmChainId.Base],
agentSafeFundingRequirements: {
[EvmChainId.Base]: 1000000000000000, // 0.001 eth
[EvmChainId.Base]: +formatEther(0.03),
},
operatingThresholds: {
[WalletOwnerType.Master]: {
[WalletType.EOA]: {
[TokenSymbol.ETH]: +formatEther(0.0001),
},
[WalletType.Safe]: {
[TokenSymbol.ETH]: +formatEther(0.0001),
},
},
[WalletOwnerType.Agent]: {
[WalletType.EOA]: {
[TokenSymbol.ETH]: +formatEther(0.0001),
},
[WalletType.Safe]: {
[TokenSymbol.ETH]: +formatEther(0.0001),
},
},
},
requiresMasterSafesOn: [EvmChainId.Base],
serviceApi: MemeooorBaseService,
Expand Down
1 change: 0 additions & 1 deletion frontend/constants/thresholds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,3 @@ export const MIN_ETH_BALANCE_THRESHOLDS: Record<

// TODO: update to support multi-chain, very poor implementation
export const LOW_AGENT_SAFE_BALANCE = 1.5;
export const LOW_MASTER_SAFE_BALANCE = 2;
9 changes: 9 additions & 0 deletions frontend/types/Agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MiddlewareChain } from '@/client';
import { EvmChainId } from '@/enums/Chain';
import { TokenSymbol } from '@/enums/Token';
import { WalletOwnerType } from '@/enums/Wallet';
import { PredictTraderService } from '@/service/agents/PredictTrader';

export type StakedAgentServiceInstance = PredictTraderService;
Expand All @@ -13,4 +15,11 @@ export type AgentConfig = {
serviceApi: typeof PredictTraderService;
displayName: string;
description: string;
operatingThresholds: {
[owner: string | WalletOwnerType]: {
[walletType: string | WalletOwnerType]: {
[tokenSymbol: string | TokenSymbol]: number;
};
};
};
};

0 comments on commit a80a22c

Please sign in to comment.