update liquidpledging wrapper

This commit is contained in:
perissology 2017-09-20 11:44:34 +02:00
parent 9404393220
commit e993ec27ae

View File

@ -19,21 +19,31 @@ const estimateGas = (web3, method, opts) => {
.then(gas => opts.$extraGas ? gas + opts.$extraGas : Math.floor(gas * 1.1)); .then(gas => opts.$extraGas ? gas + opts.$extraGas : Math.floor(gas * 1.1));
}; };
// estimate gas before send if necessary // if constant method, executes a call, otherwise, estimates gas and executes send
const sendWithDefaults = (web3, txObject) => { const execute = (web3, txObject, opts, cb) => {
const origSend = txObject.send; const { _method } = txObject;
if (_method.constant) return txObject.call(opts);
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
txObject.send = (opts = {}, cb) => estimateGas(web3, txObject, opts) return estimateGas(web3, txObject, opts)
.then((gas) => { .then((gas) => {
Object.assign(opts, { gas }); Object.assign(opts, { gas });
return (cb) ? origSend(opts, cb) : origSend(opts); return (cb) ? txObject.send(opts, cb) : txObject.send(opts);
}); });
return txObject;
}; };
const extendMethod = (web3, method) => (...args) => sendWithDefaults(web3, method(...args)); 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 = (test) => { module.exports = (test) => {
@ -58,8 +68,10 @@ module.exports = (test) => {
this.notes = []; this.notes = [];
this.managers = []; this.managers = [];
Object.keys(this.$contract.methods).forEach((key) => { Object.keys(this.$contract.methods)
this[key] = extendMethod(web3, this.$contract.methods[key]); .filter(key => !key.startsWith('0x'))
.forEach((key) => {
this[key] = (...args) => methodWrapper(web3, this.$contract.methods[key], ...args);
}); });
// set default from address // set default from address
@ -73,12 +85,12 @@ module.exports = (test) => {
const note = { const note = {
delegates: [], delegates: [],
}; };
const res = await this.getNote(idNote).call(); const res = await this.getNote(idNote);
note.amount = this.$toNumber(res.amount); note.amount = this.$toNumber(res.amount);
note.owner = res.owner; note.owner = res.owner;
for (let i = 1; i <= this.$toDecimal(res.nDelegates); i += 1) { for (let i = 1; i <= this.$toDecimal(res.nDelegates); i += 1) {
const delegate = {}; const delegate = {};
const resd = await this.getNoteDelegate(idNote, i).call(); const resd = await this.getNoteDelegate(idNote, i);
delegate.id = this.$toDecimal(resd.idDelegate); delegate.id = this.$toDecimal(resd.idDelegate);
delegate.addr = resd.addr; delegate.addr = resd.addr;
delegate.name = resd.name; delegate.name = resd.name;
@ -105,7 +117,7 @@ module.exports = (test) => {
async $getManager(idManager) { async $getManager(idManager) {
const manager = {}; const manager = {};
const res = await this.getNoteManager(idManager).call(); const res = await this.getNoteManager(idManager);
if (res.managerType === '0') { if (res.managerType === '0') {
manager.type = 'Donor'; manager.type = 'Donor';
} else if (res.managerType === '1') { } else if (res.managerType === '1') {
@ -130,13 +142,13 @@ module.exports = (test) => {
notes: [null], notes: [null],
managers: [null], managers: [null],
}; };
const nNotes = await this.numberOfNotes().call(); const nNotes = await this.numberOfNotes();
for (let i = 1; i <= nNotes; i += 1) { for (let i = 1; i <= nNotes; i += 1) {
const note = await this.$getNote(i); const note = await this.$getNote(i);
st.notes.push(note); st.notes.push(note);
} }
const nManagers = await this.numberOfNoteManagers().call(); const nManagers = await this.numberOfNoteManagers();
for (let i = 1; i <= nManagers; i += 1) { for (let i = 1; i <= nManagers; i += 1) {
const manager = await this.$getManager(i); const manager = await this.$getManager(i);
st.managers.push(manager); st.managers.push(manager);
@ -262,7 +274,7 @@ module.exports = (test) => {
return getAccount() return getAccount()
.then(account => Object.assign(opts, { from: account })) .then(account => Object.assign(opts, { from: account }))
.then(() => sendWithDefaults(web3, deploy).send(opts)) .then(() => execute(web3, deploy, opts))
.then(contract => new LiquidPledging(web3, contract.options.address)); .then(contract => new LiquidPledging(web3, contract.options.address));
} }
}; };