UNPKG

create-tezos-smart-contract

Version:

Node.js toolset to write, test and deploy Tezos smart contracts

135 lines (134 loc) 6.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deployContract = void 0; const tezos_builder_suite_1 = require("tezos-builder-suite"); const compile_1 = require("../../commands/compile"); const console_1 = require("../../console"); const bundle_1 = require("../bundle"); const config_1 = require("../config"); const flextesa_1 = require("../flextesa"); const _failWith = (str) => { (0, console_1.error)(str); process.exit(1); }; const deployInSandbox = async (contract, config) => { if (!await (0, flextesa_1.isFlextesaRunning)()) { _failWith(`ERROR: Unable to find a running Sandbox to deploy contracts.\nPlease get it running via the start-sandbox command.`); } (0, tezos_builder_suite_1.launchDeployer)({ contracts: [contract], endpoint: { url: `http://${config.sandbox.host}:${config.sandbox.port}`, scope: 'sandbox', signerPrivateKey: config.networks.sandbox.defaultSignerSK, protocolVersion: `${config.sandbox.protocol}net`, }, onDeployCompleted: (_, address) => { (0, console_1.em)(`Contract successfully deployed at address: ${address}`); process.exit(0); } }, { openBrowser: true }); }; const deployInTestnet = async (contract, config) => { (0, tezos_builder_suite_1.launchDeployer)({ contracts: [contract], endpoint: { url: `${config.networks.testnet.host}:${config.networks.testnet.port}`, scope: 'testnet', faucet: config.networks.testnet.faucet || undefined, protocolVersion: tezos_builder_suite_1.NetworkType.GRANADANET, }, onDeployCompleted: (_, address) => { (0, console_1.em)(`Contract successfully deployed at address: ${address}`); process.exit(0); } }, { openBrowser: true }); }; const deployInMainnet = async (contract, config) => { (0, tezos_builder_suite_1.launchDeployer)({ contracts: [contract], endpoint: { url: `${config.networks.testnet.host}:${config.networks.testnet.port}`, scope: 'mainnet', protocolVersion: tezos_builder_suite_1.NetworkType.MAINNET, }, onDeployCompleted: (_, address) => { (0, console_1.em)(`Contract successfully deployed at address: ${address}`); process.exit(0); } }, { openBrowser: true }); }; const handleOutdatedBuildfile = async (config, contractName, options) => { if (options.network === config_1.ToolchainNetworks.MAINNET) { _failWith(`ERROR: It seems the contract "${contractName}" has been edited since last compilation. To deploy in MainNet, you MUST compile it (and test it again) before deploying.`); } if (options.oldBuild) { return; // Go on with the deploy } if (!config.autoCompile) { _failWith(`ERROR: It seems the contract "${contractName}" has been edited since last compilation.\nYou can turn on "autoCompile" in config.json, compile it manually or ask the deploy to be run on old version passing --old-build to the deploy command.`); } await (0, compile_1.compile)({ contract: contractName }); }; const deployContract = async (bundle, options) => { const contractName = options.contract; const sourcePath = bundle.getContractFile(contractName); if (!bundle.exists(sourcePath)) { _failWith(`ERROR: Specified contract "${contractName}" doesn't exist.`); } if (!bundle.buildFileExists(contractName)) { _failWith(`ERROR: Specified contract "${contractName}" has never been compiled.`); } const contract = await bundle.readContract(contractName); const hash = bundle.generateHash(contract); const buildFile = await bundle.readBuildFile(contractName); const config = await bundle.readConfigFile(); if (options.network == "testnet") { if (config.networks.testnet.faucet) { if (config.networks.testnet.faucet.secret) { (0, console_1.error)(`ERROR: Invalid Faucet account for Testnet found in config.json. The Faucet you're using has an outdated structure.`); (0, console_1.warn)("Please provide a new Faucet account your can obtain at: https://teztnets.xyz/"); process.exit(0); } } } // Validate build file switch (bundle.isBuildValid(sourcePath, hash, buildFile)) { case bundle_1.BuildErrorCodes.MICHELSON_MISSING: _failWith(`ERROR: Invalid contract "${contractName}", Michelson code is missing!`); case bundle_1.BuildErrorCodes.INVALID_HASH: await handleOutdatedBuildfile(config, contractName, options); break; case bundle_1.BuildErrorCodes.INVALID_SOURCE_PATH: _failWith(`ERROR: The compiled version for "${contractName}" was compiled from a different source path "${buildFile.sourcePath}", stopping deploy.`); case true: break; } // Parse JSON-michelson let code = []; try { code = JSON.parse(buildFile.michelson); } catch (err) { _failWith(`ERROR: Failed to parse JSON-Michelson for contract ${contractName}, given error was: ${err}`); } const contractObject = { name: contractName, code: contract, michelson: JSON.stringify(code), }; switch (options.network) { case config_1.ToolchainNetworks.SANDBOX: return await deployInSandbox(contractObject, config); case config_1.ToolchainNetworks.TESTNET: return await deployInTestnet(contractObject, config); case config_1.ToolchainNetworks.MAINNET: return await deployInMainnet(contractObject, config); default: (0, console_1.warn)(`Unknown network "${options.network}", defaulting to sandbox environment.`); return await deployInSandbox(contractObject, config); } }; exports.deployContract = deployContract;