Skip to content

Commit

Permalink
Prettier result for parseEvent. Add unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Oct 26, 2023
1 parent 2c1d335 commit 8e66acc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
33 changes: 33 additions & 0 deletions src/smartcontracts/resultsParser.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -41,6 +45,9 @@ describe("test smart contract results parser", () => {
},
stringToBuffers(_joinedString) {
return []
},
stringToValues(_joinedString, _parameters) {
return [new U64Value(42)];
}
}
});
Expand Down Expand Up @@ -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((<IAddress>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 () => {
Expand Down
20 changes: 12 additions & 8 deletions src/smartcontracts/resultsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface IParameterDefinition {
}

interface IEventInputDefinition {
name: string;
type: Type;
indexed: boolean;
}
Expand Down Expand Up @@ -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");
Expand All @@ -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;
}
}

0 comments on commit 8e66acc

Please sign in to comment.