@sentio/sdk 2.62.0-rc.15 → 2.62.0-rc.17
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.
- package/lib/core/base-context.test.d.ts +2 -0
- package/lib/core/base-context.test.d.ts.map +1 -0
- package/lib/core/base-context.test.js.map +1 -0
- package/lib/store/convert.test.js.map +1 -1
- package/lib/testing/test-processor-server.d.ts +0 -4
- package/lib/testing/test-processor-server.d.ts.map +1 -1
- package/lib/testing/test-processor-server.js +0 -6
- package/lib/testing/test-processor-server.js.map +1 -1
- package/package.json +5 -10
- package/src/testing/test-processor-server.ts +0 -6
- package/lib/btc/btc-plugin.d.ts +0 -19
- package/lib/btc/btc-plugin.d.ts.map +0 -1
- package/lib/btc/btc-plugin.js +0 -161
- package/lib/btc/btc-plugin.js.map +0 -1
- package/lib/btc/btc-processor.d.ts +0 -34
- package/lib/btc/btc-processor.d.ts.map +0 -1
- package/lib/btc/btc-processor.js +0 -101
- package/lib/btc/btc-processor.js.map +0 -1
- package/lib/btc/filter.d.ts +0 -44
- package/lib/btc/filter.d.ts.map +0 -1
- package/lib/btc/filter.js +0 -127
- package/lib/btc/filter.js.map +0 -1
- package/lib/btc/filter.test.d.ts +0 -2
- package/lib/btc/filter.test.d.ts.map +0 -1
- package/lib/btc/filter.test.js.map +0 -1
- package/lib/btc/index.d.ts +0 -5
- package/lib/btc/index.d.ts.map +0 -1
- package/lib/btc/index.js +0 -5
- package/lib/btc/index.js.map +0 -1
- package/lib/btc/types.d.ts +0 -89
- package/lib/btc/types.d.ts.map +0 -1
- package/lib/btc/types.js +0 -60
- package/lib/btc/types.js.map +0 -1
- package/lib/stark/codegen/codegen.d.ts +0 -2
- package/lib/stark/codegen/codegen.d.ts.map +0 -1
- package/lib/stark/codegen/codegen.js +0 -110
- package/lib/stark/codegen/codegen.js.map +0 -1
- package/lib/stark/codegen/index.d.ts +0 -2
- package/lib/stark/codegen/index.d.ts.map +0 -1
- package/lib/stark/codegen/index.js +0 -2
- package/lib/stark/codegen/index.js.map +0 -1
- package/lib/stark/codegen/run.d.ts +0 -2
- package/lib/stark/codegen/run.d.ts.map +0 -1
- package/lib/stark/codegen/run.js +0 -11
- package/lib/stark/codegen/run.js.map +0 -1
- package/lib/stark/codegen/utils.d.ts +0 -2
- package/lib/stark/codegen/utils.d.ts.map +0 -1
- package/lib/stark/codegen/utils.js +0 -4
- package/lib/stark/codegen/utils.js.map +0 -1
- package/lib/stark/context.d.ts +0 -28
- package/lib/stark/context.d.ts.map +0 -1
- package/lib/stark/context.js +0 -59
- package/lib/stark/context.js.map +0 -1
- package/lib/stark/contract.d.ts +0 -12
- package/lib/stark/contract.d.ts.map +0 -1
- package/lib/stark/contract.js +0 -43
- package/lib/stark/contract.js.map +0 -1
- package/lib/stark/event.d.ts +0 -7
- package/lib/stark/event.d.ts.map +0 -1
- package/lib/stark/event.js +0 -11
- package/lib/stark/event.js.map +0 -1
- package/lib/stark/index.d.ts +0 -7
- package/lib/stark/index.d.ts.map +0 -1
- package/lib/stark/index.js +0 -7
- package/lib/stark/index.js.map +0 -1
- package/lib/stark/starknet-plugin.d.ts +0 -15
- package/lib/stark/starknet-plugin.d.ts.map +0 -1
- package/lib/stark/starknet-plugin.js +0 -87
- package/lib/stark/starknet-plugin.js.map +0 -1
- package/lib/stark/starknet-processor.d.ts +0 -35
- package/lib/stark/starknet-processor.d.ts.map +0 -1
- package/lib/stark/starknet-processor.js +0 -133
- package/lib/stark/starknet-processor.js.map +0 -1
- package/lib/stark/types.d.ts +0 -11
- package/lib/stark/types.d.ts.map +0 -1
- package/lib/stark/types.js +0 -2
- package/lib/stark/types.js.map +0 -1
- package/lib/testing/btc-facet.d.ts +0 -10
- package/lib/testing/btc-facet.d.ts.map +0 -1
- package/lib/testing/btc-facet.js +0 -39
- package/lib/testing/btc-facet.js.map +0 -1
- package/lib/testing/starknet-facet.d.ts +0 -9
- package/lib/testing/starknet-facet.d.ts.map +0 -1
- package/lib/testing/starknet-facet.js +0 -53
- package/lib/testing/starknet-facet.js.map +0 -1
- package/src/btc/btc-plugin.ts +0 -209
- package/src/btc/btc-processor.ts +0 -165
- package/src/btc/filter.ts +0 -206
- package/src/btc/index.ts +0 -4
- package/src/btc/types.ts +0 -134
- package/src/stark/codegen/codegen.ts +0 -125
- package/src/stark/codegen/index.ts +0 -1
- package/src/stark/codegen/run.ts +0 -10
- package/src/stark/codegen/utils.ts +0 -3
- package/src/stark/context.ts +0 -78
- package/src/stark/contract.ts +0 -55
- package/src/stark/event.ts +0 -7
- package/src/stark/index.ts +0 -9
- package/src/stark/starknet-plugin.ts +0 -116
- package/src/stark/starknet-processor.ts +0 -164
- package/src/stark/types.ts +0 -11
- package/src/testing/btc-facet.ts +0 -46
- package/src/testing/starknet-facet.ts +0 -62
package/src/btc/btc-plugin.ts
DELETED
@@ -1,209 +0,0 @@
|
|
1
|
-
import { errorString, GLOBAL_CONFIG, mergeProcessResults, Plugin, PluginManager, USER_PROCESSOR } from '@sentio/runtime'
|
2
|
-
import {
|
3
|
-
ContractConfig,
|
4
|
-
DataBinding,
|
5
|
-
HandlerType,
|
6
|
-
InitResponse,
|
7
|
-
ProcessConfigResponse,
|
8
|
-
ProcessResult,
|
9
|
-
ProcessStreamResponse_Partitions,
|
10
|
-
StartRequest
|
11
|
-
} from '@sentio/protos'
|
12
|
-
|
13
|
-
import { ServerError, Status } from 'nice-grpc'
|
14
|
-
import { PartitionHandlerManager } from '../core/index.js'
|
15
|
-
import { HandlerRegister } from '../core/handler-register.js'
|
16
|
-
import { TemplateInstanceState } from '../core/template.js'
|
17
|
-
import { BTCProcessorState } from './btc-processor.js'
|
18
|
-
import { filters2Proto, TransactionFilter } from './filter.js'
|
19
|
-
|
20
|
-
export class BTCPlugin extends Plugin {
|
21
|
-
name: string = 'BTCPlugin'
|
22
|
-
handlerRegister = new HandlerRegister()
|
23
|
-
partitionManager = new PartitionHandlerManager()
|
24
|
-
|
25
|
-
async init(config: InitResponse) {
|
26
|
-
for (const aptosProcessor of BTCProcessorState.INSTANCE.getValues()) {
|
27
|
-
const chainId = aptosProcessor.config.chainId
|
28
|
-
config.chainIds.push(chainId)
|
29
|
-
}
|
30
|
-
}
|
31
|
-
|
32
|
-
async configure(config: ProcessConfigResponse, forChainId?: string) {
|
33
|
-
this.handlerRegister.clear(forChainId as any)
|
34
|
-
|
35
|
-
for (const processor of BTCProcessorState.INSTANCE.getValues()) {
|
36
|
-
const chainId = processor.config.chainId
|
37
|
-
if (forChainId !== undefined && forChainId !== chainId.toString()) {
|
38
|
-
continue
|
39
|
-
}
|
40
|
-
const contractConfig = ContractConfig.fromPartial({
|
41
|
-
processorType: USER_PROCESSOR,
|
42
|
-
contract: {
|
43
|
-
name: processor.config.name,
|
44
|
-
chainId: processor.config.chainId.toString(),
|
45
|
-
address: processor.config.address || '*',
|
46
|
-
abi: ''
|
47
|
-
},
|
48
|
-
startBlock: processor.config.startBlock,
|
49
|
-
endBlock: processor.config.endBlock
|
50
|
-
})
|
51
|
-
for (const callHandler of processor.callHandlers) {
|
52
|
-
const handlerId = this.handlerRegister.register(callHandler.handler, chainId)
|
53
|
-
this.partitionManager.registerPartitionHandler(
|
54
|
-
HandlerType.BTC_TRANSACTION,
|
55
|
-
handlerId,
|
56
|
-
callHandler.partitionHandler
|
57
|
-
)
|
58
|
-
const handlerName = callHandler.handlerName
|
59
|
-
|
60
|
-
if (callHandler.filter) {
|
61
|
-
contractConfig.btcTransactionConfigs.push({
|
62
|
-
filters: filters2Proto(callHandler.filter),
|
63
|
-
handlerId,
|
64
|
-
handlerName
|
65
|
-
})
|
66
|
-
} else if (contractConfig.contract?.address != '*') {
|
67
|
-
contractConfig.btcTransactionConfigs.push({
|
68
|
-
filters: filters2Proto({
|
69
|
-
outputFilter: {
|
70
|
-
script_address: processor.config.address
|
71
|
-
}
|
72
|
-
} as TransactionFilter),
|
73
|
-
handlerId,
|
74
|
-
handlerName
|
75
|
-
})
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
for (const blockHandler of processor.blockHandlers) {
|
80
|
-
const handlerId = this.handlerRegister.register(blockHandler.handler, chainId)
|
81
|
-
this.partitionManager.registerPartitionHandler(HandlerType.BTC_BLOCK, handlerId, blockHandler.partitionHandler)
|
82
|
-
contractConfig.intervalConfigs.push({
|
83
|
-
slot: 0,
|
84
|
-
slotInterval: blockHandler.blockInterval,
|
85
|
-
minutes: 0,
|
86
|
-
minutesInterval: blockHandler.timeIntervalInMinutes,
|
87
|
-
handlerId,
|
88
|
-
handlerName: blockHandler.handlerName,
|
89
|
-
fetchConfig: {
|
90
|
-
transaction: blockHandler.fetchConfig?.getTransactions ?? false,
|
91
|
-
trace: false,
|
92
|
-
block: true,
|
93
|
-
transactionReceipt: false,
|
94
|
-
transactionReceiptLogs: false
|
95
|
-
}
|
96
|
-
})
|
97
|
-
}
|
98
|
-
|
99
|
-
// Finish up a contract
|
100
|
-
config.contractConfigs.push(contractConfig)
|
101
|
-
}
|
102
|
-
}
|
103
|
-
|
104
|
-
supportedHandlers = [HandlerType.BTC_TRANSACTION, HandlerType.BTC_BLOCK]
|
105
|
-
|
106
|
-
processBinding(request: DataBinding): Promise<ProcessResult> {
|
107
|
-
switch (request.handlerType) {
|
108
|
-
case HandlerType.BTC_TRANSACTION:
|
109
|
-
return this.processTransaction(request)
|
110
|
-
case HandlerType.BTC_BLOCK:
|
111
|
-
return this.processBlock(request)
|
112
|
-
default:
|
113
|
-
throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
|
114
|
-
}
|
115
|
-
}
|
116
|
-
|
117
|
-
async partition(request: DataBinding): Promise<ProcessStreamResponse_Partitions> {
|
118
|
-
let data: any
|
119
|
-
switch (request.handlerType) {
|
120
|
-
case HandlerType.BTC_TRANSACTION:
|
121
|
-
if (!request.data?.btcTransaction) {
|
122
|
-
throw new ServerError(Status.INVALID_ARGUMENT, "btcTransaction can't be empty")
|
123
|
-
}
|
124
|
-
data = request.data.btcTransaction
|
125
|
-
break
|
126
|
-
case HandlerType.BTC_BLOCK:
|
127
|
-
if (!request.data?.btcBlock) {
|
128
|
-
throw new ServerError(Status.INVALID_ARGUMENT, "btcBlock can't be empty")
|
129
|
-
}
|
130
|
-
data = request.data.btcBlock
|
131
|
-
break
|
132
|
-
default:
|
133
|
-
throw new ServerError(Status.INVALID_ARGUMENT, 'No handle type registered ' + request.handlerType)
|
134
|
-
}
|
135
|
-
const partitions = await this.partitionManager.processPartitionForHandlerType(
|
136
|
-
request.handlerType,
|
137
|
-
request.handlerIds,
|
138
|
-
data
|
139
|
-
)
|
140
|
-
return {
|
141
|
-
partitions
|
142
|
-
}
|
143
|
-
}
|
144
|
-
|
145
|
-
async start(request: StartRequest) {}
|
146
|
-
|
147
|
-
stateDiff(config: ProcessConfigResponse): boolean {
|
148
|
-
return TemplateInstanceState.INSTANCE.getValues().length !== config.templateInstances.length
|
149
|
-
}
|
150
|
-
|
151
|
-
async processTransaction(binding: DataBinding): Promise<ProcessResult> {
|
152
|
-
if (!binding.data?.btcTransaction) {
|
153
|
-
throw new ServerError(Status.INVALID_ARGUMENT, "BTCEvents can't be null")
|
154
|
-
}
|
155
|
-
|
156
|
-
const promises: Promise<ProcessResult>[] = []
|
157
|
-
|
158
|
-
const result = binding.data?.btcTransaction
|
159
|
-
|
160
|
-
for (const handlerId of binding.handlerIds) {
|
161
|
-
const promise = this.handlerRegister
|
162
|
-
.getHandlerById(
|
163
|
-
binding.chainId,
|
164
|
-
handlerId
|
165
|
-
)(binding.data?.btcTransaction)
|
166
|
-
.catch((e: any) => {
|
167
|
-
throw new ServerError(
|
168
|
-
Status.INTERNAL,
|
169
|
-
'error processing transaction: ' + JSON.stringify(result) + '\n' + errorString(e)
|
170
|
-
)
|
171
|
-
})
|
172
|
-
if (GLOBAL_CONFIG.execution.sequential) {
|
173
|
-
await promise
|
174
|
-
}
|
175
|
-
promises.push(promise)
|
176
|
-
}
|
177
|
-
return mergeProcessResults(await Promise.all(promises))
|
178
|
-
}
|
179
|
-
|
180
|
-
private async processBlock(request: DataBinding) {
|
181
|
-
if (!request.data?.btcBlock) {
|
182
|
-
throw new ServerError(Status.INVALID_ARGUMENT, "Block can't be empty")
|
183
|
-
}
|
184
|
-
|
185
|
-
const block = request.data.btcBlock
|
186
|
-
|
187
|
-
const promises: Promise<ProcessResult>[] = []
|
188
|
-
for (const handlerId of request.handlerIds) {
|
189
|
-
const promise = this.handlerRegister
|
190
|
-
.getHandlerById(
|
191
|
-
request.chainId,
|
192
|
-
handlerId
|
193
|
-
)(block)
|
194
|
-
.catch((e: any) => {
|
195
|
-
throw new ServerError(
|
196
|
-
Status.INTERNAL,
|
197
|
-
'error processing block: ' + JSON.stringify(block) + '\n' + errorString(e)
|
198
|
-
)
|
199
|
-
})
|
200
|
-
if (GLOBAL_CONFIG.execution.sequential) {
|
201
|
-
await promise
|
202
|
-
}
|
203
|
-
promises.push(promise)
|
204
|
-
}
|
205
|
-
return mergeProcessResults(await Promise.all(promises))
|
206
|
-
}
|
207
|
-
}
|
208
|
-
|
209
|
-
PluginManager.INSTANCE.register(new BTCPlugin())
|
package/src/btc/btc-processor.ts
DELETED
@@ -1,165 +0,0 @@
|
|
1
|
-
import { ListStateStorage } from '@sentio/runtime'
|
2
|
-
import { BlockHandler, BTCBlock, BTCBlockContext, BTCContext, BTCOnIntervalFetchConfig, Transaction } from './types.js'
|
3
|
-
import { Data_BTCBlock, Data_BTCTransaction, HandleInterval, ProcessResult } from '@sentio/protos'
|
4
|
-
import { TransactionFilters } from './filter.js'
|
5
|
-
import { HandlerOptions, PromiseOrVoid } from '../core/index.js'
|
6
|
-
import { ServerError, Status } from 'nice-grpc'
|
7
|
-
import { getHandlerName, proxyProcessor } from '../utils/metrics.js'
|
8
|
-
|
9
|
-
export class BTCProcessorState extends ListStateStorage<BTCProcessor> {
|
10
|
-
static INSTANCE = new BTCProcessorState()
|
11
|
-
}
|
12
|
-
|
13
|
-
export class BTCProcessor {
|
14
|
-
callHandlers: CallHandler<Data_BTCTransaction>[] = []
|
15
|
-
blockHandlers: BlockHandler[] = []
|
16
|
-
|
17
|
-
constructor(readonly config: BTCProcessorConfig) {
|
18
|
-
return proxyProcessor(this)
|
19
|
-
}
|
20
|
-
|
21
|
-
static bind(config: BTCProcessorConfig): BTCProcessor {
|
22
|
-
const processor = new BTCProcessor(config)
|
23
|
-
BTCProcessorState.INSTANCE.addValue(processor)
|
24
|
-
return processor
|
25
|
-
}
|
26
|
-
|
27
|
-
public onTransaction(
|
28
|
-
handler: (transaction: Transaction, ctx: BTCContext) => void | Promise<void>,
|
29
|
-
filter?: TransactionFilters,
|
30
|
-
handlerOptions?: HandlerOptions<object, Transaction>
|
31
|
-
) {
|
32
|
-
const callHandler = {
|
33
|
-
handlerName: getHandlerName(),
|
34
|
-
handler: async (call: Data_BTCTransaction) => {
|
35
|
-
const tx = call.transaction as Transaction
|
36
|
-
|
37
|
-
const ctx = new BTCContext(
|
38
|
-
this.config.chainId,
|
39
|
-
this.config.name ?? this.config.address ?? '',
|
40
|
-
tx,
|
41
|
-
this.config.address ?? tx.vout?.[0]?.scriptPubKey?.address
|
42
|
-
)
|
43
|
-
await handler(tx, ctx)
|
44
|
-
return ctx.stopAndGetResult()
|
45
|
-
},
|
46
|
-
filter,
|
47
|
-
partitionHandler: async (call: Data_BTCTransaction): Promise<string | undefined> => {
|
48
|
-
const p = handlerOptions?.partitionKey
|
49
|
-
if (!p) return undefined
|
50
|
-
if (typeof p === 'function') {
|
51
|
-
const tx = call.transaction as Transaction
|
52
|
-
return p(tx)
|
53
|
-
}
|
54
|
-
return p
|
55
|
-
}
|
56
|
-
}
|
57
|
-
this.callHandlers.push(callHandler)
|
58
|
-
return this
|
59
|
-
}
|
60
|
-
|
61
|
-
public onInterval(
|
62
|
-
handler: (block: BTCBlock, ctx: BTCBlockContext) => PromiseOrVoid,
|
63
|
-
timeInterval: HandleInterval | undefined,
|
64
|
-
blockInterval: HandleInterval | undefined,
|
65
|
-
handlerOptions?: HandlerOptions<BTCOnIntervalFetchConfig, BTCBlock>
|
66
|
-
): this {
|
67
|
-
if (timeInterval) {
|
68
|
-
if (timeInterval.backfillInterval < timeInterval.recentInterval) {
|
69
|
-
timeInterval.backfillInterval = timeInterval.recentInterval
|
70
|
-
}
|
71
|
-
}
|
72
|
-
|
73
|
-
const processor = this
|
74
|
-
|
75
|
-
this.blockHandlers.push({
|
76
|
-
blockInterval,
|
77
|
-
timeIntervalInMinutes: timeInterval,
|
78
|
-
handlerName: getHandlerName(),
|
79
|
-
handler: async function (data: Data_BTCBlock) {
|
80
|
-
const header = data.block
|
81
|
-
if (!header) {
|
82
|
-
throw new ServerError(Status.INVALID_ARGUMENT, 'Block is empty')
|
83
|
-
}
|
84
|
-
|
85
|
-
const block = {
|
86
|
-
...header
|
87
|
-
} as BTCBlock
|
88
|
-
if (handlerOptions?.getTransactions) {
|
89
|
-
block.tx = header.rawtx?.map((tx: any) => tx as Transaction)
|
90
|
-
}
|
91
|
-
|
92
|
-
const ctx = new BTCBlockContext(
|
93
|
-
processor.config.chainId,
|
94
|
-
processor.config.name ?? processor.config.address ?? '',
|
95
|
-
block,
|
96
|
-
processor.config.address
|
97
|
-
)
|
98
|
-
await handler(block, ctx)
|
99
|
-
return ctx.stopAndGetResult()
|
100
|
-
},
|
101
|
-
fetchConfig: handlerOptions,
|
102
|
-
partitionHandler: async (data: Data_BTCBlock): Promise<string | undefined> => {
|
103
|
-
const p = handlerOptions?.partitionKey
|
104
|
-
if (!p) return undefined
|
105
|
-
if (typeof p === 'function') {
|
106
|
-
const header = data.block
|
107
|
-
if (!header) return undefined
|
108
|
-
const block = { ...header } as BTCBlock
|
109
|
-
if (handlerOptions?.getTransactions) {
|
110
|
-
block.tx = header.rawtx?.map((tx: any) => tx as Transaction)
|
111
|
-
}
|
112
|
-
return p(block)
|
113
|
-
}
|
114
|
-
return p
|
115
|
-
}
|
116
|
-
})
|
117
|
-
return this
|
118
|
-
}
|
119
|
-
|
120
|
-
public onBlockInterval(
|
121
|
-
handler: (block: BTCBlock, ctx: BTCBlockContext) => PromiseOrVoid,
|
122
|
-
blockInterval = 250,
|
123
|
-
backfillBlockInterval = 1000,
|
124
|
-
handlerOptions?: HandlerOptions<BTCOnIntervalFetchConfig, BTCBlock>
|
125
|
-
): this {
|
126
|
-
return this.onInterval(
|
127
|
-
handler,
|
128
|
-
undefined,
|
129
|
-
{
|
130
|
-
recentInterval: blockInterval,
|
131
|
-
backfillInterval: backfillBlockInterval
|
132
|
-
},
|
133
|
-
handlerOptions
|
134
|
-
)
|
135
|
-
}
|
136
|
-
|
137
|
-
public onTimeInterval(
|
138
|
-
handler: (block: BTCBlock, ctx: BTCBlockContext) => PromiseOrVoid,
|
139
|
-
timeIntervalInMinutes = 60,
|
140
|
-
backfillTimeIntervalInMinutes = 240,
|
141
|
-
handlerOptions?: HandlerOptions<BTCOnIntervalFetchConfig, BTCBlock>
|
142
|
-
): this {
|
143
|
-
return this.onInterval(
|
144
|
-
handler,
|
145
|
-
{ recentInterval: timeIntervalInMinutes, backfillInterval: backfillTimeIntervalInMinutes },
|
146
|
-
undefined,
|
147
|
-
handlerOptions
|
148
|
-
)
|
149
|
-
}
|
150
|
-
}
|
151
|
-
|
152
|
-
interface BTCProcessorConfig {
|
153
|
-
chainId: string
|
154
|
-
name?: string
|
155
|
-
address?: string
|
156
|
-
startBlock?: bigint
|
157
|
-
endBlock?: bigint
|
158
|
-
}
|
159
|
-
|
160
|
-
export type CallHandler<T> = {
|
161
|
-
handlerName: string
|
162
|
-
handler: (call: T) => Promise<ProcessResult>
|
163
|
-
filter?: TransactionFilters
|
164
|
-
partitionHandler?: (call: T) => Promise<string | undefined>
|
165
|
-
}
|
package/src/btc/filter.ts
DELETED
@@ -1,206 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
BTCTransactionFilter,
|
3
|
-
BTCTransactionFilter_Condition,
|
4
|
-
BTCTransactionFilter_Filter,
|
5
|
-
BTCTransactionFilter_Filters,
|
6
|
-
BTCTransactionFilter_VinFilter,
|
7
|
-
BTCTransactionFilter_VOutFilter
|
8
|
-
} from '@sentio/protos'
|
9
|
-
import { serializeRichValue } from '../store/util.js'
|
10
|
-
import { BigDecimal } from '@sentio/bigdecimal'
|
11
|
-
import { Transaction, Vin, Vout } from './types.js'
|
12
|
-
|
13
|
-
export type TransactionFields = keyof Omit<Transaction, 'vin' | 'vout'>
|
14
|
-
|
15
|
-
export type VinFields = keyof Omit<Vin, 'pre_vout' | 'pre_transaction' | 'scriptSig'> | 'script_asm' | 'script_hex'
|
16
|
-
|
17
|
-
export type VOutFields =
|
18
|
-
| keyof Omit<Vout, 'scriptPubKey'>
|
19
|
-
| 'script_asm'
|
20
|
-
| 'script_hex'
|
21
|
-
| 'script_type'
|
22
|
-
| 'script_address'
|
23
|
-
|
24
|
-
export type Filter<F extends string> = {
|
25
|
-
[K in F]?: Condition | Comparable
|
26
|
-
}
|
27
|
-
|
28
|
-
export type Condition = {
|
29
|
-
eq?: Comparable
|
30
|
-
gt?: Comparable
|
31
|
-
gte?: Comparable
|
32
|
-
lt?: Comparable
|
33
|
-
lte?: Comparable
|
34
|
-
ne?: Comparable
|
35
|
-
// string has the prefix
|
36
|
-
prefix?: string
|
37
|
-
// string contains
|
38
|
-
contains?: string
|
39
|
-
notContains?: string
|
40
|
-
length?: number
|
41
|
-
lengthGt?: number
|
42
|
-
lengthLt?: number
|
43
|
-
// array contains any of the values
|
44
|
-
hasAny?: Array<Comparable>
|
45
|
-
// array contains all the values
|
46
|
-
hasAll?: Array<Comparable>
|
47
|
-
in?: Array<Comparable>
|
48
|
-
}
|
49
|
-
|
50
|
-
export type Filters<T extends string> = Filter<T> | Filter<T>[]
|
51
|
-
|
52
|
-
export type VinFilter = Filter<VinFields> & {
|
53
|
-
preVOut?: Filter<VOutFields>
|
54
|
-
preTransaction?: {
|
55
|
-
filter?: Array<Filter<TransactionFields>>
|
56
|
-
outputFilter?: Filters<VOutFields>
|
57
|
-
// can't have inputFilter here, we can only support one level of nesting
|
58
|
-
}
|
59
|
-
}
|
60
|
-
|
61
|
-
export type VOutFilter = Filter<VOutFields>
|
62
|
-
|
63
|
-
export type TransactionFilter = {
|
64
|
-
inputFilter?: VinFilter | VinFilter[]
|
65
|
-
outputFilter?: VOutFilter | VOutFilter[]
|
66
|
-
filter?: Array<Filter<TransactionFields>>
|
67
|
-
}
|
68
|
-
|
69
|
-
export type Comparable = number | BigDecimal | bigint | Date | string | boolean
|
70
|
-
|
71
|
-
export type TransactionFilters = TransactionFilter | TransactionFilter[]
|
72
|
-
|
73
|
-
function toVinFilter(inputFilter?: VinFilter | VinFilter[]): BTCTransactionFilter_VinFilter | undefined {
|
74
|
-
if (inputFilter) {
|
75
|
-
const ret = BTCTransactionFilter_VinFilter.create()
|
76
|
-
const filters = []
|
77
|
-
const vouts: Filter<VOutFields>[] = []
|
78
|
-
const txs: TransactionFilters = []
|
79
|
-
|
80
|
-
for (const f of Array.isArray(inputFilter) ? inputFilter : [inputFilter]) {
|
81
|
-
const { preVOut, preTransaction, ...rest } = f
|
82
|
-
filters.push(rest)
|
83
|
-
if (preVOut) {
|
84
|
-
vouts.push(preVOut)
|
85
|
-
}
|
86
|
-
if (preTransaction) {
|
87
|
-
txs.push(preTransaction)
|
88
|
-
}
|
89
|
-
}
|
90
|
-
ret.filters = BTCTransactionFilter_Filters.create({
|
91
|
-
filters: convertFilters(filters)
|
92
|
-
})
|
93
|
-
|
94
|
-
if (vouts.length > 0) {
|
95
|
-
const voutFilters = convertFilters(vouts)
|
96
|
-
if (voutFilters && voutFilters.length > 0) {
|
97
|
-
ret.preVOut = voutFilters[0]
|
98
|
-
}
|
99
|
-
}
|
100
|
-
if (txs.length > 0) {
|
101
|
-
ret.preTransaction = filters2Proto(txs)?.[0]
|
102
|
-
}
|
103
|
-
|
104
|
-
return ret
|
105
|
-
}
|
106
|
-
|
107
|
-
return undefined
|
108
|
-
}
|
109
|
-
|
110
|
-
function toVOutFilter(outputFilter?: VOutFilter | VOutFilter[]): BTCTransactionFilter_VOutFilter | undefined {
|
111
|
-
if (outputFilter) {
|
112
|
-
const ret = BTCTransactionFilter_VOutFilter.create()
|
113
|
-
const filters = Array.isArray(outputFilter) ? outputFilter : ([outputFilter] as Filter<VOutFields>[])
|
114
|
-
ret.filters = BTCTransactionFilter_Filters.create({
|
115
|
-
filters: convertFilters(filters)
|
116
|
-
})
|
117
|
-
|
118
|
-
return ret
|
119
|
-
}
|
120
|
-
return undefined
|
121
|
-
}
|
122
|
-
|
123
|
-
export function filters2Proto(filter: TransactionFilters): BTCTransactionFilter[] {
|
124
|
-
const filters = Array.isArray(filter) ? filter : [filter]
|
125
|
-
return filters.map((f) => {
|
126
|
-
return {
|
127
|
-
filter: convertFilters(f.filter),
|
128
|
-
inputFilter: toVinFilter(f.inputFilter),
|
129
|
-
outputFilter: toVOutFilter(f.outputFilter)
|
130
|
-
} as BTCTransactionFilter
|
131
|
-
})
|
132
|
-
}
|
133
|
-
|
134
|
-
function toCondition(value: Condition | Comparable): BTCTransactionFilter_Condition {
|
135
|
-
const ret: BTCTransactionFilter_Condition = BTCTransactionFilter_Condition.create()
|
136
|
-
if (value instanceof Date) {
|
137
|
-
ret.eq = serializeRichValue(value)
|
138
|
-
}
|
139
|
-
if (typeof value === 'number') {
|
140
|
-
ret.eq = serializeRichValue(value)
|
141
|
-
}
|
142
|
-
if (typeof value === 'string') {
|
143
|
-
ret.eq = serializeRichValue(value)
|
144
|
-
}
|
145
|
-
if (value instanceof BigDecimal) {
|
146
|
-
ret.eq = serializeRichValue(value)
|
147
|
-
}
|
148
|
-
if (typeof value === 'bigint') {
|
149
|
-
ret.eq = serializeRichValue(value)
|
150
|
-
}
|
151
|
-
if (typeof value === 'boolean') {
|
152
|
-
ret.eq = serializeRichValue(value)
|
153
|
-
}
|
154
|
-
if (Array.isArray(value)) {
|
155
|
-
ret.in = { values: value.map((v) => serializeRichValue(v)) }
|
156
|
-
}
|
157
|
-
|
158
|
-
for (const [k, v] of Object.entries(value)) {
|
159
|
-
switch (k) {
|
160
|
-
case 'prefix':
|
161
|
-
case 'contains':
|
162
|
-
case 'notContains':
|
163
|
-
ret[k] = v.toString()
|
164
|
-
break
|
165
|
-
case 'length':
|
166
|
-
ret['lengthEq'] = v
|
167
|
-
break
|
168
|
-
case 'lengthGt':
|
169
|
-
case 'lengthLt':
|
170
|
-
ret[k] = v
|
171
|
-
break
|
172
|
-
case 'hasAny':
|
173
|
-
case 'hasAll':
|
174
|
-
case 'in':
|
175
|
-
ret[k] = { values: v.map((v: any) => serializeRichValue(v)) }
|
176
|
-
break
|
177
|
-
case 'eq':
|
178
|
-
case 'gt':
|
179
|
-
case 'gte':
|
180
|
-
case 'lt':
|
181
|
-
case 'lte':
|
182
|
-
case 'ne':
|
183
|
-
ret[k] = serializeRichValue(v)
|
184
|
-
break
|
185
|
-
default:
|
186
|
-
throw new Error('Unknown condition op: ' + k)
|
187
|
-
}
|
188
|
-
}
|
189
|
-
|
190
|
-
return ret
|
191
|
-
}
|
192
|
-
|
193
|
-
function convertFilters<T extends string>(filters?: Array<Filter<T>>): BTCTransactionFilter_Filter[] | undefined {
|
194
|
-
if (filters && filters.length > 0) {
|
195
|
-
const ret: BTCTransactionFilter_Filter[] = []
|
196
|
-
for (const filter of filters) {
|
197
|
-
const f: BTCTransactionFilter_Filter = BTCTransactionFilter_Filter.create()
|
198
|
-
for (const [key, value] of Object.entries(filter)) {
|
199
|
-
f.conditions[key] = toCondition(value as any)
|
200
|
-
}
|
201
|
-
ret.push(f)
|
202
|
-
}
|
203
|
-
return ret
|
204
|
-
}
|
205
|
-
return []
|
206
|
-
}
|
package/src/btc/index.ts
DELETED