diff --git a/libs/remix-lib/src/execution/eventsDecoder.ts b/libs/remix-lib/src/execution/eventsDecoder.ts index a6c0454736..debbd37440 100644 --- a/libs/remix-lib/src/execution/eventsDecoder.ts +++ b/libs/remix-lib/src/execution/eventsDecoder.ts @@ -1,6 +1,6 @@ 'use strict' import { ethers } from 'ethers' -const txHelper = require('./txHelper') +import { visitContracts } from './txHelper' /** * Register to txListener and extract events @@ -50,7 +50,7 @@ export class EventsDecoder { _eventsABI (compiledContracts) { const eventsABI = {} - txHelper.visitContracts(compiledContracts, (contract) => { + visitContracts(compiledContracts, (contract) => { eventsABI[contract.name] = this._eventABI(contract.object) }) return eventsABI diff --git a/libs/remix-lib/src/execution/execution-context.ts b/libs/remix-lib/src/execution/execution-context.ts index 611693c3d0..a40b550b60 100644 --- a/libs/remix-lib/src/execution/execution-context.ts +++ b/libs/remix-lib/src/execution/execution-context.ts @@ -3,14 +3,12 @@ import Web3 from 'web3' const EventManager = require('../eventManager') const EthJSVM = require('ethereumjs-vm').default -const ethUtil = require('ethereumjs-util') +import { rlp, keccak, bufferToHex } from 'ethereumjs-util' const StateManager = require('ethereumjs-vm/dist/state/stateManager').default const Web3VMProvider = require('../web3Provider/web3VmProvider') const LogsManager = require('./logsManager.js') -const rlp = ethUtil.rlp - let web3 if (typeof window !== 'undefined' && typeof window['ethereum'] !== 'undefined') { var injectedProvider = window['ethereum'] @@ -32,7 +30,7 @@ class StateManagerCommonStorageDump extends StateManager { } putContractStorage (address, key, value, cb) { - this.keyHashes[ethUtil.keccak(key).toString('hex')] = ethUtil.bufferToHex(key) + this.keyHashes[keccak(key).toString('hex')] = bufferToHex(key) super.putContractStorage(address, key, value, cb) } diff --git a/libs/remix-lib/src/execution/txListener.ts b/libs/remix-lib/src/execution/txListener.ts index 590fbfe4f8..e8bd2953d4 100644 --- a/libs/remix-lib/src/execution/txListener.ts +++ b/libs/remix-lib/src/execution/txListener.ts @@ -1,13 +1,13 @@ 'use strict' import async from 'async' import { ethers } from 'ethers' -const ethJSUtil = require('ethereumjs-util') +import { toBuffer } from 'ethereumjs-util' const EventManager = require('../eventManager') const codeUtil = require('../util') -const defaultExecutionContext = require('./execution-context') -const txFormat = require('./txFormat') -const txHelper = require('./txHelper') +import { ExecutionContext } from './execution-context' +import { decodeResponse } from './txFormat' +import { getFunction, getReceiveInterface, getConstructorInterface, visitContracts, makeFullTypeDefinition } from './txHelper' function addExecutionCosts(txResult, tx) { if (txResult && txResult.result) { @@ -26,7 +26,7 @@ function addExecutionCosts(txResult, tx) { * trigger 'newBlock' * */ -class TxListener { +export class TxListener { event executionContext @@ -42,7 +42,7 @@ class TxListener { constructor (opt, executionContext) { this.event = new EventManager() // has a default for now for backwards compatability - this.executionContext = executionContext || defaultExecutionContext + this.executionContext = executionContext || new ExecutionContext() this._api = opt.api this._resolvedTransactions = {} this._resolvedContracts = {} @@ -71,7 +71,7 @@ class TxListener { input: data, hash: txResult.transactionHash ? txResult.transactionHash : 'call' + (from || '') + to + data, isCall: true, - returnValue: this.executionContext.isVM() ? txResult.result.execResult.returnValue : ethJSUtil.toBuffer(txResult.result), + returnValue: this.executionContext.isVM() ? txResult.result.execResult.returnValue : toBuffer(txResult.result), envMode: this.executionContext.getProvider() } @@ -289,7 +289,7 @@ class TxListener { const methodIdentifiers = contract.object.evm.methodIdentifiers for (let fn in methodIdentifiers) { if (methodIdentifiers[fn] === inputData.substring(0, 8)) { - const fnabi = txHelper.getFunction(abi, fn) + const fnabi = getFunction(abi, fn) this._resolvedTransactions[tx.hash] = { contractName: contract.name, to: tx.to, @@ -297,13 +297,13 @@ class TxListener { params: this._decodeInputParams(inputData.substring(8), fnabi) } if (tx.returnValue) { - this._resolvedTransactions[tx.hash].decodedReturnValue = txFormat.decodeResponse(tx.returnValue, fnabi) + this._resolvedTransactions[tx.hash].decodedReturnValue = decodeResponse(tx.returnValue, fnabi) } return this._resolvedTransactions[tx.hash] } } // receive function - if (!inputData && txHelper.getReceiveInterface(abi)) { + if (!inputData && getReceiveInterface(abi)) { this._resolvedTransactions[tx.hash] = { contractName: contract.name, to: tx.to, @@ -323,7 +323,7 @@ class TxListener { const bytecode = contract.object.evm.bytecode.object let params = null if (bytecode && bytecode.length) { - params = this._decodeInputParams(inputData.substring(bytecode.length), txHelper.getConstructorInterface(abi)) + params = this._decodeInputParams(inputData.substring(bytecode.length), getConstructorInterface(abi)) } this._resolvedTransactions[tx.hash] = { contractName: contract.name, @@ -337,7 +337,7 @@ class TxListener { _tryResolveContract (codeToResolve, compiledContracts, isCreation) { let found = null - txHelper.visitContracts(compiledContracts, (contract) => { + visitContracts(compiledContracts, (contract) => { const bytes = isCreation ? contract.object.evm.bytecode.object : contract.object.evm.deployedBytecode.object if (codeUtil.compareByteCode(codeToResolve, '0x' + bytes)) { found = contract @@ -348,13 +348,13 @@ class TxListener { } _decodeInputParams (data, abi) { - data = ethJSUtil.toBuffer('0x' + data) + data = toBuffer('0x' + data) if (!data.length) data = new Uint8Array(32 * abi.inputs.length) // ensuring the data is at least filled by 0 cause `AbiCoder` throws if there's not engouh data const inputTypes = [] for (let i = 0; i < abi.inputs.length; i++) { const type = abi.inputs[i].type - inputTypes.push(type.indexOf('tuple') === 0 ? txHelper.makeFullTypeDefinition(abi.inputs[i]) : type) + inputTypes.push(type.indexOf('tuple') === 0 ? makeFullTypeDefinition(abi.inputs[i]) : type) } const abiCoder = new ethers.utils.AbiCoder() const decoded = abiCoder.decode(inputTypes, data) @@ -365,5 +365,3 @@ class TxListener { return ret } } - -module.exports = TxListener diff --git a/libs/remix-lib/src/execution/txRunner.ts b/libs/remix-lib/src/execution/txRunner.ts index a4e3e3f588..0b325add04 100644 --- a/libs/remix-lib/src/execution/txRunner.ts +++ b/libs/remix-lib/src/execution/txRunner.ts @@ -1,8 +1,8 @@ 'use strict' -const EthJSTX = require('ethereumjs-tx').Transaction -const EthJSBlock = require('ethereumjs-block') +import { Transaction } from 'ethereumjs-tx' +import { Block } from 'ethereumjs-block' import { BN } from 'ethereumjs-util' -const defaultExecutionContext = require('./execution-context') +import { ExecutionContext } from './execution-context' const EventManager = require('../eventManager') class TxRunner { @@ -20,7 +20,7 @@ class TxRunner { constructor (vmaccounts, api, executionContext) { this.event = new EventManager() // has a default for now for backwards compatability - this.executionContext = executionContext || defaultExecutionContext + this.executionContext = executionContext || new ExecutionContext() this._api = api this.blockNumber = 0 this.runAsync = true @@ -117,7 +117,7 @@ class TxRunner { // See https://github.com/ethereumjs/ethereumjs-tx/blob/master/docs/classes/transaction.md#constructor // for initialization fields and their types value = value ? parseInt(value) : 0 - const tx = new EthJSTX({ + const tx = new Transaction({ nonce: new BN(res.nonce), gasPrice: '0x1', gasLimit: gasLimit, @@ -128,7 +128,7 @@ class TxRunner { tx.sign(account.privateKey) const coinbases = ['0x0e9281e9c6a0808672eaba6bd1220e144c9bb07a', '0x8945a1288dc78a6d8952a92c77aee6730b414778', '0x94d76e24f818426ae84aa404140e8d5f60e10e7e'] const difficulties = [new BN('69762765929000', 10), new BN('70762765929000', 10), new BN('71762765929000', 10)] - const block = new EthJSBlock({ + const block = new Block({ header: { timestamp: timestamp || (new Date().getTime() / 1000 | 0), number: self.blockNumber, @@ -166,7 +166,7 @@ class TxRunner { this.executionContext.trackTx('0x' + tx.hash().toString('hex'), block) callback(null, { result: result, - transactionHash: ethJSUtil.bufferToHex(Buffer.from(tx.hash())) + transactionHash: bufferToHex(Buffer.from(tx.hash())) }) }).catch(function (err) { callback(err) diff --git a/libs/remix-lib/src/execution/typeConversion.ts b/libs/remix-lib/src/execution/typeConversion.ts index 04559eb8f6..207057e7f9 100644 --- a/libs/remix-lib/src/execution/typeConversion.ts +++ b/libs/remix-lib/src/execution/typeConversion.ts @@ -1,35 +1,33 @@ 'use strict' -const ethJSUtil = require('ethereumjs-util') -const BN = ethJSUtil.BN +import { BN, bufferToHex } from 'ethereumjs-util' -module.exports = { - toInt: (h) => { - if (h.indexOf && h.indexOf('0x') === 0) { - return (new BN(h.replace('0x', ''), 16)).toString(10) - } else if (h.constructor && h.constructor.name === 'BigNumber' || BN.isBN(h)) { - return h.toString(10) - } - return h - }, - stringify: stringify +export function toInt (h) { + if (h.indexOf && h.indexOf('0x') === 0) { + return (new BN(h.replace('0x', ''), 16)).toString(10) + } else if (h.constructor && h.constructor.name === 'BigNumber' || BN.isBN(h)) { + return h.toString(10) + } + return h } + +export var stringify = convertToString -function stringify (v) { +function convertToString (v) { try { if (v instanceof Array) { const ret = [] for (var k in v) { - ret.push(stringify(v[k])) + ret.push(convertToString(v[k])) } return ret } else if (BN.isBN(v) || (v.constructor && v.constructor.name === 'BigNumber')) { return v.toString(10) } else if (v._isBuffer) { - return ethJSUtil.bufferToHex(v) + return bufferToHex(v) } else if (typeof v === 'object') { const retObject = {} for (let i in v) { - retObject[i] = stringify(v[i]) + retObject[i] = convertToString(v[i]) } return retObject } else {