envio 3.7.0-svm-alpha.1 → 3.8.0

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 (45) hide show
  1. package/index.d.ts +331 -142
  2. package/package.json +6 -6
  3. package/src/ChainState.res +9 -1
  4. package/src/ChainState.res.mjs +6 -1
  5. package/src/Config.res +19 -10
  6. package/src/Config.res.mjs +17 -7
  7. package/src/Core.res +4 -0
  8. package/src/Envio.res +54 -76
  9. package/src/EventConfigBuilder.res +159 -30
  10. package/src/EventConfigBuilder.res.mjs +120 -24
  11. package/src/HandlerRegister.res +20 -11
  12. package/src/HandlerRegister.res.mjs +22 -5
  13. package/src/Hasura.res +30 -22
  14. package/src/Hasura.res.mjs +11 -5
  15. package/src/InMemoryStore.res +1 -6
  16. package/src/InMemoryStore.res.mjs +1 -3
  17. package/src/Internal.res +33 -13
  18. package/src/Internal.res.mjs +12 -16
  19. package/src/Main.res +2 -5
  20. package/src/MemoryStorage.res +40 -8
  21. package/src/MemoryStorage.res.mjs +38 -16
  22. package/src/Persistence.res +4 -2
  23. package/src/PgStorage.res +6 -1
  24. package/src/PgStorage.res.mjs +1 -1
  25. package/src/SimulateItems.res +261 -9
  26. package/src/SimulateItems.res.mjs +218 -47
  27. package/src/TestIndexer.res +26 -10
  28. package/src/TestIndexer.res.mjs +34 -12
  29. package/src/bindings/ClickHouse.res +49 -17
  30. package/src/bindings/ClickHouse.res.mjs +35 -14
  31. package/src/db/InternalTable.res +11 -0
  32. package/src/db/InternalTable.res.mjs +7 -0
  33. package/src/sources/EvmHyperSyncSource.res +8 -3
  34. package/src/sources/EvmHyperSyncSource.res.mjs +1 -1
  35. package/src/sources/SimulateSource.res +8 -4
  36. package/src/sources/SimulateSource.res.mjs +7 -3
  37. package/src/sources/Svm.res +34 -7
  38. package/src/sources/Svm.res.mjs +32 -4
  39. package/src/sources/SvmHyperSyncClient.res +10 -12
  40. package/src/sources/SvmHyperSyncClient.res.mjs +3 -2
  41. package/src/sources/SvmHyperSyncSource.res +96 -35
  42. package/src/sources/SvmHyperSyncSource.res.mjs +78 -38
  43. package/src/sources/TransactionStore.res +38 -0
  44. package/src/sources/TransactionStore.res.mjs +5 -0
  45. package/svm.schema.json +27 -78
@@ -10,50 +10,105 @@ type options = {
10
10
  addressStore: AddressStore.t,
11
11
  }
12
12
 
13
- // Parse the Rust-decoded instruction (args/accounts arrive as JSON strings to
14
- // side-step napi-rs's lack of native JSON passthrough) into the public shape.
15
- let parseDecoded = (
16
- d: SvmHyperSyncClient.ResponseTypes.decodedInstruction,
17
- ): Envio.svmInstructionParams => {
18
- let args = try JSON.parseOrThrow(d.argsJson) catch {
13
+ let parseArgs = (d: SvmHyperSyncClient.ResponseTypes.decodedInstruction): JSON.t =>
14
+ try JSON.parseOrThrow(d.argsJson) catch {
19
15
  | _ => JSON.Object(Dict.make())
20
16
  }
21
- let accounts = try {
22
- JSON.parseOrThrow(d.accountsJson)->(Utils.magic: JSON.t => dict<string>)
23
- } catch {
24
- | _ => Dict.make()
17
+
18
+ let namedAccounts = (
19
+ ~idlNames: array<string>,
20
+ ~accountArguments: array<string>,
21
+ ): dict<Envio.svmInstructionAccount> => {
22
+ let out = Dict.make()
23
+ idlNames->Array.forEachWithIndex((name, i) =>
24
+ switch accountArguments->Array.get(i) {
25
+ | Some(address) =>
26
+ out->Dict.set(
27
+ name,
28
+ {
29
+ Envio.address: address->SvmTypes.Pubkey.fromStringUnsafe,
30
+ accountName: name,
31
+ instructionAccountIndex: i,
32
+ },
33
+ )
34
+ | None => ()
35
+ }
36
+ )
37
+ out
38
+ }
39
+
40
+ let selectedLog = (log: SvmHyperSyncClient.EventItems.log, ~logFields: Utils.Set.t<string>): Envio.svmLog => {
41
+ let out = Dict.make()
42
+ if logFields->Utils.Set.has("kind") {
43
+ switch log.kind {
44
+ | Some(kind) => out->Dict.set("kind", kind)
45
+ | None => ()
46
+ }
25
47
  }
26
- {
27
- name: d.name,
28
- args,
29
- accounts,
30
- extraAccounts: d.extraAccounts,
48
+ if logFields->Utils.Set.has("message") {
49
+ switch log.message {
50
+ | Some(message) => out->Dict.set("message", message)
51
+ | None => ()
52
+ }
31
53
  }
54
+ out->(Utils.magic: dict<string> => Envio.svmLog)
32
55
  }
33
56
 
34
- // `block` is omitted; it's materialised from the block store at batch prep.
57
+ let setField = (out: dict<unknown>, name: string, value: 'a) =>
58
+ out->Dict.set(name, value->(Utils.magic: 'a => unknown))
59
+
60
+ // `block` and `transaction` are omitted; they're materialised from the stores
61
+ // at batch prep. Named-account `.activity` is attached then too.
62
+ // Unselected instruction keys must be omitted, not assigned `undefined`.
35
63
  let toSvmInstruction = (
36
64
  item: SvmHyperSyncClient.EventItems.item,
37
65
  ~programName,
38
66
  ~instructionName,
67
+ ~eventConfig: Internal.svmInstructionEventConfig,
68
+ ~fieldSelection: Internal.fieldSelection,
39
69
  ): Envio.svmInstruction => {
40
- programName,
41
- instructionName,
42
- programId: item.programId->SvmTypes.Pubkey.fromStringUnsafe,
43
- data: item.data,
44
- accounts: item.accounts->SvmTypes.Pubkey.fromStringsUnsafe,
45
- instructionAddress: item.instructionAddress,
46
- isInner: item.isInner,
47
- d1: ?item.d1,
48
- d2: ?item.d2,
49
- d4: ?item.d4,
50
- d8: ?item.d8,
51
- params: ?(item.decoded->Option.map(parseDecoded)),
52
- logs: ?(
53
- item.logs->Option.map(logs =>
54
- logs->Array.map((log): Envio.svmLog => {kind: log.kind, message: log.message})
70
+ let hasSelection = name => fieldSelection.instructionFields->Utils.Set.has(name)
71
+ let discriminator = switch eventConfig.discriminator {
72
+ | Some(d) => d
73
+ | None => item.data
74
+ }
75
+ let out = Dict.make()
76
+ out->setField("programName", programName)
77
+ out->setField("instructionName", instructionName)
78
+ out->setField("discriminator", discriminator)
79
+ if hasSelection("programId") {
80
+ out->setField("programId", item.programId->SvmTypes.Pubkey.fromStringUnsafe)
81
+ }
82
+ if hasSelection("data") {
83
+ out->setField("data", item.data)
84
+ }
85
+ if hasSelection("path") {
86
+ out->setField("path", item.path)
87
+ }
88
+ if hasSelection("isInner") {
89
+ out->setField("isInner", item.isInner)
90
+ }
91
+ if hasSelection("args") {
92
+ out->setField("args", item.decoded->Option.map(parseArgs))
93
+ }
94
+ if hasSelection("accounts") {
95
+ out->setField(
96
+ "accounts",
97
+ namedAccounts(~idlNames=eventConfig.accounts, ~accountArguments=item.accounts),
55
98
  )
56
- ),
99
+ }
100
+ if hasSelection("accountArguments") {
101
+ out->setField("accountArguments", item.accounts->SvmTypes.Pubkey.fromStringsUnsafe)
102
+ }
103
+ if fieldSelection.logFields->Utils.Set.size > 0 {
104
+ out->setField(
105
+ "logs",
106
+ item.logs
107
+ ->Option.getOr([])
108
+ ->Array.map(log => selectedLog(log, ~logFields=fieldSelection.logFields)),
109
+ )
110
+ }
111
+ out->(Utils.magic: dict<unknown> => Envio.svmInstruction)
57
112
  }
58
113
 
59
114
  let make = (
@@ -144,18 +199,20 @@ let make = (
144
199
  item,
145
200
  ~programName=eventConfig.contractName,
146
201
  ~instructionName=eventConfig.name,
202
+ ~eventConfig,
203
+ ~fieldSelection=onEventRegistration.fieldSelection,
147
204
  )
148
205
  Internal.Event({
149
206
  onEventRegistration,
150
207
  chainId,
151
208
  blockNumber: item.slot,
152
- // A slot orders by `(transactionIndex, instructionAddress)` — the
209
+ // A slot orders by `(transactionIndex, path)` — the
153
210
  // transaction, then the instruction's position in its CPI tree. Both
154
211
  // ride the item so the buffer comparator can order on the pair
155
212
  // directly; no single integer can hold it (Solana allows a CPI depth
156
213
  // of 5, which needs more bits than a JS integer is exact to).
157
214
  logIndex: item.transactionIndex,
158
- orderPath: item.instructionAddress,
215
+ orderPath: item.path,
159
216
  // The parent transaction is materialised from the store at batch prep.
160
217
  transactionIndex: item.transactionIndex,
161
218
  payload: payload->(Utils.magic: Envio.svmInstruction => Internal.eventPayload),
@@ -182,7 +239,11 @@ let make = (
182
239
  }
183
240
  }
184
241
 
185
- let getBlockHashes = HyperSync.makeGetBlockHashes(~query=client.getBlockHashes)
242
+ // Called through the client rather than passed as a value: the client is a
243
+ // napi class, so a detached method reference loses the instance it belongs to.
244
+ let getBlockHashes = HyperSync.makeGetBlockHashes(
245
+ ~query=(~blockNumbers) => client.getBlockHashes(~blockNumbers),
246
+ )
186
247
 
187
248
  {
188
249
  name,
@@ -8,46 +8,83 @@ import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js
8
8
  import * as SvmHyperSyncClient from "./SvmHyperSyncClient.res.mjs";
9
9
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
10
10
 
11
- function parseDecoded(d) {
12
- let args;
11
+ function parseArgs(d) {
13
12
  try {
14
- args = JSON.parse(d.argsJson);
13
+ return JSON.parse(d.argsJson);
15
14
  } catch (exn) {
16
- args = {};
15
+ return {};
17
16
  }
18
- let accounts;
19
- try {
20
- accounts = JSON.parse(d.accountsJson);
21
- } catch (exn$1) {
22
- accounts = {};
17
+ }
18
+
19
+ function namedAccounts(idlNames, accountArguments) {
20
+ let out = {};
21
+ idlNames.forEach((name, i) => {
22
+ let address = accountArguments[i];
23
+ if (address !== undefined) {
24
+ out[name] = {
25
+ address: address,
26
+ accountName: name,
27
+ instructionAccountIndex: i
28
+ };
29
+ return;
30
+ }
31
+ });
32
+ return out;
33
+ }
34
+
35
+ function selectedLog(log, logFields) {
36
+ let out = {};
37
+ if (logFields.has("kind")) {
38
+ let kind = log.kind;
39
+ if (kind !== undefined) {
40
+ out["kind"] = kind;
41
+ }
23
42
  }
24
- return {
25
- name: d.name,
26
- args: args,
27
- accounts: accounts,
28
- extraAccounts: d.extraAccounts
29
- };
43
+ if (logFields.has("message")) {
44
+ let message = log.message;
45
+ if (message !== undefined) {
46
+ out["message"] = message;
47
+ }
48
+ }
49
+ return out;
30
50
  }
31
51
 
32
- function toSvmInstruction(item, programName, instructionName) {
33
- return {
34
- programName: programName,
35
- instructionName: instructionName,
36
- programId: item.programId,
37
- data: item.data,
38
- accounts: item.accounts,
39
- instructionAddress: item.instructionAddress,
40
- isInner: item.isInner,
41
- d1: item.d1,
42
- d2: item.d2,
43
- d4: item.d4,
44
- d8: item.d8,
45
- params: Stdlib_Option.map(item.decoded, parseDecoded),
46
- logs: Stdlib_Option.map(item.logs, logs => logs.map(log => ({
47
- kind: log.kind,
48
- message: log.message
49
- })))
50
- };
52
+ function setField(out, name, value) {
53
+ out[name] = value;
54
+ }
55
+
56
+ function toSvmInstruction(item, programName, instructionName, eventConfig, fieldSelection) {
57
+ let d = eventConfig.discriminator;
58
+ let discriminator = d !== undefined ? d : item.data;
59
+ let out = {};
60
+ setField(out, "programName", programName);
61
+ setField(out, "instructionName", instructionName);
62
+ setField(out, "discriminator", discriminator);
63
+ if (fieldSelection.instructionFields.has("programId")) {
64
+ setField(out, "programId", item.programId);
65
+ }
66
+ if (fieldSelection.instructionFields.has("data")) {
67
+ setField(out, "data", item.data);
68
+ }
69
+ if (fieldSelection.instructionFields.has("path")) {
70
+ setField(out, "path", item.path);
71
+ }
72
+ if (fieldSelection.instructionFields.has("isInner")) {
73
+ setField(out, "isInner", item.isInner);
74
+ }
75
+ if (fieldSelection.instructionFields.has("args")) {
76
+ setField(out, "args", Stdlib_Option.map(item.decoded, parseArgs));
77
+ }
78
+ if (fieldSelection.instructionFields.has("accounts")) {
79
+ setField(out, "accounts", namedAccounts(eventConfig.accounts, item.accounts));
80
+ }
81
+ if (fieldSelection.instructionFields.has("accountArguments")) {
82
+ setField(out, "accountArguments", item.accounts);
83
+ }
84
+ if (fieldSelection.logFields.size > 0) {
85
+ setField(out, "logs", Stdlib_Option.getOr(item.logs, []).map(log => selectedLog(log, fieldSelection.logFields)));
86
+ }
87
+ return out;
51
88
  }
52
89
 
53
90
  function make(param) {
@@ -97,14 +134,14 @@ function make(param) {
97
134
  let parsedQueueItems = resp.items.map(item => {
98
135
  let onEventRegistration = onEventRegistrations[item.onEventRegistrationIndex];
99
136
  let eventConfig = onEventRegistration.eventConfig;
100
- let payload = toSvmInstruction(item, eventConfig.contractName, eventConfig.name);
137
+ let payload = toSvmInstruction(item, eventConfig.contractName, eventConfig.name, eventConfig, onEventRegistration.fieldSelection);
101
138
  return {
102
139
  kind: 0,
103
140
  onEventRegistration: onEventRegistration,
104
141
  chainId: chainId,
105
142
  blockNumber: item.slot,
106
143
  logIndex: item.transactionIndex,
107
- orderPath: item.instructionAddress,
144
+ orderPath: item.path,
108
145
  transactionIndex: item.transactionIndex,
109
146
  payload: payload
110
147
  };
@@ -127,7 +164,7 @@ function make(param) {
127
164
  requestStats: requestStats
128
165
  };
129
166
  };
130
- let getBlockHashes = HyperSync.makeGetBlockHashes(client.getBlockHashes);
167
+ let getBlockHashes = HyperSync.makeGetBlockHashes(blockNumbers => client.getBlockHashes(blockNumbers));
131
168
  return {
132
169
  name: "SvmHyperSync",
133
170
  sourceFor: "Sync",
@@ -152,7 +189,10 @@ function make(param) {
152
189
  }
153
190
 
154
191
  export {
155
- parseDecoded,
192
+ parseArgs,
193
+ namedAccounts,
194
+ selectedLog,
195
+ setField,
156
196
  toSvmInstruction,
157
197
  make,
158
198
  }
@@ -31,6 +31,44 @@ let fieldCodes = FieldMask.fieldCodes
31
31
  // Drain another store (a fetch-response page) into this one.
32
32
  @send external merge: (t, t) => unit = "merge"
33
33
 
34
+ type svmTxInput = {
35
+ slot: int,
36
+ transactionIndex: int,
37
+ signature?: string,
38
+ allSignatures?: array<string>,
39
+ feePayer?: string,
40
+ success?: bool,
41
+ err?: string,
42
+ fee?: bigint,
43
+ computeUnitsConsumed?: bigint,
44
+ accountKeys?: array<string>,
45
+ recentBlockhash?: string,
46
+ version?: string,
47
+ }
48
+
49
+ type svmActivityInput = {
50
+ slot: int,
51
+ transactionIndex: int,
52
+ account: string,
53
+ accountIndex?: int,
54
+ isSigner?: bool,
55
+ isWritable?: bool,
56
+ preBalance?: bigint,
57
+ postBalance?: bigint,
58
+ mint?: string,
59
+ owner?: string,
60
+ decimals?: int,
61
+ preAmount?: bigint,
62
+ postAmount?: bigint,
63
+ }
64
+
65
+ @send
66
+ external fromJsSvm: (Core.transactionStoreCtor, array<svmTxInput>, array<svmActivityInput>) => t =
67
+ "fromJsSvm"
68
+
69
+ let fromSvmJs = (transactions: array<svmTxInput>, activities: array<svmActivityInput>): t =>
70
+ Core.getAddon().transactionStore->fromJsSvm(transactions, activities)
71
+
34
72
  // Bulk-materialise transactions off the JS thread, one row per
35
73
  // (blockNumbers[i], transactionIndices[i]) key, decoding only the fields set in
36
74
  // that row's own masks[i]. Result is aligned with the input.
@@ -15,6 +15,10 @@ function make(ecosystem, shouldChecksum) {
15
15
  }
16
16
  }
17
17
 
18
+ function fromSvmJs(transactions, activities) {
19
+ return Core.getAddon().TransactionStore.fromJsSvm(transactions, activities);
20
+ }
21
+
18
22
  let makeMaskFn = FieldMask.makeMaskFn;
19
23
 
20
24
  let orMask = FieldMask.orMask;
@@ -26,5 +30,6 @@ export {
26
30
  makeMaskFn,
27
31
  orMask,
28
32
  fieldCodes,
33
+ fromSvmJs,
29
34
  }
30
35
  /* Core Not a pure module */
package/svm.schema.json CHANGED
@@ -212,6 +212,10 @@
212
212
  "Chain": {
213
213
  "type": "object",
214
214
  "properties": {
215
+ "id": {
216
+ "description": "Identifies the Svm cluster: the label \"solana\" (7565164) or \"solana-devnet\" (7565165), or an explicit number of your choosing for other clusters (up to Number.MAX_SAFE_INTEGER). Svm has no native numeric chain id, so the label ids are assigned by Envio and match the ids used for HyperSync usage attribution.",
217
+ "$ref": "#/$defs/ChainId"
218
+ },
215
219
  "skip": {
216
220
  "description": "Excludes the chain from indexing and migrations. Code generation is unaffected. For testing, prefer using a test framework instead.",
217
221
  "type": [
@@ -264,9 +268,31 @@
264
268
  },
265
269
  "additionalProperties": false,
266
270
  "required": [
271
+ "id",
267
272
  "start_block"
268
273
  ]
269
274
  },
275
+ "ChainId": {
276
+ "description": "The chain id of an Svm chain: either a known cluster label or an\nexplicit number (for private chains, rollups, or custom clusters).",
277
+ "anyOf": [
278
+ {
279
+ "$ref": "#/$defs/ChainLabel"
280
+ },
281
+ {
282
+ "type": "integer",
283
+ "format": "uint64",
284
+ "minimum": 0,
285
+ "maximum": 9007199254740991
286
+ }
287
+ ]
288
+ },
289
+ "ChainLabel": {
290
+ "type": "string",
291
+ "enum": [
292
+ "solana",
293
+ "solana-devnet"
294
+ ]
295
+ },
270
296
  "Experimental": {
271
297
  "type": "object",
272
298
  "properties": {
@@ -373,19 +399,8 @@
373
399
  }
374
400
  ]
375
401
  },
376
- "field_selection": {
377
- "description": "Select which additional data to fetch for each matched instruction. Each key accepts `true` (include all fields) or a list of field names (per-field selection, not yet supported). When absent, only the instruction itself is included.",
378
- "anyOf": [
379
- {
380
- "$ref": "#/$defs/SvmFieldSelection"
381
- },
382
- {
383
- "type": "null"
384
- }
385
- ]
386
- },
387
402
  "accounts": {
388
- "description": "Optional positional account names. The Nth entry names account slot N on the dispatched instruction; surfaces as `event.instruction.decoded.accounts.<name>`. Accounts beyond the named list become `extra_accounts`.",
403
+ "description": "Optional positional account names. The Nth entry names account slot N on the dispatched instruction; surfaces as `instruction.accounts.<name>` when `fields.instruction` includes `accounts`.",
389
404
  "type": [
390
405
  "array",
391
406
  "null"
@@ -466,72 +481,6 @@
466
481
  "any_of"
467
482
  ]
468
483
  },
469
- "SvmFieldSelection": {
470
- "type": "object",
471
- "properties": {
472
- "transaction_fields": {
473
- "description": "Parent-transaction fields to include on each matched instruction, as a list of field names. Omit (or pass an empty list) to include no transaction.",
474
- "type": [
475
- "array",
476
- "null"
477
- ],
478
- "items": {
479
- "$ref": "#/$defs/SvmTransactionField"
480
- }
481
- },
482
- "block_fields": {
483
- "description": "Block fields to include on each matched instruction's `block`, as a list of field names. `slot`/`time`/`hash` are always included; this adds `height`/`parentSlot`/`parentHash`.",
484
- "type": [
485
- "array",
486
- "null"
487
- ],
488
- "items": {
489
- "$ref": "#/$defs/SvmBlockField"
490
- }
491
- },
492
- "log_fields": {
493
- "description": "Set to `true` to include program logs scoped to each matched instruction.",
494
- "type": [
495
- "boolean",
496
- "null"
497
- ]
498
- },
499
- "token_balance_fields": {
500
- "description": "Set to `true` to include SPL Token / Token-2022 balance snapshots for the parent transaction, exposed as `transaction.tokenBalances`. Independent of `transaction_fields`.",
501
- "type": [
502
- "boolean",
503
- "null"
504
- ]
505
- }
506
- },
507
- "additionalProperties": false
508
- },
509
- "SvmTransactionField": {
510
- "description": "Selectable parent-transaction field names (camelCase), matching the\npublic `svmTransaction` shape. `tokenBalances` is selected via the\nseparate `token_balance_fields` toggle, so it isn't listed here.",
511
- "type": "string",
512
- "enum": [
513
- "transactionIndex",
514
- "signature",
515
- "feePayer",
516
- "success",
517
- "err",
518
- "fee",
519
- "computeUnitsConsumed",
520
- "accountKeys",
521
- "recentBlockhash",
522
- "version",
523
- "allSignatures"
524
- ]
525
- },
526
- "SvmBlockField": {
527
- "description": "Selectable block field names (camelCase), matching the public\n`instruction.block` shape. `slot`/`time`/`hash` are always included,\nso everything here is opt-in on top of that trio.",
528
- "type": "string",
529
- "enum": [
530
- "height",
531
- "parentSlot",
532
- "parentHash"
533
- ]
534
- },
535
484
  "ArgDef": {
536
485
  "description": "One named argument of an instruction. Mirrors\n`hypersync_client_solana::decode::NamedField`.",
537
486
  "type": "object",