Skip to content

Commit

Permalink
Handle events without contract IDs correctly (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Nov 29, 2023
1 parent f2366e3 commit f3384ce
Show file tree
Hide file tree
Showing 5 changed files with 233 additions and 213 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; ([#883](https://github.com/stellar/js-stellar-sdk/pull/883))).


## [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
8 changes: 6 additions & 2 deletions src/soroban/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ export function parseRawEvents(
return {
latestLedger: r.latestLedger,
events: (r.events ?? []).map((evt) => {
const clone: Omit<Api.RawEventResponse, 'contractId'> = { ...evt };
delete (clone as any).contractId; // `as any` hack because contractId field isn't optional

// the contractId may be empty so we omit the field in that case
return {
...evt,
contractId: new Contract(evt.contractId),
...clone,
...(evt.contractId !== '' && { contractId: new Contract(evt.contractId) }),
topic: evt.topic.map((topic) => xdr.ScVal.fromXDR(topic, 'base64')),
value: xdr.ScVal.fromXDR(evt.value.xdr, 'base64')
};
Expand Down
Loading

0 comments on commit f3384ce

Please sign in to comment.