aboutsummaryrefslogtreecommitdiffstats
path: root/test/stub/provider.js
blob: 85e1da707ceb3e352517c6ef7148d150a6eb0ad4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const JsonRpcEngine = require('json-rpc-engine')
const scaffoldMiddleware = require('eth-json-rpc-middleware/scaffold')

module.exports = {
  createEngineForTestData,
  providerFromEngine,
  scaffoldMiddleware,
  createEthJsQueryStub,
  createStubedProvider,
}


function createEngineForTestData () {
  return new JsonRpcEngine()
}

function providerFromEngine (engine) {
  const provider = { sendAsync: engine.handle.bind(engine) }
  return provider
}

function createEthJsQueryStub (stubProvider) {
  return new Proxy({}, {
    get: (obj, method) => {
      return (...params) => {
        return new Promise((resolve, reject) => {
          stubProvider.sendAsync({ method: `eth_${method}`, params }, (err, ress) => resolve(ress.result))
        })
      }
    },
  })
}

function createStubedProvider (resultStub) {
  const engine = createEngineForTestData()
  engine.push(scaffoldMiddleware(resultStub))
  return providerFromEngine(engine)
}