eris-db-promise
Advanced tools
Comparing version 1.0.2 to 1.0.3
'use strict' | ||
// const _ = require('lodash') | ||
const _ = require('lodash') | ||
@@ -26,2 +26,8 @@ /** | ||
.call('eventSubscribe', { event_id: eventId }) | ||
.then((info) => { | ||
if (!_.isObject(info) || !info.sub_id) | ||
return Promise.reject(new Error('Wrong RPC response received')) | ||
return info.sub_id | ||
}) | ||
} | ||
@@ -31,11 +37,17 @@ | ||
* Unsubscribe to an event type | ||
* @param {String} eventID | ||
* @param {String} subscriptionId | ||
* @return {Promise} | ||
*/ | ||
eventUnsubscribe (eventId) { | ||
if (!eventId || !eventId.length) | ||
return Promise.reject(new Error('EventId is required parameter')) | ||
eventUnsubscribe (subscriptionId) { | ||
if (!subscriptionId || !subscriptionId.length) | ||
return Promise.reject(new Error('SubscriptionId is required parameter')) | ||
return this._eris.request | ||
.call('eventUnsubscribe', { event_id: eventId }) | ||
.call('eventUnsubscribe', { sub_id: subscriptionId }) | ||
.then((info) => { | ||
if (!_.isObject(info) || !info.result) | ||
return Promise.reject(new Error('Wrong RPC response received')) | ||
return info.result | ||
}) | ||
} | ||
@@ -47,13 +59,19 @@ | ||
* because then the events will be passed automatically over the socket | ||
* @param {String} eventId | ||
* @param {String} subscriptionId | ||
* @return {Promise} | ||
*/ | ||
eventPoll (eventId) { | ||
if (!eventId || !eventId.length) | ||
return Promise.reject(new Error('EventId is required parameter')) | ||
eventPoll (subscriptionId) { | ||
if (!subscriptionId || !subscriptionId.length) | ||
return Promise.reject(new Error('SubscriptionId is required parameter')) | ||
return this._eris.request | ||
.call('eventPoll', { event_id: eventId }) | ||
.call('eventPoll', { sub_id: subscriptionId }) | ||
.then((info) => { | ||
if (!_.isObject(info) || !info.events) | ||
return Promise.reject(new Error('Wrong RPC response received')) | ||
return info.events | ||
}) | ||
} | ||
} |
@@ -48,27 +48,60 @@ 'use strict' | ||
_waitForConfirmation (tx, result) { | ||
return Promise.reject(new Error('Not implemented yet !')) | ||
return new Promise((resolve, reject) => { | ||
const checkBlock = (step = 0) => { | ||
const checkEvents = (subId, step = 0) => { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
this._eris | ||
.blockchain | ||
.getLatestBlock() | ||
.then((block) => { | ||
if (block.data.txs && block.data.txs.length > 0) | ||
console.log(new Buffer(block.data.txs[0], 'hex').toString()) | ||
console.log('==========================') | ||
console.log(block) | ||
console.log('==========================') | ||
.events | ||
.eventPoll(subId) | ||
.then((events) => { | ||
step++ | ||
if (step > 2) | ||
resolve(block) | ||
else | ||
checkBlock(step) | ||
if (!_.isArray(events) || !events.length) { | ||
// No need to check after 50 steps | ||
if (step >= 50) | ||
return reject(new Error('Transaction was not confirmed')) | ||
else | ||
return checkEvents(subId, step) | ||
} | ||
for (let i = 0; i < events.length; i++) { | ||
if (events[i].tx_id == result.tx_hash && _.isObject(events[i].call_data)) | ||
return resolve(events[i]) | ||
// handle error | ||
if (events[i].tx_id == result.tx_hash && events[i].exception.length > 0) | ||
return reject(new Error(events[i].exception)) | ||
} | ||
}) | ||
.catch(reject) | ||
}, 500) | ||
} | ||
}) | ||
} | ||
checkBlock() | ||
}) | ||
let subId | ||
return this._eris | ||
.events | ||
.eventSubscribe(`Acc/${result.contract_addr}/Call`) | ||
.then((newSubId) => { | ||
subId = newSubId | ||
return checkEvents(subId) | ||
}) | ||
.then((data) => { | ||
// unsubscribe | ||
return this._eris | ||
.events | ||
.eventUnsubscribe(subId) | ||
.then((result) => { | ||
return data | ||
}) | ||
}) | ||
.catch((err) => { | ||
//unsubscribe | ||
return this._eris | ||
.events | ||
.eventUnsubscribe(subId) | ||
.then(() => { | ||
return Promise.reject(err) | ||
}) | ||
.catch(() => { | ||
return Promise.reject(err) | ||
}) | ||
}) | ||
} | ||
@@ -75,0 +108,0 @@ |
{ | ||
"name": "eris-db-promise", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,2 +7,4 @@ 'use strict' | ||
let subscriptionId | ||
describe('component :: ', () => { | ||
@@ -34,5 +36,6 @@ | ||
.eventSubscribe(eventId) | ||
.then((info) => { | ||
expect(info).to.be.an('object') | ||
.and.to.have.property('sub_id') | ||
.then((newSubscriberId) => { | ||
expect(newSubscriberId).to.be.a('string') | ||
subscriptionId = newSubscriberId | ||
}) | ||
@@ -51,3 +54,3 @@ }) | ||
expect(err).to.be.an('error') | ||
.and.to.have.property('message', 'EventId is required parameter') | ||
.and.to.have.property('message', 'SubscriptionId is required parameter') | ||
}) | ||
@@ -67,10 +70,9 @@ }) | ||
xit('should poll to event', () => { | ||
const eventId = 'NewBlock' | ||
it('should poll to event', () => { | ||
expect(subscriptionId).to.be.ok | ||
return global.erisdb | ||
.events | ||
.eventPoll(eventId) | ||
.then((info) => { | ||
expect(info).to.be.an('object') | ||
.and.to.have.property('events') | ||
.eventPoll(subscriptionId) | ||
.then((events) => { | ||
expect(events).to.be.an('array') | ||
}) | ||
@@ -89,3 +91,3 @@ }) | ||
expect(err).to.be.an('error') | ||
.and.to.have.property('message', 'EventId is required parameter') | ||
.and.to.have.property('message', 'SubscriptionId is required parameter') | ||
}) | ||
@@ -95,9 +97,7 @@ }) | ||
it('should unsubscribe to event', () => { | ||
const eventId = 'NewBlock' | ||
return global.erisdb | ||
.events | ||
.eventUnsubscribe(eventId) | ||
.then((info) => { | ||
expect(info).to.be.an('object') | ||
.and.to.have.property('result') | ||
.eventUnsubscribe(subscriptionId) | ||
.then((result) => { | ||
expect(result).to.be.true | ||
}) | ||
@@ -104,0 +104,0 @@ }) |
@@ -7,3 +7,3 @@ 'use strict' | ||
xdescribe('Transaction.sendTransaction :: ', () => { | ||
describe('Transaction.sendTransaction :: ', () => { | ||
@@ -77,16 +77,21 @@ const SampleContract = ` | ||
it('should sign transaction', () => { | ||
it('should send transaction', () => { | ||
return global.erisdb | ||
.transactions | ||
.sendTransaction(tx, config.account.privKey) | ||
.then((data) => { | ||
console.log('==========================') | ||
console.log(data) | ||
console.log('==========================') | ||
expect(data).to.be.an('object') | ||
.then((info) => { | ||
expect(info).to.be.an('object') | ||
.and.to.contain.all.keys([ | ||
'tx_hash', 'creates_contract', 'contract_addr' | ||
'call_data', 'tx_id', 'return', 'origin' | ||
]) | ||
expect(info.call_data).to.be.an('object') | ||
.and.to.contain.all.keys([ | ||
'caller', 'callee', 'data', 'value', 'gas' | ||
]) | ||
expect(info.call_data.data).to.be | ||
.eq(compiled.bytecode.toUpperCase()) | ||
}) | ||
}) | ||
}) |
3707701
107454