Skip to content

Commit

Permalink
Fix event log decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Le Cam committed Sep 12, 2019
1 parent 04a3585 commit 90e7cc1
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 209 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018-present Mainframe and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Mainframe Ethereum client

Used by [Mainframe OS](https://github.com/MainframeHQ/mainframe-os) and the [Mainframe SDK](https://github.com/MainframeHQ/mainframe-sdk).

## License

MIT.\
See [LICENSE](LICENSE) file.
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mainframe/eth",
"description": "Mainframe Ethereum API and utilities",
"version": "0.4.0",
"version": "0.4.1",
"author": "Mainframe",
"license": "MIT",
"main": "lib/index.js",
Expand All @@ -18,24 +18,24 @@
"test": "yarn lint && yarn test:types"
},
"dependencies": {
"@babel/runtime": "^7.5.5",
"ethers": "^4.0.33",
"@babel/runtime": "^7.6.0",
"ethers": "^4.0.37",
"web3-eth-abi": "^1.2.1",
"web3-providers": "^1.0.0-beta.55",
"web3-utils": "1.2.1"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/plugin-proposal-class-properties": "^7.5.5",
"@babel/plugin-transform-runtime": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/plugin-transform-runtime": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@babel/preset-flow": "^7.0.0",
"babel-eslint": "^10.0.2",
"del-cli": "^2.0.0",
"eslint": "^6.2.0",
"babel-eslint": "^10.0.3",
"del-cli": "^3.0.0",
"eslint": "^6.3.0",
"eslint-config-mainframe": "^4.0.1",
"flow-bin": "^0.105.2",
"flow-bin": "^0.107.0",
"flow-copy-source": "^2.0.7",
"prettier": "^1.18.2"
}
Expand Down
4 changes: 2 additions & 2 deletions src/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,15 @@ export default class EthClient extends EventEmitter {

// Sending transactions

sendETH(params: SendParams) {
async sendETH(params: SendParams): Promise<TXEventEmitter> {
const valueWei = toWei(String(params.value))
const valueHex = toHex(valueWei)
const txParams = {
to: params.to,
from: params.from,
value: valueHex,
}
return this.sendAndListen(txParams, params.confirmations)
return await this.sendAndListen(txParams, params.confirmations)
}

async getConfirmations(txHash: string): Promise<?number> {
Expand Down
57 changes: 23 additions & 34 deletions src/Contracts/BaseContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Web3EthAbi from 'web3-eth-abi'

import type EthClient from '../Client'
import type { TXEventEmitter } from '../types'

export default class Contract {
_ethClient: EthClient
Expand Down Expand Up @@ -35,9 +36,9 @@ export default class Contract {
return abi
}

encodeCall(name: string, params?: Array<any>) {
encodeCall(name: string, params?: Array<any> = []) {
const abi = this.getFunctionABI(name)
return Web3EthAbi.encodeFunctionCall(abi, params || [])
return Web3EthAbi.encodeFunctionCall(abi, params)
}

decodeCallResult(name: string, result: Object): any {
Expand All @@ -55,45 +56,38 @@ export default class Contract {
}

async call(method: string, params?: Array<any>) {
const data = this.encodeCall(method, params)
const res = await this.ethClient.send('eth_call', [
{ data, to: this.address },
{ data: this.encodeCall(method, params), to: this.address },
'latest',
])
return this.decodeCallResult(method, res)
}

async send(method: string, params: Array<any>, options: Object) {
const data = this.encodeCall(method, params)
const txParams = { ...options, to: this.address, data }
return this.ethClient.sendAndListen(txParams)
async send(
method: string,
params: Array<any>,
options: Object,
): Promise<TXEventEmitter> {
return await this.ethClient.sendAndListen({
...options,
to: this.address,
data: this.encodeCall(method, params),
})
}

// Topics should not include the event signature

decodeEventLog(eventName: string, log: Object) {
decodeEventLog(eventName: string, log: Object): Object {
const abi = this.abi.find(abi => {
return abi.type === 'event' && abi.name === eventName
})
if (!abi) {
throw new Error(`Event '${eventName}' not found in ABI`)
}
const inputs = [
{
indexed: true,
name: 'signature',
type: 'string',
},
...abi.inputs,
]
// Remove event sig topic as expected by decodeLog
const topics = log.topics.slice(1)
return Web3EthAbi.decodeLog(inputs, log.data, topics)
return Web3EthAbi.decodeLog(abi.inputs, log.data, log.topics.slice(1))
}

async subscribeToEvents(
name: string,
topics: Array<string>,
topics: Array<string> = [],
): Promise<string> {
if (!this.ethClient.web3Provider.subscribe) {
throw new Error('Subscriptions not supported')
Expand All @@ -105,15 +99,10 @@ export default class Contract {
throw new Error(`Event '${name}' not found in ABI`)
}
const encodedSig = Web3EthAbi.encodeEventSignature(abi)
if (topics) {
topics.unshift(encodedSig)
} else {
topics = [encodedSig]
}
const params = [
{
address: this.address,
topics: topics,
topics: [encodedSig].concat(topics),
},
]
return this.ethClient.subscribe('logs', params)
Expand All @@ -123,18 +112,18 @@ export default class Contract {
const abi = this.abi.find(abi => {
return abi.type === 'event' && abi.name === name
})

if (!abi) {
throw new Error(`Event '${name}' not found in ABI`)
}

const encodedSig = Web3EthAbi.encodeEventSignature(abi)
const topics = [encodedSig].concat(params.topics || [])
params.address = this.address
const res = await this.ethClient.getPastEvents({
...params,
address: this.address,
topics: [encodedSig].concat(params.topics || []),
})

const res = await this.ethClient.getPastEvents({ ...params, topics })
const events = []

res.forEach(log => {
try {
const event = this.decodeEventLog(name, log)
Expand Down
Loading

0 comments on commit 90e7cc1

Please sign in to comment.