Skip to content

Commit

Permalink
Fix event subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
Lbqds committed Dec 13, 2024
1 parent 617fdd1 commit 489a28d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/web3/src/contract/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
import * as web3 from '../global'
import { node } from '../api'
import { Subscription, SubscribeOptions } from '../utils'
import { ContractEvents } from '../api/api-alephium'

export interface EventSubscribeOptions<Message> extends SubscribeOptions<Message> {
onEventCountChanged?: (eventCount: number) => Promise<void> | void
Expand All @@ -40,11 +41,22 @@ export class EventSubscription extends Subscription<node.ContractEvent> {
return this.fromCount
}

override async polling(): Promise<void> {
private async getEvents(start: number): Promise<ContractEvents> {
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<void> {
try {
const events = await this.getEvents(this.fromCount)
if (this.fromCount === events.nextStart) {
return
}
Expand Down

0 comments on commit 489a28d

Please sign in to comment.