@sentio/sdk 1.7.14 → 1.7.15

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 (248) hide show
  1. package/{base-processor-template.d.ts → lib/base-processor-template.d.ts} +0 -0
  2. package/{base-processor-template.js → lib/base-processor-template.js} +0 -0
  3. package/{base-processor-template.js.map → lib/base-processor-template.js.map} +0 -0
  4. package/{base-processor.d.ts → lib/base-processor.d.ts} +0 -0
  5. package/{base-processor.js → lib/base-processor.js} +0 -0
  6. package/{base-processor.js.map → lib/base-processor.js.map} +0 -0
  7. package/{bind-options.d.ts → lib/bind-options.d.ts} +0 -0
  8. package/{bind-options.js → lib/bind-options.js} +0 -0
  9. package/{bind-options.js.map → lib/bind-options.js.map} +0 -0
  10. package/{binds.d.ts → lib/binds.d.ts} +0 -0
  11. package/{binds.js → lib/binds.js} +0 -0
  12. package/{binds.js.map → lib/binds.js.map} +0 -0
  13. package/{builtin → lib/builtin}/erc20/index.d.ts +0 -0
  14. package/{builtin → lib/builtin}/erc20/index.js +0 -0
  15. package/{builtin → lib/builtin}/erc20/index.js.map +0 -0
  16. package/{builtin → lib/builtin}/erc20/test-utils.d.ts +0 -0
  17. package/{builtin → lib/builtin}/erc20/test-utils.js +0 -0
  18. package/{builtin → lib/builtin}/erc20/test-utils.js.map +0 -0
  19. package/{builtin → lib/builtin}/internal/Erc20.d.ts +0 -0
  20. package/{builtin → lib/builtin}/internal/Erc20.js +0 -0
  21. package/{builtin → lib/builtin}/internal/Erc20.js.map +0 -0
  22. package/{builtin → lib/builtin}/internal/common.d.ts +0 -0
  23. package/{builtin → lib/builtin}/internal/common.js +0 -0
  24. package/{builtin → lib/builtin}/internal/common.js.map +0 -0
  25. package/{builtin → lib/builtin}/internal/erc20_processor.d.ts +0 -0
  26. package/{builtin → lib/builtin}/internal/erc20_processor.js +0 -0
  27. package/{builtin → lib/builtin}/internal/erc20_processor.js.map +0 -0
  28. package/{builtin → lib/builtin}/internal/factories/Erc20__factory.d.ts +0 -0
  29. package/{builtin → lib/builtin}/internal/factories/Erc20__factory.js +0 -0
  30. package/{builtin → lib/builtin}/internal/factories/Erc20__factory.js.map +0 -0
  31. package/{builtin → lib/builtin}/internal/factories/index.d.ts +0 -0
  32. package/{builtin → lib/builtin}/internal/factories/index.js +0 -0
  33. package/{builtin → lib/builtin}/internal/factories/index.js.map +0 -0
  34. package/{builtin → lib/builtin}/internal/index.d.ts +0 -0
  35. package/{builtin → lib/builtin}/internal/index.js +0 -0
  36. package/{builtin → lib/builtin}/internal/index.js.map +0 -0
  37. package/{chain-config.d.ts → lib/chain-config.d.ts} +0 -0
  38. package/{chain-config.js → lib/chain-config.js} +0 -0
  39. package/{chain-config.js.map → lib/chain-config.js.map} +0 -0
  40. package/{cli → lib/cli}/build.d.ts +0 -0
  41. package/{cli → lib/cli}/build.js +0 -0
  42. package/{cli → lib/cli}/build.js.map +0 -0
  43. package/{cli → lib/cli}/cli.d.ts +0 -0
  44. package/{cli → lib/cli}/cli.js +0 -0
  45. package/{cli → lib/cli}/cli.js.map +0 -0
  46. package/{cli → lib/cli}/config.d.ts +0 -0
  47. package/{cli → lib/cli}/config.js +0 -0
  48. package/{cli → lib/cli}/config.js.map +0 -0
  49. package/{cli → lib/cli}/key.d.ts +0 -0
  50. package/{cli → lib/cli}/key.js +0 -0
  51. package/{cli → lib/cli}/key.js.map +0 -0
  52. package/{cli → lib/cli}/solana-code-gen.d.ts +0 -0
  53. package/{cli → lib/cli}/solana-code-gen.js +0 -0
  54. package/{cli → lib/cli}/solana-code-gen.js.map +0 -0
  55. package/{cli → lib/cli}/upload.d.ts +0 -0
  56. package/{cli → lib/cli}/upload.js +0 -0
  57. package/{cli → lib/cli}/upload.js.map +0 -0
  58. package/{cli → lib/cli}/webpack.config.js +0 -0
  59. package/{context.d.ts → lib/context.d.ts} +0 -0
  60. package/{context.js → lib/context.js} +0 -0
  61. package/{context.js.map → lib/context.js.map} +0 -0
  62. package/{contract-namer.d.ts → lib/contract-namer.d.ts} +0 -0
  63. package/{contract-namer.js → lib/contract-namer.js} +0 -0
  64. package/{contract-namer.js.map → lib/contract-namer.js.map} +0 -0
  65. package/{error.d.ts → lib/error.d.ts} +0 -0
  66. package/{error.js → lib/error.js} +0 -0
  67. package/{error.js.map → lib/error.js.map} +0 -0
  68. package/{gen → lib/gen}/builtin.d.ts +0 -0
  69. package/{gen → lib/gen}/builtin.js +0 -0
  70. package/{gen → lib/gen}/builtin.js.map +0 -0
  71. package/{gen → lib/gen}/google/protobuf/empty.d.ts +0 -0
  72. package/{gen → lib/gen}/google/protobuf/empty.js +0 -0
  73. package/{gen → lib/gen}/google/protobuf/empty.js.map +0 -0
  74. package/{gen → lib/gen}/processor/protos/processor.d.ts +0 -0
  75. package/{gen → lib/gen}/processor/protos/processor.js +0 -0
  76. package/{gen → lib/gen}/processor/protos/processor.js.map +0 -0
  77. package/{generic-processor.d.ts → lib/generic-processor.d.ts} +0 -0
  78. package/{generic-processor.js → lib/generic-processor.js} +0 -0
  79. package/{generic-processor.js.map → lib/generic-processor.js.map} +0 -0
  80. package/{index.d.ts → lib/index.d.ts} +0 -0
  81. package/{index.js → lib/index.js} +0 -0
  82. package/{index.js.map → lib/index.js.map} +0 -0
  83. package/{meter.d.ts → lib/meter.d.ts} +0 -0
  84. package/{meter.js → lib/meter.js} +0 -0
  85. package/{meter.js.map → lib/meter.js.map} +0 -0
  86. package/{numberish.d.ts → lib/numberish.d.ts} +0 -0
  87. package/{numberish.js → lib/numberish.js} +0 -0
  88. package/{numberish.js.map → lib/numberish.js.map} +0 -0
  89. package/{numberish.test.d.ts → lib/numberish.test.d.ts} +0 -0
  90. package/{numberish.test.js → lib/numberish.test.js} +0 -0
  91. package/{numberish.test.js.map → lib/numberish.test.js.map} +0 -0
  92. package/{processor-runner.d.ts → lib/processor-runner.d.ts} +0 -0
  93. package/{processor-runner.js → lib/processor-runner.js} +0 -0
  94. package/{processor-runner.js.map → lib/processor-runner.js.map} +0 -0
  95. package/{processor-state.d.ts → lib/processor-state.d.ts} +0 -0
  96. package/{processor-state.js → lib/processor-state.js} +0 -0
  97. package/{processor-state.js.map → lib/processor-state.js.map} +0 -0
  98. package/{promise-or-void.d.ts → lib/promise-or-void.d.ts} +0 -0
  99. package/{promise-or-void.js → lib/promise-or-void.js} +0 -0
  100. package/{promise-or-void.js.map → lib/promise-or-void.js.map} +0 -0
  101. package/{provider.d.ts → lib/provider.d.ts} +0 -0
  102. package/{provider.js → lib/provider.js} +0 -0
  103. package/{provider.js.map → lib/provider.js.map} +0 -0
  104. package/{release.config.js → lib/release.config.js} +0 -0
  105. package/{service.d.ts → lib/service.d.ts} +0 -0
  106. package/{service.js → lib/service.js} +0 -0
  107. package/{service.js.map → lib/service.js.map} +0 -0
  108. package/{solana → lib/solana}/builtin/index.d.ts +0 -0
  109. package/{solana → lib/solana}/builtin/index.js +0 -0
  110. package/{solana → lib/solana}/builtin/index.js.map +0 -0
  111. package/{solana → lib/solana}/builtin/spl-token-processor.d.ts +0 -0
  112. package/{solana → lib/solana}/builtin/spl-token-processor.js +0 -0
  113. package/{solana → lib/solana}/builtin/spl-token-processor.js.map +0 -0
  114. package/{solana → lib/solana}/builtin/types.d.ts +0 -0
  115. package/{solana → lib/solana}/builtin/types.js +0 -0
  116. package/{solana → lib/solana}/builtin/types.js.map +0 -0
  117. package/{solana → lib/solana}/builtin/wormhole-processor.d.ts +0 -0
  118. package/{solana → lib/solana}/builtin/wormhole-processor.js +0 -0
  119. package/{solana → lib/solana}/builtin/wormhole-processor.js.map +0 -0
  120. package/{solana-processor.d.ts → lib/solana-processor.d.ts} +0 -0
  121. package/{solana-processor.js → lib/solana-processor.js} +0 -0
  122. package/{solana-processor.js.map → lib/solana-processor.js.map} +0 -0
  123. package/{target-ethers-sentio → lib/target-ethers-sentio}/codegen.d.ts +0 -0
  124. package/{target-ethers-sentio → lib/target-ethers-sentio}/codegen.js +0 -0
  125. package/lib/target-ethers-sentio/codegen.js.map +1 -0
  126. package/{target-ethers-sentio → lib/target-ethers-sentio}/index.d.ts +0 -0
  127. package/{target-ethers-sentio → lib/target-ethers-sentio}/index.js +0 -0
  128. package/lib/target-ethers-sentio/index.js.map +1 -0
  129. package/{test → lib/test}/codegen.test.d.ts +0 -0
  130. package/{test → lib/test}/codegen.test.js +1 -1
  131. package/{test → lib/test}/codegen.test.js.map +1 -1
  132. package/{test → lib/test}/erc20-template.d.ts +0 -0
  133. package/{test → lib/test}/erc20-template.js +0 -0
  134. package/{test → lib/test}/erc20-template.js.map +0 -0
  135. package/{test → lib/test}/erc20-template.test.d.ts +0 -0
  136. package/{test → lib/test}/erc20-template.test.js +0 -0
  137. package/{test → lib/test}/erc20-template.test.js.map +0 -0
  138. package/{test → lib/test}/erc20.d.ts +0 -0
  139. package/{test → lib/test}/erc20.js +0 -0
  140. package/{test → lib/test}/erc20.js.map +0 -0
  141. package/{test → lib/test}/erc20.test.d.ts +0 -0
  142. package/{test → lib/test}/erc20.test.js +0 -0
  143. package/{test → lib/test}/erc20.test.js.map +0 -0
  144. package/{test → lib/test}/generic-processor.test.d.ts +0 -0
  145. package/{test → lib/test}/generic-processor.test.js +0 -0
  146. package/{test → lib/test}/generic-processor.test.js.map +0 -0
  147. package/{test → lib/test}/index.d.ts +0 -0
  148. package/{test → lib/test}/index.js +0 -0
  149. package/{test → lib/test}/index.js.map +0 -0
  150. package/{test → lib/test}/metric-utils.d.ts +0 -0
  151. package/{test → lib/test}/metric-utils.js +0 -0
  152. package/{test → lib/test}/metric-utils.js.map +0 -0
  153. package/{test → lib/test}/mirrorworld.d.ts +0 -0
  154. package/{test → lib/test}/mirrorworld.js +0 -0
  155. package/{test → lib/test}/mirrorworld.js.map +0 -0
  156. package/{test → lib/test}/solana.test.d.ts +0 -0
  157. package/{test → lib/test}/solana.test.js +0 -0
  158. package/{test → lib/test}/solana.test.js.map +0 -0
  159. package/{test → lib/test}/test-processor-server.d.ts +0 -0
  160. package/{test → lib/test}/test-processor-server.js +0 -0
  161. package/{test → lib/test}/test-processor-server.js.map +0 -0
  162. package/{test → lib/test}/types/game_wallet.d.ts +0 -0
  163. package/{test → lib/test}/types/game_wallet.js +0 -0
  164. package/{test → lib/test}/types/game_wallet.js.map +0 -0
  165. package/{test → lib/test}/types/game_wallet_processor.d.ts +0 -0
  166. package/{test → lib/test}/types/game_wallet_processor.js +0 -0
  167. package/{test → lib/test}/types/game_wallet_processor.js.map +0 -0
  168. package/{test → lib/test}/wormhole-token-bridge.d.ts +0 -0
  169. package/{test → lib/test}/wormhole-token-bridge.js +0 -0
  170. package/{test → lib/test}/wormhole-token-bridge.js.map +0 -0
  171. package/{utils → lib/utils}/chainmap.d.ts +0 -0
  172. package/{utils → lib/utils}/chainmap.js +0 -0
  173. package/{utils → lib/utils}/chainmap.js.map +0 -0
  174. package/{utils → lib/utils}/convert.d.ts +0 -0
  175. package/{utils → lib/utils}/convert.js +0 -0
  176. package/{utils → lib/utils}/convert.js.map +0 -0
  177. package/{utils → lib/utils}/index.d.ts +0 -0
  178. package/{utils → lib/utils}/index.js +0 -0
  179. package/{utils → lib/utils}/index.js.map +0 -0
  180. package/package.json +47 -6
  181. package/src/abis/erc20.json +198 -0
  182. package/src/base-processor-template.ts +92 -0
  183. package/src/base-processor.ts +128 -0
  184. package/src/bind-options.ts +41 -0
  185. package/src/binds.ts +73 -0
  186. package/src/builtin/erc20/index.ts +6 -0
  187. package/src/builtin/erc20/test-utils.ts +49 -0
  188. package/src/builtin/internal/Erc20.ts +356 -0
  189. package/src/builtin/internal/common.ts +46 -0
  190. package/src/builtin/internal/erc20_processor.ts +295 -0
  191. package/src/builtin/internal/factories/Erc20__factory.ts +216 -0
  192. package/src/builtin/internal/factories/index.ts +4 -0
  193. package/src/builtin/internal/index.ts +6 -0
  194. package/src/chain-config.ts +6 -0
  195. package/src/cli/build.ts +180 -0
  196. package/src/cli/cli.ts +217 -0
  197. package/src/cli/config.ts +43 -0
  198. package/src/cli/key.ts +43 -0
  199. package/src/cli/solana-code-gen.ts +16 -0
  200. package/src/cli/upload.ts +86 -0
  201. package/src/cli/webpack.config.js +42 -0
  202. package/src/context.ts +93 -0
  203. package/src/contract-namer.ts +17 -0
  204. package/src/error.ts +43 -0
  205. package/src/gen/builtin.ts +22 -0
  206. package/src/gen/google/protobuf/empty.ts +70 -0
  207. package/src/gen/processor/protos/processor.ts +3071 -0
  208. package/src/generic-processor.ts +38 -0
  209. package/src/index.ts +19 -0
  210. package/src/meter.ts +122 -0
  211. package/src/numberish.test.ts +82 -0
  212. package/src/numberish.ts +99 -0
  213. package/src/processor-runner.ts +75 -0
  214. package/src/processor-state.ts +24 -0
  215. package/src/promise-or-void.ts +1 -0
  216. package/src/provider.ts +74 -0
  217. package/src/service.ts +434 -0
  218. package/src/solana/builtin/index.ts +2 -0
  219. package/src/solana/builtin/spl-token-processor.ts +164 -0
  220. package/src/solana/builtin/types.ts +268 -0
  221. package/src/solana/builtin/wormhole-processor.ts +178 -0
  222. package/src/solana-processor.ts +105 -0
  223. package/src/target-ethers-sentio/codegen.ts +289 -0
  224. package/src/target-ethers-sentio/index.ts +50 -0
  225. package/src/target-ethers-sentio/tsconfig.json +9 -0
  226. package/src/test/abis/evm/anyswapRouter.json +490 -0
  227. package/src/test/abis/solana/mirrorworld.json +1392 -0
  228. package/src/test/codegen.test.ts +26 -0
  229. package/src/test/erc20-template.test.ts +39 -0
  230. package/src/test/erc20-template.ts +25 -0
  231. package/src/test/erc20.test.ts +92 -0
  232. package/src/test/erc20.ts +48 -0
  233. package/src/test/generic-processor.test.ts +72 -0
  234. package/src/test/index.ts +1 -0
  235. package/src/test/metric-utils.ts +49 -0
  236. package/src/test/mirrorworld.ts +40 -0
  237. package/src/test/sentio.yaml +4 -0
  238. package/src/test/solana.test.ts +93 -0
  239. package/src/test/test-processor-server.ts +210 -0
  240. package/src/test/types/game_wallet.ts +1392 -0
  241. package/src/test/types/game_wallet_processor.ts +113 -0
  242. package/src/test/wormhole-token-bridge.ts +21 -0
  243. package/src/types/global.d.ts +17 -0
  244. package/src/utils/chainmap.ts +95 -0
  245. package/src/utils/convert.ts +6 -0
  246. package/src/utils/index.ts +2 -0
  247. package/target-ethers-sentio/codegen.js.map +0 -1
  248. package/target-ethers-sentio/index.js.map +0 -1
@@ -0,0 +1,38 @@
1
+ import { BaseContract } from '@ethersproject/contracts'
2
+ import { BoundContractView, ContractView } from './context'
3
+ import { BindOptions } from './bind-options'
4
+ import { BaseProcessor } from './base-processor'
5
+ import { getProvider } from './provider'
6
+ import { addProcessor, getContractName } from './binds'
7
+
8
+ export class GenericProcessor extends BaseProcessor<
9
+ BaseContract,
10
+ BoundContractView<BaseContract, ContractView<BaseContract>>
11
+ > {
12
+ eventAbi: string[]
13
+ constructor(eventAbi: string[], options: BindOptions) {
14
+ super(options)
15
+ this.eventAbi = eventAbi
16
+ }
17
+
18
+ protected CreateBoundContractView(): BoundContractView<BaseContract, ContractView<BaseContract>> {
19
+ const contract = new BaseContract(this.config.address, this.eventAbi, getProvider(this.config.network))
20
+ return new BoundContractView(new ContractView<BaseContract>(contract))
21
+ }
22
+
23
+ public static bind(eventAbi: string[] | string, options: BindOptions): GenericProcessor {
24
+ const AbiName = 'Generic'
25
+ if (!Array.isArray(eventAbi)) {
26
+ eventAbi = [eventAbi]
27
+ }
28
+
29
+ // let processor = getProcessor(AbiName, options) as GenericProcessor;
30
+ // if (!processor) {
31
+ const finalOptions = Object.assign({}, options)
32
+ finalOptions.name = getContractName(AbiName, options.name, options.address, options.network)
33
+ const processor = new GenericProcessor(eventAbi, finalOptions)
34
+ addProcessor(AbiName, options, processor)
35
+ // }
36
+ return processor
37
+ }
38
+ }
package/src/index.ts ADDED
@@ -0,0 +1,19 @@
1
+ export { BaseProcessor } from './base-processor'
2
+ export { GenericProcessor } from './generic-processor'
3
+ export { BaseProcessorTemplate } from './base-processor-template'
4
+ export { Context, ContractView, BoundContractView, SolanaContext } from './context'
5
+ export { ProcessorServiceImpl } from './service'
6
+ export { Counter, Meter, Gauge } from './meter'
7
+ export { getProvider, setProvider, DummyProvider } from './provider'
8
+ export { SolanaBaseProcessor } from './solana-processor'
9
+ export { ContractNamer } from './contract-namer'
10
+ export { BindOptions, SolanaBindOptions } from './bind-options'
11
+ export { transformEtherError } from './error'
12
+ export { ProcessorState } from './processor-state'
13
+ export { BigNumber as BigDecimal } from 'bignumber.js'
14
+
15
+ export { getProcessor, addProcessor, getContractByABI, addContractByABI, getContractName } from './binds'
16
+
17
+ export * from './gen/processor/protos/processor'
18
+
19
+ export { SPLTokenProcessor } from './solana/builtin'
package/src/meter.ts ADDED
@@ -0,0 +1,122 @@
1
+ import { RecordMetaData } from './gen/processor/protos/processor'
2
+ import { Context, EthContext, SolanaContext } from './context'
3
+ import { toMetricValue, Numberish } from './numberish'
4
+ import Long from 'long'
5
+
6
+ function GetRecordMetaData(ctx: EthContext | SolanaContext, name: string, labels: Labels): RecordMetaData {
7
+ if (ctx instanceof Context) {
8
+ if (ctx.log) {
9
+ return {
10
+ contractAddress: ctx.contract.rawContract.address,
11
+ blockNumber: Long.fromNumber(ctx.log.blockNumber, true), // TODO need number type to be long
12
+ transactionIndex: ctx.log.transactionIndex,
13
+ logIndex: ctx.log.logIndex,
14
+ chainId: ctx.chainId.toString(),
15
+ name: name,
16
+ labels: labels,
17
+ }
18
+ }
19
+ if (ctx.block) {
20
+ return {
21
+ contractAddress: ctx.contract.rawContract.address,
22
+ blockNumber: Long.fromNumber(ctx.block.number, true),
23
+ transactionIndex: -1,
24
+ logIndex: -1,
25
+ chainId: ctx.chainId.toString(),
26
+ name: name,
27
+ labels: labels,
28
+ }
29
+ }
30
+ } else if (ctx instanceof SolanaContext) {
31
+ return {
32
+ contractAddress: ctx.address,
33
+ blockNumber: Long.ZERO, // TODO need number type to be long
34
+ transactionIndex: 0,
35
+ logIndex: 0,
36
+ chainId: 'SOL:mainnet', // TODO set in context
37
+ name: name,
38
+ labels: labels,
39
+ }
40
+ }
41
+ throw new Error("This can't happen")
42
+ }
43
+
44
+ export type Labels = { [key: string]: string }
45
+
46
+ export class Counter {
47
+ private readonly ctx: EthContext | SolanaContext
48
+ private readonly name: string
49
+
50
+ constructor(name: string, ctx: EthContext | SolanaContext) {
51
+ this.name = name
52
+ this.ctx = ctx
53
+ }
54
+
55
+ add(value: Numberish, labels: Labels = {}) {
56
+ this.record(value, labels, true)
57
+ }
58
+
59
+ sub(value: Numberish, labels: Labels = {}) {
60
+ this.record(value, labels, false)
61
+ }
62
+
63
+ private record(value: Numberish, labels: Labels, add: boolean) {
64
+ this.ctx.counters.push({
65
+ metadata: GetRecordMetaData(this.ctx, this.name, labels),
66
+ metricValue: toMetricValue(value),
67
+ add: add,
68
+ runtimeInfo: undefined,
69
+ })
70
+ }
71
+ }
72
+
73
+ export class Gauge {
74
+ private readonly name: string
75
+ private readonly ctx: EthContext | SolanaContext
76
+
77
+ constructor(name: string, ctx: EthContext | SolanaContext) {
78
+ this.name = name
79
+ this.ctx = ctx
80
+ }
81
+
82
+ record(value: Numberish, labels: Labels = {}) {
83
+ this.ctx.gauges.push({
84
+ metadata: GetRecordMetaData(this.ctx, this.name, labels),
85
+ metricValue: toMetricValue(value),
86
+ runtimeInfo: undefined,
87
+ })
88
+ }
89
+ }
90
+
91
+ export class Meter {
92
+ private readonly ctx: EthContext | SolanaContext
93
+
94
+ // TODO is map necessary since we are sending request remotely?
95
+ // counterMap = new Map<string, Counter>()
96
+ // gaugeMap = new Map<string, Gauge>()
97
+
98
+ constructor(ctx: EthContext | SolanaContext) {
99
+ this.ctx = ctx
100
+ }
101
+
102
+ Counter(name: string): Counter {
103
+ // let counter = this.counterMap.get(name)
104
+
105
+ // if (!counter) {
106
+ // counter = new Counter(name, this.ctx)
107
+ // }
108
+ // return counter
109
+
110
+ return new Counter(name, this.ctx)
111
+ }
112
+
113
+ Gauge(name: string): Gauge {
114
+ // let gauge = this.gaugeMap.get(name)
115
+ //
116
+ // if (!gauge) {
117
+ // gauge = new Gauge(name, this.ctx)
118
+ // }
119
+ // return gauge
120
+ return new Gauge(name, this.ctx)
121
+ }
122
+ }
@@ -0,0 +1,82 @@
1
+ import { expect } from 'chai'
2
+ import { toBigInteger, toMetricValue } from './numberish'
3
+ import { webcrypto } from 'crypto'
4
+ import { BigNumber } from 'ethers'
5
+ import { performance } from 'perf_hooks'
6
+ import { BigInteger } from './gen/processor/protos/processor'
7
+ import { BigDecimal } from './index'
8
+
9
+ // TODO add test for type conversion
10
+ describe('Numberish tests', () => {
11
+ const values: bigint[] = [0n, -0n, 3815372408723498172304781320847103784n, 2132n, -18708707n, 123n << 100n]
12
+
13
+ test('big integer conversion correctness ', async () => {
14
+ for (const v of values) {
15
+ const b = toBigInteger(v)
16
+ const hex1 = BigIntegerToHex(b)
17
+ const hex2 = v.toString(16)
18
+ expect(hex1).equals(hex2)
19
+ }
20
+ })
21
+
22
+ test('random big integer conversion correctness ', async () => {
23
+ for (let i = 0; i < 1000; i++) {
24
+ const random = webcrypto.getRandomValues(new Uint8Array(256))
25
+ const v = BigNumber.from(random).toBigInt()
26
+ const b = toBigInteger(v)
27
+
28
+ expect(BigNumber.from(b.data).eq(v)).to.equals(true)
29
+ }
30
+ })
31
+
32
+ test.skip('random big integer performance', async () => {
33
+ jest.setTimeout(100000)
34
+ jest.retryTimes(3)
35
+
36
+ let timer1 = 0
37
+ let timer2 = 0
38
+ for (let i = 0; i < 1000; i++) {
39
+ // Use higher value for local debugging
40
+ const random = webcrypto.getRandomValues(new Uint8Array(256))
41
+ const v = BigNumber.from(random).toBigInt()
42
+
43
+ let start = performance.now()
44
+ toBigInteger(v)
45
+ timer1 += performance.now() - start
46
+
47
+ start = performance.now()
48
+ v.toString(16)
49
+ timer2 += performance.now() - start
50
+ }
51
+
52
+ console.log(timer1, timer2)
53
+ expect(timer1).to.lessThan(timer2 * 3)
54
+ })
55
+
56
+ test('metric values', async () => {
57
+ const longDec = '12.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002'
58
+ expect(toMetricValue(new BigDecimal(longDec)).bigDecimal == longDec)
59
+
60
+ const complexDec = '-7.350918e-428'
61
+ expect(toMetricValue(new BigDecimal(complexDec)).bigDecimal == complexDec)
62
+
63
+ expect(BigIntegerToBigInt(toMetricValue(new BigDecimal('100000')).bigInteger!) === 100000n)
64
+ })
65
+ })
66
+
67
+ // Performance very bad
68
+ function BigIntegerToHex(b: BigInteger): string {
69
+ let res = BigNumber.from(b.data).toBigInt().toString(16)
70
+ if (b.negative) {
71
+ res = '-' + res
72
+ }
73
+ return res
74
+ }
75
+
76
+ function BigIntegerToBigInt(b: BigInteger): bigint {
77
+ let res = BigNumber.from(b.data).toBigInt()
78
+ if (b.negative) {
79
+ res = -res
80
+ }
81
+ return res
82
+ }
@@ -0,0 +1,99 @@
1
+ import { BigNumber } from 'ethers'
2
+ import { BigInteger, MetricValue } from './gen/processor/protos/processor'
3
+ import { BigDecimal } from '.'
4
+ import { BN } from '@project-serum/anchor'
5
+
6
+ export type Numberish = number | BigNumber | bigint | BigDecimal //BigNumberish
7
+
8
+ export function toMetricValue(value: Numberish): MetricValue {
9
+ if (value instanceof BigNumber) {
10
+ return MetricValue.fromPartial({
11
+ bigInteger: toBigInteger(value.toBigInt()),
12
+ })
13
+ }
14
+ if (value instanceof BigDecimal) {
15
+ // Carefully consider the use case here
16
+ if (value.isInteger()) {
17
+ return MetricValue.fromPartial({
18
+ bigInteger: bigDecimalToBigInteger(value),
19
+ })
20
+ } else {
21
+ return MetricValue.fromPartial({
22
+ bigDecimal: value.toString(), // e.g. -7.350918e-428
23
+ })
24
+ }
25
+ }
26
+ if (BN.isBN(value)) {
27
+ return MetricValue.fromPartial({
28
+ bigInteger: bnToBigInteger(value),
29
+ })
30
+ }
31
+ if (typeof value === 'bigint' || Number.isInteger(value)) {
32
+ return MetricValue.fromPartial({
33
+ bigInteger: toBigInteger(value),
34
+ })
35
+ }
36
+
37
+ return MetricValue.fromPartial({
38
+ doubleValue: Number(value),
39
+ })
40
+ }
41
+ function bigDecimalToBigInteger(a: BigDecimal): BigInteger {
42
+ const negative = a.isNegative()
43
+ if (negative) {
44
+ a = a.abs()
45
+ }
46
+ return hexToBigInteger(a.toString(16), negative)
47
+ }
48
+
49
+ function bnToBigInteger(a: BN): BigInteger {
50
+ const negative = a.isNeg()
51
+ if (negative) {
52
+ a = a.abs()
53
+ }
54
+ return hexToBigInteger(a.toString(16), negative)
55
+ }
56
+
57
+ function intToBigInteger(a: bigint | number): BigInteger {
58
+ const negative = a < 0
59
+ if (negative) {
60
+ a = -a
61
+ }
62
+ return hexToBigInteger(a.toString(16), negative)
63
+ }
64
+
65
+ export function toBigInteger(a: Numberish): BigInteger {
66
+ if (a instanceof BigDecimal) {
67
+ return bigDecimalToBigInteger(a)
68
+ }
69
+ if (a instanceof BN) {
70
+ return bnToBigInteger(a)
71
+ }
72
+ if (a instanceof BigNumber) {
73
+ return intToBigInteger(a.toBigInt())
74
+ }
75
+ return intToBigInteger(a)
76
+
77
+ // Following code is actually very slow
78
+ // while (a > 0) {
79
+ // const d = a & 0xffn
80
+ // a >>= 8n
81
+ // value.push(Number(d))
82
+ // }
83
+ //
84
+ // return {
85
+ // negative, value: new Uint8Array(value.reverse()),
86
+ // }
87
+ }
88
+
89
+ function hexToBigInteger(hex: string, negative: boolean): BigInteger {
90
+ if (hex.length % 2 === 1) {
91
+ hex = '0' + hex
92
+ }
93
+ const buffer = Buffer.from(hex, 'hex')
94
+
95
+ return {
96
+ negative: negative,
97
+ data: new Uint8Array(buffer),
98
+ }
99
+ }
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env node
2
+
3
+ import commandLineArgs from 'command-line-args'
4
+ import { createServer } from 'nice-grpc'
5
+ import { ProcessorDefinition } from './gen/processor/protos/processor'
6
+ import { ProcessorServiceImpl } from './service'
7
+ import { setProvider } from './provider'
8
+
9
+ import path from 'path'
10
+ import fs from 'fs-extra'
11
+ import { ProcessorState } from './processor-state'
12
+
13
+ global.sentio_sdk = require('.')
14
+ global.PROCESSOR_STATE = new ProcessorState()
15
+
16
+ function tryRequire(name: string): { module: any; name: string; path: string } | undefined {
17
+ const req = eval('require')
18
+
19
+ try {
20
+ let path: string
21
+ try {
22
+ path = req.resolve(name, { paths: [process.cwd()] })
23
+ } catch {
24
+ path = req.resolve(name)
25
+ }
26
+
27
+ const module = { module: req(path), name, path }
28
+ console.log('Load successfully: ', name)
29
+ return module
30
+ } catch (err) {
31
+ if (err instanceof Error && err.message.startsWith(`Cannot find module '${name}'`)) {
32
+ // this error is expected
33
+ console.log("Couldn't load (expected): ", name)
34
+ return undefined
35
+ } else {
36
+ throw err
37
+ }
38
+ }
39
+ }
40
+
41
+ const optionDefinitions = [
42
+ { name: 'target', type: String, defaultOption: true },
43
+ { name: 'port', alias: 'p', type: String, defaultValue: '4000' },
44
+ { name: 'concurrency', type: Number, defaultValue: 4 },
45
+ { name: 'use-chainserver', type: Boolean, defaultValue: false },
46
+ {
47
+ name: 'chains-config',
48
+ alias: 'c',
49
+ type: String,
50
+ defaultValue: 'chains-config.json',
51
+ },
52
+ ]
53
+
54
+ const options = commandLineArgs(optionDefinitions, { partial: true })
55
+
56
+ console.log('loading', options.target)
57
+
58
+ const fullPath = path.resolve(options['chains-config'])
59
+ const chainsConfig = fs.readJsonSync(fullPath)
60
+
61
+ setProvider(chainsConfig, options.concurrency, options['use-chainserver'])
62
+
63
+ tryRequire(options.target)
64
+
65
+ console.log('Start Server', options)
66
+
67
+ console.log(global.PROCESSOR_STATE.processors.length, ' processors loaded')
68
+ console.log(global.PROCESSOR_STATE.solanaProcessors.length, ' solana processors loaded')
69
+
70
+ const server = createServer()
71
+
72
+ const service = new ProcessorServiceImpl(server.shutdown)
73
+ server.add(ProcessorDefinition, service)
74
+
75
+ server.listen('0.0.0.0:' + options.port)
@@ -0,0 +1,24 @@
1
+ import { BaseProcessor } from './base-processor'
2
+ import { BaseContract } from 'ethers'
3
+ import { BoundContractView, ContractView } from './context'
4
+ import { BaseProcessorTemplate } from './base-processor-template'
5
+ import { TemplateInstance } from './gen/processor/protos/processor'
6
+ import { Provider } from '@ethersproject/providers'
7
+ import { SolanaBaseProcessor } from './solana-processor'
8
+
9
+ export class ProcessorState {
10
+ // from abiName_address_chainId => contract wrapper
11
+ contracts = new Map<string, ContractView<BaseContract>>()
12
+ // all evm processors
13
+ processors: BaseProcessor<BaseContract, BoundContractView<BaseContract, any>>[] = []
14
+ // from abiName_options to contracts
15
+ processorMap = new Map<string, BaseProcessor<any, any>>()
16
+ // evm providers
17
+ providers = new Map<number, Provider>()
18
+ // evm processor templates
19
+ templates: BaseProcessorTemplate<BaseContract, BoundContractView<BaseContract, any>>[] = []
20
+ // evm processor template instances spec
21
+ templatesInstances: TemplateInstance[] = []
22
+
23
+ solanaProcessors: SolanaBaseProcessor[] = []
24
+ }
@@ -0,0 +1 @@
1
+ export type PromiseOrVoid = void | Promise<any>
@@ -0,0 +1,74 @@
1
+ import { getNetwork, Provider, StaticJsonRpcProvider } from '@ethersproject/providers'
2
+ import { Networkish } from '@ethersproject/networks'
3
+ import PQueue from 'p-queue'
4
+ import { ConnectionInfo } from '@ethersproject/web'
5
+ import { ChainConfig } from './chain-config'
6
+
7
+ export const DummyProvider = new StaticJsonRpcProvider(undefined, 1)
8
+
9
+ export function getProvider(networkish?: Networkish): Provider {
10
+ if (!networkish) {
11
+ networkish = 1
12
+ }
13
+ const network = getNetwork(networkish)
14
+
15
+ if (!global.PROCESSOR_STATE.providers) {
16
+ throw Error('Provider not set')
17
+ }
18
+ const value = global.PROCESSOR_STATE.providers.get(network.chainId)
19
+ if (value === undefined) {
20
+ throw Error(
21
+ 'Provider not found for chain ' +
22
+ network.chainId +
23
+ ', configured chains: ' +
24
+ [...global.PROCESSOR_STATE.providers.keys()].join(' ')
25
+ )
26
+ }
27
+ return value
28
+ }
29
+
30
+ export function setProvider(config: Record<string, ChainConfig>, concurrency = 4, useChainServer = false) {
31
+ global.PROCESSOR_STATE.providers = new Map<number, Provider>()
32
+
33
+ for (const chainIdStr in config) {
34
+ if (isNaN(Number.parseInt(chainIdStr))) {
35
+ continue
36
+ }
37
+
38
+ const chainConfig = config[chainIdStr]
39
+ const chainId = Number(chainIdStr)
40
+
41
+ // let providers: StaticJsonRpcProvider[] = []
42
+ // for (const http of chainConfig.Https) {
43
+ // providers.push(new StaticJsonRpcProvider(http, chainId))
44
+ // }
45
+ // random shuffle
46
+ // providers = providers.sort(() => Math.random() - 0.5)
47
+
48
+ // const provider = new FallbackProvider(providers)
49
+
50
+ let rpcAddress = ''
51
+ if (useChainServer && chainConfig.ChainServer) {
52
+ rpcAddress = chainConfig.ChainServer
53
+ } else {
54
+ const idx = Math.floor(Math.random() * chainConfig.Https.length)
55
+ rpcAddress = chainConfig.Https[idx]
56
+ }
57
+
58
+ const provider = new QueuedStaticJsonRpcProvider(rpcAddress, chainId, concurrency)
59
+ global.PROCESSOR_STATE.providers.set(chainId, provider)
60
+ }
61
+ }
62
+
63
+ class QueuedStaticJsonRpcProvider extends StaticJsonRpcProvider {
64
+ executor: PQueue
65
+
66
+ constructor(url: ConnectionInfo | string, network: Networkish, concurrency: number) {
67
+ super(url, network)
68
+ this.executor = new PQueue({ concurrency: concurrency })
69
+ }
70
+
71
+ send(method: string, params: Array<any>): Promise<any> {
72
+ return this.executor.add(() => super.send(method, params))
73
+ }
74
+ }