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

Apply Configs #97

Merged
merged 11 commits into from
Feb 27, 2019
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ package-lock.json
contractApis
state.vvisp.json
service.vvisp.json
vvisp-config.js
example.json
example

Expand Down
8 changes: 8 additions & 0 deletions packages/vvisp-utils/contracts/ErrorImportContract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pragma solidity ^0.4.23;

import 'errorImport/where/is/contract/file.sol';

contract ErrorContract {
function ErrorContract(){
}
}
8 changes: 7 additions & 1 deletion packages/vvisp-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"bip39": "^2.5.0",
"chalk": "^2.4.1",
"truffle-provider": "^0.1.4",
"ethereumjs-tx": "^1.3.7",
"ethereumjs-wallet": "^0.6.2",
"find-cache-dir": "^2.0.0",
Expand All @@ -54,7 +55,12 @@
"web3": "1.0.0-beta.37"
},
"devDependencies": {
"dotenv": "^5.0.1",
"openzeppelin-solidity": "2.0.0"
},
"nyc": {
"exclude": [
"test",
"*-config.js"
]
}
}
249 changes: 249 additions & 0 deletions packages/vvisp-utils/src/Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// reference: https://github.com/trufflesuite/truffle/blob/40a9c615eee78f14854ced65766ed3d370da07ae/packages/truffle-config/index.js
const _ = require('lodash');
const Provider = require('truffle-provider');
const path = require('path');

const DEFAULT_CONFIG_FILE = 'vvisp-config.js';
const DEFAULT_NETWORK = 'development';

const getConfigRoot = require('./getConfigRoot');
const getPrivateKey = require('./getPrivateKey');
const filterPrivateKey = require('./filterPrivateKey');
const forIn = require('./forIn');

function Config() {
const self = this;

const defaultTxOptions = {
gasLimit: 6721975,
gasPrice: 20000000000 // 20 gwei,
};

this._deepCopy = ['networks', 'compilers'];

this._values = {
network: null, // default config is development
networks: {},
verboseRpc: false,
gasLimit: null,
gasPrice: null,
from: null,
compilers: {
solc: {
settings: {
optimizer: {
enabled: false,
runs: 200
},
evmVersion: 'byzantium'
}
},
vyper: {}
}
};

const props = {
network: {},
networks: {},
verboseRpc: {},
compilers: {},

from: {
get: function() {
const value = self._values['from'];
if (typeof value === 'string') {
return value;
} else if (typeof value === 'object' && value !== null) {
return getPrivateKey(value.mnemonic, value.index);
} else {
throw new Error(`from is not set properly, got ${value}`);
}
},
set: function(value) {
if (typeof value === 'string') {
self._values['from'] = filterPrivateKey(value);
return;
} else if (typeof value === 'object' && value !== null) {
if (typeof value.mnemonic === 'string') {
self._values['from'] = value;
return;
}
}
throw new TypeError(`${JSON.stringify(value)} is invalid key format`);
}
},
network_config: {
get: function() {
const network = self.network;

if (network === null || network === undefined) {
throw new Error(
'Network not set. Cannot determine network to use. Set config.network or add option --network <network>'
);
}

return _.extend({}, defaultTxOptions, self.networks[network]);
},
set: function() {
throw new Error(
'Do not set config.network_config. Instead, set config.networks with the desired values.'
);
}
},
network_id: {
get: function() {
try {
return self.network_config.network_id;
} catch (e) {
return null;
}
},
set: function() {
throw new Error(
'Do not set config.network_id. Instead, set config.networks and then config.networks[<network name>].network_id'
);
}
},
gasLimit: {
get: function() {
try {
return self.network_config.gas || self.network_config.gasLimit;
} catch (e) {
return defaultTxOptions.gasLimit;
}
},
set: function() {
throw new Error(
"Don't set config.gas directly. Instead, set config.networks and then config.networks[<network name>].gas"
);
}
},
gasPrice: {
get: function() {
try {
return self.network_config.gasPrice;
} catch (e) {
return defaultTxOptions.gasPrice;
}
},
set: function() {
throw new Error(
"Don't set config.gasPrice directly. Instead, set config.networks and then config.networks[<network name>].gasPrice"
);
}
},
provider: {
get: function() {
if (!self.network) {
return null;
}

const options = self.network_config;
options.verboseRpc = self.verboseRpc;

return Provider.create(options);
},
set: function() {
throw new Error(
"Don't set config.provider directly. Instead, set config.networks and then set config.networks[<network name>].provider"
);
}
}
};

forIn(props, (value, key) => {
self.addProperty(key, value);
});
}

Config.prototype.addProperty = function(propertyName, descriptor = {}) {
Object.defineProperty(this, propertyName, {
get:
descriptor.get ||
function() {
// value is specified
if (propertyName in this._values) {
return this._values[propertyName];
}

// default getter is specified
if (descriptor.default) {
return descriptor.default();
}

// descriptor is a function
return descriptor();
},
set:
descriptor.set ||
function(value) {
this._values[propertyName] = descriptor.transform
? descriptor.transform(value)
: value;
},
configurable: true,
enumerable: true
});
};

Config.prototype.merge = function(obj = {}) {
const self = this;
const clone = _.cloneDeep(obj);

forIn(clone, (value, key) => {
if (self.hasOwnProperty(key)) {
if (typeof clone[key] === 'object' && self._deepCopy.includes(key)) {
self[key] = _.merge(self[key], clone[key]);
} else {
self[key] = clone[key];
}
}
});

return this;
};

Config.search = (options = {}) => {
const configFileName = options.configFile || DEFAULT_CONFIG_FILE;
// if there is no file, getConfigRoot will throw error
return path.join(getConfigRoot(configFileName), configFileName);
};

Config.load = options => {
const file = Config.search(options);

const config = new Config();

const fileConfig = require(file);
config.merge(fileConfig);
config.merge(options);

if (!config.network && config.networks.hasOwnProperty(DEFAULT_NETWORK)) {
config.network = DEFAULT_NETWORK;
}

return config;
};

let storedConfig;

Config.get = options => {
if (!storedConfig) {
storedConfig = Config.load(options);
}

return storedConfig;
};

Config.setStore = config => {
if (!(config instanceof Config)) {
throw new TypeError('Should set an instance of Config');
}
storedConfig = config;
};

Config.delete = () => {
storedConfig = undefined;
};

module.exports = Config;
33 changes: 27 additions & 6 deletions packages/vvisp-utils/src/compile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module.exports = async function(filePath, options) {
const DEFAULT_COMPILER_VERSION = '0.4.24';

const compilerOption = extractCompilerOption(options);

const fs = require('fs');
const path = require('path');
const solc = await getSolc();
const solc = await getSolc(compilerOption);
const chalk = require('chalk');
const printOrSilent = require('./printOrSilent');
const findNodeModules = require('find-node-modules');
Expand Down Expand Up @@ -33,6 +35,8 @@ module.exports = async function(filePath, options) {
language: 'Solidity',
sources: input,
settings: {
evmVersion: compilerOption.settings.evmVersion,
optimizer: compilerOption.settings.optimizer,
outputSelection: {
'*': {
'*': ['*']
Expand Down Expand Up @@ -102,13 +106,30 @@ module.exports = async function(filePath, options) {
return result;
}

async function getSolc() {
const CompilerSupplier = require('./utils/compilerSupplier');
async function getSolc(compilerOption) {
const CompilerSupplier = require('./compilerSupplier');
const supplier = new CompilerSupplier({
version: process.env.SOLC_VERSION
? process.env.SOLC_VERSION
: DEFAULT_COMPILER_VERSION
version: compilerOption.version
});
return supplier.load();
}

function extractCompilerOption(options) {
let compilers;
try {
compilers = options.config.compilers.solc || {};
} catch (e) {
compilers = {};
}

if (!compilers.version) {
compilers.version = DEFAULT_COMPILER_VERSION;
}

if (!compilers.settings) {
compilers.settings = {};
}

return compilers;
}
};
4 changes: 2 additions & 2 deletions packages/vvisp-utils/src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = async function(
arguments,
options
) {
const filterPrivateKey = require('./utils/filterPrivateKey');
const filterPrivateKey = require('./filterPrivateKey');
privateKey = filterPrivateKey(privateKey);

if (arguments && arguments.length === undefined) {
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = async function(
}

const sendTx = require('./sendTx');
const web3 = require('./getWeb3')();
const web3 = require('./web3Store').get();

const inputs = { data: '0x' + bytecode, arguments };
const deployData = new web3.eth.Contract(abi).deploy(inputs).encodeABI();
Expand Down
4 changes: 2 additions & 2 deletions packages/vvisp-utils/src/getConfigRoot.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = async function(configFileName) {
module.exports = function(configFileName) {
const findUp = require('find-up');
const path = require('path');

let configPath = await findUp([configFileName]);
let configPath = findUp.sync([configFileName]);
if (!configPath) {
throw new Error(`${configFileName} is not found`);
}
Expand Down
16 changes: 4 additions & 12 deletions packages/vvisp-utils/src/getPrivateKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,10 @@ module.exports = function(mnemonic, index) {
.toString('hex');

function checkInputs(mnemonic, index) {
switch (mnemonic) {
case undefined || null:
throw new TypeError(`Input mnemonic is ${mnemonic}`);
default:
if (typeof mnemonic !== 'string') {
throw new TypeError('Input 12 words in string type');
} else {
const wordsNumber = mnemonic.split(' ').length;
if (wordsNumber !== 12) {
throw new TypeError('Input 12 words, not ' + wordsNumber);
}
}
if (typeof mnemonic !== 'string') {
throw new TypeError(
`Input mnemonic is ${mnemonic}, it should be string type`
);
}
switch (index) {
case undefined || null:
Expand Down
Loading