Skip to content

Commit

Permalink
feat: don't require connecting wallet on provider info if provider ad…
Browse files Browse the repository at this point in the history
…dress is in provider artifacts for current env (#1019)

* feat: don't require connecting wallet on provider info if provider address is in provider artifacts for current env

* Apply automatic changes

* Update cli/src/lib/const.ts

Co-authored-by: folex <[email protected]>

* Apply automatic changes

---------

Co-authored-by: shamsartem <[email protected]>
Co-authored-by: folex <[email protected]>
  • Loading branch information
3 people committed Sep 4, 2024
1 parent c76cc9d commit fe63b24
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cli/docs/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1547,9 +1547,10 @@ Print nox signing wallets and peer ids
```
USAGE
$ fluence provider info [--no-input] [--env <dar | kras | stage | local | custom>] [--priv-key <private-key>]
[--nox-names <nox-1,nox-2>] [--json]
[--nox-names <nox-1,nox-2>] [--json] [--address <address>]
FLAGS
--address=<address> Provider address
--env=<dar | kras | stage | local | custom> Fluence Environment to use when running the command
--json Output JSON
--no-input Don't interactively ask for any input from the user
Expand Down
69 changes: 61 additions & 8 deletions cli/src/commands/provider/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { jsonStringify } from "../../common.js";
import { getProviderInfo } from "../../lib/chain/providerInfo.js";
import { commandObj } from "../../lib/commandObj.js";
import { NOX_NAMES_FLAG, CHAIN_FLAGS, JSON_FLAG } from "../../lib/const.js";
import { initNewProviderArtifactsConfig } from "../../lib/configs/project/providerArtifacts.js";
import {
NOX_NAMES_FLAG,
CHAIN_FLAGS,
JSON_FLAG,
ADDRESS_FLAG,
ADDRESS_FLAG_NAME,
PRIV_KEY_FLAG_NAME,
} from "../../lib/const.js";
import { initCli } from "../../lib/lifeCycle.js";
import { resolveComputePeersByNames } from "../../lib/resolveComputePeersByNames.js";
import { ensureFluenceEnv } from "../../lib/resolveFluenceEnv.js";

export default class Info extends BaseCommand<typeof Info> {
static override aliases = ["provider:i"];
Expand All @@ -33,20 +42,18 @@ export default class Info extends BaseCommand<typeof Info> {
...CHAIN_FLAGS,
...NOX_NAMES_FLAG,
...JSON_FLAG,
...ADDRESS_FLAG,
};

async run(): Promise<void> {
const { flags } = await initCli(this, await this.parse(Info));
const computePeers = await resolveComputePeersByNames(flags);
const providerInfo = await getProviderInfo();

const infoToPrint = {
providerInfo: {
...(providerInfo.name === null
? { status: "NotRegistered" }
: { status: "Registered", name: providerInfo.name }),
address: providerInfo.address,
},
...(await formatProvidersInfo(
flags[ADDRESS_FLAG_NAME],
flags[PRIV_KEY_FLAG_NAME],
)),
computePeers: computePeers.map(({ name, peerId, walletAddress }) => {
return {
nox: name,
Expand All @@ -64,3 +71,49 @@ export default class Info extends BaseCommand<typeof Info> {
commandObj.log(yamlDiffPatch("", {}, infoToPrint));
}
}

async function formatProvidersInfo(
addressFromFlags: string | undefined,
privKeyFromFlags: string | undefined,
) {
const providerArtifactsConfig = await initNewProviderArtifactsConfig();
const fluenceEnv = await ensureFluenceEnv();

const providerAddressesFromArtifacts = Object.values(
providerArtifactsConfig.offers[fluenceEnv] ?? {},
).map(({ providerAddress }) => {
return providerAddress;
});

if (
providerAddressesFromArtifacts.length === 0 ||
addressFromFlags !== undefined ||
privKeyFromFlags !== undefined
) {
const providerInfo = await getProviderInfo(addressFromFlags);

return {
providerInfo: formatProviderInfo(providerInfo),
};
}

return {
providersInfo: await Promise.all(
providerAddressesFromArtifacts.map(async (address) => {
const providerInfo = await getProviderInfo(address);
return [address, formatProviderInfo(providerInfo)] as const;
}),
),
};
}

function formatProviderInfo(
providerInfo: Awaited<ReturnType<typeof getProviderInfo>>,
) {
return {
...(providerInfo.name === null
? { status: "NotRegistered" }
: { status: "Registered", name: providerInfo.name }),
address: providerInfo.address,
};
}
4 changes: 2 additions & 2 deletions cli/src/lib/chain/providerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export async function getProviderInfoByAddress(address: string) {
return { name: name === "" ? null : name, address };
}

export async function getProviderInfo() {
return getProviderInfoByAddress(await getSignerAddress());
export async function getProviderInfo(address?: string) {
return getProviderInfoByAddress(address ?? (await getSignerAddress()));
}

export async function assertProviderIsRegistered(address: string) {
Expand Down
9 changes: 9 additions & 0 deletions cli/src/lib/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@ export const PRIV_KEY_FLAG = {
}),
};

export const ADDRESS_FLAG_NAME = "address";

export const ADDRESS_FLAG = {
[ADDRESS_FLAG_NAME]: Flags.string({
description: "Provider address",
helpValue: "<address>",
}),
};

export const CHAIN_FLAGS = {
...ENV_FLAG,
...PRIV_KEY_FLAG,
Expand Down

0 comments on commit fe63b24

Please sign in to comment.