@sentio/sdk 2.39.7-rc.3 → 2.39.7-rc.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/lib/aptos/codegen/types.test.js.map +1 -1
  2. package/lib/aptos/ext/coin-event.test.js.map +1 -1
  3. package/lib/aptos/ext/coin.test.js.map +1 -1
  4. package/lib/core/event-logger.test.js.map +1 -1
  5. package/lib/core/normalization.test.js.map +1 -1
  6. package/lib/core/numberish.test.js.map +1 -1
  7. package/lib/eth/base-processor.d.ts.map +1 -1
  8. package/lib/eth/base-processor.js +7 -6
  9. package/lib/eth/base-processor.js.map +1 -1
  10. package/lib/eth/context.d.ts +1 -1
  11. package/lib/eth/context.d.ts.map +1 -1
  12. package/lib/eth/context.js.map +1 -1
  13. package/lib/eth/eth.test.js.map +1 -1
  14. package/lib/eth/generic-processor.test.js.map +1 -1
  15. package/lib/eth/provider.d.ts.map +1 -1
  16. package/lib/eth/provider.js +12 -18
  17. package/lib/eth/provider.js.map +1 -1
  18. package/lib/eth/provider.test.js.map +1 -1
  19. package/lib/store/codegen.d.ts.map +1 -1
  20. package/lib/store/codegen.js +5 -2
  21. package/lib/store/codegen.js.map +1 -1
  22. package/lib/store/convert.js +1 -1
  23. package/lib/store/convert.js.map +1 -1
  24. package/lib/store/convert.test.js.map +1 -1
  25. package/lib/store/cursor.d.ts +14 -0
  26. package/lib/store/cursor.d.ts.map +1 -0
  27. package/lib/store/cursor.js +22 -0
  28. package/lib/store/cursor.js.map +1 -0
  29. package/lib/store/index.d.ts +1 -0
  30. package/lib/store/index.d.ts.map +1 -1
  31. package/lib/store/index.js +1 -0
  32. package/lib/store/index.js.map +1 -1
  33. package/lib/store/schema.js +1 -1
  34. package/lib/store/store.d.ts +19 -12
  35. package/lib/store/store.d.ts.map +1 -1
  36. package/lib/store/store.js +39 -14
  37. package/lib/store/store.js.map +1 -1
  38. package/lib/utils/dex-price.test.js.map +1 -1
  39. package/lib/utils/erc20.test.js.map +1 -1
  40. package/lib/utils/price.test.js.map +1 -1
  41. package/package.json +4 -5
  42. package/src/eth/base-processor.ts +8 -8
  43. package/src/eth/context.ts +1 -1
  44. package/src/eth/provider.ts +15 -19
  45. package/src/store/codegen.ts +6 -2
  46. package/src/store/convert.ts +2 -2
  47. package/src/store/cursor.ts +29 -0
  48. package/src/store/index.ts +1 -0
  49. package/src/store/schema.ts +1 -1
  50. package/src/store/store.ts +125 -23
@@ -6,6 +6,7 @@ import type { Entity as EntityStruct, RichValue } from '@sentio/protos'
6
6
  import { DBRequest_DBOperator, DBResponse } from '@sentio/protos'
7
7
  import { toBigInteger } from './convert.js'
8
8
  import { PluginManager } from '@sentio/runtime'
9
+ import { Cursor } from './cursor.js'
9
10
 
10
11
  type Value = ID | string | Int | Float | boolean | Timestamp | Bytes | BigDecimal | bigint
11
12
 
@@ -95,14 +96,60 @@ export class Store {
95
96
  await promise
96
97
  }
97
98
 
98
- async *listIterator<T extends Entity>(entity: EntityClass<T>, filters?: ListFilter<T>[]) {
99
+ async *listIterator<T extends Entity, P extends keyof T, O extends Operators<T[P]>>(
100
+ entity: EntityClass<T>,
101
+ filters: ListFilter<T, P, O>[]
102
+ ) {
99
103
  let cursor: string | undefined = undefined
100
104
 
101
105
  while (true) {
102
- const promise = this.context.sendRequest({
106
+ const response: DBResponse = await this.listRequest(entity, filters || [], cursor)
107
+ for (const data of response.entityList?.entities || []) {
108
+ yield this.newEntity(entity, data)
109
+ }
110
+ console.debug(
111
+ 'list returned ',
112
+ response.entityList?.entities?.length,
113
+ ' entities, next cursor',
114
+ response?.nextCursor
115
+ )
116
+ if (!response.nextCursor) {
117
+ break
118
+ }
119
+ cursor = response.nextCursor
120
+ }
121
+ }
122
+
123
+ async *listBatched<T extends Entity, P extends keyof T, O extends Operators<T[P]>>(
124
+ entity: EntityClass<T>,
125
+ filters: ListFilter<T, P, O>[],
126
+ batchSize = 100
127
+ ) {
128
+ let cursor: string | undefined = undefined
129
+
130
+ while (true) {
131
+ const response: DBResponse = await this.listRequest(entity, filters || [], cursor, batchSize)
132
+ const entities = (response.entityList?.entities || []).map((data) => this.newEntity(entity, data))
133
+ yield entities
134
+ if (!response.nextCursor) {
135
+ break
136
+ }
137
+ cursor = response.nextCursor
138
+ }
139
+ }
140
+
141
+ private async listRequest<T extends Entity, P extends keyof T, O extends Operators<T[P]>>(
142
+ entity: EntityClass<T>,
143
+ filters: ListFilter<T, P, O>[],
144
+ cursor: string | undefined,
145
+ pageSize?: number
146
+ ): Promise<DBResponse> {
147
+ return (await this.context.sendRequest(
148
+ {
103
149
  list: {
104
150
  entity: getEntityName(entity),
105
151
  cursor,
152
+ pageSize,
106
153
  filters:
107
154
  filters?.map((f) => ({
108
155
  field: f.field as string,
@@ -110,19 +157,21 @@ export class Store {
110
157
  value: { values: Array.isArray(f.value) ? f.value.map((v) => serialize(v)) : [serialize(f.value)] }
111
158
  })) || []
112
159
  }
113
- })
114
- const response = (await promise) as DBResponse
115
- for (const data of response.entityList?.entities || []) {
116
- yield this.newEntity(entity, data)
117
- }
118
- if (!response.nextCursor) {
119
- break
120
- }
121
- cursor = response.nextCursor
122
- }
160
+ },
161
+ 60
162
+ )) as DBResponse
123
163
  }
124
164
 
125
- async list<T extends Entity>(entity: EntityClass<T>, filters?: ListFilter<T>[]) {
165
+ async list<T extends Entity, P extends keyof T, O extends Operators<T[P]>>(
166
+ entity: EntityClass<T>,
167
+ filters: ListFilter<T, P, O>[],
168
+ cursor?: Cursor
169
+ ) {
170
+ if (cursor) {
171
+ const response = await this.listRequest(entity, filters || [], cursor.cursor, cursor.pageSize)
172
+ cursor.cursor = response.nextCursor
173
+ return response.entityList?.entities.map((data) => this.newEntity(entity, data)) || []
174
+ }
126
175
  // TODO Array.fromAsync when upgrade to node 22
127
176
  return this.fromAsync(this.listIterator(entity, filters))
128
177
  }
@@ -154,19 +203,68 @@ export class Store {
154
203
  }
155
204
  }
156
205
 
157
- export type Operators = '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not in'
206
+ type ArrayOperators = 'in' | 'not in' | 'has all' | 'has any'
158
207
 
159
- export interface ListFilter<T extends Entity> {
160
- field: keyof T
161
- op: Operators
162
- value: Value | Value[] | null
163
- }
208
+ export type Operators<T> =
209
+ T extends Array<any>
210
+ ? 'in' | 'not in' | '=' | '!=' | 'has all' | 'has any'
211
+ : T extends Int
212
+ ? '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not in'
213
+ : T extends Float
214
+ ? '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not in'
215
+ : T extends Bytes
216
+ ? '=' | '!=' | 'in' | 'not in'
217
+ : T extends ID
218
+ ? '=' | '!=' | 'like' | 'not like' | 'in' | 'not in'
219
+ : T extends string
220
+ ? '=' | '!=' | 'like' | 'not like' | 'in' | 'not in'
221
+ : T extends Timestamp
222
+ ? '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not in'
223
+ : T extends boolean
224
+ ? '=' | '!=' | 'in' | 'not in'
225
+ : T extends BigDecimal
226
+ ? '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not in'
227
+ : T extends bigint
228
+ ? '=' | '!=' | '<' | '<=' | '>' | '>=' | 'in' | 'not in'
229
+ : '=' | '!=' | 'in' | 'not in'
230
+
231
+ type CompatibleValue<T, O extends Operators<T>> = O extends ArrayOperators
232
+ ? T extends Array<infer U>
233
+ ? U[]
234
+ : T[]
235
+ :
236
+ | (T extends bigint
237
+ ? bigint
238
+ : T extends Int
239
+ ? number
240
+ : T extends Float
241
+ ? number
242
+ : T extends Bytes
243
+ ? Bytes | string
244
+ : T extends ID
245
+ ? ID | string
246
+ : T extends BigDecimal
247
+ ? BigDecimal | number
248
+ : T extends Int
249
+ ? number
250
+ : T)
251
+ | Nullable<O>
164
252
 
165
- export interface ListOptions<T extends Entity> {
166
- cursor: string
253
+ type Nullable<O> = O extends '=' | '!=' ? null : never
254
+
255
+ export type ListFilter<T extends Entity, P extends keyof T, O extends Operators<T[P]>> = {
256
+ field: P
257
+ op: O
258
+ value: CompatibleValue<T[P], O>
167
259
  }
168
260
 
169
- const ops: Record<Operators, DBRequest_DBOperator> = {
261
+ export type ArrayFilter<T extends Entity, P extends keyof T, O extends Operators<T[P]>> = [
262
+ P,
263
+ O,
264
+ CompatibleValue<T[P], O>
265
+ ]
266
+
267
+ const ops: Record<Operators<any>, DBRequest_DBOperator> = {
170
268
  '=': DBRequest_DBOperator.EQ,
171
269
  '!=': DBRequest_DBOperator.NE,
172
270
  '<': DBRequest_DBOperator.LT,
@@ -174,7 +272,11 @@ const ops: Record<Operators, DBRequest_DBOperator> = {
174
272
  '>': DBRequest_DBOperator.GT,
175
273
  '>=': DBRequest_DBOperator.GE,
176
274
  in: DBRequest_DBOperator.IN,
177
- 'not in': DBRequest_DBOperator.NOT_IN
275
+ 'not in': DBRequest_DBOperator.NOT_IN,
276
+ like: DBRequest_DBOperator.LIKE,
277
+ 'not like': DBRequest_DBOperator.NOT_LIKE,
278
+ 'has all': DBRequest_DBOperator.HAS_ALL,
279
+ 'has any': DBRequest_DBOperator.HAS_ANY
178
280
  }
179
281
 
180
282
  function serialize(v: any): RichValue {