move generateClass.js to seperate package

This commit is contained in:
perissology 2017-09-22 10:38:10 -07:00
parent bb14e679f0
commit a6cd06d6c0
4 changed files with 4 additions and 113 deletions

View File

@ -1,109 +0,0 @@
const Web3PromiEvent = require('web3-core-promievent');
function checkWeb3(web3) {
if (typeof web3.version !== 'string' || !web3.version.startsWith('1.')) {
throw new Error('web3 version 1.x is required');
}
}
const estimateGas = (web3, method, opts) => {
if (opts.$noEstimateGas) return Promise.resolve(4700000);
if (opts.$gas || opts.gas) return Promise.resolve(opts.$gas || opts.gas);
return method.estimateGas(opts)
// eslint-disable-next-line no-confusing-arrow
.then(gas => opts.$extraGas ? gas + opts.$extraGas : Math.floor(gas * 1.1));
};
// if constant method, executes a call, otherwise, estimates gas and executes send
const execute = (web3, txObject, opts, cb) => {
const { _method } = txObject;
if (_method.constant) return txObject.call(opts);
// we need to create a new PromiEvent here b/c estimateGas returns a regular promise
// however on a 'send' we want to return a PromiEvent
const defer = new Web3PromiEvent();
const relayEvent = event => (...args) => defer.eventEmitter.emit(event, ...args);
estimateGas(web3, txObject, opts)
.then((gas) => {
Object.assign(opts, { gas });
return (cb) ? txObject.send(opts, cb) : txObject.send(opts)
// relay all events to our promiEvent
.on('transactionHash', relayEvent('transactionHash'))
.on('confirmation', relayEvent('confirmation'))
.on('receipt', relayEvent('receipt'))
.on('error', relayEvent('error'));
})
.then(defer.resolve)
.catch(defer.reject);
return defer.eventEmitter;
};
const methodWrapper = (web3, method, ...args) => {
let cb;
let opts = {};
if (typeof args[args.length - 1] === 'function') cb = args.pop();
if (typeof args[args.length - 1] === 'object') opts = args.pop();
const txObject = method(...args);
return execute(web3, txObject, opts, cb);
};
module.exports = (abi, bytecode) => {
const C = function C(web3, address) {
checkWeb3(web3);
this.$web3 = web3;
this.$address = address;
this.$contract = new web3.eth.Contract(abi, address);
this.$abi = abi;
this.$byteCode = bytecode;
Object.keys(this.$contract.methods)
.filter(key => !key.startsWith('0x'))
.forEach((key) => {
this[key] = (...args) => methodWrapper(web3, this.$contract.methods[key], ...args);
});
// set default from address
web3.eth.getAccounts()
.then((accounts) => {
this.$contract.options.from = (accounts.length > 0) ? accounts[0] : undefined;
});
};
C.new = function (web3, ...args) {
let opts = {};
if (args && args.length > 0 && typeof args[args.length - 1] === 'object') {
opts = args.pop();
}
const deploy = new web3.eth.Contract(abi)
.deploy({
data: bytecode,
arguments: args,
});
const getAccount = () => {
if (opts.from) return Promise.resolve(opts.from);
return web3.eth.getAccounts()
// eslint-disable-next-line no-confusing-arrow
.then(accounts => (accounts.length > 0) ? accounts[0] : undefined);
};
return getAccount()
.then(account => Object.assign(opts, { from: account }))
.then(() => execute(web3, deploy, opts))
.then(contract => new C(web3, contract.options.address));
};
return C;
};

View File

@ -2,7 +2,7 @@ const LiquidPledgingAbi = require('../build/LiquidPledging.sol').LiquidPledgingA
const LiquidPledgingCode = require('../build/LiquidPledging.sol').LiquidPledgingByteCode;
const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi;
const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode;
const generateClass = require('./generateClass');
const generateClass = require('eth-contract-class');
module.exports = (test) => {
const $abi = (test) ? LiquidPledgingMockAbi : LiquidPledgingAbi;

View File

@ -1,5 +1,5 @@
const VaultAbi = require('../build/Vault.sol').VaultAbi;
const VaultByteCode = require('../build/Vault.sol').VaultByteCode;
const generateClass = require('./generateClass');
const generateClass = require('eth-contract-class');
module.exports = generateClass(VaultAbi, VaultByteCode);

View File

@ -42,8 +42,8 @@
"homepage": "https://github.com/Giveth/liquidpledging#readme",
"dependencies": {
"async": "^2.4.0",
"ethconnector": "0.0.25",
"chai": "^4.1.0",
"web3-core-promievent": "^1.0.0-beta.18"
"eth-contract-class": "0.0.1",
"ethconnector": "0.0.25"
}
}