From faa2f041090816b63dd87ba63246b3532c156597 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 19 Apr 2018 11:14:14 +0200 Subject: [PATCH] switch abi coder --- remix-lib/package.json | 16 +++--- remix-lib/src/execution/eventsDecoder.js | 63 +++++------------------- remix-lib/src/execution/txFormat.js | 9 ++-- remix-lib/src/execution/txHelper.js | 16 +++--- remix-lib/src/execution/txListener.js | 6 +-- 5 files changed, 33 insertions(+), 77 deletions(-) diff --git a/remix-lib/package.json b/remix-lib/package.json index 47e458feec..f64b87e2a2 100644 --- a/remix-lib/package.json +++ b/remix-lib/package.json @@ -15,22 +15,22 @@ "main": "./index.js", "dependencies": { "async": "^2.1.2", - "babel-preset-es2015": "^6.24.0", - "babel-plugin-transform-object-assign": "^6.22.0", "babel-eslint": "^7.1.1", + "babel-plugin-transform-object-assign": "^6.22.0", "babel-preset-env": "^1.6.1", + "babel-preset-es2015": "^6.24.0", "babel-preset-stage-0": "^6.24.1", "babelify": "^7.3.0", - "fast-async": "^6.1.2", - "ethereumjs-util": "^5.1.2", - "ethereumjs-abi": "https://github.com/ethereumjs/ethereumjs-abi", - "ethereumjs-vm": "^2.3.3", "ethereumjs-block": "^1.6.0", "ethereumjs-tx": "^1.3.3", - "web3": "^0.18.0", + "ethereumjs-util": "^5.1.2", + "ethereumjs-vm": "^2.3.3", + "ethers": "^3.0.15", + "fast-async": "^6.1.2", "solc": "^0.4.13", "standard": "^7.0.1", - "tape": "^4.6.0" + "tape": "^4.6.0", + "web3": "^0.18.0" }, "scripts": { "test": "standard && tape ./test/tests.js" diff --git a/remix-lib/src/execution/eventsDecoder.js b/remix-lib/src/execution/eventsDecoder.js index 4a095f0bb5..65131d615d 100644 --- a/remix-lib/src/execution/eventsDecoder.js +++ b/remix-lib/src/execution/eventsDecoder.js @@ -1,5 +1,6 @@ 'use strict' -var ethJSABI = require('ethereumjs-abi') +var ethJSUtil = require('ethereumjs-util') +var ethers = require('ethers') var txHelper = require('./txHelper') /** @@ -38,13 +39,11 @@ class EventsDecoder { _eventABI (contract) { var eventABI = {} - contract.abi.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 } - }) + var abi = new ethers.Interface(contract.abi) + for (var e in abi.events) { + var event = abi.events[e] + eventABI[ethJSUtil.sha3(new Buffer(event.signature)).toString('hex')] = { event: event.name, inputs: event.inputs, object: event } + } return eventABI } @@ -67,54 +66,16 @@ class EventsDecoder { _decodeEvents (tx, logs, contractName, compiledContracts, cb) { var eventsABI = this._eventsABI(compiledContracts) - var events = [] + var events = {} for (var i in logs) { // [address, topics, mem] var log = logs[i] var topicId = log.topics[0] var abi = this._event(topicId.replace('0x', ''), eventsABI) - if (abi) { - var event - try { - var decoded = new Array(abi.inputs.length) - event = abi.event - var indexed = 1 - var nonindexed = [] - // decode indexed param - abi.inputs.map(function (item, index) { - if (item.indexed) { - var encodedData = log.topics[indexed].replace('0x', '') - try { - decoded[index] = ethJSABI.rawDecode([item.type], new Buffer(encodedData, 'hex'))[0] - if (typeof decoded[index] !== 'string') { - decoded[index] = ethJSABI.stringify([item.type], decoded[index]) - } - } catch (e) { - decoded[index] = encodedData - } - indexed++ - } else { - nonindexed.push(item.type) - } - }) - // decode non indexed param - var nonindexededResult = ethJSABI.rawDecode(nonindexed, new Buffer(log.data.replace('0x', ''), 'hex')) - nonindexed = ethJSABI.stringify(nonindexed, nonindexededResult) - // ordering - var j = 0 - abi.inputs.map(function (item, index) { - if (!item.indexed) { - decoded[index] = nonindexed[j] - j++ - } - }) - } catch (e) { - decoded = log.data - } - events.push({ topic: topicId, event: event, args: decoded }) - } else { - events.push({ data: log.data, topics: log.topics }) - } + var topics = log.topics.map((value) => { + return value.indexOf('0x') === 0 ? value : '0x' + value + }) + events = abi.object.parse(topics, '0x' + log.data) } cb(null, { decoded: events, raw: logs }) } diff --git a/remix-lib/src/execution/txFormat.js b/remix-lib/src/execution/txFormat.js index 5928f6ff29..2654118c6e 100644 --- a/remix-lib/src/execution/txFormat.js +++ b/remix-lib/src/execution/txFormat.js @@ -1,5 +1,5 @@ 'use strict' -var ethJSABI = require('ethereumjs-abi') +var ethers = require('ethers') var helper = require('./txHelper') var executionContext = require('./execution-context') var asyncJS = require('async') @@ -96,7 +96,7 @@ module.exports = { dataHex = bytecodeToDeploy + dataHex } } else { - dataHex = Buffer.concat([helper.encodeFunctionId(funAbi), data]).toString('hex') + dataHex = helper.encodeFunctionId(funAbi) + dataHex } callback(null, { dataHex, funAbi, funArgs, contractBytecode, contractName: contractName }) }, @@ -244,10 +244,9 @@ module.exports = { } // decode data - var decodedObj = ethJSABI.rawDecode(outputTypes, response) + var abiCoder = new ethers.utils.AbiCoder() + var decodedObj = abiCoder.decode(outputTypes, response) - // format decoded data - decodedObj = ethJSABI.stringify(outputTypes, decodedObj) var json = {} for (i = 0; i < outputTypes.length; i++) { var name = fnabi.outputs[i].name diff --git a/remix-lib/src/execution/txHelper.js b/remix-lib/src/execution/txHelper.js index 6a79502dd1..d65c580819 100644 --- a/remix-lib/src/execution/txHelper.js +++ b/remix-lib/src/execution/txHelper.js @@ -1,5 +1,5 @@ 'use strict' -var ethJSABI = require('ethereumjs-abi') +var ethers = require('ethers') module.exports = { encodeParams: function (funABI, args) { @@ -16,18 +16,14 @@ module.exports = { // NOTE: the caller will concatenate the bytecode and this // it could be done here too for consistency - return ethJSABI.rawEncode(types, args) + var abiCoder = new ethers.utils.AbiCoder() + return abiCoder.encode(types, args) }, encodeFunctionId: function (funABI) { - var types = [] - if (funABI.inputs && funABI.inputs.length) { - for (var i = 0; i < funABI.inputs.length; i++) { - types.push(funABI.inputs[i].type) - } - } - - return ethJSABI.methodID(funABI.name, types) + var abi = new ethers.Interface([funABI]) + abi = abi.functions[funABI.name] + return abi.sighash }, sortAbiFunction: function (contractabi) { diff --git a/remix-lib/src/execution/txListener.js b/remix-lib/src/execution/txListener.js index d3179d4f2b..844ac2ce48 100644 --- a/remix-lib/src/execution/txListener.js +++ b/remix-lib/src/execution/txListener.js @@ -1,6 +1,6 @@ 'use strict' var async = require('async') -var ethJSABI = require('ethereumjs-abi') +var ethers = require('ethers') var ethJSUtil = require('ethereumjs-util') var EventManager = require('../eventManager') var codeUtil = require('../util') @@ -329,8 +329,8 @@ class TxListener { for (var i = 0; i < abi.inputs.length; i++) { inputTypes.push(abi.inputs[i].type) } - var decoded = ethJSABI.rawDecode(inputTypes, data) - decoded = ethJSABI.stringify(inputTypes, decoded) + var abiCoder = new ethers.utils.AbiCoder() + var decoded = abiCoder.decode(inputTypes, data) var ret = {} for (var k in abi.inputs) { ret[abi.inputs[k].type + ' ' + abi.inputs[k].name] = decoded[k]