From 8e66acc8ddf0a47fc17a64e11eb2fee4dcf8960e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Thu, 26 Oct 2023 22:48:34 +0300 Subject: [PATCH] Prettier result for parseEvent. Add unit test. --- src/smartcontracts/resultsParser.spec.ts | 33 ++++++++++++++++++++++++ src/smartcontracts/resultsParser.ts | 20 ++++++++------ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/smartcontracts/resultsParser.spec.ts b/src/smartcontracts/resultsParser.spec.ts index 25c0576b..4c42c778 100644 --- a/src/smartcontracts/resultsParser.spec.ts +++ b/src/smartcontracts/resultsParser.spec.ts @@ -1,10 +1,14 @@ import { ContractQueryResponse, ContractResultItem, ContractResults, TransactionEvent, TransactionEventTopic, TransactionLogs, TransactionOnNetwork } from "@multiversx/sdk-network-providers"; +import { TransactionEventData } from "@multiversx/sdk-network-providers/out/transactionEvents"; +import BigNumber from "bignumber.js"; import { assert } from "chai"; import * as fs from "fs"; import path from "path"; import { Address } from "../address"; +import { IAddress } from "../interface"; import { ITransactionOnNetwork } from "../interfaceOfNetwork"; import { LogLevel, Logger } from "../logger"; +import { loadAbiRegistry } from "../testutils"; import { ArgSerializer } from "./argSerializer"; import { ResultsParser } from "./resultsParser"; import { ReturnCode } from "./returnCode"; @@ -41,6 +45,9 @@ describe("test smart contract results parser", () => { }, stringToBuffers(_joinedString) { return [] + }, + stringToValues(_joinedString, _parameters) { + return [new U64Value(42)]; } } }); @@ -235,6 +242,32 @@ describe("test smart contract results parser", () => { assert.deepEqual(bundle.values, []); }); + it.only("should parse contract event", async () => { + const registry = await loadAbiRegistry("src/testdata/esdt-safe.abi.json"); + const depositEvent = registry.getEvent("deposit"); + + const event = new TransactionEvent({ + identifier: "deposit", + topics: [ + new TransactionEventTopic("ZGVwb3NpdA=="), + new TransactionEventTopic("cmzC1LRt1r10pMhNAnFb+FyudjGMq4G8CefCYdQUmmc="), + new TransactionEventTopic("AAAADFdFR0xELTAxZTQ5ZAAAAAAAAAAAAAAAAWQ="), + ], + dataPayload: new TransactionEventData(Buffer.from("AAAAAAAAA9sAAAA=", "base64")) + }); + + const bundle = parser.parseEvent(event, depositEvent); + + assert.equal((bundle.dest_address).bech32(), "erd1wfkv9495dhtt6a9yepxsyu2mlpw2ua333j4cr0qfulpxr4q5nfnshgyqun"); + assert.equal(bundle.tokens[0].token_identifier, "WEGLD-01e49d"); + assert.deepEqual(bundle.tokens[0].token_nonce, new BigNumber(0)); + assert.deepEqual(bundle.tokens[0].amount, new BigNumber(100)); + assert.deepEqual(bundle.event_data.tx_nonce, new BigNumber(987)); + assert.isNull(bundle.event_data.opt_function); + assert.isNull(bundle.event_data.opt_arguments); + assert.isNull(bundle.event_data.opt_gas_limit); + }); + // This test should be enabled manually and run against a set of sample transactions. // 2022-04-03: test ran against ~1800 transactions sampled from devnet. it.skip("should parse real-world contract outcomes", async () => { diff --git a/src/smartcontracts/resultsParser.ts b/src/smartcontracts/resultsParser.ts index e8e07b7d..e92ff244 100644 --- a/src/smartcontracts/resultsParser.ts +++ b/src/smartcontracts/resultsParser.ts @@ -28,6 +28,7 @@ interface IParameterDefinition { } interface IEventInputDefinition { + name: string; type: Type; indexed: boolean; } @@ -326,10 +327,8 @@ export class ResultsParser { return { returnCode, returnDataParts }; } - parseEvent(transactionEvent: ITransactionEvent, eventDefinition: { inputs: IEventInputDefinition[] }): { - topics: TypedValue[], - dataParts: TypedValue[] - } { + parseEvent(transactionEvent: ITransactionEvent, eventDefinition: { inputs: IEventInputDefinition[] }): any { + const result: any = {}; const topics = transactionEvent.topics.map(topic => Buffer.from(topic.hex(), "hex")); const data = transactionEvent.dataPayload?.valueOf() || Buffer.from([]); const dataHex = data.toString("hex"); @@ -341,9 +340,14 @@ export class ResultsParser { const decodedTopics = this.argsSerializer.buffersToValues(topics.slice(1), indexedInputs); const decodedDataParts = this.argsSerializer.stringToValues(dataHex, nonIndexedInputs); - return { - topics: decodedTopics, - dataParts: decodedDataParts - }; + for (let i = 0; i < indexedInputs.length; i++) { + result[indexedInputs[i].name] = decodedTopics[i].valueOf(); + } + + for (let i = 0; i < nonIndexedInputs.length; i++) { + result[nonIndexedInputs[i].name] = decodedDataParts[i].valueOf(); + } + + return result; } }