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

Review migration process #512

Draft
wants to merge 13 commits into
base: refactor/multi-chain-support-frontend
Choose a base branch
from
Draft
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
70 changes: 70 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,76 @@ Returns a list of available wallets

---

### `GET /api/extended/wallet`

Returns a list of available wallets with enriched information. It executes on-chain requests to populate the list of owners of each safe, and provides the attributes

- `consistent_backup_owner`: This flag is `true` when all safes across the chains have exactly the same set of backup owner addresses. It ensures that ownership is identical across all safes, regardless of the number of owners.
- `consistent_backup_owner_count`: This flag is `true` when all safes have the same number of owners, and that number is either 0 (no backup owners) or 1 (exactly one backup owner). It checks for uniformity in the count of owners and restricts the count to these two cases.
- `consistent_safe_address`: This flag is `true` when all chains have the same safe address. It ensures there is a single safe address consistently used across all chains.

<details>
<summary>Response</summary>

```json
[
{
"address":"0xFafd5cb31a611C5e5aa65ea8c6226EB4328175E7",
"consistent_backup_owner": false,
"consistent_backup_owner_count": false,
"consistent_safe_address": true,
"ledger_type":"ethereum",
"safe_chains":[
"gnosis",
"ethereum",
"base",
"optimistic"
],
"safe_nonce":110558881674480320952254000342160989674913430251257716940579305238321962891821,
"safes":{
"base":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"backup_owners": [], // Empty = no backup owners
"balances": {...}
}
},
"ethereum":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"backup_owners":[
"0x46eC2E77Fe3E367252f1A8a77470CE8eEd2A985b"
],
"balances": {...}
}
},
"gnosis":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"backup_owners":[
"0x46eC2E77Fe3E367252f1A8a77470CE8eEd2A985b"
],
"balances": {
"0x0000000000000000000000000000000000000000": 995899999999999999998, // xDAI
"0xDDAfbb505ad214D7b80b1f830fcCc89B60fb7A83": 0, // USDC
"0xcE11e14225575945b8E6Dc0D4F2dD4C570f79d9f": 960000000000000000000 // OLAS
}
},
"optimistic":{
"0xd56fb274ce2C66008D5c4C09980c4f36Ab81ff23":{
"backup_owners":[
"0x46eC2E77Fe3E367252f1A8a77470CE8eEd2A985b"
],
"balances": {...}
}
}
},
"single_backup_owner_per_safe":false
}
]
```

</details>

---

### `POST /api/wallet`

Creates a master wallet for given chain type. If a wallet already exists for a given chain type, it returns the already existing wallet without creating an additional one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ export const CannotStartAgentPopover = () => {
const { isAgentEvicted, isEligibleForStaking } =
useActiveStakingContractInfo();

const { hasEnoughServiceSlots, isRewardsAvailable } =
const { hasEnoughStakingSlots, isRewardsAvailable } =
useStakingContractDetails(activeStakingProgramId);

if (!isAllStakingContractDetailsRecordLoaded) return null;
if (isEligibleForStaking) return null;
if (!hasEnoughServiceSlots) return <NoJobsAvailablePopover />;
if (!hasEnoughStakingSlots) return <NoJobsAvailablePopover />;
if (!isRewardsAvailable) return <NoRewardsAvailablePopover />;
if (isAgentEvicted) return <AgentEvictedPopover />;
throw new Error('Cannot start agent, please contact support');
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/MainPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const Main = () => {
const { isAllStakingContractDetailsRecordLoaded } =
useStakingContractContext();

const { hasEnoughServiceSlots } = useStakingContractDetails(
const { hasEnoughStakingSlots } = useStakingContractDetails(
activeStakingProgramId,
);

Expand All @@ -61,7 +61,7 @@ export const Main = () => {
const hideMainOlasBalanceTopBorder = [
// !backupSafeAddress, // TODO: update this condition to check backup safe relative to selectedService
activeStakingProgramId === StakingProgramId.PearlAlpha,
isAllStakingContractDetailsRecordLoaded && !hasEnoughServiceSlots,
isAllStakingContractDetailsRecordLoaded && !hasEnoughStakingSlots,
].some((condition) => !!condition);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export const NoAvailableSlotsOnTheContract = () => {
isActiveStakingContractDetailsLoaded,
} = useActiveStakingContractInfo();

const { hasEnoughServiceSlots } = useStakingContractDetails(
const { hasEnoughStakingSlots } = useStakingContractDetails(
selectedStakingProgramId,
);

if (!isActiveStakingProgramLoaded) return null;
if (!isActiveStakingContractDetailsLoaded) return null;

if (hasEnoughServiceSlots) return null;
if (hasEnoughStakingSlots) return null;
if (isServiceStaked) return null;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { TokenSymbol } from '@/enums/Token';
import {
useBalanceContext,
useMasterBalances,
useServiceBalances,
} from '@/hooks/useBalanceContext';
import { useNeedsFunds } from '@/hooks/useNeedsFunds';
import { useServices } from '@/hooks/useServices';
Expand All @@ -28,23 +29,43 @@ type CantMigrateAlertProps = { stakingProgramId: StakingProgramId };
const AlertInsufficientMigrationFunds = ({
stakingProgramId: stakingProgramIdToMigrateTo,
}: CantMigrateAlertProps) => {
const { selectedAgentConfig } = useServices();
const { selectedAgentConfig, selectedService } = useServices();
const { isAllStakingContractDetailsRecordLoaded } =
useStakingContractContext();
const { isServiceStaked } = useActiveStakingContractInfo();
const { isLoaded: isBalanceLoaded, totalStakedOlasBalance } =
useBalanceContext();
const { isServiceStaked, isSelectedStakingContractDetailsLoaded } =
useActiveStakingContractInfo();
const { isLoaded: isBalanceLoaded } = useBalanceContext();
const { serviceSafeBalances } = useServiceBalances(
selectedService?.service_config_id,
);
const { masterSafeBalances } = useMasterBalances();
const { serviceFundRequirements, isInitialFunded } = useNeedsFunds(
stakingProgramIdToMigrateTo,
);

const serviceStakedOlasBalance = useMemo(
() =>
serviceSafeBalances?.find(
({ symbol, evmChainId }) =>
isSelectedStakingContractDetailsLoaded &&
isServiceStaked &&
symbol === TokenSymbol.OLAS &&
selectedAgentConfig.evmHomeChainId === evmChainId,
)?.balance,
[
isSelectedStakingContractDetailsLoaded,
isServiceStaked,
selectedAgentConfig.evmHomeChainId,
serviceSafeBalances,
],
);

const requiredStakedOlas =
serviceFundRequirements[selectedAgentConfig.evmHomeChainId][
TokenSymbol.OLAS
];

const safeBalance = useMemo(() => {
const masterSafeBalanceRecord = useMemo(() => {
if (!isBalanceLoaded) return;
if (isNil(masterSafeBalances) || isEmpty(masterSafeBalances)) return;
return masterSafeBalances.reduce(
Expand All @@ -60,21 +81,23 @@ const AlertInsufficientMigrationFunds = ({

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

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

const requiredOlasDeposit = isServiceStaked
? requiredStakedOlas -
(totalStakedOlasBalance + safeBalance[TokenSymbol.OLAS]) // when staked
: requiredStakedOlas - safeBalance[TokenSymbol.OLAS]; // when not staked
const requiredOlasDeposit =
isSelectedStakingContractDetailsLoaded && isServiceStaked
? requiredStakedOlas -
(serviceStakedOlasBalance + masterSafeBalanceRecord[TokenSymbol.OLAS]) // when staked
: requiredStakedOlas - masterSafeBalanceRecord[TokenSymbol.OLAS]; // when not staked

const homeChainId = selectedAgentConfig.evmHomeChainId;
const nativeTokenSymbol = getNativeTokenSymbol(homeChainId);
const { evmHomeChainId } = selectedAgentConfig;
const nativeTokenSymbol = getNativeTokenSymbol(evmHomeChainId);
const requiredNativeTokenDeposit = isInitialFunded
? LOW_MASTER_SAFE_BALANCE - (safeBalance[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance
: (serviceFundRequirements[homeChainId]?.[nativeTokenSymbol] || 0) -
(safeBalance[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements
? LOW_MASTER_SAFE_BALANCE -
(masterSafeBalanceRecord[nativeTokenSymbol] || 0) // is already funded allow minimal maintenance
: (serviceFundRequirements[evmHomeChainId]?.[nativeTokenSymbol] || 0) -
(masterSafeBalanceRecord[nativeTokenSymbol] || 0); // otherwise require full initial funding requirements

return (
<CustomAlert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ export const useMigrate = (migrateToStakingProgramId: StakingProgramId) => {
} = useServices();
const { evmHomeChainId: homeChainId } = selectedAgentConfig;
const serviceConfigId = selectedService?.service_config_id;
const { deploymentStatus: serviceStatus } = useService(serviceConfigId);
const { deploymentStatus: serviceStatus, isServiceRunning } =
useService(serviceConfigId);

const {
isLoaded: isBalanceLoaded,
totalStakedOlasBalance,
isLowBalance,
} = useBalanceContext();
const { masterSafeBalances } = useMasterBalances();

const { needsInitialFunding, hasEnoughEthForInitialFunding } = useNeedsFunds(
migrateToStakingProgramId,
);
Expand All @@ -71,7 +73,7 @@ export const useMigrate = (migrateToStakingProgramId: StakingProgramId) => {
} = useStakingContractContext();
const { isServiceStaked, isServiceStakedForMinimumDuration } =
useActiveStakingContractInfo();
const { stakingContractInfo, hasEnoughServiceSlots } =
const { stakingContractInfo, hasEnoughStakingSlots } =
useStakingContractDetails(migrateToStakingProgramId);

const safeOlasBalance = useMemo(() => {
Expand Down Expand Up @@ -123,14 +125,7 @@ export const useMigrate = (migrateToStakingProgramId: StakingProgramId) => {
return { canMigrate: false, reason: CantMigrateReason.LoadingServices };
}

// Services must be not be running or in a transitional state
if (
[
MiddlewareDeploymentStatus.DEPLOYED,
MiddlewareDeploymentStatus.DEPLOYING,
MiddlewareDeploymentStatus.STOPPING,
].some((status) => status === serviceStatus)
) {
if (isServiceRunning) {
return {
canMigrate: false,
reason: CantMigrateReason.PearlCurrentlyRunning,
Expand Down Expand Up @@ -163,10 +158,7 @@ export const useMigrate = (migrateToStakingProgramId: StakingProgramId) => {
};
}

if (
(stakingContractInfo.serviceIds ?? [])?.length >=
(stakingContractInfo.maxNumServices ?? 0)
) {
if (!hasEnoughStakingSlots) {
return {
canMigrate: false,
reason: CantMigrateReason.NoAvailableStakingSlots,
Expand Down Expand Up @@ -225,17 +217,18 @@ export const useMigrate = (migrateToStakingProgramId: StakingProgramId) => {
return { canMigrate: true };
}, [
isServicesLoaded,
isServiceRunning,
isBalanceLoaded,
isAllStakingContractDetailsRecordLoaded,
stakingContractInfo,
activeStakingProgramId,
migrateToStakingProgramId,
hasEnoughStakingSlots,
hasEnoughOlasToMigrate,
isServiceStaked,
homeChainId,
selectedAgentType,
isServiceStakedForMinimumDuration,
serviceStatus,
]);

const firstDeployValidation = useMemo<MigrateValidation>(() => {
Expand Down Expand Up @@ -338,13 +331,13 @@ export const useMigrate = (migrateToStakingProgramId: StakingProgramId) => {
if (!isBalanceLoaded) return false;
if (isLowBalance) return false;
if (needsInitialFunding) return false;
if (!hasEnoughServiceSlots) return false;
if (!hasEnoughStakingSlots) return false;
return true;
}, [
isBalanceLoaded,
isLowBalance,
needsInitialFunding,
hasEnoughServiceSlots,
hasEnoughStakingSlots,
]);

return {
Expand Down
28 changes: 10 additions & 18 deletions frontend/components/ManageStakingPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,8 @@ import { WhatAreStakingContractsSection } from './WhatAreStakingContracts';
export const ManageStakingPage = () => {
const { goto } = usePageState();
const { selectedAgentConfig } = useServices();
const {
activeStakingProgramId,
isActiveStakingProgramLoaded,
defaultStakingProgramId,
} = useStakingProgram();

const currentStakingProgramId = isActiveStakingProgramLoaded
? activeStakingProgramId || defaultStakingProgramId
: null;
const { isActiveStakingProgramLoaded, selectedStakingProgramId } =
useStakingProgram();

const stakingProgramIdsAvailable = Object.keys(
STAKING_PROGRAMS[selectedAgentConfig.evmHomeChainId],
Expand All @@ -38,7 +31,7 @@ export const ManageStakingPage = () => {
if (!isActiveStakingProgramLoaded) return acc;

// put the active staking program at the top
if (stakingProgramId === currentStakingProgramId) {
if (stakingProgramId === selectedStakingProgramId) {
return [stakingProgramId, ...acc];
}

Expand All @@ -59,7 +52,7 @@ export const ManageStakingPage = () => {
[
isActiveStakingProgramLoaded,
selectedAgentConfig.evmHomeChainId,
currentStakingProgramId,
selectedStakingProgramId,
stakingProgramIdsAvailable,
],
);
Expand All @@ -72,7 +65,7 @@ export const ManageStakingPage = () => {
STAKING_PROGRAMS[selectedAgentConfig.evmHomeChainId][stakingProgramId];

if (!info) return false;
if (currentStakingProgramId === stakingProgramId) return false;
if (selectedStakingProgramId === stakingProgramId) return false;
if (info.deprecated) return false;
return true;
},
Expand All @@ -98,12 +91,11 @@ export const ManageStakingPage = () => {
>
<WhatAreStakingContractsSection />

{isActiveStakingProgramLoaded &&
(activeStakingProgramId || defaultStakingProgramId) && (
<StakingContractSection
stakingProgramId={orderedStakingProgramIds[0]}
/>
)}
{isActiveStakingProgramLoaded && selectedStakingProgramId && (
<StakingContractSection
stakingProgramId={orderedStakingProgramIds[0]}
/>
)}

<CardSection
style={{
Expand Down
Loading