Skip to content

Commit

Permalink
Omit 'contractId' field from events when empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Nov 27, 2023
1 parent 62eab36 commit 8939d17
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ A breaking change will get clearly marked in this log.

## Unreleased

### Fixed
* The `SorobanRpc.Server.getEvents` method now correctly parses responses without a `contractId` field set. The `events[i].contractId` field on an event is now optional, omitted if there was no ID for the event (e.g. system events; ([#TODO](https://github.com/stellar/js-stellar-sdk/pull/TODO))).


## [v11.0.0-beta.6](https://github.com/stellar/js-stellar-sdk/compare/v11.0.0-beta.5...v11.0.0-beta.6)

Expand Down
4 changes: 2 additions & 2 deletions src/soroban/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export namespace Api {
}

interface EventResponse extends BaseEventResponse {
contractId: Contract;
contractId?: Contract;
topic: xdr.ScVal[];
value: xdr.ScVal;
}
Expand All @@ -171,7 +171,7 @@ export namespace Api {
inSuccessfulContractCall: boolean;
}

interface RawEventResponse extends BaseEventResponse {
export interface RawEventResponse extends BaseEventResponse {
contractId: string;
topic: string[];
value: {
Expand Down
11 changes: 8 additions & 3 deletions src/soroban/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ export function parseRawEvents(
return {
latestLedger: r.latestLedger,
events: (r.events ?? []).map((evt) => {
return {
...evt,
contractId: new Contract(evt.contractId),
// the contractId may be empty so we omit the field in that case
const cid = evt.contractId;
delete (evt as any).contractId;
const rv = {
...evt as Omit<Api.RawEventResponse, 'contractId'>,
...(cid !== '' && { contractId: new Contract(cid) }),
topic: evt.topic.map((topic) => xdr.ScVal.fromXDR(topic, 'base64')),
value: xdr.ScVal.fromXDR(evt.value.xdr, 'base64')
};
evt.contractId = cid; // put value back after parsing
return rv;
})
};
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/server/soroban/get_events_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ let getEventsResponseFixture = [
type: "system",
ledger: "1",
ledgerClosedAt: "2022-11-16T16:10:41Z",
contractId,
contractId: '',
id: "0164090849041387521-0000000003",
pagingToken: "164090849041387521-3",
inSuccessfulContractCall: true,
Expand Down

0 comments on commit 8939d17

Please sign in to comment.