UNPKG

@hashgraph/hedera-local

Version:

Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).

179 lines 9.5 kB
"use strict"; // SPDX-License-Identifier: Apache-2.0 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResourceCreationState = void 0; const sdk_1 = require("@hashgraph/sdk"); const LoggerService_1 = require("../services/LoggerService"); const ServiceLocator_1 = require("../services/ServiceLocator"); const CLIService_1 = require("../services/CLIService"); const ClientService_1 = require("../services/ClientService"); const initialResources_json_1 = require("../configuration/initialResources.json"); const EventType_1 = require("../types/EventType"); const TokenUtils_1 = require("../utils/TokenUtils"); const AccountUtils_1 = require("../utils/AccountUtils"); const constants_1 = require("../constants"); /** * Represents the state of resource creation. * This class is responsible for initializing the ResourceCreationState object. * * Uses {@link accounts} and {@link tokens} from the initialResources.json * to create initial resources for the local-node environment * * @implements {IState} */ class ResourceCreationState { /** * Represents the state of resource creation. * This class is responsible for initializing the ResourceCreationState object. */ constructor() { this.stateName = ResourceCreationState.name; this.logger = ServiceLocator_1.ServiceLocator.Current.get(LoggerService_1.LoggerService.name); this.cliService = ServiceLocator_1.ServiceLocator.Current.get(CLIService_1.CLIService.name); this.clientService = ServiceLocator_1.ServiceLocator.Current.get(ClientService_1.ClientService.name); this.logger.trace(constants_1.RESOURCE_CREATION_STATE_INIT_MESSAGE, this.stateName); } /** * Subscribes an observer to receive updates from the ResourceCreationState. * @param {IOBserver} observer The observer to subscribe. */ subscribe(observer) { this.observer = observer; } /** * This method is responsible for starting the ResourceCreationState. * It creates tokens asynchronously or synchronously based on the CLI arguments. * @returns Promise that resolves when the state is started. * @emits {EventType.Finish} When the state is finished. */ onStart() { return __awaiter(this, void 0, void 0, function* () { const { async, createInitialResources } = this.cliService.getCurrentArgv(); if (!createInitialResources) { this.observer.update(EventType_1.EventType.Finish); return; } const mode = async ? 'asynchronous' : 'synchronous'; this.logger.info(`${constants_1.LOADING} Starting Resource Creation state in ${mode} mode...`, this.stateName); const promise = this.createResources(); if (!async) { yield promise; } this.observer.update(EventType_1.EventType.Finish); }); } /** * Creates accounts and tokens with the given properties and associates them. * @returns Promise that resolves when all resources are created. */ createResources() { return __awaiter(this, void 0, void 0, function* () { const accountProps = initialResources_json_1.accounts; const tokenProps = initialResources_json_1.tokens; const tokenIds = yield this.createTokens(tokenProps); yield this.createAndAssociateAccounts(accountProps, tokenIds); yield this.mintTokens(tokenProps, tokenIds); }); } /** * Creates accounts with the given properties. * @param accountProps The properties of the accounts to create. * @param tokenIdsBySymbol Map of token symbols to token IDs. * @returns Promise that resolves when all accounts are created (and associated with tokens). */ createAndAssociateAccounts(accountProps, tokenIdsBySymbol) { return __awaiter(this, void 0, void 0, function* () { this.logger.info('Creating accounts', this.stateName); const client = this.clientService.getClient(); const promises = accountProps.map((account) => __awaiter(this, void 0, void 0, function* () { var _a; const { privateKey, accountInfo } = yield AccountUtils_1.AccountUtils.createAccountFromProps(account, client); this.logger.info(`Successfully created account with: * normal account ID: ${accountInfo.accountId.toString()} * aliased account ID: ${accountInfo.aliasKey ? `0.0.${(_a = accountInfo.aliasKey) === null || _a === void 0 ? void 0 : _a.toString()}` : 'N/A'} * private key (use this in SDK/Hedera-native wallets): ${privateKey.toStringDer()} * raw private key (use this for JSON RPC wallet import): ${privateKey.toStringRaw()}`, this.stateName); if (account.associatedTokens && account.associatedTokens.length > 0) { const associatedTokenIds = this.getTokenIdsFor(account.associatedTokens, tokenIdsBySymbol); yield TokenUtils_1.TokenUtils.associateAccountWithTokens(accountInfo.accountId, associatedTokenIds, privateKey, client); this.logger.info(`Associated account ${accountInfo.accountId} with tokens: ${associatedTokenIds.join(', ')}`, this.stateName); } })); yield Promise.all(promises); }); } /** * Creates tokens with the given properties. * @param tokenProps The properties of the tokens to create. * @returns Promise that resolves with a map of token symbols to token IDs. */ createTokens(tokenProps) { return __awaiter(this, void 0, void 0, function* () { this.logger.info('Creating tokens', this.stateName); const client = this.clientService.getClient(); const promises = tokenProps.map((token) => __awaiter(this, void 0, void 0, function* () { const tokenId = yield TokenUtils_1.TokenUtils.createToken(token, client); this.logger.info(`Successfully created ${token.tokenType} token '${token.tokenSymbol}' with ID ${tokenId}`, this.stateName); return [token.tokenSymbol, tokenId]; })); return new Map(yield Promise.all(promises)); }); } /** * Gets the token IDs associated with the given token symbols. * @param tokenSymbols The token symbols to get IDs for. * @param tokenIdsBySymbol Map of token symbols to token IDs. * @returns The token IDs associated with the account. */ getTokenIdsFor(tokenSymbols, tokenIdsBySymbol) { return (tokenSymbols === null || tokenSymbols === void 0 ? void 0 : tokenSymbols.filter(tokenSymbol => { if (!tokenIdsBySymbol.has(tokenSymbol)) { this.logger.warn(`Token ID for ${tokenSymbol} not found`, this.stateName); return false; } return true; }).map(tokenSymbol => tokenIdsBySymbol.get(tokenSymbol))) || []; } /** * Mints tokens with the given properties. * @param tokenProps The properties of the tokens to mint. * @param tokenIdsBySymbol Map of token symbols to token IDs. */ mintTokens(tokenProps, tokenIdsBySymbol) { return __awaiter(this, void 0, void 0, function* () { this.logger.info('Minting NFTs', this.stateName); const client = this.clientService.getClient(); const promises = tokenProps .filter(token => { var _a; const isNft = token.tokenType === sdk_1.TokenType.NonFungibleUnique.toString(); const shouldMint = isNft && !!((_a = token.mints) === null || _a === void 0 ? void 0 : _a.length); if (shouldMint && !tokenIdsBySymbol.has(token.tokenSymbol)) { this.logger.warn(`Token ID for ${token.tokenSymbol} not found`, this.stateName); return false; } return shouldMint; }) .map((token) => __awaiter(this, void 0, void 0, function* () { const tokenId = tokenIdsBySymbol.get(token.tokenSymbol); const supplyKey = TokenUtils_1.TokenUtils.getSupplyKey(token); yield Promise.all(token.mints.map((_a) => __awaiter(this, [_a], void 0, function* ({ CID }) { yield TokenUtils_1.TokenUtils.mintToken(tokenId, CID, supplyKey, client); this.logger.info(`Minted token ID ${tokenId} with CID '${CID}'`, this.stateName); }))); })); yield Promise.all(promises); }); } } exports.ResourceCreationState = ResourceCreationState; //# sourceMappingURL=ResourceCreationState.js.map