From 1027291167981910ba8fc0dd78837d11b73227f3 Mon Sep 17 00:00:00 2001 From: Pelle Braendgaard Date: Mon, 6 Feb 2017 20:12:35 -0600 Subject: [PATCH] fix(subprovider): handle batch requests correctly --- package.json | 1 + src/uportSubprovider.js | 11 ++++++++++- test/testData.json | 2 +- test/uportWeb3.js | 25 ++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 2dbd0e51..ecfbfcee 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ ] }, "dependencies": { + "async": "^2.1.4", "ethjs-abi": "^0.1.8", "ethjs-util": "^0.1.3", "ipfs-mini": "^1.0.1", diff --git a/src/uportSubprovider.js b/src/uportSubprovider.js index e13a6eb2..72606938 100644 --- a/src/uportSubprovider.js +++ b/src/uportSubprovider.js @@ -4,6 +4,7 @@ // eth_sendTransaction // TODO support contract.new +import async from 'async' export default class UportSubprovider { constructor ({requestAddress, sendTransaction, provider}) { @@ -27,6 +28,10 @@ export default class UportSubprovider { } } + send (payload) { + throw new Error('Uport Web3 SubProvider does not support synchronous requests.') + } + sendAsync (payload, callback) { const self = this const respond = (error, result) => { @@ -44,6 +49,10 @@ export default class UportSubprovider { }) } } + if (Array.isArray(payload)) { + async.map(payload, self.sendAsync.bind(self), callback) + return + } switch (payload.method) { // TODO consider removing, not necessary for interaction with uport case 'eth_coinbase': @@ -58,7 +67,7 @@ export default class UportSubprovider { respond(err, tx) }) default: - return self.provider.sendAsync(payload, callback) + self.provider.sendAsync(payload, callback) } } } diff --git a/test/testData.json b/test/testData.json index 1291d994..9d44881b 100644 --- a/test/testData.json +++ b/test/testData.json @@ -13,7 +13,7 @@ "type": "function" }, { - "constant": false, + "constant": true, "inputs": [ { "name": "addr", diff --git a/test/uportWeb3.js b/test/uportWeb3.js index 976e012b..7a0031c4 100644 --- a/test/uportWeb3.js +++ b/test/uportWeb3.js @@ -17,6 +17,7 @@ describe('uportWeb3 integration tests', function () { this.timeout(30000) let autosigner, status, vanillaWeb3, web3 + const coolStatus = 'Writing some tests!' before(done => { global.navigator = {} @@ -104,7 +105,6 @@ describe('uportWeb3 integration tests', function () { }) it('use contract', (done) => { - const coolStatus = 'Writing some tests!' status.updateStatus(coolStatus, (err, res) => { expect(err).to.be.null if (err) { @@ -122,4 +122,27 @@ describe('uportWeb3 integration tests', function () { }) }) }) + + it('handles batches', (done) => { + var batch = web3.createBatch() + batch.add(web3.eth.getBalance.request(autosigner.address, 'latest', (error, balance) => { + expect(error).to.be.null + expect(balance).to.be.greaterThan(0) + })) + batch.add(web3.eth.getBalance.request(autosigner.address, 'latest', (error, balance) => { + expect(error).to.be.null + expect(balance).to.be.greaterThan(0) + })) + batch.add(status.getStatus.request(autosigner.address, (error, myStatus) => { + expect(error).to.be.null + expect(myStatus).to.be.equal(coolStatus) + })) + batch.execute() + setTimeout(done, 1000) + }) + + it('does not handle sync calls', (done) => { + expect(() => web3.eth.getBalance(autosigner.address)).to.throw('Uport Web3 SubProvider does not support synchronous requests.'); + done() + }) })