Skip to content

Commit 2cf0866

Browse files
committed
feat: update type to ContractAddress for all contract address types
1 parent 4dce4ec commit 2cf0866

File tree

8 files changed

+25
-18
lines changed

8 files changed

+25
-18
lines changed

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/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/reku/src/event/crosschecker/interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Awaitable, Milliseconds, Store } from '@ora-io/utils'
1+
import type { Awaitable, ContractAddress, Milliseconds, Store } from '@ora-io/utils'
22
import type { ethers } from 'ethers'
33

44
export type FnOnMissingLog = (log: ethers.Log) => Awaitable<void>
55

66
export interface SimpleLog { transactionHash: string; index?: number }
77

88
export interface LogFilterParam {
9-
address: string
9+
address: ContractAddress
1010
topics: string[]
1111
fromBlock?: number
1212
toBlock?: number

packages/reku/src/provider/contract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { Fn } from '@ora-io/utils'
1+
import type { ContractAddress, Fn } from '@ora-io/utils'
22
import type { Interface, InterfaceAbi } from 'ethers'
33
import { ethers } from 'ethers'
44

55
export class ContractManager {
66
private _contract?: ethers.Contract
77
private _listeners: Map<ethers.ContractEventName, Fn> = new Map()
88

9-
constructor(public address: string, public abi: Interface | InterfaceAbi, public provider: ethers.JsonRpcProvider | ethers.WebSocketProvider) {
9+
constructor(public address: ContractAddress, public abi: Interface | InterfaceAbi, public provider: ethers.JsonRpcProvider | ethers.WebSocketProvider) {
1010
this._contract = new ethers.Contract(address, abi, provider)
1111
}
1212

packages/reku/src/provider/provider.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter } from 'node:events'
22
import type { InterfaceAbi } from 'ethers'
33
import { Interface, WebSocketProvider, ethers } from 'ethers'
44
import type { ErrorEvent, WebSocket } from 'ws'
5+
import type { ContractAddress } from '@ora-io/utils'
56
import { debug } from '../debug'
67
import { ContractManager } from './contract'
78

@@ -20,7 +21,7 @@ export type RekuProviderManagerEvent = 'error' | 'close'
2021

2122
export class RekuProviderManager {
2223
private _provider?: ethers.JsonRpcProvider | ethers.WebSocketProvider
23-
private _contracts: Map<string, ContractManager> = new Map()
24+
private _contracts: Map<ContractAddress, ContractManager> = new Map()
2425

2526
private _heartbeatInterval = 10 * 1000
2627

@@ -53,9 +54,9 @@ export class RekuProviderManager {
5354
return this._contracts
5455
}
5556

56-
addContract(address: string, contract: ethers.Contract): ContractManager | undefined
57-
addContract(address: string, abi: Interface | InterfaceAbi): ContractManager | undefined
58-
addContract(address: string, abi: Interface | InterfaceAbi | ethers.Contract): ContractManager | undefined {
57+
addContract(address: ContractAddress, contract: ethers.Contract): ContractManager | undefined
58+
addContract(address: ContractAddress, abi: Interface | InterfaceAbi): ContractManager | undefined
59+
addContract(address: ContractAddress, abi: Interface | InterfaceAbi | ethers.Contract): ContractManager | undefined {
5960
if (this._provider) {
6061
if (abi instanceof Interface || Array.isArray(abi)) {
6162
if (!abi)
@@ -77,11 +78,11 @@ export class RekuProviderManager {
7778
return undefined
7879
}
7980

80-
addListener(contractAddress: string, event: ethers.ContractEventName, listener: ethers.Listener) {
81+
addListener(contractAddress: ContractAddress, event: ethers.ContractEventName, listener: ethers.Listener) {
8182
this._contracts.get(contractAddress)?.addListener(event, listener)
8283
}
8384

84-
removeListener(contractAddress: string, event: ethers.ContractEventName, listener: ethers.Listener) {
85+
removeListener(contractAddress: ContractAddress, event: ethers.ContractEventName, listener: ethers.Listener) {
8586
this._contracts.get(contractAddress)?.removeListener(event, listener)
8687
}
8788

@@ -154,7 +155,7 @@ export class RekuProviderManager {
154155
// reconnect provider
155156
this.connect()
156157
// reset contracts
157-
const contracts: Map<string, ContractManager> = new Map()
158+
const contracts: Map<ContractAddress, ContractManager> = new Map()
158159
this._contracts.forEach((contract) => {
159160
if (this._provider) {
160161
const newContract = new ContractManager(contract.address, contract.abi, this._provider)

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './common'
22
export * from './store'
33
export * from './w3'
4+
export type * from './types'
45
export * from '@murongg/utils'

packages/utils/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './w3'

packages/utils/src/types/w3.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type { Addressable } from 'ethers'
2+
3+
export type ContractAddress = Addressable | string

0 commit comments

Comments
 (0)