Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12 from uport-project/feature/139248789-handle-ba…
Browse files Browse the repository at this point in the history
…tch-requests

Handle batch requests correctly
  • Loading branch information
pelle committed Feb 7, 2017
2 parents 132c71f + 1027291 commit e9c12a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -90,6 +90,7 @@
]
},
"dependencies": {
"async": "^2.1.4",
"ethjs-abi": "^0.1.8",
"ethjs-util": "^0.1.3",
"ipfs-mini": "^1.0.1",
Expand Down
11 changes: 10 additions & 1 deletion src/uportSubprovider.js
Expand Up @@ -4,6 +4,7 @@
// eth_sendTransaction

// TODO support contract.new
import async from 'async'

export default class UportSubprovider {
constructor ({requestAddress, sendTransaction, provider}) {
Expand All @@ -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) => {
Expand All @@ -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':
Expand All @@ -58,7 +67,7 @@ export default class UportSubprovider {
respond(err, tx)
})
default:
return self.provider.sendAsync(payload, callback)
self.provider.sendAsync(payload, callback)
}
}
}
2 changes: 1 addition & 1 deletion test/testData.json
Expand Up @@ -13,7 +13,7 @@
"type": "function"
},
{
"constant": false,
"constant": true,
"inputs": [
{
"name": "addr",
Expand Down
25 changes: 24 additions & 1 deletion test/uportWeb3.js
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
})
})

0 comments on commit e9c12a0

Please sign in to comment.