eventManager, storage and universalDapp

toaster-react
aniket-engg 4 years ago committed by Aniket
parent 34bc1cd6ab
commit 662faf8cdd
  1. 115
      libs/remix-lib/src/eventManager.ts
  2. 4
      libs/remix-lib/src/execution/txRunner.ts
  3. 68
      libs/remix-lib/src/storage.ts
  4. 47
      libs/remix-lib/src/universalDapp.ts

@ -1,67 +1,72 @@
'use strict' 'use strict'
function eventManager () { export class EventManager {
this.registered = {}
this.anonymous = {}
}
/* registered
* Unregister a listener. anonymous
* Note that if obj is a function. the unregistration will be applied to the dummy obj {}.
* constructor() {
* @param {String} eventName - the event name this.registered = {}
* @param {Object or Func} obj - object that will listen on this event this.anonymous = {}
* @param {Func} func - function of the listeners that will be executed
*/
eventManager.prototype.unregister = function (eventName, obj, func) {
if (!this.registered[eventName]) {
return
}
if (obj instanceof Function) {
func = obj
obj = this.anonymous
} }
for (let reg in this.registered[eventName]) {
if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) { /*
this.registered[eventName].splice(reg, 1) * Unregister a listener.
* Note that if obj is a function. the unregistration will be applied to the dummy obj {}.
*
* @param {String} eventName - the event name
* @param {Object or Func} obj - object that will listen on this event
* @param {Func} func - function of the listeners that will be executed
*/
unregister (eventName, obj, func) {
if (!this.registered[eventName]) {
return
}
if (obj instanceof Function) {
func = obj
obj = this.anonymous
}
for (let reg in this.registered[eventName]) {
if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) {
this.registered[eventName].splice(reg, 1)
}
} }
} }
}
/* /*
* Register a new listener. * Register a new listener.
* Note that if obj is a function, the function registration will be associated with the dummy object {} * Note that if obj is a function, the function registration will be associated with the dummy object {}
* *
* @param {String} eventName - the event name * @param {String} eventName - the event name
* @param {Object or Func} obj - object that will listen on this event * @param {Object or Func} obj - object that will listen on this event
* @param {Func} func - function of the listeners that will be executed * @param {Func} func - function of the listeners that will be executed
*/ */
eventManager.prototype.register = function (eventName, obj, func) { register (eventName, obj, func) {
if (!this.registered[eventName]) { if (!this.registered[eventName]) {
this.registered[eventName] = [] this.registered[eventName] = []
} }
if (obj instanceof Function) { if (obj instanceof Function) {
func = obj func = obj
obj = this.anonymous obj = this.anonymous
}
this.registered[eventName].push({obj, func})
} }
this.registered[eventName].push({obj, func})
}
/* /*
* trigger event. * trigger event.
* Every listener have their associated function executed * Every listener have their associated function executed
* *
* @param {String} eventName - the event name * @param {String} eventName - the event name
* @param {Array}j - argument that will be passed to the executed function. * @param {Array}j - argument that will be passed to the executed function.
*/ */
eventManager.prototype.trigger = function (eventName, args) { trigger (eventName, args) {
if (!this.registered[eventName]) { if (!this.registered[eventName]) {
return return
} }
for (let listener in this.registered[eventName]) { for (let listener in this.registered[eventName]) {
const l = this.registered[eventName][listener] const l = this.registered[eventName][listener]
l.func.apply(l.obj === this.anonymous ? {} : l.obj, args) l.func.apply(l.obj === this.anonymous ? {} : l.obj, args)
}
} }
} }
module.exports = eventManager

@ -5,7 +5,7 @@ import { BN, bufferToHex } from 'ethereumjs-util'
import { ExecutionContext } from './execution-context' import { ExecutionContext } from './execution-context'
const EventManager = require('../eventManager') const EventManager = require('../eventManager')
class TxRunner { export class TxRunner {
event event
executionContext executionContext
@ -272,5 +272,3 @@ function run(self, tx, stamp, confirmationCb, gasEstimationForceSend = null, pro
} }
}) })
} }
module.exports = TxRunner

@ -1,22 +1,44 @@
'use strict' 'use strict'
function Storage (prefix) { export class Storage {
this.exists = function (name) { prefix
constructor (prefix) {
this.prefix = prefix
// on startup, upgrade the old storage layout
if (typeof window !== 'undefined') {
this.safeKeys().forEach(function (name) {
if (name.indexOf('sol-cache-file-', 0) === 0) {
var content = window.localStorage.getItem(name)
window.localStorage.setItem(name.replace(/^sol-cache-file-/, 'sol:'), content)
window.localStorage.removeItem(name)
}
})
}
// remove obsolete key
if (typeof window !== 'undefined') {
window.localStorage.removeItem('editor-size-cache')
}
}
exists (name) {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
return this.get(name) !== null return this.get(name) !== null
} }
} }
this.get = function (name) { get (name) {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
return window.localStorage.getItem(prefix + name) return window.localStorage.getItem(this.prefix + name)
} }
} }
this.set = function (name, content) { set (name, content) {
try { try {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.localStorage.setItem(prefix + name, content) window.localStorage.setItem(this.prefix + name, content)
} }
} catch (exception) { } catch (exception) {
return false return false
@ -24,14 +46,14 @@ function Storage (prefix) {
return true return true
} }
this.remove = function (name) { remove (name) {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
window.localStorage.removeItem(prefix + name) window.localStorage.removeItem(this.prefix + name)
} }
return true return true
} }
this.rename = function (originalName, newName) { rename (originalName, newName) {
const content = this.get(originalName) const content = this.get(originalName)
if (!this.set(newName, content)) { if (!this.set(newName, content)) {
return false return false
@ -40,7 +62,7 @@ function Storage (prefix) {
return true return true
} }
function safeKeys () { safeKeys () {
// NOTE: this is a workaround for some browsers // NOTE: this is a workaround for some browsers
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined }) return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
@ -48,29 +70,11 @@ function Storage (prefix) {
return [] return []
} }
this.keys = function () { keys () {
return safeKeys() return this.safeKeys()
// filter any names not including the prefix // filter any names not including the prefix
.filter(function (item) { return item.indexOf(prefix, 0) === 0 }) .filter(function (item) { return item.indexOf(this.prefix, 0) === 0 })
// remove prefix from filename and add the 'browser' path // remove prefix from filename and add the 'browser' path
.map(function (item) { return item.substr(prefix.length) }) .map(function (item) { return item.substr(this.prefix.length) })
}
// on startup, upgrade the old storage layout
if (typeof window !== 'undefined') {
safeKeys().forEach(function (name) {
if (name.indexOf('sol-cache-file-', 0) === 0) {
var content = window.localStorage.getItem(name)
window.localStorage.setItem(name.replace(/^sol-cache-file-/, 'sol:'), content)
window.localStorage.removeItem(name)
}
})
}
// remove obsolete key
if (typeof window !== 'undefined') {
window.localStorage.removeItem('editor-size-cache')
} }
} }
module.exports = Storage

@ -1,21 +1,30 @@
const async = require('async') import { waterfall } from 'async'
const { BN, privateToAddress, isValidPrivate, stripHexPrefix, toChecksumAddress } = require('ethereumjs-util') import { BN, privateToAddress, isValidPrivate, toChecksumAddress } from 'ethereumjs-util'
const crypto = require('crypto') import { stripHexPrefix } from 'ethjs-util'
const { EventEmitter } = require('events') import { randomBytes } from 'crypto'
import { EventEmitter } from 'events'
const TxRunner = require('./execution/txRunner')
const txHelper = require('./execution/txHelper') import { TxRunner } from './execution/txRunner'
const EventManager = require('./eventManager') import { sortAbiFunction, getFallbackInterface, getReceiveInterface, inputParametersDeclarationToString } from './execution/txHelper'
const defaultExecutionContext = require('./execution/execution-context') import { EventManager } from './eventManager'
const { resultToRemixTx } = require('./helpers/txResultHelper') import { ExecutionContext } from './execution/execution-context'
import { resultToRemixTx } from './helpers/txResultHelper'
module.exports = class UniversalDApp {
export class UniversalDApp {
events
event
executionContext
config
txRunner
accounts
transactionContextAPI
constructor (config, executionContext) { constructor (config, executionContext) {
this.events = new EventEmitter() this.events = new EventEmitter()
this.event = new EventManager() this.event = new EventManager()
// has a default for now for backwards compatability // has a default for now for backwards compatability
this.executionContext = executionContext || defaultExecutionContext this.executionContext = executionContext || new ExecutionContext()
this.config = config this.config = config
this.txRunner = new TxRunner({}, { this.txRunner = new TxRunner({}, {
@ -97,7 +106,7 @@ module.exports = class UniversalDApp {
} }
let privateKey let privateKey
do { do {
privateKey = crypto.randomBytes(32) privateKey = randomBytes(32)
} while (!isValidPrivate(privateKey)) } while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000') this._addAccount(privateKey, '0x56BC75E2D63100000')
cb(null, '0x' + privateToAddress(privateKey).toString('hex')) cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
@ -247,22 +256,22 @@ module.exports = class UniversalDApp {
} }
getABI (contract) { getABI (contract) {
return txHelper.sortAbiFunction(contract.abi) return sortAbiFunction(contract.abi)
} }
getFallbackInterface (contractABI) { getFallbackInterface (contractABI) {
return txHelper.getFallbackInterface(contractABI) return getFallbackInterface(contractABI)
} }
getReceiveInterface (contractABI) { getReceiveInterface (contractABI) {
return txHelper.getReceiveInterface(contractABI) return getReceiveInterface(contractABI)
} }
getInputs (funABI) { getInputs (funABI) {
if (!funABI.inputs) { if (!funABI.inputs) {
return '' return ''
} }
return txHelper.inputParametersDeclarationToString(funABI.inputs) return inputParametersDeclarationToString(funABI.inputs)
} }
/** /**
@ -309,7 +318,7 @@ module.exports = class UniversalDApp {
runTx (args, confirmationCb, continueCb, promptCb, cb) { runTx (args, confirmationCb, continueCb, promptCb, cb) {
const self = this const self = this
async.waterfall([ waterfall([
function getGasLimit (next) { function getGasLimit (next) {
if (self.transactionContextAPI.getGasLimit) { if (self.transactionContextAPI.getGasLimit) {
return self.transactionContextAPI.getGasLimit(next) return self.transactionContextAPI.getGasLimit(next)

Loading…
Cancel
Save