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 balance related blockers for migration #612

Merged
merged 10 commits into from
Dec 24, 2024
19 changes: 0 additions & 19 deletions frontend/components/MainPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,6 @@ export const Main = () => {
const isStakingContractSectionEnabled = useFeatureFlag(
'staking-contract-section',
);
// const { refetch: updateServicesState } = useServices();
// const {
// updateBalances,
// isLoaded: isBalanceLoaded,
// setIsLoaded: setIsBalanceLoaded,
// } = useBalanceContext();

// TODO: reintroduce later, non critical
// useEffect(() => {
// if (!isBalanceLoaded) {
// updateServicesState?.().then(() => updateBalances());
// setIsBalanceLoaded(true);
// }
// }, [
// isBalanceLoaded,
// setIsBalanceLoaded,
// updateBalances,
// updateServicesState,
// ]);

return (
<Card
Expand Down
34 changes: 24 additions & 10 deletions frontend/components/MainPage/sections/OlasBalanceSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Balance = styled.span`
`;

export const MainOlasBalance = () => {
const { selectedService } = useServices();
const { selectedService, selectedAgentConfig } = useServices();
const { isLoaded: isBalanceLoaded } = useBalanceContext();
const { masterWalletBalances } = useMasterBalances();
const { serviceStakedBalances, serviceWalletBalances } = useServiceBalances(
Expand All @@ -36,31 +36,40 @@ export const MainOlasBalance = () => {
const isBalanceBreakdownEnabled = useFeatureFlag('manage-wallet');

const displayedBalance = useMemo(() => {
// olas across master wallets, safes and eoa
// olas across master wallet -- safes and eoa -- on relevant chains for agent
const masterWalletOlasBalance = masterWalletBalances?.reduce(
(acc, { symbol, balance }) => {
if (symbol === TokenSymbol.OLAS) {
(acc, { symbol, balance, evmChainId }) => {
if (
symbol === TokenSymbol.OLAS &&
selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId)
) {
Comment on lines 38 to +45
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fine to aggregate olas across potential chains for display balance

return acc + Number(balance);
}
return acc;
},
0,
);

// olas across all service wallets
// olas across all wallets owned by selected service
const serviceWalletOlasBalance = serviceWalletBalances?.reduce(
(acc, { symbol, balance }) => {
if (symbol === TokenSymbol.OLAS) {
(acc, { symbol, balance, evmChainId }) => {
if (
symbol === TokenSymbol.OLAS &&
selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId)
) {
Comment on lines +53 to +59
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensuring we don't calculate funds not on chains irrelevant to the agent

return acc + Number(balance);
}
return acc;
},
0,
);

// olas staked across all services
// olas staked across services on relevant chains for agent
const serviceStakedOlasBalance = serviceStakedBalances?.reduce(
(acc, { olasBondBalance, olasDepositBalance }) => {
(acc, { olasBondBalance, olasDepositBalance, evmChainId }) => {
if (!selectedAgentConfig.requiresAgentSafesOn.includes(evmChainId)) {
return acc;
}
return acc + Number(olasBondBalance) + Number(olasDepositBalance);
},
0,
Expand All @@ -73,7 +82,12 @@ export const MainOlasBalance = () => {
]);

return balanceFormat(totalOlasBalance, 2);
}, [masterWalletBalances, serviceStakedBalances, serviceWalletBalances]);
}, [
masterWalletBalances,
selectedAgentConfig.requiresAgentSafesOn,
serviceStakedBalances,
serviceWalletBalances,
]);

return (
<CardSection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flex, Typography } from 'antd';
import { isEmpty, isNil } from 'lodash';
import { isEmpty, isNil, sum } from 'lodash';
import { useMemo } from 'react';

import { CustomAlert } from '@/components/Alert';
Expand All @@ -14,10 +14,7 @@ import {
} from '@/hooks/useBalanceContext';
import { useNeedsFunds } from '@/hooks/useNeedsFunds';
import { useServices } from '@/hooks/useServices';
import {
useActiveStakingContractDetails,
useStakingContractContext,
} from '@/hooks/useStakingContractDetails';
import { useStakingContractContext } from '@/hooks/useStakingContractDetails';
import { balanceFormat } from '@/utils/numberFormatters';

import { CantMigrateReason } from './useMigrate';
Expand All @@ -32,9 +29,10 @@ const AlertInsufficientMigrationFunds = ({
const { selectedAgentConfig } = useServices();
const { isAllStakingContractDetailsRecordLoaded } =
useStakingContractContext();
const { isServiceStaked } = useActiveStakingContractDetails();
const { isLoaded: isBalanceLoaded, totalStakedOlasBalance } =
useBalanceContext();
const {
isLoaded: isBalanceLoaded,
totalStakedOlasBalance: totalStakedOlasBalanceOnHomeChain,
} = useBalanceContext();
const { masterSafeBalances } = useMasterBalances();
const { serviceFundRequirements, isInitialFunded } = useNeedsFunds(
stakingProgramIdToMigrateTo,
Expand All @@ -45,7 +43,7 @@ const AlertInsufficientMigrationFunds = ({
TokenSymbol.OLAS
];

const safeBalance = useMemo(() => {
const masterSafeBalanceOnHomeChain = useMemo(() => {
if (!isBalanceLoaded) return;
if (isNil(masterSafeBalances) || isEmpty(masterSafeBalances)) return;
return masterSafeBalances.reduce(
Expand All @@ -61,24 +59,26 @@ const AlertInsufficientMigrationFunds = ({

if (!isAllStakingContractDetailsRecordLoaded) return null;
if (isNil(requiredStakedOlas)) return null;
if (isNil(safeBalance?.[TokenSymbol.OLAS])) return null;
if (isNil(masterSafeBalanceOnHomeChain?.[TokenSymbol.OLAS])) return null;

if (isNil(totalStakedOlasBalance)) return null;
if (isNil(totalStakedOlasBalanceOnHomeChain)) return null;

const requiredOlasDeposit = isServiceStaked
? requiredStakedOlas -
(totalStakedOlasBalance + safeBalance[TokenSymbol.OLAS]) // when staked
: requiredStakedOlas - safeBalance[TokenSymbol.OLAS]; // when not staked
const requiredOlasDeposit =
requiredStakedOlas -
sum([
masterSafeBalanceOnHomeChain[TokenSymbol.OLAS],
totalStakedOlasBalanceOnHomeChain,
]);
Comment on lines +66 to +71
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this condition, may be breaking due to suspension or eviction
sum allows for null/undefined so don't need to guard here
middleware will migrate funds from the current contract if migration required


const homeChainId = selectedAgentConfig.evmHomeChainId;
const nativeTokenSymbol = getNativeTokenSymbol(homeChainId);
const requiredNativeTokenDeposit = isInitialFunded
? selectedAgentConfig.operatingThresholds[WalletOwnerType.Master][
WalletType.Safe
][CHAIN_CONFIG[selectedAgentConfig.evmHomeChainId].nativeToken.symbol] -
(safeBalance[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance
(masterSafeBalanceOnHomeChain[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance
: (serviceFundRequirements[homeChainId]?.[nativeTokenSymbol] || 0) -
(safeBalance[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements
(masterSafeBalanceOnHomeChain[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements

return (
<CustomAlert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ import { isNil } from 'lodash';
import { useMemo } from 'react';

import { MiddlewareDeploymentStatus, ServiceTemplate } from '@/client';
import {
getServiceTemplate,
SERVICE_TEMPLATES,
} from '@/constants/serviceTemplates';
import { SERVICE_TEMPLATES } from '@/constants/serviceTemplates';
import { Pages } from '@/enums/Pages';
import { StakingProgramId } from '@/enums/StakingProgram';
import { useBalanceContext } from '@/hooks/useBalanceContext';
import { useModals } from '@/hooks/useModals';
import { usePageState } from '@/hooks/usePageState';
import { useService } from '@/hooks/useService';
import { useServices } from '@/hooks/useServices';
import {
useActiveStakingContractDetails,
Expand Down Expand Up @@ -43,14 +39,12 @@ export const MigrateButton = ({
isServicesLoaded && selectedService
? selectedService.service_config_id
: '';
const { service } = useService(serviceConfigId);
const serviceTemplate = useMemo<ServiceTemplate | undefined>(
() =>
(service && getServiceTemplate(service.hash)) ??
SERVICE_TEMPLATES.find(
(template) => template.agentType === selectedAgentType,
),
[selectedAgentType, service],
[selectedAgentType],
);

const { setIsPaused: setIsBalancePollingPaused } = useBalanceContext();
Expand Down
10 changes: 7 additions & 3 deletions frontend/components/YourWalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ const Address = () => {
};

const OlasBalance = () => {
const { selectedAgentConfig } = useServices();
const { totalStakedOlasBalance } = useBalanceContext();
const { masterWalletBalances } = useMasterBalances();
const { middlewareChain, evmHomeChainId } = useYourWallet();
const { middlewareChain } = useYourWallet();

const masterSafeOlasBalance = masterWalletBalances
?.filter(
({ symbol, evmChainId }) =>
symbol === TokenSymbol.OLAS && evmChainId === evmHomeChainId,
(walletBalance) =>
walletBalance.symbol === TokenSymbol.OLAS &&
selectedAgentConfig.requiresMasterSafesOn.includes(
walletBalance.evmChainId,
),
)
.reduce((acc, balance) => acc + balance.balance, 0);

Expand Down
Loading