diff --git a/packages/web3/src/contract/events.ts b/packages/web3/src/contract/events.ts index 472a0c600..f054e66bf 100644 --- a/packages/web3/src/contract/events.ts +++ b/packages/web3/src/contract/events.ts @@ -19,6 +19,7 @@ along with the library. If not, see . import * as web3 from '../global' import { node } from '../api' import { Subscription, SubscribeOptions } from '../utils' +import { ContractEvents } from '../api/api-alephium' export interface EventSubscribeOptions extends SubscribeOptions { onEventCountChanged?: (eventCount: number) => Promise | void @@ -40,11 +41,22 @@ export class EventSubscription extends Subscription { return this.fromCount } - override async polling(): Promise { + private async getEvents(start: number): Promise { try { - const events = await web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, { + return await web3.getCurrentNodeProvider().events.getEventsContractContractaddress(this.contractAddress, { start: this.fromCount }) + } catch (error) { + if (error instanceof Error && error.message.includes(`Contract events of ${this.contractAddress} not found`)) { + return { events: [], nextStart: start } + } + throw error + } + } + + override async polling(): Promise { + try { + const events = await this.getEvents(this.fromCount) if (this.fromCount === events.nextStart) { return }