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

Commit

Permalink
fix: change getWeb3 to web3Store
Browse files Browse the repository at this point in the history
  • Loading branch information
JhChoy committed Feb 25, 2019
1 parent 25a1277 commit cf99582
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/vvisp-utils/src/deploy.js
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion packages/vvisp-utils/src/getTxCount.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = async function(target) {
const web3 = require('./getWeb3')();
const web3 = require('./web3Store').get();
if (web3.utils.isAddress(target)) {
return web3.eth.getTransactionCount(target);
} else {
Expand Down
18 changes: 0 additions & 18 deletions packages/vvisp-utils/src/getWeb3.js

This file was deleted.

4 changes: 2 additions & 2 deletions packages/vvisp-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const getAllFiles = require('./getAllFiles');
const getCompiledContracts = require('./getCompiledContracts');
const getCycle = require('./getCycle');
const getTxCount = require('./getTxCount');
const getWeb3 = require('./getWeb3');
const web3Store = require('./web3Store');
const getPrivateKey = require('./getPrivateKey');
const printOrSilent = require('./printOrSilent');
const privateKeyToAddress = require('./privateKeyToAddress');
Expand All @@ -35,7 +35,7 @@ module.exports = {
getCompiledContracts,
getCycle,
getTxCount,
getWeb3,
web3Store,
getPrivateKey,
printOrSilent,
privateKeyToAddress,
Expand Down
2 changes: 1 addition & 1 deletion packages/vvisp-utils/src/privateKeyToAddress.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = function(privateKey) {
const filterPrivateKey = require('./filterPrivateKey');
privateKey = filterPrivateKey(privateKey);
const web3 = require('./getWeb3')();
const web3 = require('./web3Store').get();

return web3.eth.accounts.privateKeyToAccount(privateKey).address;
};
9 changes: 6 additions & 3 deletions packages/vvisp-utils/src/sendTx.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module.exports = async function(_toAddr, _value, _privKey, options) {
const Tx = require('ethereumjs-tx');
const web3 = require('./getWeb3')();
const web3 = require('./web3Store').get();
const privateKeyToAddress = require('./privateKeyToAddress');
const printOrSilent = require('./printOrSilent');

const DEFAULT_GAS_LIMIT = web3.utils.toHex(4600000);
const DEFAULT_GAS_PRICE = web3.utils.toHex(10e9);

return main(_toAddr, _value, _privKey, options);

async function main(_toAddr, _value, _privKey, options) {
Expand All @@ -14,8 +17,8 @@ module.exports = async function(_toAddr, _value, _privKey, options) {
// construct the transaction data
const txData = {
nonce: web3.utils.toHex(txCount),
gasLimit: options.gasLimit || web3.utils.toHex(4600000), // TODO: mv to config file
gasPrice: options.gasPrice || web3.utils.toHex(10e9),
gasLimit: options.gasLimit || DEFAULT_GAS_LIMIT,
gasPrice: options.gasPrice || DEFAULT_GAS_PRICE,
to: _toAddr,
from: fromAddr,
value: _value || '0x',
Expand Down
35 changes: 35 additions & 0 deletions packages/vvisp-utils/src/web3Store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const Web3 = require('web3');

let web3;
const voidWeb3 = new Web3();
module.exports = (function() {
return {
/**
@dev setWithProvider set web3 with provider
*/
setWithProvider: provider => {
web3 = new Web3(provider);
},
/**
@dev setWithURL set web3 with http url
*/
setWithURL: url => {
web3 = new Web3(new Web3.providers.HttpProvider(url));
},
/**
@dev get getter of stored web3, if there isn't, return void web3
*/
get: () => {
if (!web3) {
return voidWeb3;
}
return web3;
},
/**
@dev delete delete stored web3 for test
*/
delete: () => {
web3 = undefined;
}
};
})();
17 changes: 13 additions & 4 deletions packages/vvisp-utils/test/deploy.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
const chai = require('chai');
chai.use(require('chai-as-promised')).should();
require('dotenv').config();

const {
compile,
deploy,
getCompiledContracts,
getWeb3,
web3Store,
getPrivateKey,
privateKeyToAddress
} = require('../src');
const web3 = getWeb3();
const path = require('path');
const fs = require('fs-extra');
const testEnv = fs.readJsonSync(path.join(__dirname, 'test.env.json'));

const RIGHT_CONTRACT = path.join(__dirname, '../contracts/DependencyA.sol');
const ARRAY_INPUT_CONTRACT = path.join(
__dirname,
'../contracts/DependencyD.sol'
);
const NO_INPUT_CONTRACT = path.join(__dirname, '../contracts/SecondB.sol');
const PRIV_KEY = getPrivateKey(process.env.MNEMONIC);
const PRIV_KEY = getPrivateKey(testEnv.mnemonic);
const SENDER = privateKeyToAddress(PRIV_KEY);

describe('# deploy test', function() {
this.timeout(50000);

let web3;

before(async function() {
web3Store.setWithURL(testEnv.url);
web3 = web3Store.get();

const compileOutput = await compile(
[RIGHT_CONTRACT, ARRAY_INPUT_CONTRACT, NO_INPUT_CONTRACT],
{ silent: true }
Expand All @@ -38,6 +43,10 @@ describe('# deploy test', function() {
this.secondB = getCompiledContracts(compileOutput, NO_INPUT_CONTRACT);
});

after(function() {
web3Store.delete();
});

describe('# input arguments', function() {
it('should reject wrong input arguments length', async function() {
await deploy(this.dependencyA, PRIV_KEY).should.be.rejectedWith(Error);
Expand Down
13 changes: 0 additions & 13 deletions packages/vvisp-utils/test/getWeb3.test.js

This file was deleted.

4 changes: 4 additions & 0 deletions packages/vvisp-utils/test/test.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"mnemonic": "piano garage flag neglect spare title drill basic strong aware enforce fury",
"url": "http://localhost:8545"
}
57 changes: 57 additions & 0 deletions packages/vvisp-utils/test/web3Store.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-as-promised')).should();

const Web3 = require('web3');
const { web3Store } = require('../src');

const path = require('path');
const fs = require('fs-extra');
const testEnv = fs.readJsonSync(path.join(__dirname, 'test.env.json'));

describe('web3Store', function() {
beforeEach(function() {
web3Store.delete();
});

after(function() {
web3Store.delete();
});

describe('#setWithProvider()', function() {
it('should set with provider', async function() {
web3Store.setWithProvider(new Web3.providers.HttpProvider(testEnv.url));
const web3 = web3Store.get();
expect(web3).to.be.an.instanceOf(Web3);
await web3.eth.getCoinbase().should.be.fulfilled;
});
});

describe('#setWithURL()', function() {
it('should set with url', async function() {
web3Store.setWithURL(testEnv.url);
const web3 = web3Store.get();
expect(web3).to.be.an.instanceOf(Web3);
await web3.eth.getCoinbase().should.be.fulfilled;
});
});

describe('#get()', function() {
it('should return void web3 when it is not set', async function() {
const web3 = web3Store.get();
expect(web3).to.be.an.instanceOf(Web3);
await web3.eth.getCoinbase().should.be.rejected;
});
});

describe('#delete()', function() {
it('should delete stored web3', async function() {
web3Store.setWithURL(testEnv.url);
web3Store.delete();

const web3 = web3Store.get();
expect(web3).to.be.an.instanceOf(Web3);
await web3.eth.getCoinbase().should.be.rejected;
});
});
});
3 changes: 1 addition & 2 deletions packages/vvisp/scripts/deploy-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = async function(options) {
checkEnv();
checkConfigExist();

const { printOrSilent, getWeb3 } = require('@haechi-labs/vvisp-utils');
const { printOrSilent } = require('@haechi-labs/vvisp-utils');
const { writeState } = require('./utils');
const DeployState = require('./DeployState');
const preProcess = require('./preProcess');
Expand All @@ -30,7 +30,6 @@ module.exports = async function(options) {
notImportant: chk.gray,
warning: chk.yellow
};
global.web3 = getWeb3();

await main();

Expand Down
1 change: 1 addition & 0 deletions packages/vvisp/scripts/deploy-service/preProcess/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = async function(deployState, options) {
const { SERVICE_PATH, STATE_PATH } = require('../../../config/Constant');
const { VARIABLES } = require('../constants');
const { forIn, printOrSilent } = require('@haechi-labs/vvisp-utils');
const web3 = options.web3;

let stateClone = deployState.getState();

Expand Down
7 changes: 5 additions & 2 deletions packages/vvisp/scripts/deploy-service/utils/pathToInstance.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = function(compileOutput, filePath) {
const { getCompiledContracts, getWeb3 } = require('@haechi-labs/vvisp-utils');
const web3 = getWeb3();
const {
getCompiledContracts,
web3Store
} = require('@haechi-labs/vvisp-utils');
const web3 = web3Store.get();

const abi = getCompiledContracts(compileOutput, filePath).interface;
return new web3.eth.Contract(JSON.parse(abi));
Expand Down

0 comments on commit cf99582

Please sign in to comment.