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 start agent balance thresholds #154

Merged
merged 4 commits into from
May 29, 2024
Merged
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
33 changes: 28 additions & 5 deletions frontend/components/Main/MainHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { COLOR, LOW_BALANCE, SERVICE_TEMPLATES } from '@/constants';
import { useBalance, useServiceTemplates } from '@/hooks';
import { useElectronApi } from '@/hooks/useElectronApi';
import { useServices } from '@/hooks/useServices';
import { useStore } from '@/hooks/useStore';
import { useWallet } from '@/hooks/useWallet';
import { ServicesService } from '@/service';
import { WalletService } from '@/service/Wallet';
Expand All @@ -25,6 +26,7 @@ enum ServiceButtonLoadingState {
}

export const MainHeader = () => {
const { storeState } = useStore();
const { services, serviceStatus, setServiceStatus } = useServices();
const { showNotification, setTrayIcon } = useElectronApi();
const { getServiceTemplates } = useServiceTemplates();
Expand Down Expand Up @@ -204,23 +206,42 @@ export const MainHeader = () => {
18,
),
);

const olasRequiredToStake = Number(
formatUnits(
`${SERVICE_TEMPLATES[0].configuration.olas_required_to_stake}`,
18,
),
);
const monthlyGasEstimate = Number(

const requiredOlas = olasCostOfBond + olasRequiredToStake;

const requiredGas = Number(
formatUnits(
`${SERVICE_TEMPLATES[0].configuration.monthly_gas_estimate}`,
18,
),
);

if (
(totalOlasBalance ?? 0) < olasCostOfBond + olasRequiredToStake ||
(totalEthBalance ?? 0) < monthlyGasEstimate
) {
const isDeployable = (() => {
// case where required values are undefined (not fetched from the server)
if (totalEthBalance === undefined) return false;
if (totalOlasBalance === undefined) return false;
if (!services) return false;

// deployment statuses where agent should not be deployed
// if (serviceStatus === DeploymentStatus.DEPLOYED) return false; // condition already checked above
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need this commented code line? if yes, don't understand how it's checked above

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the condition is covered on L179:

if (serviceStatus === DeploymentStatus.DEPLOYED) {

had left the comment for context around why it's not included in the statuses where agent should not be deployable

can remove though, let me know 🙏

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks good, but it might be personal preference, I haven't commonly seen IIFE being used. Also, regarding condition, can be added just to avoid confusion, but it is up to you.

function checkDeployability() {
  // case where required values are undefined (not fetched from the server)
  if (totalEthBalance === undefined || totalOlasBalance === undefined || !services) {
    return false;
  }

  // deployment statuses where agent should not be deployed
  if (
    serviceStatus === DeploymentStatus.DEPLOYED || 
    serviceStatus === DeploymentStatus.DEPLOYING ||
    serviceStatus === DeploymentStatus.STOPPING
  ) {
    return false;
  }

  // case where service exists & user has initial funded
  if (services[0] && storeState?.isInitialFunded) {
    return totalOlasBalance >= requiredOlas; // at present agent will always require staked/bonded OLAS
  }

  return totalOlasBalance >= requiredOlas && totalEthBalance > requiredGas;
}

const isDeployable = checkDeployability();

if (!isDeployable) {
  return (
    <Button type="default" size="large" disabled>
      Start agent
    </Button>
  );
}

if (serviceStatus === DeploymentStatus.DEPLOYING) return false;
if (serviceStatus === DeploymentStatus.STOPPING) return false;

// case where service exists & user has initial funded
if (services[0] && storeState?.isInitialFunded)
return totalOlasBalance >= requiredOlas; // at present agent will always require staked/bonded OLAS

return totalOlasBalance >= requiredOlas && totalEthBalance > requiredGas;
})();

if (!isDeployable) {
return (
<Button type="default" size="large" disabled>
Start agent
Expand All @@ -239,6 +260,8 @@ export const MainHeader = () => {
isBalanceLoaded,
serviceButtonState,
serviceStatus,
services,
storeState?.isInitialFunded,
totalEthBalance,
totalOlasBalance,
]);
Expand Down
Loading