cc-zos
Version:
Command-line interface for the ZeppelinOS smart contract platform
118 lines (99 loc) • 3.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _ccZosLib = require('cc-zos-lib');
var _util = require('util');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const log = new _ccZosLib.Logger('Truffle');
const Truffle = {
config() {
try {
const TruffleConfig = require('truffle-config');
return TruffleConfig.detect({ logger: console });
} catch (error) {
if (error.message === 'Could not find suitable configuration file.') {
throw Error('Could not find truffle.js config file, remember to initialize your project running "zos init".');
} else {
throw Error('Could not load truffle.js config file.\n' + error);
}
}
},
async compile(config = undefined) {
log.info("Compiling contracts");
config = config || this.config();
config.all = true;
const TruffleCompile = require('truffle-workflow-compile');
return new Promise((resolve, reject) => {
TruffleCompile.compile(config, (error, abstractions, paths) => {
if (error) reject(error);else resolve(abstractions, paths);
});
});
},
init(root = process.cwd()) {
this._initContractsDir(root);
this._initMigrationsDir(root);
this._initTruffleConfig(root);
},
// This function fixes a truffle issue related to HDWalletProvider that occurs when assigning
// the network provider as a function (that returns an HDWalletProvider instance) instead of
// assigning the HDWalletProvider instance directly.
// (see https://github.com/trufflesuite/truffle-hdwallet-provider/issues/65)
setNonceTrackerIfNeeded({ resolver, provider }) {
if (provider.engine && provider.engine.constructor.name === 'Web3ProviderEngine') {
const NonceSubprovider = require('web3-provider-engine/subproviders/nonce-tracker');
const nonceTracker = new NonceSubprovider();
provider.engine._providers.forEach((provider, index) => {
if (provider.constructor.name === 'ProviderSubprovider') {
nonceTracker.setEngine(provider.engine);
provider.engine._providers.splice(index, 0, nonceTracker);
}
});
resolver.options = Object.assign({}, resolver.options, { provider });
}
return { resolver, provider };
},
async getNetworkName() {
const version = await (0, _util.promisify)(global.web3.version.getNetwork.bind(global.web3.version))();
// Reference: see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md#list-of-chain-ids
switch (version) {
case "1":
return 'mainnet';
case "2":
return 'morden';
case "3":
return 'ropsten';
case "4":
return 'rinkeby';
case "42":
return 'kovan';
default:
return `dev-${version}`;
}
},
_initContractsDir(root) {
const contractsDir = `${root}/contracts`;
this._initDir(contractsDir);
},
_initMigrationsDir(root) {
const migrationsDir = `${root}/migrations`;
this._initDir(migrationsDir);
},
_initTruffleConfig(root) {
const truffleFile = `${root}/truffle.js`;
const truffleConfigFile = `${root}/truffle-config.js`;
if (!_ccZosLib.FileSystem.exists(truffleFile) && !_ccZosLib.FileSystem.exists(truffleConfigFile)) {
const blueprint = _path2.default.resolve(__dirname, './blueprint.truffle.js');
_ccZosLib.FileSystem.copy(blueprint, truffleConfigFile);
}
},
_initDir(dir) {
if (!_ccZosLib.FileSystem.exists(dir)) {
_ccZosLib.FileSystem.createDir(dir);
_ccZosLib.FileSystem.write(`${dir}/.gitkeep`, '');
}
}
};
exports.default = Truffle;