Skip to content

Commit

Permalink
Ensure stream info is initialized before pool activation
Browse files Browse the repository at this point in the history
Fixes #60

Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
  • Loading branch information
awrichar committed Mar 15, 2022
1 parent 31668a0 commit 1667298
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class TokensService {
instanceUrl: string;
topic: string;
shortPrefix: string;
stream: EventStream;
stream: EventStream | undefined;
username: string;
password: string;

Expand Down Expand Up @@ -120,7 +120,7 @@ export class TokensService {
* One-time initialization of event stream and base subscription.
*/
async init() {
this.stream = await this.eventstream.createOrUpdateStream(this.topic);
this.stream = await this.getStream();
await this.eventstream.getOrCreateSubscription(
this.instancePath,
this.stream.id,
Expand All @@ -129,6 +129,13 @@ export class TokensService {
);
}

private async getStream() {
if (this.stream === undefined) {
this.stream = await this.eventstream.createOrUpdateStream(this.topic);
}
return this.stream;
}

/**
* If there is an existing event stream whose subscriptions don't match the current
* events and naming format, delete the stream so we'll start over.
Expand Down Expand Up @@ -218,31 +225,32 @@ export class TokensService {
}

async activatePool(dto: TokenPoolActivate) {
const stream = await this.getStream();
await Promise.all([
this.eventstream.getOrCreateSubscription(
this.instancePath,
this.stream.id,
stream.id,
tokenCreateEvent,
packSubscriptionName(this.topic, dto.poolId, tokenCreateEvent),
dto.transaction?.blockNumber ?? '0',
),
this.eventstream.getOrCreateSubscription(
this.instancePath,
this.stream.id,
stream.id,
transferSingleEvent,
packSubscriptionName(this.topic, dto.poolId, transferSingleEvent),
dto.transaction?.blockNumber ?? '0',
),
this.eventstream.getOrCreateSubscription(
this.instancePath,
this.stream.id,
stream.id,
transferBatchEvent,
packSubscriptionName(this.topic, dto.poolId, transferBatchEvent),
dto.transaction?.blockNumber ?? '0',
),
this.eventstream.getOrCreateSubscription(
this.instancePath,
this.stream.id,
stream.id,
approvalForAllEvent,
packSubscriptionName(this.topic, dto.poolId, approvalForAllEvent),
// Block number is 0 because it is important to receive all approval events,
Expand Down

0 comments on commit 1667298

Please sign in to comment.