-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
220 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const NB_THREADS = 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './types' | ||
export * from './jsonRPCClient' | ||
export * from './publicAPI' | ||
export * from './constants' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { EventEmitter } from 'events' | ||
import { Provider } from '../provider' | ||
import { EventFilter, NB_THREADS, SCEvent, Slot } from '../client' | ||
|
||
/** Smart Contracts Event Poller */ | ||
export const ON_MASSA_EVENT_DATA = 'ON_MASSA_EVENT' | ||
export const ON_MASSA_EVENT_ERROR = 'ON_MASSA_ERROR' | ||
|
||
export const DEFAULT_POLL_INTERVAL_MS = 1000 | ||
|
||
// get the next slot | ||
function nextSlot(prevSlot: Slot): Slot { | ||
const slot = prevSlot | ||
if (slot.thread < NB_THREADS - 1) { | ||
slot.thread++ | ||
} else { | ||
slot.thread = 0 | ||
slot.period++ | ||
} | ||
return slot | ||
} | ||
|
||
/** | ||
* The EventPoller class provides a convenient way to poll events from the Massa network. | ||
*/ | ||
export class EventPoller extends EventEmitter { | ||
private intervalId: NodeJS.Timeout | ||
private lastSlot: Slot | ||
|
||
/** | ||
* Constructor of the EventPoller object. | ||
* | ||
* @param provider - The provider to use for polling. | ||
* @param eventsFilter - The filter to use for the events. | ||
* @param pollIntervalMs - The interval in milliseconds to poll for events. | ||
*/ | ||
public constructor( | ||
private readonly provider: Provider, | ||
private readonly eventsFilter: EventFilter, | ||
private readonly pollIntervalMs: number | ||
) { | ||
super() | ||
} | ||
|
||
private poll = async (): Promise<void> => { | ||
try { | ||
// get all events using the filter. | ||
if (this.lastSlot) { | ||
this.eventsFilter.start = nextSlot(this.lastSlot) | ||
} | ||
const events = await this.provider.getEvents(this.eventsFilter) | ||
|
||
if (events.length) { | ||
this.emit(ON_MASSA_EVENT_DATA, events) | ||
this.lastSlot = events[events.length - 1].context.slot | ||
} | ||
} catch (ex) { | ||
this.emit(ON_MASSA_EVENT_ERROR, ex) | ||
} | ||
|
||
// reset the interval. | ||
this.intervalId = setTimeout(this.poll, this.pollIntervalMs) | ||
} | ||
|
||
/** | ||
* Stops polling for events. | ||
*/ | ||
private stop = (): void => { | ||
if (this.intervalId?.hasRef()) { | ||
clearInterval(this.intervalId) | ||
} | ||
} | ||
|
||
/** | ||
* Starts polling for events. | ||
*/ | ||
private start(): void { | ||
this.stop() | ||
this.intervalId = setInterval(this.poll, this.pollIntervalMs) | ||
} | ||
|
||
/** | ||
* Starts polling for events and returns the stopPolling function. | ||
* | ||
* @param provider - The provider to use for polling. | ||
* @param eventsFilter - The filter to use for the events. | ||
* @param onData - The callback function to call when new events are found. | ||
* @param onError - The callback function to call when an error occurs. | ||
* @param pollIntervalMs - The interval in milliseconds to poll for events. Default is 1000Ms. | ||
* | ||
* @returns An object containing the stopPolling function. | ||
*/ | ||
// eslint-disable-next-line max-params | ||
public static start( | ||
provider: Provider, | ||
eventsFilter: EventFilter, | ||
onData?: (data: SCEvent[]) => void, | ||
onError?: (err: Error) => void, | ||
pollIntervalMs = DEFAULT_POLL_INTERVAL_MS | ||
): { stopPolling: () => void } { | ||
const eventPoller = new EventPoller(provider, eventsFilter, pollIntervalMs) | ||
if (onData) { | ||
eventPoller.on(ON_MASSA_EVENT_DATA, (data: SCEvent[]) => { | ||
onData(data) | ||
}) | ||
} | ||
if (onError) { | ||
eventPoller.on(ON_MASSA_EVENT_ERROR, (e) => { | ||
onError(e) | ||
}) | ||
} | ||
|
||
eventPoller.start() | ||
|
||
return { | ||
stopPolling: eventPoller.stop, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './eventsPoller' |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { EventFilter, EventPoller, SCEvent } from '../../src' | ||
import { MRC20 } from '../../src/contracts-wrappers' | ||
import { provider } from './setup' | ||
import waitForExpect from 'wait-for-expect' | ||
|
||
const USDC = 'AS12k8viVmqPtRuXzCm6rKXjLgpQWqbuMjc37YHhB452KSUUb9FgL' | ||
|
||
let usdcContract: MRC20 | ||
|
||
describe('SC Event tests', () => { | ||
beforeAll(async () => { | ||
usdcContract = new MRC20(provider, USDC) | ||
}) | ||
|
||
test('poll transfer event from caller and contract addr', async () => { | ||
const amount = 1_000n | ||
const currentSlot = await provider.client.getCurrentSlot() | ||
const operation = await usdcContract.transfer( | ||
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53', | ||
amount | ||
) | ||
await operation.waitSpeculativeExecution() | ||
|
||
let events: SCEvent[] = [] | ||
|
||
const filter = { | ||
smartContractAddress: USDC, | ||
callerAddress: provider.address, | ||
start: currentSlot, | ||
} | ||
|
||
const { stopPolling } = EventPoller.start(provider, filter, (data) => { | ||
events = data | ||
}) | ||
|
||
await waitForExpect(() => { | ||
expect(events.length).toEqual(1) | ||
expect(events[0].data).toEqual('TRANSFER SUCCESS') | ||
}) | ||
stopPolling() | ||
}) | ||
|
||
test('poll transfer event from operationId', async () => { | ||
const amount = 1_000n | ||
const currentSlot = await provider.client.getCurrentSlot() | ||
const operation = await usdcContract.transfer( | ||
'AU1wN8rn4SkwYSTDF3dHFY4U28KtsqKL1NnEjDZhHnHEy6cEQm53', | ||
amount | ||
) | ||
await operation.waitSpeculativeExecution() | ||
|
||
let events: SCEvent[] = [] | ||
|
||
const filter: EventFilter = { | ||
operationId: operation.id, | ||
start: currentSlot, | ||
} | ||
|
||
const { stopPolling } = EventPoller.start(provider, filter, (data) => { | ||
events = data | ||
}) | ||
|
||
await waitForExpect(() => { | ||
expect(events.length).toEqual(1) | ||
expect(events[0].data).toEqual('TRANSFER SUCCESS') | ||
}) | ||
stopPolling() | ||
}) | ||
}) |