create a generateClass function

This commit is contained in:
perissology 2017-09-20 17:07:34 +02:00
parent e993ec27ae
commit a8e89a4451
3 changed files with 261 additions and 250 deletions

95
js/generateClass.js Normal file
View File

@ -0,0 +1,95 @@
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);
// eslint-disable-next-line no-param-reassign
return estimateGas(web3, txObject, opts)
.then((gas) => {
Object.assign(opts, { gas });
return (cb) ? txObject.send(opts, cb) : txObject.send(opts);
});
};
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

@ -3,85 +3,15 @@ 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;
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);
// eslint-disable-next-line no-param-reassign
return estimateGas(web3, txObject, opts)
.then((gas) => {
Object.assign(opts, { gas });
return (cb) ? txObject.send(opts, cb) : txObject.send(opts);
});
};
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);
};
const generateClass = require('./generateClass');
module.exports = (test) => {
const $abi = (test) ? LiquidPledgingMockAbi : LiquidPledgingAbi;
const $byteCode = (test) ? LiquidPledgingMockCode : LiquidPledgingCode;
const LiquidPledging = generateClass($abi, $byteCode);
return class LiquidPledging {
constructor(web3, address) {
checkWeb3(web3);
this.$web3 = web3;
this.$address = address;
this.$contract = new web3.eth.Contract($abi, address);
this.$abi = $abi;
this.$byteCode = $byteCode;
// helpers
this.$toNumber = web3.utils.toBN;
this.$toDecimal = web3.utils.toDecimal;
this.notes = [];
this.managers = [];
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;
});
}
async $getNote(idNote) {
async function $getNote(idNote) {
const note = {
delegates: [],
};
@ -115,7 +45,9 @@ module.exports = (test) => {
return note;
}
async $getManager(idManager) {
LiquidPledging.prototype.$getNote = $getNote;
async function $getManager(idManager) {
const manager = {};
const res = await this.getNoteManager(idManager);
if (res.managerType === '0') {
@ -135,9 +67,11 @@ module.exports = (test) => {
manager.canceled = res.canceled;
}
return manager;
}
};
async getState() {
LiquidPledging.prototype.$getManager = $getManager;
async function getState() {
const st = {
notes: [null],
managers: [null],
@ -154,9 +88,11 @@ module.exports = (test) => {
st.managers.push(manager);
}
return st;
}
};
generateDonorsState() {
LiquidPledging.prototype.getState = getState;
LiquidPledging.prototype.generateDonorsState = function () {
const donorsState = [];
const getDonor = (idNote) => {
@ -255,27 +191,7 @@ module.exports = (test) => {
}
this.donorsState = donorsState;
}
static new(web3, vault, opts = {}) {
const deploy = new web3.eth.Contract($abi)
.deploy({
data: $byteCode,
arguments: [vault],
});
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 LiquidPledging(web3, contract.options.address));
}
};
return LiquidPledging;
};

View File

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