-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: destruct blocks into separate service
- Loading branch information
1 parent
970a6de
commit bf512f0
Showing
15 changed files
with
821 additions
and
733 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Dexie, { DexieOptions } from "dexie" | ||
|
||
import { AnyEVMBlock } from "../../networks" | ||
import { NetworkInterface } from "../../constants/networks/networkTypes" | ||
import { ChainDatabase } from "../chain/db" | ||
|
||
export class BlockDatabase extends Dexie { | ||
private blocks!: Dexie.Table<AnyEVMBlock, [string, string]> | ||
|
||
constructor(options?: DexieOptions) { | ||
super("pelagus/blocks", options) | ||
this.version(1).stores({ | ||
migrations: null, | ||
blocks: | ||
"&[hash+network.baseAsset.name],[network.baseAsset.name+timestamp],hash,network.baseAsset.name,timestamp,parentHash,blockHeight,[blockHeight+network.baseAsset.name]", | ||
}) | ||
} | ||
|
||
async getLatestBlock(network: NetworkInterface): Promise<AnyEVMBlock | null> { | ||
return ( | ||
( | ||
await this.blocks | ||
.where("[network.baseAsset.name+timestamp]") | ||
// Only query blocks from the last 86 seconds | ||
.aboveOrEqual([network.baseAsset.name, Date.now() - 60 * 60 * 24]) | ||
.and( | ||
(block) => block.network.baseAsset.name === network.baseAsset.name | ||
) | ||
.reverse() | ||
.sortBy("timestamp") | ||
)[0] || null | ||
) | ||
} | ||
|
||
async getBlock( | ||
network: NetworkInterface, | ||
blockHash: string | ||
): Promise<AnyEVMBlock | null> { | ||
return ( | ||
( | ||
await this.blocks | ||
.where("[hash+network.baseAsset.name]") | ||
.equals([blockHash, network.baseAsset.name]) | ||
.toArray() | ||
)[0] || null | ||
) | ||
} | ||
|
||
async addBlock(block: AnyEVMBlock): Promise<void> { | ||
await this.blocks.put(block) | ||
} | ||
} | ||
|
||
export function createDB(options?: DexieOptions): BlockDatabase { | ||
return new BlockDatabase(options) | ||
} |
Oops, something went wrong.