From 1a7fc663b7465a210b2d7f4b86d82911ba821f02 Mon Sep 17 00:00:00 2001 From: Pascal Precht Date: Thu, 12 Dec 2019 11:34:50 +0100 Subject: [PATCH] fix(@embark/transaction-logger): handle non-contract transactions The transaction logger takes care of intercepting any responses coming from the blockchain proxy, determines what response it's dealing with and then extracts information from the response to output useful log information to the user, such as when a contract methods has been called etc. It turned out that it didn't properly handle cases where value transactions from account to account (such as done here in [Status Teller](https://github.com/status-im/status-teller-network/blob/a5ab4d4b26afccf15ac7472a18728385dd8b2461/embarkConfig/data.js#L100-L110)) have been made. Since Embark couldn't map any of those accounts to actual contracts, while still having stored corresponding transactions, it logged them as transactions from "Unknown Smart Contracts". This commit ensures via heuristics that, if the address a transactions is sent to is included in any of the nodes accounts, the transaction logger logs a useful message that a certain value is sent from one to anover account. --- .../plugins/transaction-logger/src/index.js | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/plugins/transaction-logger/src/index.js b/packages/plugins/transaction-logger/src/index.js index 6b00aa04d..23c6d6669 100644 --- a/packages/plugins/transaction-logger/src/index.js +++ b/packages/plugins/transaction-logger/src/index.js @@ -36,11 +36,12 @@ class TransactionLogger { this.events.on('outputDone', () => { this.outputDone = true; }); - this.events.on("contractsDeployed", () => { - this.contractsDeployed = true; + this.embark.registerActionForEvent("deployment:deployContracts:afterAll", { priority: 38 }, (params, cb) => { + this.contractsDeployed = true; this._getContractsList((contractsList) => { this.addressToContract = getAddressToContract(contractsList, this.addressToContract); + cb(null, params); }); }); @@ -70,6 +71,16 @@ class TransactionLogger { })(); } + get web3Accounts() { + return (async () => { + if (!this._web3Accounts) { + const web3 = await this.web3; + this._web3Accounts = await web3.eth.getAccounts(); + } + return this._web3Accounts; + })(); + } + _getContractsList(callback) { this.events.request("contracts:list", (err, contractsList) => { if (err) { @@ -98,6 +109,7 @@ class TransactionLogger { } async _onLogRequest(args) { + /*eslint complexity: ["error", 22]*/ const method = args.request.method; if (!this.contractsDeployed || !LISTENED_METHODS.includes(method)) { return; @@ -108,6 +120,7 @@ class TransactionLogger { this.transactions[args.response.result] = { address: args.request.params[0].to, data: args.request.params[0].data, + value: args.request.params[0].value, txHash: args.response.result }; return; @@ -143,8 +156,16 @@ class TransactionLogger { // It's a deployment return; } - const contract = this.addressToContract[address]; + const accounts = await this.web3Accounts; + + if (accounts.map(account => account.toLowerCase()).includes(address.toLowerCase())) { + const web3 = await this.web3; + const value = web3.utils.fromWei(web3.utils.hexToNumberString(dataObject.value)); + return this.logger.info(`Blockchain>`.underline + ` transferring ${value} ETH from ${dataObject.from} to ${address}`.bold); + } + + const contract = this.addressToContract[address]; if (!contract) { this.logger.info(`Contract log for unknown contract: ${JSON.stringify(args)}`); return this._getContractsList((contractsList) => {