2021-01-15 05:30:19 +01:00
|
|
|
if (typeof module != 'undefined') {
|
2021-05-10 18:15:25 +02:00
|
|
|
module.exports = {
|
|
|
|
Driver: Driver,
|
|
|
|
};
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function Driver(w3, lo, filters, syncer, callback) {
|
2021-05-10 18:15:25 +02:00
|
|
|
this.w3 = w3;
|
|
|
|
this.lo = lo;
|
|
|
|
this.hi = undefined;
|
|
|
|
this.filters = filters;
|
|
|
|
this.syncer = syncer;
|
|
|
|
this.callback = callback;
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
Driver.prototype.start = function (hi) {
|
|
|
|
const self = this;
|
2021-01-15 05:30:19 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
if (hi !== undefined) {
|
|
|
|
self.sync(hi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.w3.eth
|
|
|
|
.getBlockNumber()
|
|
|
|
.then(function (n) {
|
|
|
|
self.sync(n);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error('blocknumber fail', e);
|
|
|
|
});
|
2021-01-15 05:30:19 +01:00
|
|
|
};
|
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
Driver.prototype.sync = function (n) {
|
|
|
|
const self = this;
|
2021-01-15 05:30:19 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
self.hi = n;
|
2021-01-15 05:30:19 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
const countGetter = async (b) => {
|
|
|
|
return await self.getCount(b);
|
|
|
|
};
|
2021-01-15 05:30:19 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
const processor = async (b, t) => {
|
|
|
|
return await self.process(b, t);
|
|
|
|
};
|
2021-05-18 21:10:08 +02:00
|
|
|
self.syncer(self.lo, self.hi, self.filters[0], self.filters[1], countGetter, processor);
|
2021-01-15 05:30:19 +01:00
|
|
|
};
|
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
Driver.prototype.process = function (b, t) {
|
|
|
|
const self = this;
|
2021-01-15 05:30:19 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
self.w3.eth
|
|
|
|
.getTransactionFromBlock(b, t)
|
|
|
|
.then((t) => {
|
|
|
|
self.w3.eth
|
|
|
|
.getTransactionReceipt(t.hash)
|
|
|
|
.then((r) => {
|
|
|
|
console.log('Receipt: ', r);
|
|
|
|
self.callback(r);
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error('fail get receipt', e);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(function (e) {
|
|
|
|
//this.postMessage(['failed getTransactionFromBlock(' + b + ', ' + t + ')']);
|
|
|
|
self.callback([undefined]);
|
|
|
|
console.error('failed getTransactionFromBlock(' + b + ', ' + t + ')');
|
|
|
|
});
|
|
|
|
};
|
2021-01-15 05:30:19 +01:00
|
|
|
|
|
|
|
Driver.prototype.getCount = async function (b) {
|
2021-05-10 18:15:25 +02:00
|
|
|
const self = this;
|
2021-01-15 05:30:19 +01:00
|
|
|
|
2021-05-10 18:15:25 +02:00
|
|
|
const n = await self.w3.eth.getBlockTransactionCount(b);
|
|
|
|
return n;
|
2021-01-15 05:30:19 +01:00
|
|
|
};
|