Skip to content

Commit

Permalink
Refactor service creation logic in SpawnStakingCheck component
Browse files Browse the repository at this point in the history
  • Loading branch information
truemiller committed Feb 29, 2024
1 parent 4c27a36 commit 259da57
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions frontend/components/Spawn/SpawnStakingCheck.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,47 +54,51 @@ export const SpawnStakingCheck = ({
message.error('Service creation already in progress');
return;
}

setIsCreating(true);
return createService({
...serviceTemplate,
configuration: {
...serviceTemplate.configuration,
rpc,
use_staking: isStaking,
},
})
.then((service: Service) => {
setService(service);

// Set agent funding requirements
if (service.chain_data?.instances) {
setAgentFundRequirements(
service.chain_data.instances.reduce(
(acc: FundsRequirementMap, address: Address) => ({
...acc,
[address]:
serviceTemplate.configuration.fund_requirements.agent,
}),
{},
),
);
}

// Set multisig funding requirements from multisig/safe
if (service.chain_data?.multisig) {
const { multisig } = service.chain_data;
const { safe } = serviceTemplate.configuration.fund_requirements;
setAgentFundRequirements((prev: FundsRequirementMap) => ({
...prev,
[multisig]: safe,
}));
}
return Promise.resolve(service);
})
.catch(() => {
return Promise.reject();
})
.finally(() => setIsCreating(false));

let service: Service;
try {
service = await createService({
...serviceTemplate,
configuration: {
...serviceTemplate.configuration,
rpc,
use_staking: isStaking,
},
});
} catch (e) {
message.error('Failed to create service');
setIsCreating(false);
return;
}

setService(service);

// Set agent funding requirements
if (service.chain_data?.instances) {
setAgentFundRequirements(
service.chain_data.instances.reduce(
(acc: FundsRequirementMap, address: Address) => ({
...acc,
[address]: serviceTemplate.configuration.fund_requirements.agent,
}),
{},
),
);
}

// Set multisig funding requirements from multisig/safe
if (service.chain_data?.multisig) {
const { multisig } = service.chain_data;
const { safe } = serviceTemplate.configuration.fund_requirements;
setAgentFundRequirements((prev: FundsRequirementMap) => ({
...prev,
[multisig]: safe,
}));
}

return service;
},
[
createService,
Expand Down Expand Up @@ -145,7 +149,9 @@ export const SpawnStakingCheck = ({
message.error(`${userPublicKey} requires more OLAS to stake`);
return setButtonClicked(undefined);
}

const service: Service | undefined = await create(true);

if (!service) {
message.error('Failed to create service');
} else {
Expand All @@ -157,14 +163,16 @@ export const SpawnStakingCheck = ({
setIsStaking(true);
setSpawnScreenState(nextPage);
}
setButtonClicked(undefined);
}

setButtonClicked(undefined);
};

const handleNo = async () => {
setButtonClicked(ButtonOptions.NO);

const service: Service | undefined = await create(false);

if (!service) {
message.error('Failed to create service');
} else {
Expand Down

0 comments on commit 259da57

Please sign in to comment.