replace runethtx with web3 1.0

This commit is contained in:
perissology 2017-09-16 06:05:50 +02:00
parent 1f0c5fd987
commit 9404393220
2 changed files with 109 additions and 39 deletions

View File

@ -3,49 +3,99 @@ const LiquidPledgingAbi = require('../build/LiquidPledging.sol').LiquidPledgingA
const LiquidPledgingCode = require('../build/LiquidPledging.sol').LiquidPledgingByteCode; const LiquidPledgingCode = require('../build/LiquidPledging.sol').LiquidPledgingByteCode;
const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi; const LiquidPledgingMockAbi = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockAbi;
const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode; const LiquidPledgingMockCode = require('../build/LiquidPledgingMock.sol').LiquidPledgingMockByteCode;
const runethtx = require('runethtx');
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));
};
// estimate gas before send if necessary
const sendWithDefaults = (web3, txObject) => {
const origSend = txObject.send;
// eslint-disable-next-line no-param-reassign
txObject.send = (opts = {}, cb) => estimateGas(web3, txObject, opts)
.then((gas) => {
Object.assign(opts, { gas });
return (cb) ? origSend(opts, cb) : origSend(opts);
});
return txObject;
};
const extendMethod = (web3, method) => (...args) => sendWithDefaults(web3, method(...args));
module.exports = (test) => { module.exports = (test) => {
const LiquidPladgingContract = test ? const $abi = (test) ? LiquidPledgingMockAbi : LiquidPledgingAbi;
runethtx.generateClass(LiquidPledgingMockAbi, LiquidPledgingMockCode) : const $byteCode = (test) ? LiquidPledgingMockCode : LiquidPledgingCode;
runethtx.generateClass(LiquidPledgingAbi, LiquidPledgingCode);
return class LiquidPledging extends LiquidPladgingContract {
return class LiquidPledging {
constructor(web3, address) { constructor(web3, address) {
super(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.notes = [];
this.managers = []; this.managers = [];
this.b = "xxxxxxx b xxxxxxxx";
Object.keys(this.$contract.methods).forEach((key) => {
this[key] = extendMethod(web3, this.$contract.methods[key]);
});
// set default from address
web3.eth.getAccounts()
.then((accounts) => {
this.$contract.options.from = (accounts.length > 0) ? accounts[0] : undefined;
});
} }
async $getNote(idNote) { async $getNote(idNote) {
const note = { const note = {
delegates: [], delegates: [],
}; };
const res = await this.getNote(idNote); const res = await this.getNote(idNote).call();
note.amount = res[0]; note.amount = this.$toNumber(res.amount);
note.owner = res[1]; note.owner = res.owner;
for (let i = 1; i <= res[2].toNumber(); i += 1) { for (let i = 1; i <= this.$toDecimal(res.nDelegates); i += 1) {
const delegate = {}; const delegate = {};
const resd = await this.getNoteDelegate(idNote, i); const resd = await this.getNoteDelegate(idNote, i).call();
delegate.id = resd[0].toNumber(); delegate.id = this.$toDecimal(resd.idDelegate);
delegate.addr = resd[1]; delegate.addr = resd.addr;
delegate.name = resd[2]; delegate.name = resd.name;
note.delegates.push(delegate); note.delegates.push(delegate);
} }
if (res[3].toNumber()) { if (res.proposedProject) {
note.proposedProject = res[3].toNumber(); note.proposedProject = this.$toDecimal(res.proposedProject);
note.commmitTime = res[4].toNumber(); note.commmitTime = this.$toDecimal(res.commitTime);
} }
if (res[5].toNumber()) { if (res.oldNote) {
note.oldProject = res[5].toNumber(); note.oldProject = this.$toDecimal(res.oldNote);
} }
if (res[6].toNumber() === 0) { if (res.paymentState === '0') {
note.paymentState = 'NotPaid'; note.paymentState = 'NotPaid';
} else if (res[6].toNumber() === 1) { } else if (res.paymentState === '1') {
note.paymentState = 'Paying'; note.paymentState = 'Paying';
} else if (res[6].toNumber() === 2) { } else if (res.paymentState === '2') {
note.paymentState = 'Paid'; note.paymentState = 'Paid';
} else { } else {
note.paymentState = 'Unknown'; note.paymentState = 'Unknown';
@ -55,22 +105,22 @@ module.exports = (test) => {
async $getManager(idManager) { async $getManager(idManager) {
const manager = {}; const manager = {};
const res = await this.getNoteManager(idManager); const res = await this.getNoteManager(idManager).call();
if (res[0].toNumber() === 0) { if (res.managerType === '0') {
manager.paymentState = 'Donor'; manager.type = 'Donor';
} else if (res[0].toNumber() === 1) { } else if (res.managerType === '1') {
manager.paymentState = 'Delegate'; manager.type = 'Delegate';
} else if (res[0].toNumber() === 2) { } else if (res.managerType === '2') {
manager.paymentState = 'Project'; manager.type = 'Project';
} else { } else {
manager.paymentState = 'Unknown'; manager.type = 'Unknown';
} }
manager.addr = res[1]; manager.addr = res.addr;
manager.name = res[2]; manager.name = res.name;
manager.commitTime = res[3].toNumber(); manager.commitTime = this.$toDecimal(res.commitTime);
if (manager.paymentState === 'Project') { if (manager.paymentState === 'Project') {
manager.parentProject = res[4]; manager.parentProject = res.parentProject;
manager.canceled = res[5]; manager.canceled = res.canceled;
} }
return manager; return manager;
} }
@ -80,13 +130,13 @@ module.exports = (test) => {
notes: [null], notes: [null],
managers: [null], managers: [null],
}; };
const nNotes = await this.numberOfNotes(); const nNotes = await this.numberOfNotes().call();
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(); const nManagers = await this.numberOfNoteManagers().call();
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);
@ -194,5 +244,26 @@ module.exports = (test) => {
this.donorsState = donorsState; 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(() => sendWithDefaults(web3, deploy).send(opts))
.then(contract => new LiquidPledging(web3, contract.options.address));
}
}; };
}; };

View File

@ -43,7 +43,6 @@
"dependencies": { "dependencies": {
"async": "^2.4.0", "async": "^2.4.0",
"ethconnector": "0.0.25", "ethconnector": "0.0.25",
"runethtx": "0.1.1",
"chai": "^4.1.0" "chai": "^4.1.0"
} }
} }