-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
159 lines (139 loc) · 4.13 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
import { ServiceBroker } from 'moleculer'
import { MsIds } from '../../common.js'
import { IndexerClientI } from '../interface.js'
import { EntityRequest } from './dal/entityRequest.js'
import {
AccountIndexerState,
AccountIndexerRequestArgs,
InvokeMethodRequestArgs,
GetAccountIndexingEntityStateRequestArgs,
GetEntityPendingRequestsRequestArgs,
} from './types.js'
import { BlockchainRequestArgs } from '../../types.js'
import { Blockchain, IndexableEntityType } from '../../../types.js'
/**
* Client to access the main indexer service through the broker.
*/
export abstract class BaseIndexerClient implements IndexerClientI {
/**
* @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 indexer service.
*/
constructor(
protected blockchainId: Blockchain,
protected broker: ServiceBroker,
protected msId: MsIds = MsIds.Indexer,
) {}
abstract normalizeAccount(account: string): string
abstract normalizeEntityId(entity: IndexableEntityType, id: string): string
async indexAccount(
args: Omit<AccountIndexerRequestArgs, keyof BlockchainRequestArgs>,
broadcast = false,
): Promise<void> {
const account = this.normalizeAccount(args.account)
if (broadcast) {
const nodes = Object.keys(this.broker.registry.nodes.nodes)
await Promise.all(
nodes.map((nodeID) =>
this.broker.call(
`${this.msId}.indexAccount`,
{
partitionKey: args.partitionKey || account,
blockchainId: this.blockchainId,
...args,
account,
},
{ nodeID },
),
),
)
return
}
return this.broker.call(`${this.msId}.indexAccount`, {
partitionKey: args.partitionKey || account,
blockchainId: this.blockchainId,
...args,
account,
})
}
async deleteAccount(
args: Omit<AccountIndexerRequestArgs, keyof BlockchainRequestArgs>,
broadcast = false,
): Promise<void> {
const account = this.normalizeAccount(args.account)
if (broadcast) {
const nodes = Object.keys(this.broker.registry.nodes.nodes)
await Promise.all(
nodes.map((nodeID) =>
this.broker.call(
`${this.msId}.deleteAccount`,
{
partitionKey: args.partitionKey || account,
blockchainId: this.blockchainId,
...args,
account,
},
{ nodeID },
),
),
)
return
}
return this.broker.call(`${this.msId}.deleteAccount`, {
partitionKey: args.partitionKey || account,
blockchainId: this.blockchainId,
...args,
account,
})
}
getAccountState(
args: Omit<
GetAccountIndexingEntityStateRequestArgs,
keyof BlockchainRequestArgs
>,
): Promise<AccountIndexerState | undefined> {
const account = this.normalizeAccount(args.account)
return this.broker.call(`${this.msId}.getAccountState`, {
partitionKey: args.partitionKey || account,
blockchainId: this.blockchainId,
...args,
account,
})
}
invokeDomainMethod(
args: Omit<InvokeMethodRequestArgs, keyof BlockchainRequestArgs>,
): Promise<unknown> {
const account = this.normalizeAccount(args.account)
return this.broker.call(
`${this.msId}.invokeDomainMethod`,
{
partitionKey: args.partitionKey || account,
blockchainId: this.blockchainId,
...args,
account,
},
{ meta: { $streamObjectMode: true }, nodeID: args.indexer },
)
}
// Private API
getEntityPendingRequests(
args: Omit<
GetEntityPendingRequestsRequestArgs,
keyof BlockchainRequestArgs
>,
): Promise<EntityRequest[]> {
const account = args.account
? this.normalizeAccount(args.account)
: args.account
return this.broker.call(
`${this.msId}.getEntityPendingRequests`,
{
blockchainId: this.blockchainId,
...args,
account,
},
{ nodeID: args.indexer },
)
}
}