get web3VMProvider out of txlistener

pull/1/head
yann300 7 years ago
parent 7830b3420b
commit 72619b994d
  1. 61
      src/app.js
  2. 24
      src/app/eventsDecoder.js
  3. 29
      src/app/txListener.js

@ -7,7 +7,8 @@ var base64 = require('js-base64').Base64
var swarmgw = require('swarmgw')
var csjs = require('csjs-inject')
var yo = require('yo-yo')
var EventManager = require('ethereum-remix').lib.EventManager
var remix = require('ethereum-remix')
var EventManager = remix.lib.EventManager
var UniversalDApp = require('./universal-dapp.js')
var Remixd = require('./lib/remixd')
@ -30,10 +31,9 @@ var EditorPanel = require('./app/editor-panel')
var RighthandPanel = require('./app/righthand-panel')
var examples = require('./app/example-contracts')
var modalDialogCustom = require('./app/modal-dialog-custom')
/*
var Txlistener = require('./app/txListener')
var EventsDecoder = require('./app/eventsDecoder')
*/
var Web3VMProvider = remix.web3.web3VMProvider
var css = csjs`
html { box-sizing: border-box; }
@ -752,7 +752,32 @@ function run () {
// ----------------- Tx listener -----------------
// not used right now
/*
// TODO the following should be put in execution context
var web3VM = new Web3VMProvider()
web3VM.setVM(executionContext.vm())
var currentWeb3 = function () {
return executionContext.isVM() ? web3VM : executionContext.web3()
}
var transactionReceiptResolver = {
_transactionReceipts: {},
resolve: function (tx, cb) {
if (this._transactionReceipts[tx.hash]) {
return cb(null, this._transactionReceipts[tx.hash])
}
currentWeb3().eth.getTransactionReceipt(tx.hash, (error, receipt) => {
if (!error) {
this._transactionReceipts[tx.hash] = receipt
cb(null, receipt)
} else {
cb(error)
}
})
}
}
var compiledContracts = function () {
if (compiler.lastCompilationResult && compiler.lastCompilationResult.data) {
return compiler.lastCompilationResult.data.contracts
@ -762,22 +787,29 @@ function run () {
var txlistener = new Txlistener({
api: {
web3: function () { return executionContext.web3() },
web3: function () { return currentWeb3() },
isVM: function () { return executionContext.isVM() },
vm: function () { return executionContext.vm() },
contracts: compiledContracts,
context: function () {
return executionContext.getProvider()
},
resolveReceipt: function (tx, cb) {
transactionReceiptResolver.resolve(tx, cb)
}
},
event: {
executionContext: executionContext.event,
udapp: udapp.event
}})
var eventsDecoder = new EventsDecoder({
api: {
resolveReceipt: function (tx, cb) {
transactionReceiptResolver.resolve(tx, cb)
}
}
})
var eventsDecoder = new EventsDecoder({ txListener: txlistener })
txlistener.startListening()
txlistener.event.register('newTransaction', (tx) => {
@ -788,22 +820,27 @@ function run () {
address = resolvedTransaction.contractAddress ? resolvedTransaction.contractAddress : tx.to
resolvedContract = txlistener.resolvedContract(address)
if (resolvedContract) {
eventsDecoder.parseLogs(tx, resolvedContract, compiledContracts(), () => {
eventsDecoder.parseLogs(tx, resolvedContract, compiledContracts(), (error, log) => {
console.log({
tx: tx,
resolvedContract: resolvedContract,
resolvedTransaction: resolvedTransaction,
resolvedEvents: eventsDecoder.eventsOf(tx.hash)
resolvedContract: resolvedContract,
resolvedEvents: (!error ? log : error)
})
})
} else {
console.log({
tx: tx,
resolvedTransaction: resolvedTransaction
})
}
} else {
// contract unknown - just displaying raw tx.
console.log({
tx: tx
})
}
})
*/
// ----------------- autoCompile -----------------
var autoCompile = document.querySelector('#autoCompile').checked

@ -8,8 +8,7 @@ var ethJSABI = require('ethereumjs-abi')
*/
class EventsDecoder {
constructor (opt = {}) {
this.txListener = opt.txListener
this.resolvedEvents = {}
this._api = opt.api
}
/**
@ -20,19 +19,18 @@ class EventsDecoder {
* @param {Function} cb - callback
*/
parseLogs (tx, contractName, compiledContracts, cb) {
this.txListener.resolveTransactionReceipt(tx, (error, receipt) => {
this._api.resolveReceipt(tx, (error, receipt) => {
if (error) cb(error)
this._decodeLogs(tx, receipt, contractName, compiledContracts, cb)
})
}
eventsOf (hash) {
return this.resolvedEvents[hash]
}
_decodeLogs (tx, receipt, contract, contracts, cb) {
if (!contract || !receipt.logs) {
return cb()
if (!contract || !receipt) {
return cb('cannot decode logs - contract or receipt not resolved ')
}
if (!receipt.logs) {
return cb(null, [])
}
this._decodeEvents(tx, receipt.logs, contract, contracts, cb)
}
@ -53,6 +51,7 @@ class EventsDecoder {
_decodeEvents (tx, logs, contractName, compiledContracts, cb) {
var eventABI = this._eventABI(contractName, compiledContracts)
// FIXME: support indexed events
var events = []
for (var i in logs) {
// [address, topics, mem]
var log = logs[i]
@ -70,12 +69,9 @@ class EventsDecoder {
} catch (e) {
decoded = log.data
}
if (!this.resolvedEvents[tx.hash]) {
this.resolvedEvents[tx.hash] = []
}
this.resolvedEvents[tx.hash].push({ event: event, args: decoded })
events.push({ event: event, args: decoded })
}
cb()
cb(null, events)
}
}

@ -5,7 +5,6 @@ var ethJSUtil = require('ethereumjs-util')
var EventManager = require('ethereum-remix').lib.EventManager
var remix = require('ethereum-remix')
var codeUtil = remix.util.code
var Web3VMProvider = remix.web3.web3VMProvider
/**
* poll web3 each 2s if web3
@ -18,11 +17,8 @@ class TxListener {
constructor (opt) {
this.event = new EventManager()
this._api = opt.api
this._web3VMProvider = new Web3VMProvider() // TODO this should maybe be put in app.js
this._web3VMProvider.setVM(opt.api.vm())
this._resolvedTransactions = {}
this._resolvedContracts = {}
this._transactionReceipts = {}
this.init()
opt.event.executionContext.register('contextChanged', (context) => {
if (this.loopId) {
@ -31,7 +27,7 @@ class TxListener {
})
opt.event.udapp.register('transactionExecuted', (to, data, lookupOnly, txResult) => {
if (this.loopId && this._api.isVM()) {
this._web3VMProvider.getTransaction(txResult.transactionHash, (error, tx) => {
this._api.web3().eth.getTransaction(txResult.transactionHash, (error, tx) => {
if (error) return console.log(error)
this._newBlock({
type: 'VM',
@ -65,6 +61,7 @@ class TxListener {
} else {
this.loopId = setInterval(() => {
this._api.web3().eth.getBlockNumber((error, blockNumber) => {
if (this.loopId === null || this.loopId === 'vm-listener') return
if (error) return console.log(error)
if (!this.lastBlock || blockNumber > this.lastBlock) {
this.lastBlock = blockNumber
@ -79,10 +76,6 @@ class TxListener {
}
}
currentWeb3 () { // TODO this should maybe be put in app.js
return this._api.isVM() ? this._web3VMProvider : this._api.web3()
}
/**
* stop listening for incoming transactions. do not reset the recorded pool.
*
@ -147,7 +140,7 @@ class TxListener {
var code = tx.input
contractName = this._tryResolveContract(code, contracts, 'bytecode')
if (contractName) {
this.resolveTransactionReceipt(tx, (error, receipt) => {
this._api.resolveReceipt(tx, (error, receipt) => {
if (error) return cb(error)
var address = receipt.contractAddress
this._resolvedContracts[address] = contractName
@ -164,7 +157,7 @@ class TxListener {
// first check known contract, resolve against the `runtimeBytecode` if not known
contractName = this._resolvedContracts[tx.to]
if (!contractName) {
this.currentWeb3().eth.getCode(tx.to, (error, code) => {
this._api.web3().eth.getCode(tx.to, (error, code) => {
if (error) return cb(error)
if (code) {
var contractName = this._tryResolveContract(code, contracts, 'runtimeBytecode')
@ -186,20 +179,6 @@ class TxListener {
}
}
resolveTransactionReceipt (tx, cb) {
if (this._transactionReceipts[tx.hash]) {
return cb(null, this._transactionReceipts[tx.hash])
}
this.currentWeb3().eth.getTransactionReceipt(tx.hash, (error, receipt) => {
if (!error) {
this._transactionReceipts[tx.hash] = receipt
cb(null, receipt)
} else {
cb(error)
}
})
}
_resolveFunction (contractName, compiledContracts, tx, isCtor) {
var abi = JSON.parse(compiledContracts[contractName].interface)
var inputData = tx.input.replace('0x', '')

Loading…
Cancel
Save