11import { type UUID , unknownToError } from "@happychain/common"
22import { bigIntToZeroPadded } from "@happychain/common"
33import { type Result , ResultAsync } from "neverthrow"
4- import { Randomness , type RandomnessStatus } from "./Randomness"
4+ import type { Randomness , RandomnessStatus } from "./Randomness"
55import { db } from "./db/driver"
6- import type { RandomnessRow } from "./db/types"
6+ import { randomnessEntityToRow , randomnessRowToEntity } from "./db/types"
77
88const COMMITMENT_PRUNE_INTERVAL_SECONDS = 120n // 2 minutes
99
@@ -13,30 +13,8 @@ export const DIGITS_MAX_UINT256 = 78
1313export class RandomnessRepository {
1414 private readonly map = new Map < bigint , Randomness > ( )
1515
16- private rowToEntity ( row : RandomnessRow ) : Randomness {
17- return new Randomness ( {
18- timestamp : BigInt ( row . timestamp ) ,
19- value : BigInt ( row . value ) ,
20- hashedValue : row . hashedValue ,
21- commitmentTransactionIntentId : row . commitmentTransactionIntentId ,
22- revealTransactionIntentId : row . revealTransactionIntentId ,
23- status : row . status ,
24- } )
25- }
26-
27- private entityToRow ( entity : Randomness ) : RandomnessRow {
28- return {
29- timestamp : bigIntToZeroPadded ( entity . timestamp , DIGITS_MAX_UINT256 ) ,
30- value : bigIntToZeroPadded ( entity . value , DIGITS_MAX_UINT256 ) ,
31- hashedValue : entity . hashedValue ,
32- commitmentTransactionIntentId : entity . commitmentTransactionIntentId ,
33- revealTransactionIntentId : entity . revealTransactionIntentId ,
34- status : entity . status ,
35- }
36- }
37-
3816 async start ( ) : Promise < void > {
39- const randomnessesDb = ( await db . selectFrom ( "randomnesses" ) . selectAll ( ) . execute ( ) ) . map ( this . rowToEntity )
17+ const randomnessesDb = ( await db . selectFrom ( "randomnesses" ) . selectAll ( ) . execute ( ) ) . map ( randomnessRowToEntity )
4018 for ( const randomness of randomnessesDb ) {
4119 this . map . set ( randomness . timestamp , randomness )
4220 }
@@ -70,7 +48,7 @@ export class RandomnessRepository {
7048 */
7149 async saveRandomness ( randomness : Randomness ) : Promise < Result < void , Error > > {
7250 this . map . set ( randomness . timestamp , randomness )
73- const row = this . entityToRow ( randomness )
51+ const row = randomnessEntityToRow ( randomness )
7452 return await ResultAsync . fromPromise ( db . insertInto ( "randomnesses" ) . values ( row ) . execute ( ) , unknownToError ) . map (
7553 ( ) => undefined ,
7654 )
@@ -82,7 +60,7 @@ export class RandomnessRepository {
8260 */
8361 async updateRandomness ( randomness : Randomness ) : Promise < Result < void , Error > > {
8462 this . map . set ( randomness . timestamp , randomness )
85- const row = this . entityToRow ( randomness )
63+ const row = randomnessEntityToRow ( randomness )
8664 return await ResultAsync . fromPromise (
8765 db
8866 . updateTable ( "randomnesses" )
0 commit comments