@subsquid/portal-client 0.3.0 → 0.4.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 (131) hide show
  1. package/lib/client.d.ts +40 -18
  2. package/lib/client.d.ts.map +1 -1
  3. package/lib/client.js +199 -222
  4. package/lib/client.js.map +1 -1
  5. package/lib/query/common/data.d.ts +21 -0
  6. package/lib/query/common/data.d.ts.map +1 -0
  7. package/lib/query/common/data.js +3 -0
  8. package/lib/query/common/data.js.map +1 -0
  9. package/lib/query/common/query.d.ts +7 -0
  10. package/lib/query/common/query.d.ts.map +1 -0
  11. package/lib/query/common/query.js +3 -0
  12. package/lib/query/common/query.js.map +1 -0
  13. package/lib/query/evm/data.d.ts +159 -0
  14. package/lib/query/evm/data.d.ts.map +1 -0
  15. package/lib/query/evm/data.js +3 -0
  16. package/lib/query/evm/data.js.map +1 -0
  17. package/lib/query/evm/fields.d.ts +50 -0
  18. package/lib/query/evm/fields.d.ts.map +1 -0
  19. package/lib/query/evm/fields.js +3 -0
  20. package/lib/query/evm/fields.js.map +1 -0
  21. package/lib/query/evm/index.d.ts +4 -0
  22. package/lib/query/evm/index.d.ts.map +1 -0
  23. package/lib/query/evm/index.js +7 -0
  24. package/lib/query/evm/index.js.map +1 -0
  25. package/lib/query/evm/query.d.ts +54 -0
  26. package/lib/query/evm/query.d.ts.map +1 -0
  27. package/lib/query/evm/query.js +3 -0
  28. package/lib/query/evm/query.js.map +1 -0
  29. package/lib/query/evm/schema.d.ts +322 -0
  30. package/lib/query/evm/schema.d.ts.map +1 -0
  31. package/lib/query/evm/schema.js +224 -0
  32. package/lib/query/evm/schema.js.map +1 -0
  33. package/lib/query/index.d.ts +576 -8
  34. package/lib/query/index.d.ts.map +1 -1
  35. package/lib/query/index.js +15 -24
  36. package/lib/query/index.js.map +1 -1
  37. package/lib/query/solana/data.d.ts +115 -0
  38. package/lib/query/solana/data.d.ts.map +1 -0
  39. package/lib/query/solana/data.js +3 -0
  40. package/lib/query/solana/data.js.map +1 -0
  41. package/lib/query/solana/fields.d.ts +31 -0
  42. package/lib/query/solana/fields.d.ts.map +1 -0
  43. package/lib/query/solana/fields.js +3 -0
  44. package/lib/query/solana/fields.js.map +1 -0
  45. package/lib/query/solana/index.d.ts +4 -0
  46. package/lib/query/solana/index.d.ts.map +1 -0
  47. package/lib/query/solana/index.js +7 -0
  48. package/lib/query/solana/index.js.map +1 -0
  49. package/lib/query/solana/query.d.ts +74 -0
  50. package/lib/query/solana/query.d.ts.map +1 -0
  51. package/lib/query/solana/query.js +3 -0
  52. package/lib/query/solana/query.js.map +1 -0
  53. package/lib/query/solana/schema.d.ts +160 -0
  54. package/lib/query/solana/schema.d.ts.map +1 -0
  55. package/lib/query/solana/schema.js +130 -0
  56. package/lib/query/solana/schema.js.map +1 -0
  57. package/lib/query/substrate/data.d.ts +98 -0
  58. package/lib/query/substrate/data.d.ts.map +1 -0
  59. package/lib/query/substrate/data.js +3 -0
  60. package/lib/query/substrate/data.js.map +1 -0
  61. package/lib/query/substrate/fields.d.ts +22 -0
  62. package/lib/query/substrate/fields.d.ts.map +1 -0
  63. package/lib/query/substrate/fields.js +3 -0
  64. package/lib/query/substrate/fields.js.map +1 -0
  65. package/lib/query/substrate/index.d.ts +4 -0
  66. package/lib/query/substrate/index.d.ts.map +1 -0
  67. package/lib/query/substrate/index.js +7 -0
  68. package/lib/query/substrate/index.js.map +1 -0
  69. package/lib/query/substrate/query.d.ts +50 -0
  70. package/lib/query/substrate/query.d.ts.map +1 -0
  71. package/lib/query/substrate/query.js +3 -0
  72. package/lib/query/substrate/query.js.map +1 -0
  73. package/lib/query/substrate/schema.d.ts +102 -0
  74. package/lib/query/substrate/schema.d.ts.map +1 -0
  75. package/lib/query/substrate/schema.js +88 -0
  76. package/lib/query/substrate/schema.js.map +1 -0
  77. package/lib/query/type-util.d.ts +13 -0
  78. package/lib/query/type-util.d.ts.map +1 -0
  79. package/lib/query/type-util.js +3 -0
  80. package/lib/query/type-util.js.map +1 -0
  81. package/lib/query/util.d.ts +5 -0
  82. package/lib/query/util.d.ts.map +1 -0
  83. package/lib/query/util.js +23 -0
  84. package/lib/query/util.js.map +1 -0
  85. package/lib/util.d.ts +19 -0
  86. package/lib/util.d.ts.map +1 -0
  87. package/lib/util.js +57 -0
  88. package/lib/util.js.map +1 -0
  89. package/package.json +3 -3
  90. package/src/client.ts +261 -278
  91. package/src/query/common/data.ts +24 -0
  92. package/src/query/common/query.ts +6 -0
  93. package/src/query/evm/data.ts +182 -0
  94. package/src/query/evm/fields.ts +105 -0
  95. package/src/query/evm/index.ts +3 -0
  96. package/src/query/evm/query.ts +59 -0
  97. package/src/query/evm/schema.ts +277 -0
  98. package/src/query/index.ts +19 -36
  99. package/src/query/solana/data.ts +132 -0
  100. package/src/query/solana/fields.ts +42 -0
  101. package/src/query/solana/index.ts +3 -0
  102. package/src/query/solana/query.ts +89 -0
  103. package/src/query/solana/schema.ts +164 -0
  104. package/src/query/substrate/data.ts +101 -0
  105. package/src/query/substrate/fields.ts +30 -0
  106. package/src/query/substrate/index.ts +3 -0
  107. package/src/query/substrate/query.ts +60 -0
  108. package/src/query/substrate/schema.ts +114 -0
  109. package/src/query/type-util.ts +25 -0
  110. package/src/query/util.ts +23 -0
  111. package/src/util.ts +56 -0
  112. package/lib/query/common.d.ts +0 -56
  113. package/lib/query/common.d.ts.map +0 -1
  114. package/lib/query/common.js +0 -16
  115. package/lib/query/common.js.map +0 -1
  116. package/lib/query/evm.d.ts +0 -267
  117. package/lib/query/evm.d.ts.map +0 -1
  118. package/lib/query/evm.js +0 -245
  119. package/lib/query/evm.js.map +0 -1
  120. package/lib/query/solana.d.ts +0 -224
  121. package/lib/query/solana.d.ts.map +0 -1
  122. package/lib/query/solana.js +0 -121
  123. package/lib/query/solana.js.map +0 -1
  124. package/lib/query/substrate.d.ts +0 -173
  125. package/lib/query/substrate.d.ts.map +0 -1
  126. package/lib/query/substrate.js +0 -71
  127. package/lib/query/substrate.js.map +0 -1
  128. package/src/query/common.ts +0 -83
  129. package/src/query/evm.ts +0 -677
  130. package/src/query/solana.ts +0 -438
  131. package/src/query/substrate.ts +0 -288
@@ -1,50 +1,33 @@
1
1
  import {unexpectedCase} from '@subsquid/util-internal'
2
- import {Simplify} from './common'
3
2
  import * as evm from './evm'
4
3
  import * as solana from './solana'
5
4
  import * as substrate from './substrate'
6
- import {Validator} from '@subsquid/util-internal-validation'
7
5
 
8
- export type {PortalBlock, PortalQuery} from './common'
9
- export type {evm, solana, substrate}
6
+ export {evm, solana, substrate}
10
7
 
11
- export type Query = evm.Query | solana.Query | substrate.Query
8
+ export type GetQueryBlock<Q> = Q extends evm.Query<infer F>
9
+ ? evm.Block<F>
10
+ : Q extends solana.Query<infer F>
11
+ ? solana.Block<F>
12
+ : Q extends substrate.Query<infer F>
13
+ ? substrate.Block<F>
14
+ : never
12
15
 
13
- export type GetBlock<Q extends Query> = Q extends evm.Query
14
- ? evm.Block<Q['fields']>
15
- : Q extends solana.Query
16
- ? solana.Block<Q['fields']>
17
- : substrate.Block<Q['fields']>
18
-
19
- export function createQuery<Q extends Query>(query: Q): Simplify<Q & Query> {
20
- return {
21
- ...query,
22
- type: query.type,
23
- fields: query.fields,
24
- }
25
- }
26
-
27
- const BLOCK_SCHEMAS = new WeakMap<Query, Validator<any, any>>()
28
-
29
- export function getBlockSchema<Q extends Query>(query: Q): Validator<GetBlock<Q>, any> {
30
- let schema = BLOCK_SCHEMAS.get(query)
31
- if (schema) return schema
16
+ export type AnyQuery = evm.Query | solana.Query | substrate.Query
32
17
 
18
+ export function getQuery<Q extends AnyQuery>(query: Q) {
19
+ query = {...query}
33
20
  switch (query.type) {
34
- case 'solana':
35
- schema = solana.getBlockSchema(query.fields)
36
- break
37
21
  case 'evm':
38
- schema = evm.getBlockSchema(query.fields)
39
- break
22
+ query.fields = evm.patchQueryFields(query.fields ?? {})
23
+ return [query, evm.getBlockSchema(query.fields)] as const
24
+ case 'solana':
25
+ query.fields = solana.patchQueryFields(query.fields ?? {})
26
+ return [query, solana.getBlockSchema(query.fields)] as const
40
27
  case 'substrate':
41
- schema = substrate.getBlockSchema(query.fields)
42
- break
28
+ query.fields = substrate.patchQueryFields(query.fields ?? {})
29
+ return [query, substrate.getBlockSchema(query.fields)] as const
43
30
  default:
44
- throw unexpectedCase(query['type'])
31
+ throw unexpectedCase((query as any).type)
45
32
  }
46
-
47
- BLOCK_SCHEMAS.set(query, schema)
48
-
49
- return schema
50
33
  }
@@ -0,0 +1,132 @@
1
+ import type {Base58Bytes} from '../common/data'
2
+
3
+ export interface BlockHeader {
4
+ number: number
5
+ hash: Base58Bytes
6
+ parentNumber: number
7
+ parentHash: Base58Bytes
8
+ height: number
9
+ timestamp: number
10
+ }
11
+
12
+ export interface Transaction {
13
+ /**
14
+ * Transaction position in block
15
+ */
16
+ transactionIndex: number
17
+ version: 'legacy' | number
18
+ // transaction message
19
+ accountKeys: Base58Bytes[]
20
+ addressTableLookups: AddressTableLookup[]
21
+ numReadonlySignedAccounts: number
22
+ numReadonlyUnsignedAccounts: number
23
+ numRequiredSignatures: number
24
+ recentBlockhash: Base58Bytes
25
+ signatures: Base58Bytes[]
26
+ // meta fields
27
+ err: null | object
28
+ computeUnitsConsumed: bigint
29
+ fee: bigint
30
+ loadedAddresses: {
31
+ readonly: Base58Bytes[]
32
+ writable: Base58Bytes[]
33
+ }
34
+ hasDroppedLogMessages: boolean
35
+ }
36
+
37
+ export interface AddressTableLookup {
38
+ accountKey: Base58Bytes
39
+ readonlyIndexes: number[]
40
+ writableIndexes: number[]
41
+ }
42
+
43
+ export interface Instruction {
44
+ transactionIndex: number
45
+ instructionAddress: number[]
46
+ programId: Base58Bytes
47
+ accounts: Base58Bytes[]
48
+ data: Base58Bytes
49
+ // execution result extracted from logs
50
+ computeUnitsConsumed?: bigint
51
+ error?: string
52
+ /**
53
+ * `true` when transaction completed successfully, `false` otherwise
54
+ */
55
+ isCommitted: boolean
56
+ hasDroppedLogMessages: boolean
57
+ }
58
+
59
+ export interface LogMessage {
60
+ transactionIndex: number
61
+ logIndex: number
62
+ instructionAddress: number[]
63
+ programId: Base58Bytes
64
+ kind: 'log' | 'data' | 'other'
65
+ message: string
66
+ }
67
+
68
+ export interface Balance {
69
+ transactionIndex: number
70
+ account: Base58Bytes
71
+ pre: bigint
72
+ post: bigint
73
+ }
74
+
75
+ export type TokenBalance = PreTokenBalance | PostTokenBalance | PrePostTokenBalance
76
+
77
+ export interface PreTokenBalance {
78
+ transactionIndex: number
79
+ account: Base58Bytes
80
+
81
+ preProgramId?: Base58Bytes
82
+ preMint: Base58Bytes
83
+ preDecimals: number
84
+ preOwner?: Base58Bytes
85
+ preAmount: bigint
86
+
87
+ postProgramId?: undefined
88
+ postMint?: undefined
89
+ postDecimals?: undefined
90
+ postOwner?: undefined
91
+ postAmount?: undefined
92
+ }
93
+
94
+ export interface PostTokenBalance {
95
+ transactionIndex: number
96
+ account: Base58Bytes
97
+
98
+ preProgramId?: undefined
99
+ preMint?: undefined
100
+ preDecimals?: undefined
101
+ preOwner?: undefined
102
+ preAmount?: undefined
103
+
104
+ postProgramId?: Base58Bytes
105
+ postMint: Base58Bytes
106
+ postDecimals: number
107
+ postOwner?: Base58Bytes
108
+ postAmount: bigint
109
+ }
110
+
111
+ export interface PrePostTokenBalance {
112
+ transactionIndex: number
113
+ account: Base58Bytes
114
+ preProgramId?: Base58Bytes
115
+ preMint: Base58Bytes
116
+ preDecimals: number
117
+ preOwner?: Base58Bytes
118
+ preAmount: bigint
119
+ postProgramId?: Base58Bytes
120
+ postMint: Base58Bytes
121
+ postDecimals: number
122
+ postOwner?: Base58Bytes
123
+ postAmount: bigint
124
+ }
125
+
126
+ export interface Reward {
127
+ pubkey: Base58Bytes
128
+ lamports: bigint
129
+ postBalance: bigint
130
+ rewardType?: string
131
+ commission?: number
132
+ }
@@ -0,0 +1,42 @@
1
+ import type {GetFields, Select, Selector, Simplify} from '../type-util'
2
+ import type * as data from './data'
3
+
4
+ type BlockRequiredFields = 'number' | 'hash' | 'parentHash' | 'parentNumber'
5
+
6
+ export interface FieldSelection {
7
+ block?: Selector<Exclude<keyof data.BlockHeader, BlockRequiredFields>>
8
+ transaction?: Selector<keyof data.Transaction>
9
+ instruction?: Selector<keyof data.Instruction>
10
+ log?: Selector<keyof data.LogMessage>
11
+ balance?: Selector<keyof data.Balance>
12
+ tokenBalance?: Selector<keyof data.TokenBalance>
13
+ reward?: Selector<keyof data.Reward>
14
+ }
15
+
16
+ export type BlockHeader<F extends FieldSelection> = Simplify<
17
+ Pick<data.BlockHeader, BlockRequiredFields> & Select<data.BlockHeader, GetFields<F['block']>>
18
+ >
19
+
20
+ type Item<T, F extends FieldSelection, K extends keyof F> = Select<T, GetFields<F[K]>>
21
+
22
+ export type Transaction<F extends FieldSelection> = Item<data.Transaction, F, 'transaction'>
23
+
24
+ export type Instruction<F extends FieldSelection> = Item<data.Instruction, F, 'instruction'>
25
+
26
+ export type LogMessage<F extends FieldSelection> = Item<data.LogMessage, F, 'log'>
27
+
28
+ export type Balance<F extends FieldSelection> = Item<data.Balance, F, 'balance'>
29
+
30
+ export type TokenBalance<F extends FieldSelection> = Item<data.TokenBalance, F, 'tokenBalance'>
31
+
32
+ export type Reward<F extends FieldSelection> = Item<data.Reward, F, 'reward'>
33
+
34
+ export interface Block<F extends FieldSelection> {
35
+ header: BlockHeader<F>
36
+ transactions: Transaction<F>[]
37
+ instructions: Instruction<F>[]
38
+ logs: LogMessage<F>[]
39
+ balances: Balance<F>[]
40
+ tokenBalances: TokenBalance<F>[]
41
+ rewards: Reward<F>[]
42
+ }
@@ -0,0 +1,3 @@
1
+ export type * from './fields'
2
+ export type * from './query'
3
+ export {getBlockSchema, patchQueryFields} from './schema'
@@ -0,0 +1,89 @@
1
+ import type {QueryBase} from '../common/query'
2
+ import type {Base58Bytes} from '../common/data'
3
+ import type {LogMessage} from './data'
4
+ import type {FieldSelection} from './fields'
5
+
6
+
7
+ export interface Query<F extends FieldSelection = FieldSelection> extends QueryBase, ItemQuery {
8
+ type: 'solana'
9
+ fields: F
10
+ }
11
+
12
+
13
+ export interface ItemQuery {
14
+ transactions?: TransactionRequest[]
15
+ instructions?: InstructionRequest[]
16
+ logs?: LogRequest[]
17
+ balances?: BalanceRequest[]
18
+ tokenBalances?: TokenBalanceRequest[]
19
+ rewards?: RewardRequest[]
20
+ }
21
+
22
+
23
+ export interface TransactionRequest {
24
+ feePayer?: Base58Bytes[]
25
+ instructions?: boolean
26
+ logs?: boolean
27
+ }
28
+
29
+
30
+ export interface InstructionRequest {
31
+ programId?: Base58Bytes[]
32
+ discriminator?: string[]
33
+ a0?: Base58Bytes[]
34
+ a1?: Base58Bytes[]
35
+ a2?: Base58Bytes[]
36
+ a3?: Base58Bytes[]
37
+ a4?: Base58Bytes[]
38
+ a5?: Base58Bytes[]
39
+ a6?: Base58Bytes[]
40
+ a7?: Base58Bytes[]
41
+ a8?: Base58Bytes[]
42
+ a9?: Base58Bytes[]
43
+ a10?: Base58Bytes[]
44
+ a11?: Base58Bytes[]
45
+ a12?: Base58Bytes[]
46
+ a13?: Base58Bytes[]
47
+ a14?: Base58Bytes[]
48
+ a15?: Base58Bytes[]
49
+ isCommitted?: boolean
50
+ transaction?: boolean
51
+ transactionBalances?: boolean
52
+ transactionTokenBalances?: boolean
53
+ transactionInstructions?: boolean
54
+ logs?: boolean
55
+ innerInstructions?: boolean
56
+ }
57
+
58
+
59
+ export interface LogRequest {
60
+ programId?: Base58Bytes[]
61
+ kind?: LogMessage['kind'][]
62
+ transaction?: boolean
63
+ instruction?: boolean
64
+ }
65
+
66
+
67
+ export interface BalanceRequest {
68
+ account?: Base58Bytes[]
69
+ transaction?: boolean
70
+ transactionInstructions?: boolean
71
+ }
72
+
73
+
74
+ export interface TokenBalanceRequest {
75
+ account?: Base58Bytes[]
76
+ preProgramId?: Base58Bytes[]
77
+ postProgramId?: Base58Bytes[]
78
+ preMint?: Base58Bytes[]
79
+ postMint?: Base58Bytes[]
80
+ preOwner?: Base58Bytes[]
81
+ postOwner?: Base58Bytes[]
82
+ transaction?: boolean
83
+ transactionInstructions?: boolean
84
+ }
85
+
86
+
87
+ export interface RewardRequest {
88
+ pubkey?: Base58Bytes[]
89
+ }
@@ -0,0 +1,164 @@
1
+ import {
2
+ ANY_OBJECT,
3
+ array,
4
+ B58,
5
+ BIG_NAT,
6
+ BOOLEAN,
7
+ constant,
8
+ NAT,
9
+ nullable,
10
+ object,
11
+ oneOf,
12
+ option,
13
+ STRING,
14
+ withDefault,
15
+ } from '@subsquid/util-internal-validation'
16
+ import {project} from '../util'
17
+ import type {FieldSelection} from './fields'
18
+
19
+ export function patchQueryFields(fields: FieldSelection): FieldSelection {
20
+ fields = {...fields}
21
+ let {height, ...block} = (fields.block as any) ?? {}
22
+ fields.block = {
23
+ ...block,
24
+ number: true,
25
+ hash: true,
26
+ parentHash: true,
27
+ }
28
+ return fields
29
+ }
30
+
31
+ export function getBlockSchema(fields: FieldSelection) {
32
+ return object({
33
+ header: getBlockHeaderSchema(fields),
34
+ transactions: withDefault([], array(getTransactionSchema(fields))),
35
+ instructions: withDefault([], array(getInstructionSchema(fields))),
36
+ logs: withDefault([], array(getLogSchema(fields))),
37
+ balances: withDefault([], array(getBalanceSchema(fields))),
38
+ tokenBalances: withDefault([], array(getTokenBalanceSchema(fields))),
39
+ rewards: withDefault([], array(getRewardSchema(fields))),
40
+ })
41
+ }
42
+
43
+ function getBlockHeaderSchema(fields: FieldSelection) {
44
+ return object({
45
+ number: NAT,
46
+ hash: B58,
47
+ parentHash: B58,
48
+ ...project(fields.block, {
49
+ parentSlot: NAT,
50
+ height: NAT,
51
+ timestamp: NAT,
52
+ }),
53
+ })
54
+ }
55
+
56
+ const AddressTableLookup = object({
57
+ accountKey: B58,
58
+ readonlyIndexes: array(NAT),
59
+ writableIndexes: array(NAT),
60
+ })
61
+
62
+ function getTransactionSchema(fields: FieldSelection) {
63
+ return object(
64
+ project(fields.transaction, {
65
+ transactionIndex: NAT,
66
+ version: oneOf({
67
+ legacy: constant('legacy'),
68
+ versionNumber: NAT,
69
+ }),
70
+ accountKeys: array(B58),
71
+ addressTableLookups: array(AddressTableLookup),
72
+ numReadonlySignedAccounts: NAT,
73
+ numReadonlyUnsignedAccounts: NAT,
74
+ numRequiredSignatures: NAT,
75
+ recentBlockhash: B58,
76
+ signatures: array(B58),
77
+ err: nullable(ANY_OBJECT),
78
+ computeUnitsConsumed: BIG_NAT,
79
+ fee: BIG_NAT,
80
+ loadedAddresses: option(
81
+ object({
82
+ readonly: array(B58),
83
+ writable: array(B58),
84
+ })
85
+ ),
86
+ hasDroppedLogMessages: BOOLEAN,
87
+ })
88
+ )
89
+ }
90
+
91
+ function getInstructionSchema(fields: FieldSelection) {
92
+ return object(
93
+ project(fields.instruction, {
94
+ transactionIndex: NAT,
95
+ instructionAddress: array(NAT),
96
+ programId: B58,
97
+ accounts: array(B58),
98
+ data: B58,
99
+ computeUnitsConsumed: option(BIG_NAT),
100
+ error: option(STRING),
101
+ isCommitted: BOOLEAN,
102
+ hasDroppedLogMessages: BOOLEAN,
103
+ })
104
+ )
105
+ }
106
+
107
+ function getLogSchema(fields: FieldSelection) {
108
+ return object(
109
+ project(fields.log, {
110
+ transactionIndex: NAT,
111
+ logIndex: NAT,
112
+ instructionAddress: array(NAT),
113
+ programId: B58,
114
+ kind: oneOf({
115
+ log: constant('log'),
116
+ data: constant('data'),
117
+ other: constant('other'),
118
+ }),
119
+ message: STRING,
120
+ })
121
+ )
122
+ }
123
+
124
+ function getBalanceSchema(fields: FieldSelection) {
125
+ return object(
126
+ project(fields.balance, {
127
+ transactionIndex: NAT,
128
+ account: B58,
129
+ pre: BIG_NAT,
130
+ post: BIG_NAT,
131
+ })
132
+ )
133
+ }
134
+
135
+ function getTokenBalanceSchema(fields: FieldSelection) {
136
+ return object(
137
+ project(fields.tokenBalance, {
138
+ transactionIndex: NAT,
139
+ account: B58,
140
+ preProgramId: option(B58),
141
+ postProgramId: option(B58),
142
+ preMint: option(B58),
143
+ postMint: option(B58),
144
+ preDecimals: option(NAT),
145
+ postDecimals: option(NAT),
146
+ preOwner: option(B58),
147
+ postOwner: option(B58),
148
+ preAmount: option(BIG_NAT),
149
+ postAmount: option(BIG_NAT),
150
+ })
151
+ )
152
+ }
153
+
154
+ function getRewardSchema(fields: FieldSelection) {
155
+ return object(
156
+ project(fields.reward, {
157
+ pubkey: B58,
158
+ lamports: BIG_NAT,
159
+ postBalance: BIG_NAT,
160
+ rewardType: option(STRING),
161
+ commission: option(NAT),
162
+ })
163
+ )
164
+ }
@@ -0,0 +1,101 @@
1
+ import type {Bytes} from '../common/data'
2
+
3
+ /**
4
+ * @example 'Balances.Transfer'
5
+ */
6
+ export type QualifiedName = string & {}
7
+
8
+ export interface BlockHeader {
9
+ /**
10
+ * Block number (height)
11
+ */
12
+ number: number
13
+ /**
14
+ * Block hash
15
+ */
16
+ hash: Bytes
17
+ /**
18
+ * Hash of the parent block
19
+ */
20
+ parentHash: Bytes
21
+ /**
22
+ * Root hash of the state merkle tree
23
+ */
24
+ stateRoot: Bytes
25
+ /**
26
+ * Root hash of the extrinsics merkle tree
27
+ */
28
+ extrinsicsRoot: Bytes
29
+ digest: {logs: Bytes[]}
30
+ specName: string
31
+ specVersion: number
32
+ implName: string
33
+ implVersion: number
34
+ /**
35
+ * Block timestamp as set by `timestamp.now()` (unix epoch ms, compatible with `Date`).
36
+ */
37
+ timestamp?: number
38
+ /**
39
+ * Account address of block validator
40
+ */
41
+ validator?: Bytes
42
+ }
43
+
44
+ export interface ExtrinsicSignature {
45
+ address: Bytes
46
+ signature: unknown
47
+ signedExtensions: unknown
48
+ }
49
+
50
+ export interface Extrinsic {
51
+ /**
52
+ * Ordinal index in the extrinsics array of the current block
53
+ */
54
+ index: number
55
+ version: number
56
+ signature?: ExtrinsicSignature
57
+ fee?: bigint
58
+ tip?: bigint
59
+ error?: unknown
60
+ success?: boolean
61
+ /**
62
+ * Blake2b 128-bit hash of the raw extrinsic
63
+ */
64
+ hash?: Bytes
65
+ }
66
+
67
+ export interface Call {
68
+ extrinsicIndex: number
69
+ address: number[]
70
+ name: QualifiedName
71
+ args: unknown
72
+ origin?: unknown
73
+ /**
74
+ * Call error.
75
+ *
76
+ * Absence of error doesn't imply that the call was executed successfully,
77
+ * check {@link success} property for that.
78
+ */
79
+ error?: unknown
80
+ success?: boolean
81
+ }
82
+
83
+ export interface Event {
84
+ /**
85
+ * Ordinal index in the event array of the current block
86
+ */
87
+ index: number
88
+ /**
89
+ * Event name
90
+ */
91
+ name: QualifiedName
92
+ args: unknown
93
+ phase: 'Initialization' | 'ApplyExtrinsic' | 'Finalization'
94
+ extrinsicIndex?: number
95
+ callAddress?: number[]
96
+ /**
97
+ * This field is not supported by all currently deployed archives.
98
+ * Requesting it may cause internal error.
99
+ */
100
+ topics: Bytes[]
101
+ }
@@ -0,0 +1,30 @@
1
+ import type {GetFields, Select, Selector, Simplify} from '../type-util'
2
+ import type * as data from './data'
3
+
4
+ type BlockRequiredFields = 'number' | 'hash'
5
+
6
+ export interface FieldSelection {
7
+ block?: Selector<Exclude<keyof data.BlockHeader, BlockRequiredFields>>
8
+ extrinsic?: Selector<keyof data.Extrinsic>
9
+ call?: Selector<keyof data.Call>
10
+ event?: Selector<keyof data.Event>
11
+ }
12
+
13
+ export type BlockHeader<F extends FieldSelection> = Simplify<
14
+ Pick<data.BlockHeader, BlockRequiredFields> & Select<data.BlockHeader, GetFields<F['block']>>
15
+ >
16
+
17
+ type Item<T, F extends FieldSelection, K extends keyof F> = Select<T, GetFields<F[K]>>
18
+
19
+ export type Extrinsic<F extends FieldSelection> = Item<data.Extrinsic, F, 'extrinsic'>
20
+
21
+ export type Call<F extends FieldSelection> = Item<data.Call, F, 'call'>
22
+
23
+ export type Event<F extends FieldSelection> = Item<data.Event, F, 'event'>
24
+
25
+ export interface Block<F extends FieldSelection> {
26
+ header: BlockHeader<F>
27
+ extrinsics: Extrinsic<F>[]
28
+ calls: Call<F>[]
29
+ events: Event<F>[]
30
+ }
@@ -0,0 +1,3 @@
1
+ export type * from './fields'
2
+ export type * from './query'
3
+ export {getBlockSchema, patchQueryFields} from './schema'
@@ -0,0 +1,60 @@
1
+ import type {QueryBase} from '../common/query'
2
+ import type {Bytes} from '../common/data'
3
+ import type {FieldSelection} from './fields'
4
+ import type {QualifiedName} from './data'
5
+
6
+ export interface Query<F extends FieldSelection = FieldSelection> extends QueryBase, ItemQuery {
7
+ type: 'substrate'
8
+ fields?: F
9
+ }
10
+
11
+ export interface ItemQuery {
12
+ events?: EventRequest[]
13
+ calls?: CallRequest[]
14
+ evmLogs?: EvmLogRequest[]
15
+ ethereumTransactions?: EthereumTransactRequest[]
16
+ contractsEvents?: ContractsContractEmittedRequest[]
17
+ gearMessagesQueued?: GearMessageQueuedRequest[]
18
+ gearUserMessagesSent?: GearUserMessageSentRequest[]
19
+ }
20
+
21
+ export interface EventRelations {
22
+ extrinsic?: boolean
23
+ call?: boolean
24
+ stack?: boolean
25
+ }
26
+
27
+ export interface EventRequest extends EventRelations {
28
+ name?: QualifiedName[]
29
+ }
30
+
31
+ export interface CallRelations {
32
+ extrinsic?: boolean
33
+ stack?: boolean
34
+ events?: boolean
35
+ }
36
+
37
+ export interface CallRequest extends CallRelations {
38
+ name?: QualifiedName[]
39
+ }
40
+
41
+ export interface EvmLogRequest extends EventRelations {
42
+ address?: Bytes[]
43
+ }
44
+
45
+ export interface EthereumTransactRequest extends CallRelations {
46
+ to?: Bytes[]
47
+ sighash?: Bytes[]
48
+ }
49
+
50
+ export interface ContractsContractEmittedRequest extends EventRelations {
51
+ address?: Bytes[]
52
+ }
53
+
54
+ export interface GearMessageQueuedRequest extends EventRelations {
55
+ programId?: Bytes[]
56
+ }
57
+
58
+ export interface GearUserMessageSentRequest extends EventRelations {
59
+ programId?: Bytes[]
60
+ }