From 31f40b6c8366364793329671796e95b134b113e5 Mon Sep 17 00:00:00 2001 From: George Date: Mon, 22 Jan 2024 16:43:36 -0800 Subject: [PATCH] Actually omit the 'contractId' field from humanized events when it's empty (#721) --- src/events.js | 7 +++---- test/unit/events_test.js | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/events.js b/src/events.js index 7af8db9a..7677a421 100644 --- a/src/events.js +++ b/src/events.js @@ -29,10 +29,9 @@ export function humanizeEvents(events) { function extractEvent(event) { return { - contractId: - event.contractId() === null - ? '' - : StrKey.encodeContract(event.contractId()), + ...(event.contractId() != null && { + contractId: StrKey.encodeContract(event.contractId()) + }), type: event.type().name, topics: event .body() diff --git a/test/unit/events_test.js b/test/unit/events_test.js index e5847046..cdfcd060 100644 --- a/test/unit/events_test.js +++ b/test/unit/events_test.js @@ -12,9 +12,14 @@ describe('humanizing raw events', function () { const data1 = nativeToScVal({ hello: 'world' }); // workaround for xdr.ContractEventBody.0(...) being invalid lol - const bodyModel = xdr.ContractEventBody.fromXDR('AAAAAAAAAAAAAAAB', 'base64'); const cloneAndSet = (newBody) => { - const clone = xdr.ContractEventBody.fromXDR(bodyModel.toXDR()); + const clone = new xdr.ContractEventBody( + 0, + new xdr.ContractEventV0({ + topics: [], + data: xdr.ScVal.scvVoid() + }) + ); clone.v0().topics(newBody.topics); clone.v0().data(newBody.data); return clone; @@ -32,6 +37,17 @@ describe('humanizing raw events', function () { data: data1 }) }) + }), + new xdr.DiagnosticEvent({ + inSuccessfulContractCall: true, + event: new xdr.ContractEvent({ + ext: new xdr.ExtensionPoint(0), + type: xdr.ContractEventType.contract(), + body: cloneAndSet({ + topics: topics1, + data: data1 + }) + }) }) ]; @@ -50,5 +66,10 @@ describe('humanizing raw events', function () { topics: topics1.map(scValToNative), data: scValToNative(data1) }); + expect(readable[1]).to.eql({ + type: 'contract', + topics: topics1.map(scValToNative), + data: scValToNative(data1) + }); }); });