Skip to content

Commit

Permalink
chore: remove unused useServiceTemplates hook and related code; refac…
Browse files Browse the repository at this point in the history
…tor useNeedsFunds for improved balance handling
  • Loading branch information
truemiller committed Nov 21, 2024
1 parent 9146e1b commit 2e2b67e
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 48 deletions.
4 changes: 1 addition & 3 deletions frontend/hooks/backup/useAddress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* @note Access address via wallet or service, relative to current chain, service id
*/
// TODO: remove this file, uneccessary

// const useAddress = () => {
// const { service } = useServices();
Expand Down
103 changes: 103 additions & 0 deletions frontend/hooks/backup/useLogs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// TODO: reintroduce, low prio for refactor

// import { useQueryClient } from '@tanstack/react-query';
// import { useMemo } from 'react';

// import { REACT_QUERY_KEYS } from '@/constants/react-query-keys';

// import { useBalanceContext } from './useBalanceContext';
// import { useMultisig } from './useMultisig';
// import { useServices } from './useServices';
// import { useStore } from './useStore';
// import { useMasterWalletContext } from './useWallet';

// const useAddressesLogs = () => {
// const { wallets, masterEoaAddress, masterSafeAddress } = useMasterWalletContext();

// const { backupSafeAddress, masterSafeOwners } = useMultisig();

// return {
// isLoaded: wallets?.length !== 0 && !!masterSafeOwners,
// data: [
// { backupSafeAddress: backupSafeAddress ?? 'undefined' },
// { masterSafeAddress: masterSafeAddress ?? 'undefined' },
// { masterEoaAddress: masterEoaAddress ?? 'undefined' },
// { masterSafeOwners: masterSafeOwners ?? 'undefined' },
// ],
// };
// };

// const useBalancesLogs = () => {
// const {
// isBalanceLoaded,
// totalEthBalance,
// totalOlasBalance,
// wallets,
// walletBalances,
// totalOlasStakedBalance,
// } = useBalanceContext();

// return {
// isLoaded: isBalanceLoaded,
// data: [
// { wallets: wallets ?? 'undefined' },
// { walletBalances: walletBalances ?? 'undefined' },
// { totalOlasStakedBalance: totalOlasStakedBalance ?? 'undefined' },
// { totalEthBalance: totalEthBalance ?? 'undefined' },
// { totalOlasBalance: totalOlasBalance ?? 'undefined' },
// ],
// };
// };

// const useServicesLogs = () => {
// const { services, isFetched: isLoaded } = useServices();
// const { getQueryData } = useQueryClient();

// return {
// isLoaded: isLoaded,
// data: {
// services:
// services?.map((item) => ({
// ...item,
// keys: item.keys.map((key) => key.address),
// deploymentStatus: getQueryData<string>([
// REACT_QUERY_KEYS.SERVICE_DEPLOYMENT_STATUS_KEY(
// item.service_config_id,
// ),
// item.service_config_id,
// ]),
// })) ?? 'undefined',
// },
// };
// };

// export const useLogs = () => {
// const { storeState } = useStore();

// const { isLoaded: isServicesLoaded, data: services } = useServicesLogs();
// const { isLoaded: isBalancesLoaded, data: balances } = useBalancesLogs();
// const { isLoaded: isAddressesLoaded, data: addresses } = useAddressesLogs();

// const logs = useMemo(() => {
// if (isServicesLoaded && isBalancesLoaded && isAddressesLoaded) {
// return {
// store: storeState,
// debugData: {
// services,
// addresses,
// balances,
// },
// };
// }
// }, [
// addresses,
// balances,
// isAddressesLoaded,
// isBalancesLoaded,
// isServicesLoaded,
// services,
// storeState,
// ]);

// return logs;
// };
116 changes: 88 additions & 28 deletions frontend/hooks/useNeedsFunds.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,114 @@
import { formatUnits } from 'ethers/lib/utils';
import { formatEther, formatUnits } from 'ethers/lib/utils';
import { useMemo } from 'react';

import { ServiceTemplate } from '@/client';
import { CHAIN_CONFIG } from '@/config/chains';
import { STAKING_PROGRAMS } from '@/config/stakingPrograms';
import { getServiceTemplate } from '@/constants/serviceTemplates';

import { useBalanceContext } from './useBalanceContext';
import { useServiceTemplates } from './useServiceTemplates';
import { useService } from './useService';
import { useStore } from './useStore';
import { TokenSymbol } from '@/enums/Token';
import { getNativeTokenSymbol, NATIVE_TOKEN_CONFIG } from '@/config/tokens';
import { useMasterWalletContext } from './useWallet';

export const useNeedsFunds = () => {
const { getServiceTemplates } = useServiceTemplates();
export const useNeedsFunds = (serviceConfigId: string) => {
const { storeState } = useStore();
const { service } = useService({ serviceConfigId });
const { masterSafes } = useMasterWalletContext();
const { isLoaded: isBalanceLoaded, walletBalances } =
useBalanceContext();


const isInitialFunded = storeState?.isInitialFunded;

const serviceTemplate = useMemo(
() => getServiceTemplates()[0],
[getServiceTemplates],
const serviceTemplate = useMemo<ServiceTemplate | undefined>(
() => (service ? getServiceTemplate(service.hash) : undefined),
[service],
);

const { storeState } = useStore();
const isInitialFunded = storeState?.isInitialFunded;
const serviceFundRequirements = useMemo< {
[chainId: number]: {
[tokenSymbol: string]: number;
}
}>(() => {
if (!serviceTemplate) return {};

const {
isBalanceLoaded,
masterSafeBalance: safeBalance,
totalOlasStakedBalance,
} = useBalanceContext();

const serviceFundRequirements = useMemo(() => {
const gasEstimate =
serviceTemplate.configurations[CHAIN_CONFIG.OPTIMISM.chainId]
.monthly_gas_estimate;
const monthlyGasEstimate = Number(formatUnits(`${gasEstimate}`, 18));
const minimumStakedAmountRequired =
getMinimumStakedAmountRequired(serviceTemplate);

return { eth: monthlyGasEstimate, olas: minimumStakedAmountRequired };
const results: {
[chainId: number]: {
[tokenSymbol: string]: number;
}
} = {};

Object.entries(serviceTemplate.configurations).forEach(
([chainId, config]) => {
const serviceTemplateDefault = serviceTemplate.configurations[+chainId].staking_program_id
const serviceCurrent = service?.chain_configs[+chainId]?.chain_data?.user_params?.staking_program_id

if (!serviceCurrent && !serviceTemplateDefault) return;

if (!service?.chain_configs[+chainId]) return;
const gasEstimate = config.monthly_gas_estimate;
const monthlyGasEstimate = Number(formatUnits(`${gasEstimate}`, 18));
const minimumStakedAmountRequired =
STAKING_PROGRAMS[+chainId][
service?.chain_configs[+chainId]?.chain_data?.user_params
?.staking_program_id ??
serviceTemplate.configurations[+chainId].staking_program_id
].stakingRequirements.OLAS;

const nativeTokenSymbol = getNativeTokenSymbol(+chainId);

results[+chainId] = {
[TokenSymbol.OLAS]: +formatEther(minimumStakedAmountRequired),
[nativeTokenSymbol]: +formatEther(monthlyGasEstimate),
// TODO: extend with any further erc20s..
};
},
);

return results;
}, [serviceTemplate]);

const hasEnoughEthForInitialFunding = useMemo(
() => (safeBalance?.ETH || 0) >= (serviceFundRequirements?.eth || 0),
[serviceFundRequirements?.eth, safeBalance],
() => {
if (!serviceFundRequirements) return ;
if (!walletBalances) return ;

const nativeBalancesByChain = walletBalances.reduce<{[chainId: number]: number}>((acc, {symbol, balance, chainId}) => {
if (getNativeTokenSymbol(chainId) !== symbol) return acc;

if (!acc[chainId]) acc[chainId] = 0;
acc[chainId] += balance;

return acc;
}, {});

const chainIds = Object.keys(serviceFundRequirements).map(Number);

return chainIds.every(chainId => {
const nativeTokenSymbol = getNativeTokenSymbol(chainId);
const nativeTokenBalance = nativeBalancesByChain[chainId] || 0;
const nativeTokenRequired = serviceFundRequirements[chainId]?.[nativeTokenSymbol] || 0;

return nativeTokenBalance >= nativeTokenRequired;
});

},
[],
);

// TODO: refactor this to use the new balance context
const hasEnoughOlasForInitialFunding = useMemo(() => {
const olasInSafe = safeBalance?.OLAS || 0;
const olasStakedBySafe = totalOlasStakedBalance || 0;
const olasStakedBySafe = totalStakedOlasBalance || 0;
const olasRequiredToFundService = serviceFundRequirements.olas || 0;
const olasInSafeAndStaked = olasInSafe + olasStakedBySafe;
return olasInSafeAndStaked >= olasRequiredToFundService;
}, [
safeBalance?.OLAS,
totalOlasStakedBalance,
totalStakedOlasBalance,
serviceFundRequirements?.olas,
]);

Expand Down
2 changes: 1 addition & 1 deletion frontend/hooks/useNotifyOnNewEpoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type EpochStatusNotification = {
*/
export const useNotifyOnNewEpoch = () => {
const { showNotification } = useElectronApi();
const { isServiceNotRunning } = useServices();
const { isServiceNotRunning } = useServices(); //TODO: refactor to use single service hook

const { activeStakingContractDetails, isActiveStakingContractDetailsLoaded } =
useActiveStakingContractInfo();
Expand Down
16 changes: 0 additions & 16 deletions frontend/hooks/useServiceTemplates.ts

This file was deleted.

0 comments on commit 2e2b67e

Please sign in to comment.