|
| 1 | +/** |
| 2 | + * Fluence CLI |
| 3 | + * Copyright (C) 2024 Fluence DAO |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, version 3. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +import { BaseCommand } from "../../baseCommand.js"; |
| 19 | +import { |
| 20 | + filterOffersFoundOnChain, |
| 21 | + resolveSingleOfferFromProviderConfig, |
| 22 | +} from "../../lib/chain/offer/offer.js"; |
| 23 | +import { assertProviderIsRegistered } from "../../lib/chain/providerInfo.js"; |
| 24 | +import { commandObj } from "../../lib/commandObj.js"; |
| 25 | +import { |
| 26 | + CHAIN_FLAGS, |
| 27 | + CLUSTER_ADDRESS_FLAG, |
| 28 | + SINGLE_OFFER_FLAGS, |
| 29 | + ADDRESS_FLAG_NAME, |
| 30 | +} from "../../lib/const.js"; |
| 31 | +import { getContracts, sign } from "../../lib/dealClient.js"; |
| 32 | +import { aliasesText } from "../../lib/helpers/aliasesText.js"; |
| 33 | +import { initCli } from "../../lib/lifeCycle.js"; |
| 34 | +import { confirm, input } from "../../lib/prompt.js"; |
| 35 | + |
| 36 | +export default class OfferAccessAddress extends BaseCommand< |
| 37 | + typeof OfferAccessAddress |
| 38 | +> { |
| 39 | + static override hiddenAliases = ["provider:sck"]; |
| 40 | + static override description = `Set access address for offer for use in cluster software.${aliasesText.apply(this)}`; |
| 41 | + static override flags = { |
| 42 | + ...SINGLE_OFFER_FLAGS, |
| 43 | + ...CHAIN_FLAGS, |
| 44 | + ...CLUSTER_ADDRESS_FLAG, |
| 45 | + }; |
| 46 | + |
| 47 | + async run(): Promise<void> { |
| 48 | + const { flags } = await initCli(this, await this.parse(OfferAccessAddress)); |
| 49 | + const configOffer = await resolveSingleOfferFromProviderConfig(flags); |
| 50 | + const offersFoundOnChain = await filterOffersFoundOnChain([configOffer]); |
| 51 | + const chainOffer = offersFoundOnChain[0]; |
| 52 | + |
| 53 | + if (chainOffer === undefined) { |
| 54 | + commandObj.error(`Offer ${configOffer.offerName} is not found on chain`); |
| 55 | + } |
| 56 | + |
| 57 | + const address = |
| 58 | + flags[ADDRESS_FLAG_NAME] ?? |
| 59 | + (await input({ |
| 60 | + message: "Enter cluster address", |
| 61 | + validate(input: string) { |
| 62 | + return ( |
| 63 | + input.length === 42 || |
| 64 | + "Please enter an address (40 hex digits + 0x)" |
| 65 | + ); |
| 66 | + }, |
| 67 | + })); |
| 68 | + |
| 69 | + commandObj.logToStderr( |
| 70 | + `Setting cluster address ${address} for offer ${chainOffer.offerName} (${chainOffer.offerId})`, |
| 71 | + ); |
| 72 | + |
| 73 | + if ( |
| 74 | + !(await confirm({ |
| 75 | + message: "Would you like to continue", |
| 76 | + default: true, |
| 77 | + })) |
| 78 | + ) { |
| 79 | + commandObj.logToStderr("Setting cluster address cancelled"); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const { contracts } = await getContracts(); |
| 84 | + |
| 85 | + await sign({ |
| 86 | + validateAddress: assertProviderIsRegistered, |
| 87 | + title: `Setting cluster address ${address} for offer ${chainOffer.offerName} (${chainOffer.offerId})`, |
| 88 | + method: contracts.diamond.setClusterKey, |
| 89 | + args: [chainOffer.offerId, address], |
| 90 | + }); |
| 91 | + } |
| 92 | +} |
0 commit comments