@@ -6,8 +6,23 @@ import {
66 TransactionLike ,
77} from "../../ckb/index.js" ;
88import { HexLike } from "../../hex/index.js" ;
9+ import { NumLike } from "../../num/index.js" ;
910import { ClientCollectableSearchKeyLike } from "../clientTypes.advanced.js" ;
11+ import {
12+ ClientBlock ,
13+ ClientBlockHeader ,
14+ ClientBlockHeaderLike ,
15+ ClientBlockLike ,
16+ ClientTransactionResponse ,
17+ ClientTransactionResponseLike ,
18+ } from "../clientTypes.js" ;
1019
20+ /**
21+ * @public
22+ * The ClientCache class is mainly designed for chained transactions.
23+ * Consumed & Created cells are "marked" so they can be correctly handled when composing transactions.
24+ * It also act as cache for rpc requests to reduce cost, but this is optional.
25+ */
1126export abstract class ClientCache {
1227 abstract markUsable ( ...cellLikes : ( CellLike | CellLike [ ] ) [ ] ) : Promise < void > ;
1328 abstract markUnusable (
@@ -17,7 +32,12 @@ export abstract class ClientCache {
1732 ...transactionLike : ( TransactionLike | TransactionLike [ ] ) [ ]
1833 ) : Promise < void > {
1934 await Promise . all ( [
20- this . recordTransactions ( ...transactionLike ) ,
35+ this . recordTransactionResponses (
36+ transactionLike . flat ( ) . map ( ( transaction ) => ( {
37+ transaction : transaction ,
38+ status : "sent" ,
39+ } ) ) ,
40+ ) ,
2141 ...transactionLike . flat ( ) . map ( ( transactionLike ) => {
2242 const tx = Transaction . from ( transactionLike ) ;
2343 const txHash = tx . hash ( ) ;
@@ -42,34 +62,121 @@ export abstract class ClientCache {
4262 abstract findCells (
4363 filter : ClientCollectableSearchKeyLike ,
4464 ) : AsyncGenerator < Cell > ;
65+ abstract isUnusable ( outPointLike : OutPointLike ) : Promise < boolean > ;
66+
67+ // ======
68+ // Following methods are for requests caching and optional.
69+ // ======
70+
71+ /**
72+ * Record known cells
73+ * Implement this method to enable cells query caching
74+ * @param _cells
75+ */
76+ async recordCells ( ..._cells : ( CellLike | CellLike [ ] ) [ ] ) : Promise < void > { }
4577 /**
4678 * Get a known cell by out point
79+ * Implement this method to enable cells query caching
4780 * @param _outPoint
4881 */
49- abstract getCell ( _outPoint : OutPointLike ) : Promise < Cell | undefined > ;
50- abstract isUnusable ( outPointLike : OutPointLike ) : Promise < boolean > ;
82+ async getCell ( _outPoint : OutPointLike ) : Promise < Cell | undefined > {
83+ return ;
84+ }
5185
5286 /**
53- * Record known transactions
87+ * Record known transaction responses.
5488 * Implement this method to enable transactions query caching
5589 * @param _transactions
5690 */
57- async recordTransactions (
58- ..._transactions : ( TransactionLike | TransactionLike [ ] ) [ ]
91+ async recordTransactionResponses (
92+ ..._transactions : (
93+ | ClientTransactionResponseLike
94+ | ClientTransactionResponseLike [ ]
95+ ) [ ]
5996 ) : Promise < void > { }
6097 /**
61- * Get a known transaction by hash
98+ * Get a known transaction response by hash
6299 * Implement this method to enable transactions query caching
63100 * @param _txHash
64101 */
65- async getTransaction ( _txHash : HexLike ) : Promise < Transaction | undefined > {
102+ async getTransactionResponse (
103+ _txHash : HexLike ,
104+ ) : Promise < ClientTransactionResponse | undefined > {
105+ return ;
106+ }
107+ /**
108+ * Record known transactions.
109+ * @param transactions
110+ */
111+ async recordTransactions (
112+ ...transactions : ( TransactionLike | TransactionLike [ ] ) [ ]
113+ ) : Promise < void > {
114+ return this . recordTransactionResponses (
115+ transactions . flat ( ) . map ( ( transaction ) => ( {
116+ transaction,
117+ status : "unknown" ,
118+ } ) ) ,
119+ ) ;
120+ }
121+ /**
122+ * Get a known transaction by hash
123+ * @param txHash
124+ */
125+ async getTransaction ( txHash : HexLike ) : Promise < Transaction | undefined > {
126+ return ( await this . getTransactionResponse ( txHash ) ) ?. transaction ;
127+ }
128+
129+ /**
130+ * Record known block headers.
131+ * Implement this method to enable block headers query caching
132+ * @param _headers
133+ */
134+ async recordHeaders (
135+ ..._headers : ( ClientBlockHeaderLike | ClientBlockHeaderLike [ ] ) [ ]
136+ ) : Promise < void > { }
137+ /**
138+ * Get a known block header by hash
139+ * Implement this method to enable block headers query caching
140+ * @param _hash
141+ */
142+ async getHeaderByHash (
143+ _hash : HexLike ,
144+ ) : Promise < ClientBlockHeader | undefined > {
145+ return ;
146+ }
147+ /**
148+ * Get a known block header by number
149+ * Implement this method to enable block headers query caching
150+ * @param _number
151+ */
152+ async getHeaderByNumber (
153+ _number : NumLike ,
154+ ) : Promise < ClientBlockHeader | undefined > {
66155 return ;
67156 }
68157
69158 /**
70- * Record known cells
71- * Implement this method to enable cells query caching
72- * @param _cells
159+ * Record known blocks.
160+ * Implement this method to enable blocks query caching
161+ * @param _blocks
73162 */
74- async recordCells ( ..._cells : ( CellLike | CellLike [ ] ) [ ] ) : Promise < void > { }
163+ async recordBlocks (
164+ ..._blocks : ( ClientBlockLike | ClientBlockLike [ ] ) [ ]
165+ ) : Promise < void > { }
166+ /**
167+ * Get a known block header by hash
168+ * Implement this method to enable block headers query caching
169+ * @param _hash
170+ */
171+ async getBlockByHash ( _hash : HexLike ) : Promise < ClientBlock | undefined > {
172+ return ;
173+ }
174+ /**
175+ * Get a known block header by number
176+ * Implement this method to enable block headers query caching
177+ * @param _number
178+ */
179+ async getBlockByNumber ( _number : NumLike ) : Promise < ClientBlock | undefined > {
180+ return ;
181+ }
75182}
0 commit comments