From fe63b2457171bb82d7b1ab63cf4c0f67775c51ce Mon Sep 17 00:00:00 2001 From: shamsartem Date: Wed, 4 Sep 2024 13:17:19 +0200 Subject: [PATCH] feat: don't require connecting wallet on provider info if provider address 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 <0xdxdy@gmail.com> * Apply automatic changes --------- Co-authored-by: shamsartem Co-authored-by: folex <0xdxdy@gmail.com> --- cli/docs/commands/README.md | 3 +- cli/src/commands/provider/info.ts | 69 +++++++++++++++++++++++++++---- cli/src/lib/chain/providerInfo.ts | 4 +- cli/src/lib/const.ts | 9 ++++ 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/cli/docs/commands/README.md b/cli/docs/commands/README.md index b53514d63..785278052 100644 --- a/cli/docs/commands/README.md +++ b/cli/docs/commands/README.md @@ -1547,9 +1547,10 @@ Print nox signing wallets and peer ids ``` USAGE $ fluence provider info [--no-input] [--env ] [--priv-key ] - [--nox-names ] [--json] + [--nox-names ] [--json] [--address
] FLAGS + --address=
Provider address --env= Fluence Environment to use when running the command --json Output JSON --no-input Don't interactively ask for any input from the user diff --git a/cli/src/commands/provider/info.ts b/cli/src/commands/provider/info.ts index 7af26758b..76c152eba 100644 --- a/cli/src/commands/provider/info.ts +++ b/cli/src/commands/provider/info.ts @@ -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 { static override aliases = ["provider:i"]; @@ -33,20 +42,18 @@ export default class Info extends BaseCommand { ...CHAIN_FLAGS, ...NOX_NAMES_FLAG, ...JSON_FLAG, + ...ADDRESS_FLAG, }; async run(): Promise { 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, @@ -64,3 +71,49 @@ export default class Info extends BaseCommand { 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>, +) { + return { + ...(providerInfo.name === null + ? { status: "NotRegistered" } + : { status: "Registered", name: providerInfo.name }), + address: providerInfo.address, + }; +} diff --git a/cli/src/lib/chain/providerInfo.ts b/cli/src/lib/chain/providerInfo.ts index 415ae2018..14c1041e2 100644 --- a/cli/src/lib/chain/providerInfo.ts +++ b/cli/src/lib/chain/providerInfo.ts @@ -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) { diff --git a/cli/src/lib/const.ts b/cli/src/lib/const.ts index 5bd5f9716..9ad183439 100644 --- a/cli/src/lib/const.ts +++ b/cli/src/lib/const.ts @@ -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: "
", + }), +}; + export const CHAIN_FLAGS = { ...ENV_FLAG, ...PRIV_KEY_FLAG,