Skip to content

Commit 718f580

Browse files
authored
Merge pull request #25 from ora-io/dev
Dev
2 parents 96ad5b5 + 7017f4b commit 718f580

File tree

23 files changed

+100
-60
lines changed

23 files changed

+100
-60
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ lib-cov
99
logs
1010
node_modules
1111
temp
12-
*.env
12+
*.env
13+
14+
packages/orap/examples/testDemo/*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ora-stack",
3-
"version": "0.2.2",
3+
"version": "0.2.4",
44
"private": true,
55
"packageManager": "[email protected]",
66
"description": "",

packages/orap/README.md

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const handle2 = (...args: any[]) => {
9292

9393
// define event signal with crosscheck, and customized cacheable tasks
9494
// note: use redis as the cache layer
95-
orap.event(eventSignalParam)
95+
orap.event('contract address', 'contract abi', 'event name')
9696
.crosscheck(ccOptions)
9797
// add a task
9898
.task()
@@ -122,9 +122,11 @@ orap.listen(
122122

123123
Each `new Orap()` starts a `Orap Flow`
124124

125-
**.event(eventSignalParam, handlFn)**
126-
- `eventSignalParam`: defines an event signal and enters an Event Flow
127-
- `handlFn`: customized hook on new event received.
125+
**.event(address, abi, eventName, handleFn)**
126+
- `address`: defines the contract address
127+
- `abi`: defines the contract abi
128+
- `eventName`: defines the event name
129+
- `handleFn`: customized hook on new event received.
128130
- `return true` to continue the rest of processes
129131
- `return false` to hijack the rest of processes
130132

@@ -147,9 +149,9 @@ Each `.event(...)` starts an `Event Flow`
147149
- add a task for this event type
148150
- starts a `Task Flow`
149151

150-
**.handle(handlFn)**
151-
- same as `.event(.., handlFn)`
152-
- `handlFn`: customized hook on new event received.
152+
**.handle(handleFn)**
153+
- same as `.event(.., handleFn)`
154+
- `handleFn`: customized hook on new event received.
153155
- `return true` to continue the rest of processes
154156
- `return false` to hijack the rest of processes
155157

@@ -303,7 +305,7 @@ NOTE: handle is a middleware that can be flexibly placed at any position within
303305
httpProvider: new ethers.JsonRpcProvider('http://127.0.0.1')
304306
}
305307

306-
orap.event(eventSignalParam)
308+
orap.event(eventSignalParam.address, eventSignalParam.abi, eventSignalParam.eventName)
307309
// add a task
308310
.task()
309311
.cache(sm)
@@ -353,7 +355,31 @@ const providers = {
353355
httpProvider: new ethers.JsonRpcProvider('http://127.0.0.1')
354356
}
355357

356-
orap.event(eventSignalParam)
358+
orap.event(eventSignalParam.address, eventSignalParam.abi, eventSignalParam.eventName)
359+
// add a task
360+
.task()
361+
.cache(sm)
362+
.prefix('ora-stack:orap:demo:TransferTask:', 'ora-stack:orap:demo:Done-TransferTask:')
363+
.ttl({ taskTtl: 120000, doneTtl: 60000 })
364+
.use(async (...args) => {
365+
const { next } = getMiddlewareContext(...args)
366+
console.log(1)
367+
await next()
368+
})
369+
.handle(handleTask)
370+
.use(async (...args) => {
371+
const { next } = getMiddlewareContext(...args)
372+
console.log(3)
373+
await next()
374+
})
375+
.use(async (...args) => {
376+
const { next } = getMiddlewareContext(...args)
377+
console.log(4)
378+
})
379+
.use(async (...args) => {
380+
const { next } = getMiddlewareContext(...args)
381+
console.log(5)
382+
})
357383
// add a task
358384
.task()
359385
.cache(sm)
@@ -414,7 +440,7 @@ const eventSignalParam = {
414440

415441
const handle = (...args: any) => { console.log('handle', args) }
416442

417-
orap.event(eventSignalParam, handle)
443+
orap.event(eventSignalParam.address, eventSignalParam.abi, eventSignalParam.eventName, handle)
418444
.crosscheck({ pollingInterval: 1000, batchBlocksCount: 1, blockInterval: 12000 })
419445

420446
orap.listen(
@@ -479,7 +505,7 @@ All events that arrive the oracle service and trigger following actions are defi
479505
etc.
480506

481507
**EventSignal**
482-
- define event listener as simple as: `orap.event({address:"0x", abi:"", eventName: "Transfer"}, handleSignal)`
508+
- define event listener as simple as: `orap.event(address:"0x", abi:"", eventName: "Transfer", handleSignal)`
483509
- natively integrate `crosschecker` features from [@ora-io/reku](https://github.com/ora-io/ora-stack/blob/main/packages/reku/), available config please check out `AutoCrossCheckParam` in `reku`
484510
- each event signal only accept at most one crosschecker.
485511
- `callback`: the user provided handle function to handle the new signals.

packages/orap/examples/declarativeDemo/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function startDemo(options: ListenOptions, storeConfig?: any) {
2424

2525
const toKey: ToKeyFn = (from: string, _to: string, _amount: number) => `${from}_${randomStr(4)}`
2626

27-
orap.event(eventSignalParam)
27+
orap.event(eventSignalParam.address, eventSignalParam.abi, eventSignalParam.eventName)
2828
.crosscheck({
2929
store,
3030
storeKeyPrefix: 'ora-stack:orap:demo:cc:',
@@ -43,7 +43,7 @@ export function startDemo(options: ListenOptions, storeConfig?: any) {
4343
.key(toKey)
4444
.prefix('ora-stack:orap:demo:TransferTask:', 'ora-stack:orap:demo:Done-TransferTask:')
4545
.ttl({ taskTtl: 120000, doneTtl: 60000 })
46-
.use(CheckTransactionStatus)
46+
.use(CheckTransactionStatus(options.wsProvider))
4747
.handle(handleTask)
4848
// add another task
4949
.another()

packages/orap/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ora-io/orap",
33
"type": "module",
4-
"version": "0.2.2",
4+
"version": "0.2.4",
55
"packageManager": "[email protected]",
66
"description": "",
77
"author": "Norman (nom4dv3), MuRong",

packages/orap/src/flow/orap.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Providers } from '@ora-io/reku'
2-
import type { Fn } from '@ora-io/utils'
2+
import type { ContractAddress, Fn } from '@ora-io/utils'
3+
import { isAddressable } from 'ethers'
34
import type { Interface, InterfaceAbi } from 'ethers'
45
import { OrapVerse } from '../verse/orap'
56
import type { EventSignalRegisterParams } from '../signal'
@@ -27,12 +28,11 @@ export class OrapFlow implements Flow {
2728
}
2829

2930
event(params: EventSignalRegisterParams, handler?: HandleFn): EventFlow
30-
event(params: string, abi: Interface | InterfaceAbi | HandleFn, eventName: string, handler?: HandleFn): EventFlow
31-
event(params: EventSignalRegisterParams | string, abi?: Interface | InterfaceAbi | HandleFn, eventName?: string, handler?: HandleFn): EventFlow {
32-
if (typeof params === 'string')
31+
event(address: ContractAddress, abi: Interface | InterfaceAbi | HandleFn, eventName: string, handler?: HandleFn): EventFlow
32+
event(params: EventSignalRegisterParams | ContractAddress, abi?: Interface | InterfaceAbi | HandleFn, eventName?: string, handler?: HandleFn): EventFlow {
33+
if (typeof params === 'string' || isAddressable(params))
3334
params = { address: params, abi: abi as Interface | InterfaceAbi, eventName: eventName as string }
34-
else
35-
handler = abi as HandleFn
35+
else handler = abi as HandleFn
3636

3737
const eventFlow = new EventFlow(this, params, handler)
3838
this.subflows.event.push(eventFlow)

packages/orap/src/flow/task.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Milliseconds } from '@ora-io/utils'
12
import { alphabetHex, memoryStore, randomStr } from '@ora-io/utils'
23
import type { Context, TaskRaplized } from '../task'
34
import { StoreManager } from '../store'
@@ -15,15 +16,15 @@ const defaultHandleFn: HandleFn = () => {
1516
}
1617

1718
const defaultToKeyFn: ToKeyFn = _ => randomStr(8, alphabetHex)
18-
export interface TaskFlowTTL { taskTtl: number; doneTtl: number }
19+
export interface TaskFlowTTL { taskTtl: Milliseconds; doneTtl: Milliseconds }
1920

2021
// TODO: add 'Failed-Task:' ?
2122
export class TaskFlow implements Flow {
2223
sm: StoreManager = new StoreManager(memoryStore())
2324
taskPrefix: Prefix = 'Task:'
2425
donePrefix: Prefix = 'Done-Task:'
25-
taskTtl?: number
26-
doneTtl?: number
26+
taskTtl?: Milliseconds
27+
doneTtl?: Milliseconds
2728
toKeyFn: ToKeyFn = defaultToKeyFn
2829
handleFn: HandleFn = defaultHandleFn
2930
successFn: HandleResultFn = defaultSuccessFn
@@ -60,10 +61,10 @@ export class TaskFlow implements Flow {
6061
return this
6162
}
6263

63-
ttl(TTLs: number): this
64+
ttl(TTLs: Milliseconds): this
6465
ttl(TTLs: TaskFlowTTL): this
65-
ttl(taskTtl: number, doneTtl: number): this
66-
ttl(TTLs: TaskFlowTTL | number, doneTtl?: number): this {
66+
ttl(taskTtl: Milliseconds, doneTtl: Milliseconds): this
67+
ttl(TTLs: TaskFlowTTL | Milliseconds, doneTtl?: Milliseconds): this {
6768
if (typeof TTLs === 'number') {
6869
this.taskTtl = TTLs
6970
this.doneTtl = doneTtl ?? TTLs

packages/orap/src/signal/event.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import type { EventFragment, Interface, InterfaceAbi, Log } from 'ethers'
22
import { ContractEventPayload, ContractUnknownEventPayload, ethers } from 'ethers'
33
import { AutoCrossChecker, ONE_MINUTE_MS, RekuProviderManager } from '@ora-io/reku'
44
import type { AutoCrossCheckParam, Providers } from '@ora-io/reku'
5+
import type { ContractAddress } from '@ora-io/utils'
56
import type { Signal } from './interface'
67

78
export interface EventSignalRegisterParams {
8-
address: string
9+
address: ContractAddress
910
abi: Interface | InterfaceAbi
1011
eventName: string
1112
// esig?: string,

packages/orap/src/task/storable.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Awaitable, Constructor } from '@ora-io/utils'
1+
import type { Awaitable, Constructor, Milliseconds } from '@ora-io/utils'
22
import type { StoreManager } from '../store/storemanager'
33
import type { Context } from './context'
44
import { TaskBase } from './base'
@@ -9,8 +9,8 @@ export abstract class TaskStorable extends TaskBase {
99
// overwrite these for store key customize
1010
static readonly taskPrefix: string = 'Task:'
1111
static readonly taskPrefixDone: string = 'Done-Task:'
12-
static readonly taskTtl: number | undefined = undefined
13-
static readonly taskTtlDone: number | undefined = undefined
12+
static readonly taskTtl: Milliseconds | undefined = undefined
13+
static readonly taskTtlDone: Milliseconds | undefined = undefined
1414

1515
getTaskPrefix(_context?: Context): Awaitable<string> {
1616
return (this.constructor as TypeofTaskStorable).taskPrefix
@@ -20,19 +20,19 @@ export abstract class TaskStorable extends TaskBase {
2020
return (this.constructor as TypeofTaskStorable).taskPrefixDone
2121
}
2222

23-
get taskTtl(): number | undefined {
23+
get taskTtl(): Milliseconds | undefined {
2424
return (this.constructor as TypeofTaskStorable).taskTtl
2525
}
2626

27-
get taskTtlDone(): number | undefined {
27+
get taskTtlDone(): Milliseconds | undefined {
2828
return (this.constructor as TypeofTaskStorable).taskTtlDone
2929
}
3030

31-
getTaskTtl(_context?: Context): number | undefined {
31+
getTaskTtl(_context?: Context): Milliseconds | undefined {
3232
return (this.constructor as TypeofTaskStorable).taskTtl
3333
}
3434

35-
getTaskTtlDone(_context?: Context): number | undefined {
35+
getTaskTtlDone(_context?: Context): Milliseconds | undefined {
3636
return (this.constructor as TypeofTaskStorable).taskTtlDone
3737
}
3838

packages/orap/src/task/verse.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('TaskRaplized', () => {
7777
taskRaplized.eventLog = eventLog
7878
await taskRaplized.handle()
7979
await sleep(0)
80-
expect(taskFlow.handleFn).toHaveBeenCalledWith(...eventLog, taskRaplized, expect.any(Function))
80+
expect(taskFlow.handleFn).toHaveBeenCalledWith(...eventLog, expect.any(Function), taskRaplized)
8181
expect(taskFlow.successFn).toHaveBeenCalledWith(taskRaplized)
8282
})
8383

@@ -91,7 +91,7 @@ describe('TaskRaplized', () => {
9191
taskRaplized.eventLog = eventLog
9292
await taskRaplized.handle()
9393
await sleep(0)
94-
expect(taskFlow.handleFn).toHaveBeenCalledWith(...eventLog, taskRaplized, expect.any(Function))
94+
expect(taskFlow.handleFn).toHaveBeenCalledWith(...eventLog, expect.any(Function), taskRaplized)
9595
expect(taskFlow.failFn).toHaveBeenCalledWith(taskRaplized)
9696
})
9797

0 commit comments

Comments
 (0)