turn API into promise

pull/1/head
yann300 6 years ago
parent f09f7616f9
commit d766581644
  1. 45
      src/app.js
  2. 26
      src/app/editor/SourceHighlighters.js
  3. 1
      src/app/files/browser-files-tree.js
  4. 6
      src/app/tabs/compile-tab.js
  5. 60
      src/universal-dapp.js

@ -253,32 +253,43 @@ class App {
}) })
} }
getExecutionContextProvider (cb) { getExecutionContextProvider () {
cb(null, executionContext.getProvider()) return new Promise((resolve, reject) => {
resolve(executionContext.getProvider())
})
} }
getProviderEndpoint (cb) { getProviderEndpoint () {
if (executionContext.getProvider() === 'web3') { return new Promise((resolve, reject) => {
cb(null, executionContext.web3().currentProvider.host) if (executionContext.getProvider() === 'web3') {
} else { resolve(executionContext.web3().currentProvider.host)
cb('no endpoint: current provider is either injected or vm') } else {
} reject('no endpoint: current provider is either injected or vm')
}
})
} }
detectNetWork (cb) { detectNetWork () {
executionContext.detectNetwork((error, network) => { return new Promise((resolve, reject) => {
cb(error, network) executionContext.detectNetwork((error, network) => {
if (error) return reject(error)
resolve(network)
})
}) })
} }
addProvider (name, url, cb) { addProvider (name, url) {
executionContext.addProvider({ name, url }) return new Promise((resolve, reject) => {
cb() executionContext.addProvider({ name, url })
resolve()
})
} }
removeProvider (name, cb) { removeProvider (name) {
executionContext.removeProvider(name) return new Promise((resolve, reject) => {
cb() executionContext.removeProvider(name)
resolve()
})
} }
} }

@ -18,19 +18,25 @@ class SourceHighlighters {
// TODO what to do with mod? // TODO what to do with mod?
async highlight (mod, lineColumnPos, filePath, hexColor) { async highlight (mod, lineColumnPos, filePath, hexColor) {
let position return new Promise((resolve, reject) => {
try { let position
position = JSON.parse(lineColumnPos) try {
} catch (e) { position = JSON.parse(lineColumnPos)
throw e } catch (e) {
} throw e
if (!this.highlighters[mod]) this.highlighters[mod] = new SourceHighlighter() }
this.highlighters[mod].currentSourceLocation(null) if (!this.highlighters[mod]) this.highlighters[mod] = new SourceHighlighter()
this.highlighters[mod].currentSourceLocationFromfileName(position, filePath, hexColor) this.highlighters[mod].currentSourceLocation(null)
this.highlighters[mod].currentSourceLocationFromfileName(position, filePath, hexColor)
resolve()
})
} }
async discardHighlight (mod) { async discardHighlight (mod) {
if (this.highlighters[mod]) this.highlighters[mod].currentSourceLocation(null) return new Promise((resolve, reject) => {
if (this.highlighters[mod]) this.highlighters[mod].currentSourceLocation(null)
resolve()
})
} }
} }

@ -132,6 +132,7 @@ function FilesTree (name, storage) {
} }
this.profile = function () { this.profile = function () {
// TODO should make them promisable
return { return {
name: this.type, name: this.type,
methods: ['get', 'set', 'remove'], methods: ['get', 'set', 'remove'],

@ -143,6 +143,12 @@ class CompileTab {
} }
} }
getCompilationResult () {
return new Promise((resolve, reject) => {
resolve(this.compileTabLogic.compiler.lastCompilationResult)
})
}
/********* /*********
* SUB-COMPONENTS * SUB-COMPONENTS
*/ */

@ -33,7 +33,7 @@ UniversalDApp.prototype.profile = function () {
return { return {
name: 'udapp', name: 'udapp',
displayName: 'universal dapp', displayName: 'universal dapp',
methods: ['runTx', 'getAccounts', 'createVMAccount'], methods: ['runTestTx', 'getAccounts', 'createVMAccount'],
description: 'service - run transaction and access account' description: 'service - run transaction and access account'
} }
} }
@ -73,11 +73,13 @@ UniversalDApp.prototype.resetAPI = function (transactionContextAPI) {
} }
UniversalDApp.prototype.createVMAccount = function (privateKey, balance, cb) { UniversalDApp.prototype.createVMAccount = function (privateKey, balance, cb) {
if (executionContext.getProvider() !== 'vm') return cb('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed') return new Promise((resolve, reject) => {
this._addAccount(privateKey, balance) if (executionContext.getProvider() !== 'vm') return reject('plugin API does not allow creating a new account through web3 connection. Only vm mode is allowed')
executionContext.vm().stateManager.cache.flush(function () {}) this._addAccount(privateKey, balance)
privateKey = Buffer.from(privateKey, 'hex') executionContext.vm().stateManager.cache.flush(function () {})
cb(null, '0x' + ethJSUtil.privateToAddress(privateKey).toString('hex')) privateKey = Buffer.from(privateKey, 'hex')
resolve('0x' + ethJSUtil.privateToAddress(privateKey).toString('hex'))
})
} }
UniversalDApp.prototype.newAccount = function (password, passwordPromptCb, cb) { UniversalDApp.prototype.newAccount = function (password, passwordPromptCb, cb) {
@ -116,24 +118,36 @@ UniversalDApp.prototype._addAccount = function (privateKey, balance) {
} }
} }
// TODO should remove this cb
UniversalDApp.prototype.getAccounts = function (cb) { UniversalDApp.prototype.getAccounts = function (cb) {
var self = this var self = this
return new Promise((resolve, reject) => {
if (!executionContext.isVM()) { if (!executionContext.isVM()) {
// Weirdness of web3: listAccounts() is sync, `getListAccounts()` is async // Weirdness of web3: listAccounts() is sync, `getListAccounts()` is async
// See: https://github.com/ethereum/web3.js/issues/442 // See: https://github.com/ethereum/web3.js/issues/442
if (this._deps.config.get('settings/personal-mode')) { if (this._deps.config.get('settings/personal-mode')) {
return executionContext.web3().personal.getListAccounts(cb) return executionContext.web3().personal.getListAccounts((error, accounts) => {
if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
})
} else {
executionContext.web3().eth.getAccounts((error, accounts) => {
if (cb) cb(error, accounts)
if (error) return reject(error)
resolve(accounts)
})
}
} else { } else {
executionContext.web3().eth.getAccounts(cb) if (!self.accounts) {
} if (cb) cb('No accounts?')
} else { reject('No accounts?')
if (!self.accounts) { return
return cb('No accounts?') }
if (cb) cb(null, Object.keys(self.accounts))
resolve(Object.keys(self.accounts))
} }
})
cb(null, Object.keys(self.accounts))
}
} }
UniversalDApp.prototype.getBalance = function (address, cb) { UniversalDApp.prototype.getBalance = function (address, cb) {
@ -235,12 +249,12 @@ UniversalDApp.prototype.getInputs = function (funABI) {
UniversalDApp.prototype.runTestTx = function (tx) { UniversalDApp.prototype.runTestTx = function (tx) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
executionContext.detectNetwork((error, network) => { executionContext.detectNetwork((error, network) => {
if (error) reject(error) if (error) return reject(error)
if (network.name === 'Main' && network.id === '1') { if (network.name === 'Main' && network.id === '1') {
reject(new Error('It is not allowed to make this action against mainnet')) return reject(new Error('It is not allowed to make this action against mainnet'))
} }
this.silentRunTx(tx, (error, result) => { this.silentRunTx(tx, (error, result) => {
if (error) reject(error) if (error) return reject(error)
resolve({ resolve({
transactionHash: result.transactionHash, transactionHash: result.transactionHash,
status: result.result.status, status: result.result.status,

Loading…
Cancel
Save