@@ -10,7 +10,13 @@ import {
1010 NetworkBaseAsset ,
1111} from "../../networks"
1212import { FungibleAsset } from "../../assets"
13- import { BASE_ASSETS , DEFAULT_NETWORKS , GOERLI , POLYGON } from "../../constants"
13+ import {
14+ BASE_ASSETS ,
15+ CHAIN_ID_TO_RPC_URLS ,
16+ DEFAULT_NETWORKS ,
17+ GOERLI ,
18+ POLYGON ,
19+ } from "../../constants"
1420
1521export type Transaction = AnyEVMTransaction & {
1622 dataSource : "alchemy" | "local"
@@ -73,6 +79,8 @@ export class ChainDatabase extends Dexie {
7379
7480 private baseAssets ! : Dexie . Table < NetworkBaseAsset , number >
7581
82+ private rpcUrls ! : Dexie . Table < { chainID : string ; rpcUrls : string [ ] } , string >
83+
7684 constructor ( options ?: DexieOptions ) {
7785 super ( "tally/chain" , options )
7886 this . version ( 1 ) . stores ( {
@@ -153,6 +161,16 @@ export class ChainDatabase extends Dexie {
153161 this . version ( 6 ) . stores ( {
154162 baseAssets : "&chainID,symbol,name" ,
155163 } )
164+
165+ this . version ( 7 ) . stores ( {
166+ rpcUrls : "&chainID, rpcUrls" ,
167+ } )
168+ }
169+
170+ async initialize ( ) : Promise < void > {
171+ await this . initializeBaseAssets ( )
172+ await this . initializeRPCs ( )
173+ await this . initializeEVMNetworks ( )
156174 }
157175
158176 async getLatestBlock ( network : Network ) : Promise < AnyEVMBlock | null > {
@@ -183,14 +201,70 @@ export class ChainDatabase extends Dexie {
183201 )
184202 }
185203
204+ async addEVMNetwork ( {
205+ chainName,
206+ chainID,
207+ decimals,
208+ symbol,
209+ assetName,
210+ rpcUrls,
211+ } : {
212+ chainName : string
213+ chainID : string
214+ decimals : number
215+ symbol : string
216+ assetName : string
217+ rpcUrls : string [ ]
218+ } ) : Promise < void > {
219+ await this . networks . put ( {
220+ name : chainName ,
221+ chainID,
222+ family : "EVM" ,
223+ baseAsset : {
224+ decimals,
225+ symbol,
226+ name : assetName ,
227+ chainID,
228+ } ,
229+ } )
230+ // A bit awkward that we are adding the base asset to the network as well
231+ // as to its own separate table - but lets forge on for now.
232+ await this . addBaseAsset ( assetName , symbol , chainID , decimals )
233+ await this . addRpcUrls ( chainID , rpcUrls )
234+ }
235+
186236 async getAllEVMNetworks ( ) : Promise < EVMNetwork [ ] > {
187237 return this . networks . where ( "family" ) . equals ( "EVM" ) . toArray ( )
188238 }
189239
240+ private async addBaseAsset (
241+ name : string ,
242+ symbol : string ,
243+ chainID : string ,
244+ decimals : number
245+ ) {
246+ await this . baseAssets . put ( {
247+ decimals,
248+ name,
249+ symbol,
250+ chainID,
251+ } )
252+ }
253+
190254 async getAllBaseAssets ( ) : Promise < NetworkBaseAsset [ ] > {
191255 return this . baseAssets . toArray ( )
192256 }
193257
258+ async initializeRPCs ( ) : Promise < void > {
259+ await Promise . all (
260+ Object . entries ( CHAIN_ID_TO_RPC_URLS ) . map ( async ( [ chainId , rpcUrls ] ) => {
261+ if ( rpcUrls ) {
262+ await this . addRpcUrls ( chainId , rpcUrls )
263+ }
264+ } )
265+ )
266+ }
267+
194268 async initializeBaseAssets ( ) : Promise < void > {
195269 await this . updateBaseAssets ( BASE_ASSETS )
196270 }
@@ -210,6 +284,31 @@ export class ChainDatabase extends Dexie {
210284 )
211285 }
212286
287+ async getRpcUrlsByChainId ( chainId : string ) : Promise < string [ ] > {
288+ const rpcUrls = await this . rpcUrls . where ( { chainId } ) . first ( )
289+ if ( rpcUrls ) {
290+ return rpcUrls . rpcUrls
291+ }
292+ throw new Error ( `No RPC Found for ${ chainId } ` )
293+ }
294+
295+ private async addRpcUrls ( chainID : string , rpcUrls : string [ ] ) : Promise < void > {
296+ const existingRpcUrlsForChain = await this . rpcUrls . get ( chainID )
297+ if ( existingRpcUrlsForChain ) {
298+ existingRpcUrlsForChain . rpcUrls . push ( ...rpcUrls )
299+ existingRpcUrlsForChain . rpcUrls = [
300+ ...new Set ( existingRpcUrlsForChain . rpcUrls ) ,
301+ ]
302+ await this . rpcUrls . put ( existingRpcUrlsForChain )
303+ } else {
304+ await this . rpcUrls . put ( { chainID, rpcUrls } )
305+ }
306+ }
307+
308+ async getAllRpcUrls ( ) : Promise < { chainID : string ; rpcUrls : string [ ] } [ ] > {
309+ return this . rpcUrls . toArray ( )
310+ }
311+
213312 async getAllSavedTransactionHashes ( ) : Promise < IndexableTypeArray > {
214313 return this . chainTransactions . orderBy ( "hash" ) . keys ( )
215314 }
0 commit comments