@sentio/sdk 3.2.0 → 3.3.0-rc.2

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.
@@ -1,11 +1,13 @@
1
- import { ProcessResult } from '@sentio/protos'
1
+ import { Data_SolBlock, Data_SolInstruction, HandleInterval, ProcessResult } from '@sentio/protos'
2
2
  import { SolanaContext } from './solana-context.js'
3
3
  import { Instruction } from '@coral-xyz/anchor'
4
- import { SolanaBindOptions } from './solana-options.js'
4
+ import { SolanaBindOptions, SolanaFetchConfig } from './solana-options.js'
5
5
  import { ListStateStorage } from '@sentio/runtime'
6
- import { Labels } from '../core/index.js'
6
+ import { ALL_ADDRESS, Labels, PromiseOrVoid } from '../core/index.js'
7
7
  import { SolanaChainId } from '@sentio/chain'
8
8
  import { HandlerOptions } from '../core/handler-options.js'
9
+ import { getHandlerName, proxyProcessor } from '../utils/metrics.js'
10
+ import { TransactionResponse, BlockResponse } from '@solana/web3.js'
9
11
 
10
12
  type IndexConfigure = {
11
13
  startSlot: bigint
@@ -20,7 +22,16 @@ export type SolanaInstructionHandler = (instruction: Instruction, ctx: SolanaCon
20
22
 
21
23
  export interface InstructionHandlerEntry {
22
24
  handler: SolanaInstructionHandler
23
- handlerOptions?: HandlerOptions<object, Instruction>
25
+ handlerOptions?: HandlerOptions<SolanaFetchConfig, Instruction>
26
+ }
27
+
28
+ export type SolanaBlockHandler<T> = (block: T, ctx: SolanaContext) => PromiseOrVoid
29
+
30
+ export interface SolanaBlockHandlerEntry<T> {
31
+ handler: SolanaBlockHandler<T>
32
+ timeIntervalInMinutes?: HandleInterval
33
+ slotInterval?: HandleInterval
34
+ handlerName: string
24
35
  }
25
36
 
26
37
  export class SolanaProcessorState extends ListStateStorage<SolanaBaseProcessor> {
@@ -29,6 +40,7 @@ export class SolanaProcessorState extends ListStateStorage<SolanaBaseProcessor>
29
40
 
30
41
  export class SolanaBaseProcessor {
31
42
  public instructionHandlerMap: Map<string, InstructionHandlerEntry> = new Map()
43
+ public blockHandlers: SolanaBlockHandlerEntry<any>[] = []
32
44
  address: string
33
45
  endpoint: string
34
46
  contractName: string
@@ -70,12 +82,49 @@ export class SolanaBaseProcessor {
70
82
  public onInstruction(
71
83
  instructionName: string,
72
84
  handler: SolanaInstructionHandler,
73
- handlerOptions?: HandlerOptions<object, Instruction>
85
+ handlerOptions?: HandlerOptions<SolanaFetchConfig, Instruction>
74
86
  ) {
75
87
  this.instructionHandlerMap.set(instructionName, { handler, handlerOptions })
76
88
  return this
77
89
  }
78
90
 
91
+ public onTimeInterval(
92
+ handler: SolanaBlockHandler<Data_SolBlock>,
93
+ timeIntervalInMinutes = 60,
94
+ backfillTimeIntervalInMinutes = 240
95
+ ): this {
96
+ return this.onInterval(
97
+ handler,
98
+ { recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },
99
+ undefined
100
+ )
101
+ }
102
+
103
+ public onBlockInterval(
104
+ handler: SolanaBlockHandler<Data_SolBlock>,
105
+ blockInterval = 1000,
106
+ backfillBlockInterval = 4000
107
+ ): this {
108
+ return this.onInterval(handler, undefined, {
109
+ recentInterval: blockInterval,
110
+ backfillInterval: backfillBlockInterval
111
+ })
112
+ }
113
+
114
+ public onInterval<T>(
115
+ handler: SolanaBlockHandler<T>,
116
+ timeInterval: HandleInterval | undefined,
117
+ slotInterval: HandleInterval | undefined
118
+ ): this {
119
+ this.blockHandlers.push({
120
+ handler: handler,
121
+ timeIntervalInMinutes: timeInterval,
122
+ slotInterval: slotInterval,
123
+ handlerName: getHandlerName()
124
+ })
125
+ return this
126
+ }
127
+
79
128
  public getParsedInstruction(ins: string | { type: string; info: any }): Instruction | null {
80
129
  if (ins) {
81
130
  if ((ins as { type: string; info: any }).info) {
@@ -96,13 +145,35 @@ export class SolanaBaseProcessor {
96
145
  parsedInstruction: Instruction,
97
146
  accounts: string[],
98
147
  handlerEntry: InstructionHandlerEntry,
99
- slot: bigint
148
+ data: Data_SolInstruction
100
149
  ): Promise<ProcessResult> {
101
- const ctx = new SolanaContext(this.contractName, this.network, this.address, slot, this.baseLabels)
150
+ let transaction: TransactionResponse | undefined = undefined
151
+ if (data.rawTransaction) {
152
+ transaction = JSON.parse(data.rawTransaction) as TransactionResponse
153
+ }
154
+
155
+ const ctx = new SolanaContext(
156
+ this.contractName,
157
+ this.network,
158
+ this.address,
159
+ data.slot,
160
+ this.baseLabels,
161
+ transaction
162
+ )
102
163
  await handlerEntry.handler(parsedInstruction, ctx, accounts)
103
164
  return ctx.stopAndGetResult()
104
165
  }
105
166
 
167
+ public async handleBlock(
168
+ rawBlock: Data_SolBlock,
169
+ handlerEntry: SolanaBlockHandlerEntry<BlockResponse>
170
+ ): Promise<ProcessResult> {
171
+ const ctx = new SolanaContext(this.contractName, this.network, this.address, rawBlock.slot, this.baseLabels)
172
+ const block = JSON.parse(rawBlock.rawBlock) as BlockResponse
173
+ await handlerEntry.handler(block, ctx)
174
+ return ctx.stopAndGetResult()
175
+ }
176
+
106
177
  public async getPartitionKey(
107
178
  parsedInstruction: Instruction,
108
179
  handlerEntry: InstructionHandlerEntry
@@ -125,3 +196,14 @@ export class SolanaBaseProcessor {
125
196
  return this
126
197
  }
127
198
  }
199
+
200
+ export class SolanaGlobalProcessor extends SolanaBaseProcessor {
201
+ static bind(options: Omit<SolanaBindOptions, 'address'>): SolanaGlobalProcessor {
202
+ return new SolanaGlobalProcessor({ ...options, address: ALL_ADDRESS })
203
+ }
204
+
205
+ constructor(options: SolanaBindOptions) {
206
+ super(options)
207
+ return proxyProcessor(this)
208
+ }
209
+ }