Skip to content

Commit

Permalink
Actually omit the 'contractId' field from humanized events when it's …
Browse files Browse the repository at this point in the history
…empty (#721)
  • Loading branch information
Shaptic authored Jan 23, 2024
1 parent 615860b commit 31f40b6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
7 changes: 3 additions & 4 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
25 changes: 23 additions & 2 deletions test/unit/events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
})
})
})
];

Expand All @@ -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)
});
});
});

0 comments on commit 31f40b6

Please sign in to comment.