@sentio/sdk 1.37.0-rc.1 → 1.37.0-rc.3

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 (136) hide show
  1. package/lib/builtin/index.d.ts +0 -2
  2. package/lib/builtin/index.js +1 -3
  3. package/lib/builtin/index.js.map +1 -1
  4. package/lib/core/eth-plugin.js +12 -15
  5. package/lib/core/eth-plugin.js.map +1 -1
  6. package/lib/core/index.d.ts +0 -4
  7. package/lib/core/index.js +1 -9
  8. package/lib/core/index.js.map +1 -1
  9. package/lib/core/numberish.d.ts +1 -1
  10. package/lib/core/numberish.js +5 -13
  11. package/lib/core/numberish.js.map +1 -1
  12. package/lib/core/sui-plugin.js +1 -1
  13. package/lib/core/sui-plugin.js.map +1 -1
  14. package/lib/index.d.ts +1 -1
  15. package/lib/index.js +3 -14
  16. package/lib/index.js.map +1 -1
  17. package/lib/service.d.ts +0 -4
  18. package/lib/service.js +40 -71
  19. package/lib/service.js.map +1 -1
  20. package/package.json +5 -15
  21. package/src/builtin/index.ts +1 -4
  22. package/src/core/eth-plugin.ts +5 -8
  23. package/src/core/index.ts +0 -5
  24. package/src/core/numberish.ts +8 -15
  25. package/src/core/sui-plugin.ts +2 -2
  26. package/src/index.ts +3 -1
  27. package/src/service.ts +13 -71
  28. package/lib/aptos/api.d.ts +0 -2
  29. package/lib/aptos/api.js +0 -14
  30. package/lib/aptos/api.js.map +0 -1
  31. package/lib/aptos/aptos-plugin.d.ts +0 -14
  32. package/lib/aptos/aptos-plugin.js +0 -185
  33. package/lib/aptos/aptos-plugin.js.map +0 -1
  34. package/lib/aptos/aptos-processor.d.ts +0 -75
  35. package/lib/aptos/aptos-processor.js +0 -193
  36. package/lib/aptos/aptos-processor.js.map +0 -1
  37. package/lib/aptos/context.d.ts +0 -22
  38. package/lib/aptos/context.js +0 -65
  39. package/lib/aptos/context.js.map +0 -1
  40. package/lib/aptos/index.d.ts +0 -9
  41. package/lib/aptos/index.js +0 -34
  42. package/lib/aptos/index.js.map +0 -1
  43. package/lib/aptos/models.d.ts +0 -23
  44. package/lib/aptos/models.js +0 -3
  45. package/lib/aptos/models.js.map +0 -1
  46. package/lib/aptos/network.d.ts +0 -14
  47. package/lib/aptos/network.js +0 -46
  48. package/lib/aptos/network.js.map +0 -1
  49. package/lib/aptos/runtime.d.ts +0 -2
  50. package/lib/aptos/runtime.js +0 -16
  51. package/lib/aptos/runtime.js.map +0 -1
  52. package/lib/aptos/type-registry.d.ts +0 -21
  53. package/lib/aptos/type-registry.js +0 -156
  54. package/lib/aptos/type-registry.js.map +0 -1
  55. package/lib/aptos/types.d.ts +0 -9
  56. package/lib/aptos/types.js +0 -135
  57. package/lib/aptos/types.js.map +0 -1
  58. package/lib/aptos/types.test.d.ts +0 -1
  59. package/lib/aptos/types.test.js.map +0 -1
  60. package/lib/aptos/utils.d.ts +0 -7
  61. package/lib/aptos/utils.js +0 -27
  62. package/lib/aptos/utils.js.map +0 -1
  63. package/lib/aptos-codegen/codegen.d.ts +0 -23
  64. package/lib/aptos-codegen/codegen.js +0 -364
  65. package/lib/aptos-codegen/codegen.js.map +0 -1
  66. package/lib/aptos-codegen/typegen.d.ts +0 -17
  67. package/lib/aptos-codegen/typegen.js +0 -142
  68. package/lib/aptos-codegen/typegen.js.map +0 -1
  69. package/lib/builtin/aptos/0x1.d.ts +0 -1913
  70. package/lib/builtin/aptos/0x1.js +0 -2748
  71. package/lib/builtin/aptos/0x1.js.map +0 -1
  72. package/lib/builtin/aptos/0x3.d.ts +0 -482
  73. package/lib/builtin/aptos/0x3.js +0 -526
  74. package/lib/builtin/aptos/0x3.js.map +0 -1
  75. package/lib/builtin/aptos/index.d.ts +0 -2
  76. package/lib/builtin/aptos/index.js +0 -29
  77. package/lib/builtin/aptos/index.js.map +0 -1
  78. package/lib/builtin/solana/index.d.ts +0 -2
  79. package/lib/builtin/solana/index.js +0 -19
  80. package/lib/builtin/solana/index.js.map +0 -1
  81. package/lib/builtin/solana/spl-token-processor.d.ts +0 -39
  82. package/lib/builtin/solana/spl-token-processor.js +0 -258
  83. package/lib/builtin/solana/spl-token-processor.js.map +0 -1
  84. package/lib/builtin/solana/types.d.ts +0 -427
  85. package/lib/builtin/solana/types.js +0 -203
  86. package/lib/builtin/solana/types.js.map +0 -1
  87. package/lib/builtin/solana/wormhole-processor.d.ts +0 -16
  88. package/lib/builtin/solana/wormhole-processor.js +0 -195
  89. package/lib/builtin/solana/wormhole-processor.js.map +0 -1
  90. package/lib/core/solana-context.d.ts +0 -11
  91. package/lib/core/solana-context.js +0 -34
  92. package/lib/core/solana-context.js.map +0 -1
  93. package/lib/core/solana-options.d.ts +0 -8
  94. package/lib/core/solana-options.js +0 -13
  95. package/lib/core/solana-options.js.map +0 -1
  96. package/lib/core/solana-plugin.d.ts +0 -9
  97. package/lib/core/solana-plugin.js +0 -79
  98. package/lib/core/solana-plugin.js.map +0 -1
  99. package/lib/core/solana-processor.d.ts +0 -40
  100. package/lib/core/solana-processor.js +0 -80
  101. package/lib/core/solana-processor.js.map +0 -1
  102. package/lib/solana-codegen/codegen.d.ts +0 -1
  103. package/lib/solana-codegen/codegen.js +0 -113
  104. package/lib/solana-codegen/codegen.js.map +0 -1
  105. package/lib/test-abi-code-gen.d.ts +0 -2
  106. package/lib/test-abi-code-gen.js +0 -20
  107. package/lib/test-abi-code-gen.js.map +0 -1
  108. package/src/abis/aptos/0x1.json +0 -9205
  109. package/src/abis/aptos/0x3.json +0 -1515
  110. package/src/aptos/api.ts +0 -11
  111. package/src/aptos/aptos-plugin.ts +0 -216
  112. package/src/aptos/aptos-processor.ts +0 -305
  113. package/src/aptos/context.ts +0 -74
  114. package/src/aptos/index.ts +0 -10
  115. package/src/aptos/models.ts +0 -34
  116. package/src/aptos/network.ts +0 -42
  117. package/src/aptos/runtime.ts +0 -13
  118. package/src/aptos/type-registry.ts +0 -187
  119. package/src/aptos/types.ts +0 -149
  120. package/src/aptos/utils.ts +0 -26
  121. package/src/aptos-codegen/codegen.ts +0 -428
  122. package/src/aptos-codegen/tsconfig.json +0 -9
  123. package/src/aptos-codegen/typegen.ts +0 -153
  124. package/src/builtin/aptos/0x1.ts +0 -4210
  125. package/src/builtin/aptos/0x3.ts +0 -888
  126. package/src/builtin/aptos/index.ts +0 -2
  127. package/src/builtin/solana/index.ts +0 -2
  128. package/src/builtin/solana/spl-token-processor.ts +0 -298
  129. package/src/builtin/solana/types.ts +0 -279
  130. package/src/builtin/solana/wormhole-processor.ts +0 -178
  131. package/src/core/solana-context.ts +0 -34
  132. package/src/core/solana-options.ts +0 -8
  133. package/src/core/solana-plugin.ts +0 -97
  134. package/src/core/solana-processor.ts +0 -102
  135. package/src/solana-codegen/codegen.ts +0 -116
  136. package/src/test-abi-code-gen.ts +0 -16
@@ -1,178 +0,0 @@
1
- import { SolanaBaseProcessor, SolanaContext, SolanaBindOptions } from '@sentio/sdk'
2
- import { Instruction } from '@project-serum/anchor'
3
- import * as borsh from "@coral-xyz/borsh";
4
- import bs58 from 'bs58'
5
-
6
- // https://github.com/certusone/wormhole/blob/8818d4b8f0471095dd48fa6f5da9c315cbfc9b52/solana/modules/token_bridge/program/src/lib.rs#L100
7
- enum TokenBridgeInstruction {
8
- Initialize = 0,
9
- AttestToken,
10
- CompleteNative, // no args
11
- CompleteWrapped, // no args
12
- TransferWrapped,
13
- TransferNative,
14
- RegisterChain, // no args
15
- CreateWrapped, // no args
16
- UpgradeContract, // no args
17
- CompleteNativeWithPayload, // no args
18
- CompleteWrappedWithPayload, // no args
19
- TransferWrappedWithPayload,
20
- TransferNativeWithPayload,
21
- }
22
-
23
- export class TokenBridgeProcessor extends SolanaBaseProcessor {
24
-
25
- private readonly transferDataValues = [
26
- borsh.u32('nonce'),
27
- borsh.u64('amount'),
28
- borsh.u64('fee'),
29
- borsh.array(borsh.u8(undefined), 32, 'target_address'),
30
- borsh.u16('target_chain')
31
- ]
32
- // https://github.com/certusone/wormhole/blob/8818d4b8f0471095dd48fa6f5da9c315cbfc9b52/solana/modules/token_bridge/program/src/api/transfer_payload.rs#L170
33
- private readonly transferDataWithPayloadValues = [
34
- borsh.u32('nonce'),
35
- borsh.u64('amount'),
36
- borsh.array(borsh.u8(undefined), 32, 'target_address'),
37
- borsh.u16('target_chain'),
38
- borsh.vecU8('payload'),
39
- borsh.option(borsh.publicKey(undefined), 'cpi_program_id')
40
- ]
41
- private readonly attestTokenValues = [
42
- borsh.u32('nonce')
43
- ]
44
- private readonly initializeDataValues = [
45
- borsh.publicKey('bridge')
46
- ]
47
-
48
- static bind(options: SolanaBindOptions): TokenBridgeProcessor {
49
- if (options && !options.name) {
50
- options.name = 'WormholeTokenBridge'
51
- }
52
- return new TokenBridgeProcessor(options)
53
- }
54
-
55
- decodeInstruction: (rawInstruction: string) => Instruction | null = (ins) => {
56
- const u8Arr = bs58.decode(ins)
57
- if (u8Arr.length === 0) {
58
- return null
59
- }
60
-
61
- let data: any
62
- // wormhole pass intruction's enum value as its first instrcution data
63
- switch (u8Arr[0]) {
64
- case TokenBridgeInstruction.Initialize:
65
- return {
66
- data: '',
67
- name: TokenBridgeInstruction[TokenBridgeInstruction.Initialize]
68
- }
69
- case TokenBridgeInstruction.TransferNative:
70
- // struct is defined at: https://github.com/certusone/wormhole/blob/8818d4b8f0471095dd48fa6f5da9c315cbfc9b52/solana/modules/token_bridge/program/src/api/transfer.rs#L295
71
- data = borsh.struct(this.transferDataValues, 'TransferNativeData').decode(Buffer.from(u8Arr.slice(1)))
72
- return {
73
- data,
74
- name: TokenBridgeInstruction[TokenBridgeInstruction.TransferNative]
75
- }
76
- case TokenBridgeInstruction.TransferWrapped:
77
- // stuct is defined at: https://github.com/certusone/wormhole/blob/8818d4b8f0471095dd48fa6f5da9c315cbfc9b52/solana/modules/token_bridge/program/src/api/transfer.rs#L295
78
- data = borsh.struct(this.transferDataValues, 'TransferWrappedData').decode(Buffer.from(u8Arr.slice(1)))
79
- return {
80
- data,
81
- name: TokenBridgeInstruction[TokenBridgeInstruction.TransferWrapped]
82
- }
83
- case TokenBridgeInstruction.TransferNativeWithPayload:
84
- data = borsh.struct(this.transferDataWithPayloadValues, 'TransferNativeWithPayloadData').decode(Buffer.from(u8Arr.slice(1)))
85
- return {
86
- data,
87
- name: TokenBridgeInstruction[TokenBridgeInstruction.TransferNativeWithPayload]
88
- }
89
- case TokenBridgeInstruction.TransferWrappedWithPayload:
90
- data = borsh.struct(this.transferDataWithPayloadValues, 'TransferWrappedWithPayloadData').decode(Buffer.from(u8Arr.slice(1)))
91
- return {
92
- data,
93
- name: TokenBridgeInstruction[TokenBridgeInstruction.TransferWrappedWithPayload]
94
- }
95
- case TokenBridgeInstruction.Initialize:
96
- data = borsh.struct(this.initializeDataValues, 'InitializeData').decode(Buffer.from(u8Arr.slice(1)))
97
- return {
98
- data,
99
- name: TokenBridgeInstruction[TokenBridgeInstruction.Initialize]
100
- }
101
- case TokenBridgeInstruction.AttestToken:
102
- data = borsh.struct(this.attestTokenValues, 'AttestTokenData').decode(Buffer.from(u8Arr.slice(1)))
103
- return {
104
- data,
105
- name: TokenBridgeInstruction[TokenBridgeInstruction.AttestToken]
106
- }
107
- case TokenBridgeInstruction.CompleteNative:
108
- case TokenBridgeInstruction.CompleteNativeWithPayload:
109
- case TokenBridgeInstruction.CompleteWrapped:
110
- case TokenBridgeInstruction.CompleteWrappedWithPayload:
111
- case TokenBridgeInstruction.CreateWrapped:
112
- case TokenBridgeInstruction.UpgradeContract:
113
- case TokenBridgeInstruction.RegisterChain:
114
- return {
115
- data: '',
116
- name: TokenBridgeInstruction[u8Arr[0]]
117
- }
118
-
119
- default:
120
- return null
121
- }
122
- }
123
-
124
- onInitialize(handler: (args: any, accounts: string[], ctx: SolanaContext) => void): TokenBridgeProcessor {
125
- this.onInstruction('Initialize', (ins: Instruction, ctx, accounts: string[]) => {
126
- if (ins) {
127
- handler(ins.data, accounts, ctx)
128
- }
129
- })
130
- return this
131
- }
132
-
133
- onAttestToken(handler: (args: any, accounts: string[], ctx: SolanaContext) => void): TokenBridgeProcessor {
134
- this.onInstruction('AttestToken', (ins: Instruction, ctx, accounts: string[]) => {
135
- if (ins) {
136
- handler(ins.data, accounts, ctx)
137
- }
138
- })
139
- return this
140
- }
141
-
142
-
143
- onTransferWrapped(handler: (args: any, accounts: string[], ctx: SolanaContext) => void): TokenBridgeProcessor {
144
- this.onInstruction('TransferWrapped', (ins: Instruction, ctx, accounts: string[]) => {
145
- if (ins) {
146
- handler(ins.data, accounts, ctx)
147
- }
148
- })
149
- return this
150
- }
151
-
152
- onTransferNativeWithPayload(handler: (args: any, accounts: string[], ctx: SolanaContext) => void): TokenBridgeProcessor {
153
- this.onInstruction('TransferNativeWithPayload', (ins: Instruction, ctx, accounts: string[]) => {
154
- if (ins) {
155
- handler(ins.data, accounts, ctx)
156
- }
157
- })
158
- return this
159
- }
160
-
161
- onTransferWrappedWithPaylod(handler: (args: any, accounts: string[], ctx: SolanaContext) => void): TokenBridgeProcessor {
162
- this.onInstruction('TransferWrappedWithPaylod', (ins: Instruction, ctx, accounts: string[]) => {
163
- if (ins) {
164
- handler(ins.data, accounts, ctx)
165
- }
166
- })
167
- return this
168
- }
169
-
170
- onTransferNative(handler: (args: any, accounts: string[], ctx: SolanaContext) => void): TokenBridgeProcessor {
171
- this.onInstruction('TransferNative', (ins: Instruction, ctx, accounts: string[]) => {
172
- if (ins) {
173
- handler(ins.data, accounts, ctx)
174
- }
175
- })
176
- return this
177
- }
178
- }
@@ -1,34 +0,0 @@
1
- import { BaseContext } from './base-context'
2
- import { Labels } from './metadata'
3
- import { RecordMetaData } from '@sentio/sdk'
4
- import { CHAIN_IDS } from '../utils/chain'
5
- import { normalizeLabels } from './meter'
6
-
7
- export class SolanaContext extends BaseContext {
8
- network: string
9
- address: string
10
- programName: string
11
- blockNumber: bigint
12
-
13
- constructor(programName: string, network: string, address: string, slot: bigint) {
14
- super()
15
- this.network = network || CHAIN_IDS.SOLANA_MAINNET
16
- this.programName = programName
17
- this.address = address
18
- this.blockNumber = slot
19
- }
20
-
21
- getMetaData(name: string, labels: Labels): RecordMetaData {
22
- return {
23
- address: this.address,
24
- contractName: this.programName,
25
- blockNumber: this.blockNumber,
26
- transactionIndex: 0,
27
- transactionHash: '', // TODO add
28
- logIndex: 0,
29
- chainId: this.network,
30
- name: name,
31
- labels: normalizeLabels(labels),
32
- }
33
- }
34
- }
@@ -1,8 +0,0 @@
1
- export class SolanaBindOptions {
2
- address: string
3
- network?: string
4
- name?: string
5
- startBlock?: bigint | number
6
- endBlock?: bigint | number
7
- processInnerInstruction?: boolean
8
- }
@@ -1,97 +0,0 @@
1
- import { Plugin, PluginManager } from '@sentio/base'
2
- import {
3
- ContractConfig,
4
- Data_SolInstruction,
5
- DataBinding,
6
- HandlerType,
7
- ProcessConfigResponse,
8
- ProcessResult,
9
- } from '@sentio/protos'
10
- import { DEFAULT_MAX_BLOCK, mergeProcessResults, USER_PROCESSOR } from '../service'
11
-
12
- import { ServerError, Status } from 'nice-grpc'
13
-
14
- import { SolanaProcessorState } from './solana-processor'
15
- import { Instruction as SolInstruction } from '@project-serum/anchor/dist/cjs/coder/borsh/instruction'
16
-
17
- export class SolanaPlugin implements Plugin {
18
- name: string = 'SolanaPlugin'
19
-
20
- configure(config: ProcessConfigResponse): void {
21
- // Part 2, prepare solana constractors
22
- for (const solanaProcessor of SolanaProcessorState.INSTANCE.getValues()) {
23
- const contractConfig: ContractConfig = {
24
- processorType: USER_PROCESSOR,
25
- contract: {
26
- name: solanaProcessor.contractName,
27
- chainId: solanaProcessor.network,
28
- address: solanaProcessor.address,
29
- abi: '',
30
- },
31
- logConfigs: [],
32
- traceConfigs: [],
33
- intervalConfigs: [],
34
- startBlock: solanaProcessor.config.startSlot,
35
- endBlock: DEFAULT_MAX_BLOCK,
36
- instructionConfig: {
37
- innerInstruction: solanaProcessor.processInnerInstruction,
38
- parsedInstruction: solanaProcessor.fromParsedInstruction !== null,
39
- rawDataInstruction: solanaProcessor.decodeInstruction !== null,
40
- },
41
- aptosEventConfigs: [],
42
- aptosCallConfigs: [],
43
- }
44
- config.contractConfigs.push(contractConfig)
45
- }
46
- }
47
-
48
- supportedHandlers = [HandlerType.SOL_INSTRUCTION]
49
-
50
- processBinding(request: DataBinding): Promise<ProcessResult> {
51
- switch (request.handlerType) {
52
- case HandlerType.SOL_INSTRUCTION:
53
- return this.processSolInstruction(request)
54
- default:
55
- throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
56
- }
57
- }
58
-
59
- async processSolInstruction(request: DataBinding): Promise<ProcessResult> {
60
- if (!request.data) {
61
- throw new ServerError(Status.INVALID_ARGUMENT, 'instruction data cannot be empty')
62
- }
63
-
64
- const instruction = request.data.solInstruction || Data_SolInstruction.decode(request.data.raw) // JSON.parse(jsonString)
65
- const promises: Promise<ProcessResult>[] = []
66
-
67
- // Only have instruction handlers for solana processors
68
- for (const processor of SolanaProcessorState.INSTANCE.getValues()) {
69
- if (processor.address === instruction.programAccountId) {
70
- let parsedInstruction: SolInstruction | null = null
71
- if (instruction.parsed) {
72
- parsedInstruction = processor.getParsedInstruction(instruction.parsed as { type: string; info: any })
73
- } else if (instruction.instructionData) {
74
- parsedInstruction = processor.getParsedInstruction(instruction.instructionData)
75
- }
76
- if (parsedInstruction == null) {
77
- continue
78
- }
79
- const insHandler = processor.getInstructionHandler(parsedInstruction)
80
- if (insHandler == null) {
81
- continue
82
- }
83
- const res = await processor.handleInstruction(
84
- parsedInstruction,
85
- instruction.accounts,
86
- insHandler,
87
- instruction.slot
88
- )
89
-
90
- promises.push(Promise.resolve(res))
91
- }
92
- }
93
- return mergeProcessResults(await Promise.all(promises))
94
- }
95
- }
96
-
97
- PluginManager.INSTANCE.register(new SolanaPlugin())
@@ -1,102 +0,0 @@
1
- import { ProcessResult } from '@sentio/protos'
2
- import { SolanaContext } from './solana-context'
3
- import { Instruction } from '@project-serum/anchor'
4
- import { SolanaBindOptions } from './solana-options'
5
- import { ListStateStorage } from '@sentio/base'
6
- import { CHAIN_IDS } from '../utils/chain'
7
-
8
- type IndexConfigure = {
9
- startSlot: bigint
10
- endSlot?: bigint
11
- }
12
-
13
- export type SolanaInstructionHandler = (instruction: Instruction, ctx: SolanaContext, accounts?: string[]) => void
14
-
15
- export class SolanaProcessorState extends ListStateStorage<SolanaBaseProcessor> {
16
- static INSTANCE: SolanaProcessorState = new SolanaProcessorState()
17
- }
18
-
19
- export class SolanaBaseProcessor {
20
- public instructionHandlerMap: Map<string, SolanaInstructionHandler> = new Map()
21
- address: string
22
- endpoint: string
23
- contractName: string
24
- network: string
25
- processInnerInstruction: boolean
26
- config: IndexConfigure = { startSlot: 0n }
27
- decodeInstruction: (rawInstruction: string) => Instruction | null
28
- fromParsedInstruction: (instruction: { type: string; info: any }) => Instruction | null
29
-
30
- constructor(options: SolanaBindOptions) {
31
- if (options) {
32
- this.bind(options)
33
- }
34
- SolanaProcessorState.INSTANCE.addValue(this)
35
- }
36
-
37
- bind(options: SolanaBindOptions) {
38
- this.address = options.address
39
- this.contractName = options.name || ''
40
- this.processInnerInstruction = options.processInnerInstruction || false
41
- this.network = options.network || CHAIN_IDS.SOLANA_MAINNET
42
- if (options.startBlock) {
43
- this.startSlot(options.startBlock)
44
- }
45
- if (options.endBlock) {
46
- this.endBlock(options.endBlock)
47
- }
48
- this.endpoint = options.network || 'https://api.mainnet-beta.solana.com'
49
- }
50
-
51
- public onInstruction(instructionName: string, handler: SolanaInstructionHandler) {
52
- if (!this.isBind()) {
53
- throw new Error("Processor doesn't bind to an address")
54
- }
55
-
56
- this.instructionHandlerMap.set(instructionName, handler)
57
-
58
- return this
59
- }
60
-
61
- public getParsedInstruction(ins: string | { type: string; info: any }): Instruction | null {
62
- if (ins) {
63
- if ((ins as { type: string; info: any }).info) {
64
- return this.fromParsedInstruction ? this.fromParsedInstruction(ins as { type: string; info: any }) : null
65
- }
66
- if (this.decodeInstruction != null) {
67
- return this.decodeInstruction(ins as string)
68
- }
69
- }
70
- return null
71
- }
72
-
73
- public getInstructionHandler(parsedInstruction: Instruction): SolanaInstructionHandler | undefined {
74
- return this.instructionHandlerMap.get(parsedInstruction.name)
75
- }
76
-
77
- // TODO this should be a async function
78
- public handleInstruction(
79
- parsedInstruction: Instruction,
80
- accounts: string[],
81
- handler: SolanaInstructionHandler,
82
- slot: bigint
83
- ): ProcessResult {
84
- const ctx = new SolanaContext(this.contractName, this.network, this.address, slot)
85
- handler(parsedInstruction, ctx, accounts)
86
- return ctx.getProcessResult()
87
- }
88
-
89
- public isBind() {
90
- return this.address !== null
91
- }
92
-
93
- public startSlot(startSlot: bigint | number) {
94
- this.config.startSlot = BigInt(startSlot)
95
- return this
96
- }
97
-
98
- public endBlock(endBlock: bigint | number) {
99
- this.config.endSlot = BigInt(endBlock)
100
- return this
101
- }
102
- }
@@ -1,116 +0,0 @@
1
- import path from 'path'
2
- import fs from 'fs'
3
- import chalk from 'chalk'
4
-
5
- export function codeGenSolanaProcessor(abisDir: string, targetPath = path.join('src', 'types', 'solana')) {
6
- if (!fs.existsSync(abisDir)) {
7
- return
8
- }
9
-
10
- const abisFiles = fs.readdirSync(abisDir)
11
-
12
- if (abisFiles.length > 0) {
13
- console.log(chalk.green('Generated Types for Solana'))
14
- }
15
-
16
- for (const file of abisFiles) {
17
- if (path.extname(file) === '.json') {
18
- if (!fs.existsSync(targetPath)) {
19
- fs.mkdirSync(targetPath, { recursive: true })
20
- }
21
- const idlContent = fs.readFileSync(path.join(abisDir, file), 'utf-8')
22
- const idlObj = JSON.parse(idlContent)
23
- const idlName = idlObj.name
24
- const idlFile = path.join(targetPath, idlName + '.ts')
25
- fs.writeFileSync(idlFile, `export const ${idlName}_idl = ${idlContent}`)
26
- fs.writeFileSync(path.join(targetPath, `${idlName}_processor.ts`), codeGenSolanaIdlProcessor(idlObj))
27
- }
28
- }
29
- }
30
-
31
- function codeGenSolanaIdlProcessor(idlObj: any): string {
32
- const idlName = idlObj.name
33
- const idlNamePascalCase = toPascalCase(idlName)
34
- const instructions: any[] = idlObj.instructions
35
- return `import { BorshInstructionCoder, Instruction, Idl, BN } from '@project-serum/anchor'
36
- import { SolanaBaseProcessor, SolanaContext, SolanaBindOptions } from "@sentio/sdk"
37
- import { ${idlName}_idl } from "./${idlName}"
38
- import bs58 from 'bs58'
39
- import { PublicKey } from '@solana/web3.js'
40
-
41
- export class ${idlNamePascalCase}Processor extends SolanaBaseProcessor {
42
- static bind(options: SolanaBindOptions): ${idlNamePascalCase}Processor {
43
- if (options && !options.name) {
44
- options.name = '${idlNamePascalCase}'
45
- }
46
- return new ${idlNamePascalCase}Processor(options)
47
- }
48
-
49
- decodeInstruction: (rawInstruction: string) => Instruction | null = (rawInstruction) => {
50
- const instructionCoder = new BorshInstructionCoder(${idlName}_idl as Idl)
51
- const decodedIns = instructionCoder.decode(Buffer.from(bs58.decode(rawInstruction)))
52
- return decodedIns
53
- }
54
-
55
- ${instructions.map((ins) => codeGenSolanaInstruction(idlNamePascalCase, ins)).join('')}
56
- }
57
- `
58
- }
59
-
60
- function codeGenSolanaInstruction(idlName: string, ins: any): string {
61
- const instructionName = ins.name
62
- const argsTypeString = codeGenInstructionArgs(ins.args)
63
- return `
64
- on${
65
- instructionName.charAt(0).toUpperCase() + instructionName.slice(1)
66
- }(handler: (args: ${argsTypeString}, accounts: string[], ctx: SolanaContext) => void): ${idlName}Processor {
67
- this.onInstruction('${instructionName}', (ins: Instruction, ctx, accounts: string[]) => {
68
- if (ins) {
69
- handler(ins.data as ${argsTypeString}, accounts, ctx)
70
- }
71
- })
72
- return this
73
- }
74
- `
75
- }
76
-
77
- function codeGenInstructionArgs(args: { name: string; type: string }[]): string {
78
- return `{ ${args.map((arg) => arg.name + ': ' + mapType(arg.type)).join(', ')} }`
79
- }
80
-
81
- // Reference: https://github.com/coral-xyz/anchor/blob/93332766f13e86efbe77c9986722731742317ede/ts/src/program/namespace/types.ts#L104
82
- function mapType(tpe: string): string {
83
- switch (tpe) {
84
- case 'publicKey':
85
- return 'PublicKey'
86
- case 'bool':
87
- return 'boolean'
88
- case 'string':
89
- return 'string'
90
- case 'u8':
91
- case 'i8':
92
- case 'u16':
93
- case 'i16':
94
- case 'u32':
95
- case 'i32':
96
- case 'f32':
97
- case 'f64':
98
- return 'number'
99
- case 'u64':
100
- case 'i64':
101
- case 'u128':
102
- case 'i128':
103
- return 'BN'
104
- default:
105
- return 'any'
106
- }
107
- }
108
-
109
- function toPascalCase(str: string) {
110
- return `${str}`
111
- .toLowerCase()
112
- .replace(new RegExp(/[-_]+/, 'g'), ' ')
113
- .replace(new RegExp(/[^\w\s]/, 'g'), '')
114
- .replace(new RegExp(/\s+(.)(\w*)/, 'g'), ($1, $2, $3) => `${$2.toUpperCase() + $3}`)
115
- .replace(new RegExp(/\w/), (s) => s.toUpperCase())
116
- }
@@ -1,16 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import path from 'path'
4
- import { codeGenSolanaProcessor } from './solana-codegen/codegen'
5
- import { codeGenAptosProcessor } from './aptos-codegen/codegen'
6
-
7
- if (process.argv.length > 3) {
8
- const abisDir = process.argv[2]
9
- const targetDir = process.argv[3]
10
-
11
- codeGenAptosProcessor(path.join(abisDir, 'aptos'), path.join(targetDir, 'aptos'))
12
- codeGenSolanaProcessor(path.join(abisDir, 'solana'), path.join(targetDir, 'solana'))
13
- } else {
14
- console.error('Not enough argument')
15
- process.exit(1)
16
- }