UNPKG

@holographxyz/cli

Version:
135 lines (134 loc) 6.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const inquirer = tslib_1.__importStar(require("inquirer")); const fs = tslib_1.__importStar(require("fs-extra")); const node_path_1 = tslib_1.__importDefault(require("node:path")); const core_1 = require("@oclif/core"); const contracts_1 = require("@ethersproject/contracts"); const environment_1 = require("@holographxyz/environment"); const networks_1 = require("@holographxyz/networks"); const config_1 = require("../../utils/config"); const network_monitor_1 = require("../../utils/network-monitor"); const validation_1 = require("../../utils/validation"); const asset_deployment_1 = require("../../utils/asset-deployment"); class NFT extends core_1.Command { static description = 'Mint a Holographable NFT.'; static examples = [ '$ <%= config.bin %> <%= command.id %> --network="goerli" --collectionAddress="0xf90c33d5ef88a9d84d4d61f62c913ba192091fe7" --tokenId="0" --tokenUriType="ipfs" --tokenUri="QmfQhPGMAbHL31qcqAEYpSP5gXwXWQa3HZjkNVzZ2mRsRs/metadata.json"', ]; static flags = { collectionAddress: core_1.Flags.string({ description: 'The address of the collection smart contract', parse: validation_1.validateContractAddress, multiple: false, required: false, }), tokenId: core_1.Flags.string({ description: 'The token id to mint. By default the token id is 0, which mints the next available token id', default: '0', parse: validation_1.validateTokenIdInput, multiple: false, required: false, }), tokenUriType: core_1.Flags.string({ description: 'The token URI type', multiple: false, options: ['ipfs', 'https', 'arweave'], required: false, }), tokenUri: core_1.Flags.string({ description: 'The uri of the token, minus the prepend (ie "ipfs://")', multiple: false, required: false, parse: validation_1.validateNonEmptyString, }), ...network_monitor_1.networkFlag, }; /** * NFT class variables */ networkMonitor; async run() { this.log('Loading user configurations...'); const environment = (0, environment_1.getEnvironment)(); const { userWallet, configFile, supportedNetworksOptions } = await (0, config_1.ensureConfigFileIsValid)(this.config.configDir, undefined, true); const { flags } = await this.parse(NFT); this.log('User configurations loaded.'); const network = await (0, validation_1.checkOptionFlag)(supportedNetworksOptions, flags.network, 'Select the network on which to mint the nft'); const collectionAddress = await (0, validation_1.checkContractAddressFlag)(flags.collectionAddress, 'Enter the address of the collection smart contract'); const tokenId = flags.tokenId; const tokenUriType = asset_deployment_1.TokenUriTypeIndex[await (0, validation_1.checkTokenUriTypeFlag)(flags.tokenUriType, 'Select the uri of the token, minus the prepend (ie "ipfs://")')]; const tokenUri = await (0, validation_1.checkStringFlag)(flags.tokenUri, 'Enter the uri of the token, minus the prepend (ie "ipfs://")'); this.networkMonitor = new network_monitor_1.NetworkMonitor({ parent: this, configFile, networks: [network], debug: this.debug, userWallet, verbose: false, }); core_1.CliUx.ux.action.start('Loading network RPC providers'); await this.networkMonitor.run(true); core_1.CliUx.ux.action.stop(); core_1.CliUx.ux.action.start('Checking that contract is already deployed and holographable on "' + network + '" network'); const isDeployed = await this.networkMonitor.registryContract .connect(this.networkMonitor.providers[network]) .isHolographedContract(collectionAddress); core_1.CliUx.ux.action.stop(); if (!isDeployed) { throw new Error('Collection is either not deployed or not hologaphable at ' + collectionAddress + ' on "' + network + '" network'); } core_1.CliUx.ux.action.start('Retrieving collection smart contract'); const collectionABI = await fs.readJson(node_path_1.default.join(__dirname, `../../abi/${environment}/CxipERC721.json`)); const collection = new contracts_1.Contract(collectionAddress, collectionABI, this.networkMonitor.providers[network]); core_1.CliUx.ux.action.stop(); const mintPrompt = await inquirer.prompt([ { name: 'shouldContinue', message: `\nWould you like to mint the following NFT?\n\n${JSON.stringify({ network: networks_1.networks[network].shortKey, collectionAddress, tokenId, tokenUriType: asset_deployment_1.TokenUriTypeIndex[tokenUriType], tokenUri, }, undefined, 2)}\n`, type: 'confirm', default: false, }, ]); const mint = mintPrompt.shouldContinue; if (mint) { core_1.CliUx.ux.action.start('Minting NFT'); const receipt = await this.networkMonitor.executeTransaction({ network, contract: collection, methodName: 'cxipMint', args: [tokenId, tokenUriType, tokenUri], waitForReceipt: true, }); core_1.CliUx.ux.action.stop(); if (receipt === null) { throw new Error('failed to confirm that the transaction was mined'); } else { const logs = this.networkMonitor.decodeErc721TransferEvent(receipt, collectionAddress); if (logs === undefined) { throw new Error('failed to extract transfer event from transaction receipt'); } else { this.log(`NFT has been minted with token id #${logs[2].toString()}`); } } } else { this.log('NFT minting was canceled'); } this.exit(); } } exports.default = NFT;