Skip to content

Commit

Permalink
Merge pull request #589 from valory-xyz/tanya/fix-needs-funds
Browse files Browse the repository at this point in the history
fix: make isInitialFunded by agent type
  • Loading branch information
Tanya-atatakai authored Dec 15, 2024
2 parents 28a587d + 873217c commit 24e922d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 14 deletions.
15 changes: 14 additions & 1 deletion electron/store.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Store = require('electron-store');

// set schema to validate store data
const schema = {
isInitialFunded: { type: 'boolean', default: false }, // TODO: reconsider this default, can be problematic if user has already funded prior to implementation
firstStakingRewardAchieved: { type: 'boolean', default: false },
firstRewardNotificationShown: { type: 'boolean', default: false },
agentEvictionAlertShown: { type: 'boolean', default: false },
Expand All @@ -11,6 +11,8 @@ const schema = {

// agent settings
lastSelectedAgentType: { type: 'string', default: 'trader' },
isInitialFunded_trader: { type: 'boolean', default: false },
isInitialFunded_memeooorr: { type: 'boolean', default: false },
};

/**
Expand All @@ -22,6 +24,17 @@ const schema = {
const setupStoreIpc = (ipcMain, mainWindow) => {
const store = new Store({ schema });

/**
* isInitialFunded Migration
*
* Writes the old isInitialFunded value to the new isInitialFunded_trader
* And removes it from the store afterward
*/
if (store.has('isInitialFunded')) {
store.set('isInitialFunded_trader', store.get('isInitialFunded'));
store.delete('isInitialFunded');
}

store.onDidAnyChange((data) => {
if (mainWindow?.webContents)
mainWindow.webContents.send('store-changed', data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export const AgentNotRunningButton = () => {
walletBalanceResult.evmChainId === selectedAgentConfig.evmHomeChainId,
)?.balance;

if (service && storeState?.isInitialFunded && isServiceStaked) {
if (
service &&
storeState?.[`isInitialFunded_${selectedAgentType}`] &&
isServiceStaked
) {
return (serviceTotalStakedOlas ?? 0) >= requiredStakedOlas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { MainNeedsFunds } from './MainNeedsFunds';
export const LowFunds = () => {
const { storeState } = useStore();

const { selectedAgentConfig } = useServices();
const { selectedAgentConfig, selectedAgentType } = useServices();
const { selectedStakingProgramId } = useStakingProgram();
const { isLoaded: isBalanceLoaded, masterEoaNativeGasBalance } =
useMasterBalances();
Expand All @@ -31,7 +31,7 @@ export const LowFunds = () => {
const isSafeSignerBalanceLow = useMemo(() => {
if (!isBalanceLoaded) return false;
if (!masterEoaNativeGasBalance) return false;
if (!storeState?.isInitialFunded) return false;
if (!storeState?.[`isInitialFunded_${selectedAgentType}`]) return false;

return (
masterEoaNativeGasBalance <
Expand All @@ -44,7 +44,8 @@ export const LowFunds = () => {
masterEoaNativeGasBalance,
selectedAgentConfig.evmHomeChainId,
selectedAgentConfig.operatingThresholds,
storeState?.isInitialFunded,
selectedAgentType,
storeState,
]);

// 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 @@ -15,7 +15,7 @@ const { Text, Title } = Typography;

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

Expand All @@ -36,7 +36,7 @@ export const LowOperatingBalanceAlert = () => {
]);

if (!isBalanceLoaded) return null;
if (!storeState?.isInitialFunded) return;
if (!storeState?.[`isInitialFunded_${selectedAgentType}`]) return;
if (!isLowBalance) return null;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { useEffect } from 'react';
import { CustomAlert } from '@/components/Alert';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useNeedsFunds } from '@/hooks/useNeedsFunds';
import { useServices } from '@/hooks/useServices';
import { useStakingProgram } from '@/hooks/useStakingProgram';

import { FundsToActivate } from './FundsToActivate';

const { Title } = Typography;

export const MainNeedsFunds = () => {
const { selectedAgentType } = useServices();
const { selectedStakingProgramId } = useStakingProgram();

const {
Expand All @@ -28,10 +30,11 @@ export const MainNeedsFunds = () => {
hasEnoughOlasForInitialFunding &&
!isInitialFunded
) {
electronApi.store?.set?.('isInitialFunded', true);
electronApi.store?.set?.(`isInitialFunded_${selectedAgentType}`, true);
}
}, [
electronApi.store,
selectedAgentType,
hasEnoughEthForInitialFunding,
hasEnoughOlasForInitialFunding,
isInitialFunded,
Expand Down
7 changes: 4 additions & 3 deletions frontend/components/MainPage/sections/GasBalanceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const FineDot = styled(Dot)`

const BalanceStatus = () => {
const { isLoaded: isBalanceLoaded } = useBalanceContext();
const { isFetched: isServicesLoaded } = useServices();
const { isFetched: isServicesLoaded, selectedAgentType } = useServices();

const { storeState } = useStore();
const { showNotification } = useElectronApi();
Expand All @@ -55,7 +55,7 @@ const BalanceStatus = () => {
useEffect(() => {
if (!isBalanceLoaded || !isServicesLoaded) return;
if (!showNotification) return;
if (!storeState?.isInitialFunded) return;
if (!storeState?.[`isInitialFunded_${selectedAgentType}`]) return;

if (isMasterSafeLowOnNativeGas && !isLowBalanceNotificationShown) {
showNotification('Operating balance is too low.');
Expand All @@ -73,7 +73,8 @@ const BalanceStatus = () => {
isLowBalanceNotificationShown,
isMasterSafeLowOnNativeGas,
showNotification,
storeState?.isInitialFunded,
storeState,
selectedAgentType,
]);

const status = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/config/stakingPrograms/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const BASE_STAKING_PROGRAMS_CONTRACT_ADDRESSES: Record<string, Address> =
export const BASE_STAKING_PROGRAMS: StakingProgramMap = {
[StakingProgramId.MemeBaseAlpha2]: {
chainId: EvmChainId.Base,
name: 'MemeBase Alpha',
name: 'MemeBase Alpha II',
agentsSupported: [AgentType.Memeooorr],
stakingRequirements: {
[TokenSymbol.OLAS]: 100,
Expand Down
2 changes: 1 addition & 1 deletion frontend/hooks/useNeedsFunds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useNeedsFunds = (stakingProgramId: Maybe<StakingProgramId>) => {
const { isLoaded: isBalanceLoaded } = useBalanceContext();
const { masterSafeBalances } = useMasterBalances();

const isInitialFunded = storeState?.isInitialFunded;
const isInitialFunded = storeState?.[`isInitialFunded_${selectedAgentType}`];

const serviceFundRequirements = useMemo<{
[chainId: number]: {
Expand Down
3 changes: 2 additions & 1 deletion frontend/types/ElectronApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { AgentType } from '@/enums/Agent';

export type ElectronStore = {
environmentName?: string;
isInitialFunded?: boolean;
isInitialFunded_trader?: boolean;
isInitialFunded_memeooorr?: boolean;
firstStakingRewardAchieved?: boolean;
firstRewardNotificationShown?: boolean;
agentEvictionAlertShown?: boolean;
Expand Down

0 comments on commit 24e922d

Please sign in to comment.