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'
function eventManager () {
this.registered = {}
this.anonymous = {}
}
export class EventManager {
/*
* 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
*/
eventManager.prototype.unregister = function (eventName, obj, func) {
if (!this.registered[eventName]) {
return
}
if (obj instanceof Function) {
func = obj
obj = this.anonymous
registered
anonymous
constructor() {
this.registered = {}
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.
* Note that if obj is a function, the function registration will be associated with the dummy object {}
*
* @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
*/
eventManager.prototype.register = function (eventName, obj, func) {
if (!this.registered[eventName]) {
this.registered[eventName] = []
}
if (obj instanceof Function) {
func = obj
obj = this.anonymous
/*
* Register a new listener.
* Note that if obj is a function, the function registration will be associated with the dummy object {}
*
* @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
*/
register (eventName, obj, func) {
if (!this.registered[eventName]) {
this.registered[eventName] = []
}
if (obj instanceof Function) {
func = obj
obj = this.anonymous
}
this.registered[eventName].push({obj, func})
}
this.registered[eventName].push({obj, func})
}
/*
* trigger event.
* Every listener have their associated function executed
*
* @param {String} eventName - the event name
* @param {Array}j - argument that will be passed to the executed function.
*/
eventManager.prototype.trigger = function (eventName, args) {
if (!this.registered[eventName]) {
return
}
for (let listener in this.registered[eventName]) {
const l = this.registered[eventName][listener]
l.func.apply(l.obj === this.anonymous ? {} : l.obj, args)
/*
* trigger event.
* Every listener have their associated function executed
*
* @param {String} eventName - the event name
* @param {Array}j - argument that will be passed to the executed function.
*/
trigger (eventName, args) {
if (!this.registered[eventName]) {
return
}
for (let listener in this.registered[eventName]) {
const l = this.registered[eventName][listener]
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'
const EventManager = require('../eventManager')
class TxRunner {
export class TxRunner {
event
executionContext
@ -272,5 +272,3 @@ function run(self, tx, stamp, confirmationCb, gasEstimationForceSend = null, pro
}
})
}
module.exports = TxRunner

@ -1,22 +1,44 @@
'use strict'
function Storage (prefix) {
this.exists = function (name) {
export class Storage {
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') {
return this.get(name) !== null
}
}
this.get = function (name) {
get (name) {
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 {
if (typeof window !== 'undefined') {
window.localStorage.setItem(prefix + name, content)
window.localStorage.setItem(this.prefix + name, content)
}
} catch (exception) {
return false
@ -24,14 +46,14 @@ function Storage (prefix) {
return true
}
this.remove = function (name) {
remove (name) {
if (typeof window !== 'undefined') {
window.localStorage.removeItem(prefix + name)
window.localStorage.removeItem(this.prefix + name)
}
return true
}
this.rename = function (originalName, newName) {
rename (originalName, newName) {
const content = this.get(originalName)
if (!this.set(newName, content)) {
return false
@ -40,7 +62,7 @@ function Storage (prefix) {
return true
}
function safeKeys () {
safeKeys () {
// NOTE: this is a workaround for some browsers
if (typeof window !== 'undefined') {
return Object.keys(window.localStorage).filter(function (item) { return item !== null && item !== undefined })
@ -48,29 +70,11 @@ function Storage (prefix) {
return []
}
this.keys = function () {
return safeKeys()
keys () {
return this.safeKeys()
// 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
.map(function (item) { return item.substr(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')
.map(function (item) { return item.substr(this.prefix.length) })
}
}
module.exports = Storage

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

Loading…
Cancel
Save