UNPKG

@titan-suite/cli

Version:

The complete smart contract development tool

160 lines (159 loc) 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const core_1 = require("@titan-suite/core"); const cli_ux_1 = require("cli-ux"); const aionSolc = require("aion-solc"); const download = require("download-git-repo"); const fs = require("fs"); const mkdirp = require("mkdirp"); const path = require("path"); const templates_1 = require("./templates"); const utf8 = { encoding: 'utf8' }; let defaultAccount; let currentNetwork; exports.getConfig = () => { const titanrcPath = path.join(process.cwd(), 'titanrc.js'); const titanrcExists = fs.existsSync(titanrcPath); if (titanrcExists === false) { throw new Error(`${titanrcPath} not found`); } const config = require(titanrcPath); return config; }; exports.getCurrentNetwork = () => { return currentNetwork; }; exports.getProvider = (targetNetwork) => { const config = exports.getConfig(); const { networks } = config.blockchains.aion; if (!networks || Object.keys(networks).length === 0) { throw new Error('Please specify at least one network to connect to in your titanrc file'); } currentNetwork = targetNetwork && targetNetwork.length > 0 ? targetNetwork : 'development'; try { defaultAccount = networks[`${currentNetwork}`].defaultAccount; const provider = networks[`${currentNetwork}`].host; return provider; } catch (_a) { throw Error("Please specify a target network or ensure 'development' is one of the networks"); } }; const contractPath = (contract) => { const contractFile = contract.endsWith('.sol') ? contract : `${contract}.sol`; return path.join(process.cwd(), contractFile); }; exports.readUtf8 = (absolutePath) => fs.readFileSync(absolutePath, utf8); exports.readContract = (contract) => exports.readUtf8(contractPath(contract)); exports.compile = function (sol, locally) { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (locally) { return new Promise((resolve, reject) => { const { contracts, errors } = aionSolc.compile(sol, 1); if (errors) reject(errors); resolve(contracts); }); } else { const nodeAddress = exports.getProvider(); const aion = new core_1.Aion(nodeAddress); return aion.compile(sol); } }); }; exports.unlock = function (addr, pw) { return tslib_1.__awaiter(this, void 0, void 0, function* () { cli_ux_1.default.action.start('unlocking'); const nodeAddress = exports.getProvider(); const aion = new core_1.Aion(nodeAddress); const unlocked = yield aion.unlock(addr, pw); const message = unlocked ? 'Successfully unlocked' : 'Failed to unlock'; cli_ux_1.default.action.stop(message); }); }; exports.deploy = function ({ abi, code, args, privateKey }, targetNetwork) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const nodeAddress = exports.getProvider(targetNetwork); const aion = new core_1.Aion(nodeAddress); let from; if (privateKey) { from = yield aion.web3.eth.accounts.privateKeyToAccount(privateKey); } else { from = defaultAccount || (yield aion.getAccounts())[0]; } return aion .deploy({ code, abi, from, args, privateKey }) .then(txReceipt => { return txReceipt; }) .catch(error => { console.error(/Error:\s+(.+)/gi.exec(error)[1]); return error; }); }); }; const promisify = (fn, ...args) => new Promise((resolve, reject) => { fn(...args, function (err, res) { if (err) { reject(err); } resolve(res); }); }); exports.downloadPack = (_pack, _path) => tslib_1.__awaiter(this, void 0, void 0, function* () { const downloadPath = _path || process.cwd(); cli_ux_1.default.action.start('downloading'); switch (_pack) { case 'react': yield promisify(download, 'github:titan-suite-packs/react-pack', downloadPath); break; case 'react-native': yield promisify(download, 'github:titan-suite-packs/react-native-pack', downloadPath); break; case 'default': yield promisify(download, 'github:titan-suite-packs/default-pack', downloadPath); break; default: console.log('This pack is not available. View available packs at https://github.com/titan-suite-packs'); console.log('Run titan unpack -h for the correct usage'); } cli_ux_1.default.action.stop(); }); exports.createTemplate = (type, name) => tslib_1.__awaiter(this, void 0, void 0, function* () { cli_ux_1.default.action.start(`creating new ${type} file`); let boltPath; let template; switch (type) { case 'contract': boltPath = path.join(process.cwd(), 'contracts', `${name}.sol`); template = yield templates_1.getTemplateContract(name); createFile('contracts', boltPath, template); break; case 'test': boltPath = path.join(process.cwd(), 'test', `${name}.js`); template = yield templates_1.getTemplateTest(name); createFile('test', boltPath, template); break; // case 'migration': // TODO // break default: } cli_ux_1.default.action.stop(); }); const createFile = (name, filePath, content) => tslib_1.__awaiter(this, void 0, void 0, function* () { mkdirp(name, err => { if (err) { throw err; } else { fs.writeFile(filePath, content, err => { if (err) throw err; }); } }); });