Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
baas-ide
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
guxukai
baas-ide
Commits
9f48efd8
Commit
9f48efd8
authored
Aug 08, 2017
by
yann300
Committed by
GitHub
Aug 08, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #645 from ethereum/eventListener
Event Decoder
parents
f2940c0e
2115061c
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
161 additions
and
39 deletions
+161
-39
app.js
src/app.js
+69
-14
eventsDecoder.js
src/app/eventsDecoder.js
+77
-0
txListener.js
src/app/txListener.js
+15
-25
No files found.
src/app.js
View file @
9f48efd8
...
@@ -7,7 +7,8 @@ var base64 = require('js-base64').Base64
...
@@ -7,7 +7,8 @@ var base64 = require('js-base64').Base64
var
swarmgw
=
require
(
'swarmgw'
)
var
swarmgw
=
require
(
'swarmgw'
)
var
csjs
=
require
(
'csjs-inject'
)
var
csjs
=
require
(
'csjs-inject'
)
var
yo
=
require
(
'yo-yo'
)
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
UniversalDApp
=
require
(
'./universal-dapp.js'
)
var
Remixd
=
require
(
'./lib/remixd'
)
var
Remixd
=
require
(
'./lib/remixd'
)
...
@@ -30,7 +31,9 @@ var EditorPanel = require('./app/editor-panel')
...
@@ -30,7 +31,9 @@ var EditorPanel = require('./app/editor-panel')
var
RighthandPanel
=
require
(
'./app/righthand-panel'
)
var
RighthandPanel
=
require
(
'./app/righthand-panel'
)
var
examples
=
require
(
'./app/example-contracts'
)
var
examples
=
require
(
'./app/example-contracts'
)
var
modalDialogCustom
=
require
(
'./app/modal-dialog-custom'
)
var
modalDialogCustom
=
require
(
'./app/modal-dialog-custom'
)
// var Txlistener = require('./app/txListener')
var
Txlistener
=
require
(
'./app/txListener'
)
var
EventsDecoder
=
require
(
'./app/eventsDecoder'
)
var
Web3VMProvider
=
remix
.
web3
.
web3VMProvider
var
css
=
csjs
`
var
css
=
csjs
`
html { box-sizing: border-box; }
html { box-sizing: border-box; }
...
@@ -748,26 +751,62 @@ function run () {
...
@@ -748,26 +751,62 @@ function run () {
node
.
insertBefore
(
staticanalysis
.
render
(),
node
.
childNodes
[
0
])
node
.
insertBefore
(
staticanalysis
.
render
(),
node
.
childNodes
[
0
])
// ----------------- Tx listener -----------------
// ----------------- Tx listener -----------------
// Commented for now. will be used later.
// not used right now
/*
var txlistener = new Txlistener({
// TODO the following should be put in execution context
api: {
var
web3VM
=
new
Web3VMProvider
()
web3: function () { return executionContext.web3() },
web3VM
.
setVM
(
executionContext
.
vm
())
isVM: function () { return executionContext.isVM() },
vm: function () { return executionContext.vm() },
var
currentWeb3
=
function
()
{
contracts: 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
)
{
if
(
compiler
.
lastCompilationResult
&&
compiler
.
lastCompilationResult
.
data
)
{
return
compiler
.
lastCompilationResult
.
data
.
contracts
return
compiler
.
lastCompilationResult
.
data
.
contracts
}
}
return
null
return
null
},
}
var
txlistener
=
new
Txlistener
({
api
:
{
web3
:
function
()
{
return
currentWeb3
()
},
isVM
:
function
()
{
return
executionContext
.
isVM
()
},
contracts
:
compiledContracts
,
context
:
function
()
{
context
:
function
()
{
return
executionContext
.
getProvider
()
return
executionContext
.
getProvider
()
},
resolveReceipt
:
function
(
tx
,
cb
)
{
transactionReceiptResolver
.
resolve
(
tx
,
cb
)
}
}
},
},
event
:
{
event
:
{
executionContext
:
executionContext
.
event
,
executionContext
:
executionContext
.
event
,
udapp
:
udapp
.
event
udapp
:
udapp
.
event
}})
var
eventsDecoder
=
new
EventsDecoder
({
api
:
{
resolveReceipt
:
function
(
tx
,
cb
)
{
transactionReceiptResolver
.
resolve
(
tx
,
cb
)
}
}
}
})
})
...
@@ -777,15 +816,31 @@ function run () {
...
@@ -777,15 +816,31 @@ function run () {
var
resolvedTransaction
=
txlistener
.
resolvedTransaction
(
tx
.
hash
)
var
resolvedTransaction
=
txlistener
.
resolvedTransaction
(
tx
.
hash
)
var
address
=
null
var
address
=
null
if
(
resolvedTransaction
)
{
if
(
resolvedTransaction
)
{
var
resolvedContract
address
=
resolvedTransaction
.
contractAddress
?
resolvedTransaction
.
contractAddress
:
tx
.
to
address
=
resolvedTransaction
.
contractAddress
?
resolvedTransaction
.
contractAddress
:
tx
.
to
}
resolvedContract
=
txlistener
.
resolvedContract
(
address
)
if
(
resolvedContract
)
{
eventsDecoder
.
parseLogs
(
tx
,
resolvedContract
,
compiledContracts
(),
(
error
,
log
)
=>
{
console
.
log
({
tx
:
tx
,
resolvedTransaction
:
resolvedTransaction
,
resolvedContract
:
resolvedContract
,
resolvedEvents
:
(
!
error
?
log
:
error
)
})
})
}
else
{
console
.
log
({
console
.
log
({
tx
:
tx
,
tx
:
tx
,
resolvedContract: txlistener.resolvedContract(address),
resolvedTransaction
:
resolvedTransaction
resolvedTransaction
:
resolvedTransaction
})
})
}
}
else
{
// contract unknown - just displaying raw tx.
console
.
log
({
tx
:
tx
})
}
})
})
*/
// ----------------- autoCompile -----------------
// ----------------- autoCompile -----------------
var
autoCompile
=
document
.
querySelector
(
'#autoCompile'
).
checked
var
autoCompile
=
document
.
querySelector
(
'#autoCompile'
).
checked
...
...
src/app/eventsDecoder.js
0 → 100644
View file @
9f48efd8
'use strict'
var
ethJSABI
=
require
(
'ethereumjs-abi'
)
/**
* Register to txListener and extract events
*
*/
class
EventsDecoder
{
constructor
(
opt
=
{})
{
this
.
_api
=
opt
.
api
}
/**
* use Transaction Receipt to decode logs. assume that the transaction as already been resolved by txListener.
* logs are decoded only if the contract if known by remix.
*
* @param {Object} tx - transaction object
* @param {Function} cb - callback
*/
parseLogs
(
tx
,
contractName
,
compiledContracts
,
cb
)
{
this
.
_api
.
resolveReceipt
(
tx
,
(
error
,
receipt
)
=>
{
if
(
error
)
cb
(
error
)
this
.
_decodeLogs
(
tx
,
receipt
,
contractName
,
compiledContracts
,
cb
)
})
}
_decodeLogs
(
tx
,
receipt
,
contract
,
contracts
,
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
)
}
_eventABI
(
contractName
,
compiledContracts
)
{
var
contractabi
=
JSON
.
parse
(
compiledContracts
[
contractName
].
interface
)
var
eventABI
=
{}
contractabi
.
forEach
(
function
(
funABI
,
i
)
{
if
(
funABI
.
type
!==
'event'
)
{
return
}
var
hash
=
ethJSABI
.
eventID
(
funABI
.
name
,
funABI
.
inputs
.
map
(
function
(
item
)
{
return
item
.
type
}))
eventABI
[
hash
.
toString
(
'hex'
)]
=
{
event
:
funABI
.
name
,
inputs
:
funABI
.
inputs
}
})
return
eventABI
}
_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
]
var
event
var
decoded
try
{
var
abi
=
eventABI
[
log
.
topics
[
0
].
replace
(
'0x'
,
''
)]
event
=
abi
.
event
var
types
=
abi
.
inputs
.
map
(
function
(
item
)
{
return
item
.
type
})
decoded
=
ethJSABI
.
rawDecode
(
types
,
new
Buffer
(
log
.
data
.
replace
(
'0x'
,
''
),
'hex'
))
decoded
=
ethJSABI
.
stringify
(
types
,
decoded
)
}
catch
(
e
)
{
decoded
=
log
.
data
}
events
.
push
({
event
:
event
,
args
:
decoded
})
}
cb
(
null
,
events
)
}
}
module
.
exports
=
EventsDecoder
src/app/txListener.js
View file @
9f48efd8
...
@@ -5,7 +5,6 @@ var ethJSUtil = require('ethereumjs-util')
...
@@ -5,7 +5,6 @@ var ethJSUtil = require('ethereumjs-util')
var
EventManager
=
require
(
'ethereum-remix'
).
lib
.
EventManager
var
EventManager
=
require
(
'ethereum-remix'
).
lib
.
EventManager
var
remix
=
require
(
'ethereum-remix'
)
var
remix
=
require
(
'ethereum-remix'
)
var
codeUtil
=
remix
.
util
.
code
var
codeUtil
=
remix
.
util
.
code
var
Web3VMProvider
=
remix
.
web3
.
web3VMProvider
/**
/**
* poll web3 each 2s if web3
* poll web3 each 2s if web3
...
@@ -18,8 +17,6 @@ class TxListener {
...
@@ -18,8 +17,6 @@ class TxListener {
constructor
(
opt
)
{
constructor
(
opt
)
{
this
.
event
=
new
EventManager
()
this
.
event
=
new
EventManager
()
this
.
_api
=
opt
.
api
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
.
_resolvedTransactions
=
{}
this
.
_resolvedContracts
=
{}
this
.
_resolvedContracts
=
{}
this
.
init
()
this
.
init
()
...
@@ -30,7 +27,7 @@ class TxListener {
...
@@ -30,7 +27,7 @@ class TxListener {
})
})
opt
.
event
.
udapp
.
register
(
'transactionExecuted'
,
(
to
,
data
,
lookupOnly
,
txResult
)
=>
{
opt
.
event
.
udapp
.
register
(
'transactionExecuted'
,
(
to
,
data
,
lookupOnly
,
txResult
)
=>
{
if
(
this
.
loopId
&&
this
.
_api
.
isVM
())
{
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
)
if
(
error
)
return
console
.
log
(
error
)
this
.
_newBlock
({
this
.
_newBlock
({
type
:
'VM'
,
type
:
'VM'
,
...
@@ -64,6 +61,7 @@ class TxListener {
...
@@ -64,6 +61,7 @@ class TxListener {
}
else
{
}
else
{
this
.
loopId
=
setInterval
(()
=>
{
this
.
loopId
=
setInterval
(()
=>
{
this
.
_api
.
web3
().
eth
.
getBlockNumber
((
error
,
blockNumber
)
=>
{
this
.
_api
.
web3
().
eth
.
getBlockNumber
((
error
,
blockNumber
)
=>
{
if
(
this
.
loopId
===
null
||
this
.
loopId
===
'vm-listener'
)
return
if
(
error
)
return
console
.
log
(
error
)
if
(
error
)
return
console
.
log
(
error
)
if
(
!
this
.
lastBlock
||
blockNumber
>
this
.
lastBlock
)
{
if
(
!
this
.
lastBlock
||
blockNumber
>
this
.
lastBlock
)
{
this
.
lastBlock
=
blockNumber
this
.
lastBlock
=
blockNumber
...
@@ -78,10 +76,6 @@ class TxListener {
...
@@ -78,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.
* stop listening for incoming transactions. do not reset the recorded pool.
*
*
...
@@ -124,7 +118,9 @@ class TxListener {
...
@@ -124,7 +118,9 @@ class TxListener {
_resolve
(
block
,
callback
)
{
_resolve
(
block
,
callback
)
{
async
.
each
(
block
.
transactions
,
(
tx
,
cb
)
=>
{
async
.
each
(
block
.
transactions
,
(
tx
,
cb
)
=>
{
this
.
_resolveTx
(
tx
,
()
=>
{
this
.
_resolveTx
(
tx
,
(
error
,
resolvedData
)
=>
{
if
(
error
)
cb
(
error
)
if
(
resolvedData
)
this
.
event
.
trigger
(
'txResolved'
,
[
tx
,
resolvedData
])
this
.
event
.
trigger
(
'newTransaction'
,
[
tx
])
this
.
event
.
trigger
(
'newTransaction'
,
[
tx
])
cb
()
cb
()
})
})
...
@@ -144,14 +140,15 @@ class TxListener {
...
@@ -144,14 +140,15 @@ class TxListener {
var
code
=
tx
.
input
var
code
=
tx
.
input
contractName
=
this
.
_tryResolveContract
(
code
,
contracts
,
'bytecode'
)
contractName
=
this
.
_tryResolveContract
(
code
,
contracts
,
'bytecode'
)
if
(
contractName
)
{
if
(
contractName
)
{
this
.
_
resolveCreationAddress
(
tx
,
(
error
,
address
)
=>
{
this
.
_
api
.
resolveReceipt
(
tx
,
(
error
,
receipt
)
=>
{
if
(
error
)
return
cb
(
error
)
if
(
error
)
return
cb
(
error
)
var
address
=
receipt
.
contractAddress
this
.
_resolvedContracts
[
address
]
=
contractName
this
.
_resolvedContracts
[
address
]
=
contractName
this
.
_resolveFunction
(
contractName
,
contracts
,
tx
,
true
)
var
fun
=
this
.
_resolveFunction
(
contractName
,
contracts
,
tx
,
true
)
if
(
this
.
_resolvedTransactions
[
tx
.
hash
])
{
if
(
this
.
_resolvedTransactions
[
tx
.
hash
])
{
this
.
_resolvedTransactions
[
tx
.
hash
].
contractAddress
=
address
this
.
_resolvedTransactions
[
tx
.
hash
].
contractAddress
=
address
}
}
return
cb
()
return
cb
(
null
,
{
to
:
null
,
contractName
:
contractName
,
function
:
fun
,
creationAddress
:
address
}
)
})
})
return
return
}
}
...
@@ -160,13 +157,14 @@ class TxListener {
...
@@ -160,13 +157,14 @@ class TxListener {
// first check known contract, resolve against the `runtimeBytecode` if not known
// first check known contract, resolve against the `runtimeBytecode` if not known
contractName
=
this
.
_resolvedContracts
[
tx
.
to
]
contractName
=
this
.
_resolvedContracts
[
tx
.
to
]
if
(
!
contractName
)
{
if
(
!
contractName
)
{
this
.
currentW
eb3
().
eth
.
getCode
(
tx
.
to
,
(
error
,
code
)
=>
{
this
.
_api
.
w
eb3
().
eth
.
getCode
(
tx
.
to
,
(
error
,
code
)
=>
{
if
(
error
)
return
cb
(
error
)
if
(
error
)
return
cb
(
error
)
if
(
code
)
{
if
(
code
)
{
var
contractName
=
this
.
_tryResolveContract
(
code
,
contracts
,
'runtimeBytecode'
)
var
contractName
=
this
.
_tryResolveContract
(
code
,
contracts
,
'runtimeBytecode'
)
if
(
contractName
)
{
if
(
contractName
)
{
this
.
_resolvedContracts
[
tx
.
to
]
=
contractName
this
.
_resolvedContracts
[
tx
.
to
]
=
contractName
this
.
_resolveFunction
(
contractName
,
contracts
,
tx
,
false
)
var
fun
=
this
.
_resolveFunction
(
contractName
,
contracts
,
tx
,
false
)
return
cb
(
null
,
{
to
:
tx
.
to
,
contractName
:
contractName
,
function
:
fun
})
}
}
}
}
return
cb
()
return
cb
()
...
@@ -174,22 +172,13 @@ class TxListener {
...
@@ -174,22 +172,13 @@ class TxListener {
return
return
}
}
if
(
contractName
)
{
if
(
contractName
)
{
this
.
_resolveFunction
(
contractName
,
contracts
,
tx
,
false
)
var
fun
=
this
.
_resolveFunction
(
contractName
,
contracts
,
tx
,
false
)
return
cb
(
null
,
{
to
:
tx
.
to
,
contractName
:
contractName
,
function
:
fun
})
}
}
return
cb
()
return
cb
()
}
}
}
}
_resolveCreationAddress
(
tx
,
cb
)
{
this
.
currentWeb3
().
eth
.
getTransactionReceipt
(
tx
.
hash
,
(
error
,
receipt
)
=>
{
if
(
!
error
)
{
cb
(
null
,
receipt
.
contractAddress
)
}
else
{
cb
(
error
)
}
})
}
_resolveFunction
(
contractName
,
compiledContracts
,
tx
,
isCtor
)
{
_resolveFunction
(
contractName
,
compiledContracts
,
tx
,
isCtor
)
{
var
abi
=
JSON
.
parse
(
compiledContracts
[
contractName
].
interface
)
var
abi
=
JSON
.
parse
(
compiledContracts
[
contractName
].
interface
)
var
inputData
=
tx
.
input
.
replace
(
'0x'
,
''
)
var
inputData
=
tx
.
input
.
replace
(
'0x'
,
''
)
...
@@ -222,6 +211,7 @@ class TxListener {
...
@@ -222,6 +211,7 @@ class TxListener {
params
:
params
params
:
params
}
}
}
}
return
this
.
_resolvedTransactions
[
tx
.
hash
]
}
}
_tryResolveContract
(
codeToResolve
,
compiledContracts
,
type
)
{
_tryResolveContract
(
codeToResolve
,
compiledContracts
,
type
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment