5
5
HexString ,
6
6
chicmozL2TxEffectDeluxeSchema ,
7
7
} from "@chicmoz-pkg/types" ;
8
- import { SQL , and , asc , eq , getTableColumns } from "drizzle-orm" ;
8
+ import assert from "assert" ;
9
+ import { SQL , and , asc , desc , eq , getTableColumns , gte , lt } from "drizzle-orm" ;
9
10
import { z } from "zod" ;
10
11
import { DB_MAX_TX_EFFECTS } from "../../../../environment.js" ;
11
12
import {
@@ -18,23 +19,24 @@ import {
18
19
} from "../../../database/schema/l2block/index.js" ;
19
20
20
21
enum GetTypes {
21
- BlockHeight ,
22
+ BlockHeightRange ,
22
23
BlockHeightAndIndex ,
23
24
}
24
25
25
26
type GetTxEffectByBlockHeightAndIndex = {
26
27
blockHeight : bigint ;
27
- txEffectIndex : number
28
+ txEffectIndex : number ;
28
29
getType : GetTypes . BlockHeightAndIndex ;
29
30
} ;
30
31
31
- type GetTxEffectsByBlockHeight = {
32
- blockHeight : bigint ;
33
- getType : GetTypes . BlockHeight ;
32
+ type GetTxEffectsByBlockHeightRange = {
33
+ from ?: bigint ;
34
+ to ?: bigint ;
35
+ getType : GetTypes . BlockHeightRange ;
34
36
} ;
35
37
36
38
export const getTxEffectNestedByHash = async (
37
- txEffectHash : string
39
+ txEffectHash : string ,
38
40
) : Promise < Pick < ChicmozL2TxEffect , "publicDataWrites" > > => {
39
41
const publicDataWrites = await db ( )
40
42
. select ( {
@@ -52,27 +54,52 @@ export const getTxEffectNestedByHash = async (
52
54
53
55
export const getTxEffectByBlockHeightAndIndex = async (
54
56
blockHeight : bigint ,
55
- txEffectIndex : number
57
+ txEffectIndex : number ,
56
58
) : Promise < ChicmozL2TxEffectDeluxe | null > => {
57
59
const res = await _getTxEffects ( {
58
60
blockHeight,
59
61
txEffectIndex,
60
62
getType : GetTypes . BlockHeightAndIndex ,
61
63
} ) ;
62
64
63
- if ( res . length === 0 ) return null ;
65
+ if ( res . length === 0 ) { return null ; }
64
66
65
67
return res [ 0 ] ;
66
68
} ;
67
69
68
70
export const getTxEffectsByBlockHeight = async (
69
- height : bigint
71
+ height : bigint ,
70
72
) : Promise < ChicmozL2TxEffectDeluxe [ ] > => {
71
- return _getTxEffects ( { blockHeight : height , getType : GetTypes . BlockHeight } ) ;
73
+ return _getTxEffects ( {
74
+ from : height ,
75
+ to : height ,
76
+ getType : GetTypes . BlockHeightRange ,
77
+ } ) ;
78
+ } ;
79
+
80
+ export const getLatestTxEffects = async ( ) : Promise <
81
+ ChicmozL2TxEffectDeluxe [ ]
82
+ > => {
83
+ return _getTxEffects ( {
84
+ getType : GetTypes . BlockHeightRange ,
85
+ } ) ;
86
+ } ;
87
+
88
+ const generateWhereQuery = ( from ?: bigint , to ?: bigint ) => {
89
+ if ( from && ! to ) {
90
+ return gte ( l2Block . height , from ) ;
91
+ } else if ( ! from && to ) {
92
+ return lt ( l2Block . height , to ) ;
93
+ }
94
+ assert (
95
+ from && to ,
96
+ "FATAL: cannot have both from and to undefined when generating where query" ,
97
+ ) ;
98
+ return and ( gte ( l2Block . height , from ) , lt ( l2Block . height , to ) ) ;
72
99
} ;
73
100
74
101
const _getTxEffects = async (
75
- args : GetTxEffectByBlockHeightAndIndex | GetTxEffectsByBlockHeight
102
+ args : GetTxEffectByBlockHeightAndIndex | GetTxEffectsByBlockHeightRange ,
76
103
) : Promise < ChicmozL2TxEffectDeluxe [ ] > => {
77
104
const joinQuery = db ( )
78
105
. select ( {
@@ -89,19 +116,25 @@ const _getTxEffects = async (
89
116
let whereQuery ;
90
117
91
118
switch ( args . getType ) {
92
- case GetTypes . BlockHeight :
93
- whereQuery = joinQuery
94
- . where ( eq ( l2Block . height , args . blockHeight ) )
95
- . orderBy ( asc ( txEffect . index ) )
96
- . limit ( DB_MAX_TX_EFFECTS ) ;
119
+ case GetTypes . BlockHeightRange :
120
+ if ( args . from ?? args . to ) {
121
+ whereQuery = joinQuery
122
+ . where ( generateWhereQuery ( args . from , args . to ) )
123
+ . orderBy ( desc ( txEffect . index ) , desc ( l2Block . height ) )
124
+ . limit ( DB_MAX_TX_EFFECTS ) ;
125
+ } else {
126
+ whereQuery = joinQuery
127
+ . orderBy ( desc ( txEffect . index ) , desc ( l2Block . height ) )
128
+ . limit ( DB_MAX_TX_EFFECTS ) ;
129
+ }
97
130
break ;
98
131
case GetTypes . BlockHeightAndIndex :
99
132
whereQuery = joinQuery
100
133
. where (
101
134
and (
102
135
eq ( l2Block . height , args . blockHeight ) ,
103
- eq ( txEffect . index , args . txEffectIndex )
104
- )
136
+ eq ( txEffect . index , args . txEffectIndex ) ,
137
+ ) ,
105
138
)
106
139
. limit ( 1 ) ;
107
140
break ;
@@ -117,26 +150,26 @@ const _getTxEffects = async (
117
150
txBirthTimestamp : txEffect . txBirthTimestamp . valueOf ( ) ,
118
151
...nestedData ,
119
152
} ;
120
- } )
153
+ } ) ,
121
154
) ;
122
155
123
156
return z . array ( chicmozL2TxEffectDeluxeSchema ) . parse ( txEffects ) ;
124
157
} ;
125
158
126
159
export const getTxEffectByTxHash = async (
127
- txHash : HexString
160
+ txHash : HexString ,
128
161
) : Promise < ChicmozL2TxEffectDeluxe | null > => {
129
162
return getTxEffectDynamicWhere ( eq ( txEffect . txHash , txHash ) ) ;
130
163
} ;
131
164
132
165
export const getTxEffectByHash = async (
133
- hash : HexString
166
+ hash : HexString ,
134
167
) : Promise < ChicmozL2TxEffectDeluxe | null > => {
135
168
return getTxEffectDynamicWhere ( eq ( txEffect . txHash , hash ) ) ;
136
169
} ;
137
170
138
171
export const getTxEffectDynamicWhere = async (
139
- whereMatcher : SQL < unknown >
172
+ whereMatcher : SQL < unknown > ,
140
173
) : Promise < ChicmozL2TxEffectDeluxe | null > => {
141
174
const dbRes = await db ( )
142
175
. select ( {
@@ -153,7 +186,7 @@ export const getTxEffectDynamicWhere = async (
153
186
. limit ( 1 )
154
187
. execute ( ) ;
155
188
156
- if ( dbRes . length === 0 ) return null ;
189
+ if ( dbRes . length === 0 ) { return null ; }
157
190
158
191
const nestedData = await getTxEffectNestedByHash ( dbRes [ 0 ] . txHash ) ;
159
192
0 commit comments