Skip to content
This repository has been archived by the owner on Apr 18, 2021. It is now read-only.

Commit

Permalink
feat: add interactive to deploy-contract to determine the exact contr…
Browse files Browse the repository at this point in the history
…act (#172)

* feat: add interactive to deploy-contract to determine the exact contract

* fix: ready for deprecating compileAndDeploy
  • Loading branch information
JhChoy committed Jun 22, 2019
1 parent 3d651a9 commit 4924215
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 33 deletions.
2 changes: 2 additions & 0 deletions packages/vvisp-utils/src/compileAndDeploy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// TODO: will be deprecated at next major version

module.exports = async function(filePath, privateKey, arguments, options) {
const compile = require('./compile');
const deploy = require('./deploy');
Expand Down
27 changes: 27 additions & 0 deletions packages/vvisp-utils/src/getSTDInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Read one line from stdin and return it
* @param {String} question question to user by stdout
* @returns {Promise} Promise object to returns the value read from stdin
*/
module.exports = async function(question) {
if (question) {
process.stdout.write(question);
}

return new Promise(function(resolve) {
// process.stdin.once('data', function(data) {
// resolve(data.toString().trim());
// });
process.stdin.setEncoding('utf8');

process.stdin.on('data', chunk => {
// Use a loop to make sure we read all available data.
resolve(chunk);
});

process.stdin.on('end', () => {
console.log('There will be no more data.');
process.stdout.write('end');
});
});
};
14 changes: 8 additions & 6 deletions packages/vvisp-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const getRelativeFilePathsFromRoot = require('./getRelativeFilePathsFromRoot');
const getDependencyFiles = require('./getDependencyFiles');
const getSourceCodeWithoutPragma = require('./getSourceCodeWithoutPragma');
const getMaxVersion = require('./getPragmaMaxVersion');
const getSTDInput = require('./getSTDInput');
const parseLogs = require('./parseLogs');

module.exports = {
Expand All @@ -35,13 +36,14 @@ module.exports = {
getAllFiles,
getCompiledContracts,
getCycle,
web3Store,
getPrivateKey,
printOrSilent,
getConfigRoot,
getRelativeFilePathsFromRoot,
getDependencyFiles,
getSourceCodeWithoutPragma,
getMaxVersion,
parseLogs
getPrivateKey,
getRelativeFilePathsFromRoot,
getSourceCodeWithoutPragma,
getSTDInput,
parseLogs,
printOrSilent,
web3Store
};
30 changes: 9 additions & 21 deletions packages/vvisp/scripts/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require('path');
const chalk = require('chalk');
const stringArgv = require('string-argv');
const _ = require('lodash');
const { parseLogs } = require('@haechi-labs/vvisp-utils');
const { getSTDInput, parseLogs } = require('@haechi-labs/vvisp-utils');
const { STATE_FILE } = require('../config/Constant');

if (!String.prototype.format) {
Expand Down Expand Up @@ -110,8 +110,7 @@ function ApiCommander(apis) {
run: async function(options) {
while (this.end) {
const prompt = '>> ';
process.stdout.write(prompt);
const line = await readLine();
const line = await getSTDInput(prompt);
const args = stringArgv(line);

if (args.length === 0) {
Expand Down Expand Up @@ -233,14 +232,14 @@ async function register() {
'To dynamically register a contract to the console, write the contract name, address, and filename.'
);

process.stdout.write('Enter the name of contract: ');
const contractName = await readLine();
const contractName = await getSTDInput('Enter the name of contract: ');

const questions = ['address', 'fileName'];
const contract = {};
for (const key in questions) {
process.stdout.write('Enter the {0} of contract: '.format(questions[key]));
contract[questions[key]] = await readLine();
contract[questions[key]] = await getSTDInput(
'Enter the {0} of the contract: '.format(questions[key])
);
}

const f = fs.readFileSync(defaultStateFile, { encoding: 'utf8' });
Expand Down Expand Up @@ -499,8 +498,9 @@ async function getAddressFromSTDIN(rawApis) {

const contracts = {};
for (const key of Object.keys(rawApis)) {
process.stdout.write('\nEnter the address of {0}: '.format(key));
const address = await readLine();
const address = await getSTDInput(
'\nEnter the address of {0}: '.format(key)
);
contracts[key] = {
api: rawApis[key],
address: address
Expand All @@ -509,15 +509,3 @@ async function getAddressFromSTDIN(rawApis) {

return contracts;
}

/**
* Read one line from stdin and return it
* @returns {Promise} Promise object to returns the value read from stdin
*/
function readLine() {
return new Promise(function(resolve) {
process.stdin.once('data', function(data) {
resolve(data.toString().trim());
});
});
}
55 changes: 52 additions & 3 deletions packages/vvisp/scripts/deploy-contract.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
module.exports = async function(file, arguments, options) {
options = require('./utils/injectConfig')(options);

const path = require('path');
const {
compileAndDeploy,
compile,
deploy,
getSTDInput,
printOrSilent
} = require('@haechi-labs/vvisp-utils');

Expand All @@ -29,7 +32,53 @@ module.exports = async function(file, arguments, options) {
return arg;
});

await compileAndDeploy(file, privateKey, arguments, options);
const output = await compile(file, options);

printOrSilent('Deploying Finished!', options);
const compileOutputKeys = Object.keys(output.contracts);
const contractCandidates = [];
for (const compileOutputKey of compileOutputKeys) {
const splits = compileOutputKey.split(':');
if (path.join(splits[0]) === path.join(file)) {
contractCandidates.push(splits[1]);
}
}

let targetContractName;
if (contractCandidates.length > 1) {
let question = '\nSelect the number of the contract you want to deploy\n';
for (let i = 0; i < contractCandidates.length; i++) {
question += `${i + 1}. ${contractCandidates[i]}\n`;
}
question += `\nNumber(1 ~ ${contractCandidates.length}): `;
let index;
while (1) {
index = parseInt(await getSTDInput(question));
if (
typeof index !== 'number' ||
index < 1 ||
index > contractCandidates.length
) {
console.log(
'Input Type Wrong, please choose the number between 0 ~ ' +
contractCandidates.length
);
continue;
}
break;
}
targetContractName = contractCandidates[index - 1];
} else {
targetContractName = contractCandidates[0];
}

const deployTarget = output.contracts[file + ':' + targetContractName];

printOrSilent('Deploying ' + targetContractName + '...', options);
const receipt = await deploy(deployTarget, privateKey, arguments, options);

printOrSilent(targetContractName + ' Contract Created!', options);
printOrSilent('Contract Address: ' + receipt.contractAddress, options);

printOrSilent('Deployment Finished!', options);
process.exit();
};
23 changes: 20 additions & 3 deletions packages/vvisp/test/scripts/gen-script/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const fs = require('fs-extra');
const genScript = require('../../../scripts/gen-script');

const {
compileAndDeploy,
compile,
deploy,
Config,
forIn,
web3Store,
Expand Down Expand Up @@ -134,11 +135,27 @@ describe('# gen-script process test', function() {
web3Setting
);
const txCount = await web3.eth.getTransactionCount(SENDER);
this.receipt1 = await compileAndDeploy(CONTRACT_PATH1, PRIV_KEY, [], {

let output = await compile(CONTRACT_PATH1, {
silent: true
});
let deployTarget =
output.contracts[
CONTRACT_PATH1 + ':' + path.parse(CONTRACT_PATH1).name
];
this.receipt1 = await deploy(deployTarget, PRIV_KEY, [], {
silent: true,
txCount: txCount
});
this.receipt2 = await compileAndDeploy(CONTRACT_PATH2, PRIV_KEY, [], {

output = await compile(CONTRACT_PATH2, {
silent: true
});
deployTarget =
output.contracts[
CONTRACT_PATH2 + ':' + path.parse(CONTRACT_PATH2).name
];
this.receipt2 = await deploy(deployTarget, PRIV_KEY, [], {
silent: true,
txCount: txCount + 1
});
Expand Down

0 comments on commit 4924215

Please sign in to comment.