refactor get provider

pull/1/head
Iuri Matias 5 years ago
parent d563b17c44
commit 3d8dceb62e
  1. 37
      src/blockchain/blockchain.js
  2. 4
      src/blockchain/providers/injected.js
  3. 4
      src/blockchain/providers/node.js
  4. 4
      src/blockchain/providers/vm.js

@ -34,7 +34,7 @@ class Blockchain {
this.executionContext.detectNetwork(cb)
},
personalMode: () => {
return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
return this.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
}
}, this.executionContext)
this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this))
@ -66,7 +66,7 @@ class Blockchain {
}
getCurrentProvider () {
const provider = this.executionContext.getProvider()
const provider = this.getProvider()
return this.providers[provider]
}
@ -199,10 +199,6 @@ class Blockchain {
return this.executionContext.setProviderFromEndpoint(target, context, cb)
}
getProvider () {
return this.executionContext.getProvider()
}
updateNetwork (cb) {
this.networkcallid++
((callid) => {
@ -221,14 +217,18 @@ class Blockchain {
return this.executionContext.detectNetwork(cb)
}
getProvider () {
return this.executionContext.getProvider()
}
isWeb3Provider () {
const isVM = this.executionContext.isVM()
const isInjected = this.executionContext.getProvider() === 'injected'
const isVM = this.getProvider === 'vm'
const isInjected = this.getProvider() === 'injected'
return (!isVM && !isInjected)
}
isInjectedWeb3 () {
return this.executionContext.getProvider() === 'injected'
return this.getProvider() === 'injected'
}
signMessage (message, account, passphrase, cb) {
@ -263,13 +263,6 @@ class Blockchain {
if (error) {
return logCallback(`${logMsg} errored: ${error} `)
}
const isVM = this.executionContext.isVM()
if (isVM) {
const vmError = txExecution.checkVMError(txResult)
if (vmError.error) {
return logCallback(`${logMsg} errored: ${vmError.message} `)
}
}
if (lookupOnly) {
const returnValue = (this.executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result))
outputCb(returnValue)
@ -322,7 +315,7 @@ class Blockchain {
this.executionContext.detectNetwork(cb)
},
personalMode: () => {
return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
return this.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false
}
}, this.executionContext)
this.txRunner.event.register('transactionBroadcasted', (txhash) => {
@ -338,7 +331,7 @@ class Blockchain {
* @param {{privateKey: string, balance: string}} newAccount The new account to create
*/
createVMAccount (newAccount) {
if (this.executionContext.getProvider() !== 'vm') {
if (this.getProvider() !== 'vm') {
throw new Error('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed')
}
return this.providers.vm.createVMAccount(newAccount)
@ -368,6 +361,14 @@ class Blockchain {
callFunction (to, data, funAbi, confirmationCb, continueCb, promptCb, callback) {
const useCall = funAbi.stateMutability === 'view' || funAbi.stateMutability === 'pure'
this.runTx({to, data, useCall}, confirmationCb, continueCb, promptCb, (error, txResult) => {
const isVM = this.executionContext.isVM()
if (isVM) {
const vmError = txExecution.checkVMError(txResult)
if (vmError.error) {
return callback(vmError.message)
}
}
// see universaldapp.js line 660 => 700 to check possible values of txResult (error case)
callback(error, txResult)
})

@ -40,6 +40,10 @@ class InjectedProvider {
cb(e.message)
}
}
getProvider () {
return 'injected'
}
}
module.exports = InjectedProvider

@ -49,6 +49,10 @@ class NodeProvider {
cb(e.message)
}
}
getProvider () {
return this.executionContext.getProvider()
}
}
module.exports = NodeProvider

@ -83,6 +83,10 @@ class VMProvider {
cb(e.message)
}
}
getProvider () {
return 'vm'
}
}
module.exports = VMProvider

Loading…
Cancel
Save