-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
192 lines (169 loc) · 5.42 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { ServiceBroker } from 'moleculer'
import { Blockchain, IndexableEntityType } from '../../../types.js'
import { MsIds } from '../../common.js'
import {
BlockchainRequestArgs,
InvokeBlockchainMethodRequestArgs,
} from '../../types.js'
import { FetcherClientI } from '../interface.js'
import {
FetcherStateRequestArgs,
FetcherState,
EntityState,
CheckEntityRequestArgs,
DelEntityRequestArgs,
AddAccountEntityRequestArgs,
GetAccountEntityStateRequestArgs,
DelAccountEntityRequestArgs,
AccountEntityHistoryState,
FetchAccountEntitiesByDateRequestArgs,
FetchEntitiesByIdRequestArgs,
} from './types.js'
/**
* Client to access the main fetcher service through the broker.
*/
export abstract class BaseFetcherClient implements FetcherClientI {
/**
* @param blockchainId The id of the blockchain the client handles
* @param broker The broker instance to retrieve the actual service from.
* @param msId The moleculer service id of the fetcher service.
*/
constructor(
protected blockchainId: Blockchain,
protected broker: ServiceBroker,
protected msId: MsIds = MsIds.Fetcher,
) {}
abstract normalizeAccount(account: string): string
abstract normalizeEntityId(entity: IndexableEntityType, id: string): string
addAccountEntityFetcher(
args: Omit<AddAccountEntityRequestArgs, keyof BlockchainRequestArgs>,
): Promise<void> {
const account = this.normalizeAccount(args.account)
return this.broker.call(`${this.msId}.addAccountEntityFetcher`, {
partitionKey: account,
indexerId: this.broker.nodeID,
blockchainId: this.blockchainId,
...args,
account,
})
}
delAccountEntityFetcher(
args: Omit<DelAccountEntityRequestArgs, keyof BlockchainRequestArgs>,
): Promise<void> {
const account = this.normalizeAccount(args.account)
return this.broker.call(`${this.msId}.delAccountEntityFetcher`, {
partitionKey: account,
indexerId: this.broker.nodeID,
blockchainId: this.blockchainId,
...args,
account,
})
}
getAccountEntityFetcherState(
args: Omit<GetAccountEntityStateRequestArgs, keyof BlockchainRequestArgs>,
): Promise<AccountEntityHistoryState<unknown> | undefined> {
const account = this.normalizeAccount(args.account)
return this.broker.call(`${this.msId}.getAccountEntityFetcherState`, {
partitionKey: account,
indexerId: this.broker.nodeID,
blockchainId: this.blockchainId,
...args,
account,
})
}
fetchAccountEntitiesByDate(
args: Omit<
FetchAccountEntitiesByDateRequestArgs,
keyof BlockchainRequestArgs
>,
): Promise<void | AsyncIterable<string[]>> {
const account = this.normalizeAccount(args.account)
return this.broker.call(`${this.msId}.fetchAccountEntitiesByDate`, {
partitionKey: account,
indexerId: this.broker.nodeID,
blockchainId: this.blockchainId,
...args,
account,
})
}
async fetchEntitiesById(
args: Omit<FetchEntitiesByIdRequestArgs, keyof BlockchainRequestArgs>,
): Promise<void> {
const ids = this.mapEntityIds(args)
const groups = this.getEntityPartitionGroups({ ...args, ids })
await Promise.all(
Object.entries(groups).map(([partitionKey, ids]) => {
return this.broker.call(`${this.msId}.fetchEntitiesById`, {
indexerId: this.broker.nodeID,
blockchainId: this.blockchainId,
...args,
partitionKey,
ids,
})
}),
)
}
async getFetcherState(
args: Omit<FetcherStateRequestArgs, keyof BlockchainRequestArgs>,
): Promise<FetcherState> {
return this.broker.call(`${this.msId}.getFetcherState`, args, {
nodeID: args.fetcher,
})
}
async getEntityState(args: CheckEntityRequestArgs): Promise<EntityState[]> {
const ids = this.mapEntityIds(args)
const groups = this.getEntityPartitionGroups({ ids })
const states = (await Promise.all(
Object.entries(groups).map(([partitionKey, ids]) => {
return this.broker.call(`${this.msId}.getEntityState`, {
partitionKey,
ids,
})
}),
)) as EntityState[][]
return states.flatMap((state) => state)
}
async delEntityCache(args: DelEntityRequestArgs): Promise<void> {
const ids = this.mapEntityIds(args)
const groups = this.getEntityPartitionGroups({ ids })
await Promise.all(
Object.entries(groups).map(([partitionKey, ids]) => {
return this.broker.call(`${this.msId}.delEntityCache`, {
partitionKey,
ids,
})
}),
)
}
protected invokeBlockchainMethod<R, A>(
args: Omit<
InvokeBlockchainMethodRequestArgs<A>,
keyof BlockchainRequestArgs
>,
): Promise<R> {
return this.broker.call(`${this.msId}.invokeBlockchainMethod`, {
blockchainId: this.blockchainId,
...args,
})
}
protected getEntityPartitionGroups(args: {
ids: string[]
partitionKey?: string
}): Record<string, string[]> {
const partitionGroups = args.partitionKey
? { [args.partitionKey]: args.ids }
: args.ids.reduce((acc, curr) => {
const k = curr.charAt(0)
const group = (acc[k] = acc[k] || [])
group.push(curr)
return acc
}, {} as Record<string, string[]>)
return partitionGroups
}
protected mapEntityIds(args: {
ids: string[]
type: IndexableEntityType
}): string[] {
return args.ids.map((id) => this.normalizeEntityId(args.type, id))
}
}