From fcdf21b384f92943f845d84317f054bb001a7f55 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Mon, 4 Nov 2024 17:15:33 +0100 Subject: [PATCH 1/9] feat: txm hooks --- packages/randomness-service/src/index.ts | 7 ++- packages/randomness-service/tsconfig.json | 6 ++- packages/transaction-manager/lib/EventBus.ts | 1 + .../transaction-manager/lib/HookManager.ts | 50 +++++++++++++++++++ .../transaction-manager/lib/Transaction.ts | 6 +++ .../lib/TransactionManager.ts | 12 +++++ .../lib/TransactionRepository.ts | 17 +++++++ packages/transaction-manager/lib/index.ts | 1 + 8 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 packages/transaction-manager/lib/HookManager.ts diff --git a/packages/randomness-service/src/index.ts b/packages/randomness-service/src/index.ts index b9b994bd13..63383d7c1d 100644 --- a/packages/randomness-service/src/index.ts +++ b/packages/randomness-service/src/index.ts @@ -1,5 +1,5 @@ -import { TransactionManager } from "@happychain/transaction-manager" -import type { LatestBlock, Transaction } from "@happychain/transaction-manager" +import { TransactionManager, TxmHookType } from "@happychain/transaction-manager" +import type { LatestBlock, Transaction, } from "@happychain/transaction-manager" import { webSocket } from "viem" import { privateKeyToAccount } from "viem/accounts" import { anvil } from "viem/chains" @@ -34,6 +34,9 @@ class RandomnessService { this.txm.start() this.txm.addTransactionCollector(this.onCollectTransactions.bind(this)) + this.txm.addHook(TxmHookType.TransactionStatusChanged, (event) => { + console.log(event) + }) } private onCollectTransactions(block: LatestBlock): Transaction[] { diff --git a/packages/randomness-service/tsconfig.json b/packages/randomness-service/tsconfig.json index 4d863df25e..8b56a30c17 100644 --- a/packages/randomness-service/tsconfig.json +++ b/packages/randomness-service/tsconfig.json @@ -1,4 +1,8 @@ { "extends": ["../configs/tsconfig.base.json"], - "include": ["*.ts", "src"] + "include": ["*.ts", "src"], + "compilerOptions": { + "experimentalDecorators": true, + "emitDecoratorMetadata": true + } } diff --git a/packages/transaction-manager/lib/EventBus.ts b/packages/transaction-manager/lib/EventBus.ts index 63e7cd19d4..212839a644 100644 --- a/packages/transaction-manager/lib/EventBus.ts +++ b/packages/transaction-manager/lib/EventBus.ts @@ -2,6 +2,7 @@ import EventEmitter from "eventemitter3" export enum Topics { NewBlock = "NewBlock", + TransactionStatusChanged = "TransactionStatusChanged", } export type EventBus = EventEmitter diff --git a/packages/transaction-manager/lib/HookManager.ts b/packages/transaction-manager/lib/HookManager.ts new file mode 100644 index 0000000000..ca02fcbe48 --- /dev/null +++ b/packages/transaction-manager/lib/HookManager.ts @@ -0,0 +1,50 @@ +import type { UUID } from "@happychain/common" +import { Topics, eventBus } from "./EventBus.js" +import type { TransactionStatus } from "./Transaction.js" + +export enum TxmHookType { + All = "All", + TransactionStatusChanged = "TransactionStatusChanged", +} + +export type TxmHookPayload = { + type: TxmHookType, + intentId: UUID, +} + +export type TxmHookHandler = (event: TxmHookPayload) => void + + +export class HookManager { + private hooks: Record; + + constructor() { + this.hooks = { + [TxmHookType.All]: [], + [TxmHookType.TransactionStatusChanged]: [], + } + eventBus.on(Topics.TransactionStatusChanged, this.onTransactionStatusChanged.bind(this)) + } + + public async addHook(type: TxmHookType, handler: TxmHookHandler): Promise { + if (!this.hooks[type]) { + this.hooks[type] = [] + } + this.hooks[type].push(handler) + } + + private async onTransactionStatusChanged(payload: { + intentId: UUID, + status: TransactionStatus + }): Promise { + this.hooks[TxmHookType.TransactionStatusChanged].map((h) => h({ + type: TxmHookType.TransactionStatusChanged, + intentId: payload.intentId, + })) + + this.hooks[TxmHookType.All].map((h) => h({ + type: TxmHookType.All, + intentId: payload.intentId, + })) + } +} diff --git a/packages/transaction-manager/lib/Transaction.ts b/packages/transaction-manager/lib/Transaction.ts index 198386836b..4d185b8d47 100644 --- a/packages/transaction-manager/lib/Transaction.ts +++ b/packages/transaction-manager/lib/Transaction.ts @@ -3,6 +3,7 @@ import type { Insertable, Selectable } from "kysely" import type { Address, ContractFunctionArgs, Hash } from "viem" import type { LatestBlock } from "./BlockMonitor" import type { TransactionTable } from "./db/types.js" +import { eventBus, Topics } from "./EventBus.js" export enum TransactionStatus { Pending = "Pending", @@ -113,6 +114,11 @@ export class Transaction { changeStatus(status: TransactionStatus): void { this.status = status + + eventBus.emit(Topics.TransactionStatusChanged, { + intentId: this.intentId, + status, + }) } get attemptCount(): number { diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index 3985bad14e..0dc17a7a19 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -1,6 +1,7 @@ import { type SafeViemPublicClient, type SafeViemWalletClient, + type UUID, convertToSafeViemPublicClient, convertToSafeViemWalletClient, } from "@happychain/common" @@ -16,6 +17,7 @@ import { TransactionRepository } from "./TransactionRepository.js" import { TransactionSubmitter } from "./TransactionSubmitter.js" import { TxMonitor } from "./TxMonitor.js" import { type EIP1559Parameters, opStackDefaultEIP1559Parameters } from "./eip1559.js" +import { HookManager, type TxmHookType, type TxmHookHandler } from "./HookManager.js" export type TransactionManagerConfig = { /** The transport protocol used for the client. See {@link Transport} from viem for more details. */ @@ -89,6 +91,7 @@ export class TransactionManager { public readonly transactionRepository: TransactionRepository public readonly transactionCollector: TransactionCollector public readonly transactionSubmitter: TransactionSubmitter + public readonly hookManager: HookManager public readonly id: string public readonly eip1559: EIP1559Parameters @@ -122,6 +125,7 @@ export class TransactionManager { this.transactionRepository = new TransactionRepository(this) this.transactionCollector = new TransactionCollector(this) this.transactionSubmitter = new TransactionSubmitter(this) + this.hookManager = new HookManager() this.id = _config.id this.eip1559 = _config.eip1559 || opStackDefaultEIP1559Parameters @@ -138,6 +142,14 @@ export class TransactionManager { this.collectors.push(collector) } + public async addHook(type: TxmHookType, handler: TxmHookHandler): Promise { + await this.hookManager.addHook(type, handler); + } + + public async getTransaction(txIntentId: UUID): Promise { + return this.transactionRepository.getTransaction(txIntentId) + } + public async start(): Promise { // Start the gas price oracle to prevent other parts of the application from calling `suggestGasForNextBlock` before the gas price oracle has initialized the gas price after processing the first block const priceOraclePromise = this.gasPriceOracle.start() diff --git a/packages/transaction-manager/lib/TransactionRepository.ts b/packages/transaction-manager/lib/TransactionRepository.ts index 1c6b9e94fe..46ca9e2877 100644 --- a/packages/transaction-manager/lib/TransactionRepository.ts +++ b/packages/transaction-manager/lib/TransactionRepository.ts @@ -1,5 +1,6 @@ import { unknownToError } from "@happychain/common" import { type Result, ResultAsync } from "neverthrow" +import type { UUID } from "@happychain/common" import { NotFinalizedStatuses, Transaction } from "./Transaction.js" import type { TransactionManager } from "./TransactionManager.js" import { db } from "./db/driver.js" @@ -27,6 +28,22 @@ export class TransactionRepository { return [...this.notFinalizedTransactions] } + async getTransaction(intentId: UUID): Promise { + const cachedTransaction = this.notFinalizedTransactions.find((t) => t.intentId === intentId) + + if (cachedTransaction) { + return cachedTransaction; + } + + const persistedTransaction = await db + .selectFrom("transaction") + .where("intentId", "=", intentId) + .selectAll() + .executeTakeFirst() + + return persistedTransaction ? Transaction.fromDbRow(persistedTransaction) : undefined + } + async saveTransactions(transactions: Transaction[]): Promise> { const result = await ResultAsync.fromPromise( db diff --git a/packages/transaction-manager/lib/index.ts b/packages/transaction-manager/lib/index.ts index 9620c954da..8ff89421ec 100644 --- a/packages/transaction-manager/lib/index.ts +++ b/packages/transaction-manager/lib/index.ts @@ -2,3 +2,4 @@ export { Transaction } from "./Transaction.js" export { TransactionManager } from "./TransactionManager.js" export { GasEstimator, EstimateGasErrorCause } from "./GasEstimator.js" export type { LatestBlock } from "./BlockMonitor.js" +export { TxmHookType, type TxmHookHandler } from "./HookManager.js" \ No newline at end of file From 49947205e45f9bd34c4b1290a4e38acf76fb582f Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Wed, 6 Nov 2024 12:04:56 +0100 Subject: [PATCH 2/9] chore: format --- packages/randomness-service/src/index.ts | 2 +- .../transaction-manager/lib/HookManager.ts | 31 ++++++++++--------- .../lib/TransactionManager.ts | 4 +-- packages/transaction-manager/lib/index.ts | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/randomness-service/src/index.ts b/packages/randomness-service/src/index.ts index 63383d7c1d..1ae2c66287 100644 --- a/packages/randomness-service/src/index.ts +++ b/packages/randomness-service/src/index.ts @@ -1,5 +1,5 @@ import { TransactionManager, TxmHookType } from "@happychain/transaction-manager" -import type { LatestBlock, Transaction, } from "@happychain/transaction-manager" +import type { LatestBlock, Transaction } from "@happychain/transaction-manager" import { webSocket } from "viem" import { privateKeyToAccount } from "viem/accounts" import { anvil } from "viem/chains" diff --git a/packages/transaction-manager/lib/HookManager.ts b/packages/transaction-manager/lib/HookManager.ts index ca02fcbe48..6041780343 100644 --- a/packages/transaction-manager/lib/HookManager.ts +++ b/packages/transaction-manager/lib/HookManager.ts @@ -8,15 +8,14 @@ export enum TxmHookType { } export type TxmHookPayload = { - type: TxmHookType, - intentId: UUID, + type: TxmHookType + intentId: UUID } export type TxmHookHandler = (event: TxmHookPayload) => void - export class HookManager { - private hooks: Record; + private hooks: Record constructor() { this.hooks = { @@ -34,17 +33,21 @@ export class HookManager { } private async onTransactionStatusChanged(payload: { - intentId: UUID, + intentId: UUID status: TransactionStatus }): Promise { - this.hooks[TxmHookType.TransactionStatusChanged].map((h) => h({ - type: TxmHookType.TransactionStatusChanged, - intentId: payload.intentId, - })) - - this.hooks[TxmHookType.All].map((h) => h({ - type: TxmHookType.All, - intentId: payload.intentId, - })) + this.hooks[TxmHookType.TransactionStatusChanged].map((h) => + h({ + type: TxmHookType.TransactionStatusChanged, + intentId: payload.intentId, + }), + ) + + this.hooks[TxmHookType.All].map((h) => + h({ + type: TxmHookType.All, + intentId: payload.intentId, + }), + ) } } diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index 0dc17a7a19..9dda034962 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -10,6 +10,7 @@ import { ABIManager } from "./AbiManager.js" import { BlockMonitor, type LatestBlock } from "./BlockMonitor.js" import { GasEstimator } from "./GasEstimator.js" import { GasPriceOracle } from "./GasPriceOracle.js" +import { HookManager, type TxmHookHandler, type TxmHookType } from "./HookManager.js" import { NonceManager } from "./NonceManager.js" import type { Transaction } from "./Transaction.js" import { TransactionCollector } from "./TransactionCollector.js" @@ -17,7 +18,6 @@ import { TransactionRepository } from "./TransactionRepository.js" import { TransactionSubmitter } from "./TransactionSubmitter.js" import { TxMonitor } from "./TxMonitor.js" import { type EIP1559Parameters, opStackDefaultEIP1559Parameters } from "./eip1559.js" -import { HookManager, type TxmHookType, type TxmHookHandler } from "./HookManager.js" export type TransactionManagerConfig = { /** The transport protocol used for the client. See {@link Transport} from viem for more details. */ @@ -143,7 +143,7 @@ export class TransactionManager { } public async addHook(type: TxmHookType, handler: TxmHookHandler): Promise { - await this.hookManager.addHook(type, handler); + await this.hookManager.addHook(type, handler) } public async getTransaction(txIntentId: UUID): Promise { diff --git a/packages/transaction-manager/lib/index.ts b/packages/transaction-manager/lib/index.ts index 8ff89421ec..f5cec82e01 100644 --- a/packages/transaction-manager/lib/index.ts +++ b/packages/transaction-manager/lib/index.ts @@ -2,4 +2,4 @@ export { Transaction } from "./Transaction.js" export { TransactionManager } from "./TransactionManager.js" export { GasEstimator, EstimateGasErrorCause } from "./GasEstimator.js" export type { LatestBlock } from "./BlockMonitor.js" -export { TxmHookType, type TxmHookHandler } from "./HookManager.js" \ No newline at end of file +export { TxmHookType, type TxmHookHandler } from "./HookManager.js" From b7abacd3fa559ce9cff248378ae2a61c258249f3 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Wed, 6 Nov 2024 15:28:57 +0100 Subject: [PATCH 3/9] feat(txm): hook handler receives a transaction instead of the intent id --- .../transaction-manager/lib/HookManager.ts | 41 +++++++++---------- .../lib/TransactionManager.ts | 9 +++- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/packages/transaction-manager/lib/HookManager.ts b/packages/transaction-manager/lib/HookManager.ts index 6041780343..a8cf431e22 100644 --- a/packages/transaction-manager/lib/HookManager.ts +++ b/packages/transaction-manager/lib/HookManager.ts @@ -1,52 +1,49 @@ -import type { UUID } from "@happychain/common" import { Topics, eventBus } from "./EventBus.js" -import type { TransactionStatus } from "./Transaction.js" +import type { Transaction } from "./Transaction.js" export enum TxmHookType { - All = "All", TransactionStatusChanged = "TransactionStatusChanged", } export type TxmHookPayload = { type: TxmHookType - intentId: UUID + transaction: Transaction } export type TxmHookHandler = (event: TxmHookPayload) => void export class HookManager { - private hooks: Record + private hooks: Record constructor() { this.hooks = { - [TxmHookType.All]: [], - [TxmHookType.TransactionStatusChanged]: [], + All: [], + TransactionStatusChanged: [], } eventBus.on(Topics.TransactionStatusChanged, this.onTransactionStatusChanged.bind(this)) } - public async addHook(type: TxmHookType, handler: TxmHookHandler): Promise { - if (!this.hooks[type]) { - this.hooks[type] = [] + /** + * Adds a hook to the hook manager. + * @param type - The type of hook to add. Defaults to All. + * @param handler - The handler function to add. + */ + public async addHook({ type, handler }: { type?: TxmHookType; handler: TxmHookHandler }): Promise { + const mapKey = type ?? "All" + + if (!this.hooks[mapKey]) { + this.hooks[mapKey] = [] } - this.hooks[type].push(handler) + this.hooks[mapKey].push(handler) } private async onTransactionStatusChanged(payload: { - intentId: UUID - status: TransactionStatus + transaction: Transaction }): Promise { - this.hooks[TxmHookType.TransactionStatusChanged].map((h) => + this.hooks[TxmHookType.TransactionStatusChanged].concat(this.hooks.All).map((h) => h({ type: TxmHookType.TransactionStatusChanged, - intentId: payload.intentId, - }), - ) - - this.hooks[TxmHookType.All].map((h) => - h({ - type: TxmHookType.All, - intentId: payload.intentId, + transaction: payload.transaction, }), ) } diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index 9dda034962..fc3dca7cc7 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -142,8 +142,13 @@ export class TransactionManager { this.collectors.push(collector) } - public async addHook(type: TxmHookType, handler: TxmHookHandler): Promise { - await this.hookManager.addHook(type, handler) + /** + * Adds a hook to the hook manager. + * @param type - The type of hook to add. Defaults to All. + * @param handler - The handler function to add. + */ + public async addHook(payload: { type?: TxmHookType; handler: TxmHookHandler }): Promise { + await this.hookManager.addHook(payload) } public async getTransaction(txIntentId: UUID): Promise { From da5fcf678a5ddd13085492c16e7b2ace2fa6fd08 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Mon, 11 Nov 2024 23:19:23 +0100 Subject: [PATCH 4/9] chore(txm): format --- packages/transaction-manager/lib/Transaction.ts | 2 +- packages/transaction-manager/lib/TransactionRepository.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/transaction-manager/lib/Transaction.ts b/packages/transaction-manager/lib/Transaction.ts index 4d185b8d47..52432166c1 100644 --- a/packages/transaction-manager/lib/Transaction.ts +++ b/packages/transaction-manager/lib/Transaction.ts @@ -2,8 +2,8 @@ import { type UUID, bigIntReplacer, bigIntReviver, createUUID } from "@happychai import type { Insertable, Selectable } from "kysely" import type { Address, ContractFunctionArgs, Hash } from "viem" import type { LatestBlock } from "./BlockMonitor" +import { Topics, eventBus } from "./EventBus.js" import type { TransactionTable } from "./db/types.js" -import { eventBus, Topics } from "./EventBus.js" export enum TransactionStatus { Pending = "Pending", diff --git a/packages/transaction-manager/lib/TransactionRepository.ts b/packages/transaction-manager/lib/TransactionRepository.ts index 46ca9e2877..2301115c4a 100644 --- a/packages/transaction-manager/lib/TransactionRepository.ts +++ b/packages/transaction-manager/lib/TransactionRepository.ts @@ -1,6 +1,6 @@ import { unknownToError } from "@happychain/common" -import { type Result, ResultAsync } from "neverthrow" import type { UUID } from "@happychain/common" +import { type Result, ResultAsync } from "neverthrow" import { NotFinalizedStatuses, Transaction } from "./Transaction.js" import type { TransactionManager } from "./TransactionManager.js" import { db } from "./db/driver.js" @@ -29,10 +29,10 @@ export class TransactionRepository { } async getTransaction(intentId: UUID): Promise { - const cachedTransaction = this.notFinalizedTransactions.find((t) => t.intentId === intentId) + const cachedTransaction = this.notFinalizedTransactions.find((t) => t.intentId === intentId) if (cachedTransaction) { - return cachedTransaction; + return cachedTransaction } const persistedTransaction = await db From 9abf1ba677162a207802bb7cee8513b474d447d5 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Tue, 12 Nov 2024 11:05:08 +0100 Subject: [PATCH 5/9] chore(txm): self review --- bun.lockb | Bin 997696 -> 997728 bytes packages/randomness-service/package.json | 1 + .../src/CommitmentManager.ts | 9 ++++-- packages/randomness-service/src/index.ts | 29 +++++++++++------- packages/randomness-service/tsconfig.json | 6 +--- .../transaction-manager/lib/HookManager.ts | 5 --- .../transaction-manager/lib/Transaction.ts | 3 +- .../lib/TransactionCollector.ts | 5 +-- .../lib/TransactionManager.ts | 2 +- packages/transaction-manager/lib/index.ts | 2 +- 10 files changed, 33 insertions(+), 29 deletions(-) diff --git a/bun.lockb b/bun.lockb index 9d4daed6869bd02965aa4e0645677da3e7c32de8..2670ca3a46d0b24ddd75dd04e53c2df691066f3e 100755 GIT binary patch delta 18157 zcmeI4dzemD-^cHJ#+cb=#+YHq*$@-sJQ&OjGY-jdOhu(ADw4=4D(NOUA0lb-L=q+S zsEFrAjuEMxI+qU46e^_1>G^zTe!A}WeO%A=KG*fW*Za?`>-zev_51zS+H0-7_HAu@ zZyQf%>^q$?y_0|U=$x@R{;@e>D|5n6dVxrv&(|o{=c@^OL6!T$FxU?&iG|@H=)uR$ z{{$>f`59OU&V+vW98?ljo&}4*`A~5Sq1t~X))(-VBeMyE4*UU(fIBQ;7gPhgjeE@h z1yl;9I{2;m_d}JBLe)QJ@uH6Ni2)@yGB|8YUYwHPSi}K1jxjYNi)ZPLN(mP>itis_;lhm11+pxKpBm+4OEXinnP6i z7ULaI4crOUKrg7Ny%&bV-cTu&s{asn)f)hHoFP#5#~|$le4{MjseFTPB2*G}fJs&s zucQ33l|!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LsMF~i6rB6hY&}38{)k5`AL!{>*@1yGc($~w06;KPLPkiFhEf(BvUT)lj z_M(00UuZx2u7KM))hQ6^GXJk5{qF=Xy8}|4QHfJn2h>Ya|wZ%&A(BHt4ibo`dgL zlzT1JX&N~In?BR8f~xvkH+S}X%Y0AQ^!~bD;a<+P>yr#^5cPiWPi~VI{8oQ}qV|XB zC<9eS`rNM{A$rBU8of_O6;L~xYL7aiuIKQvc4AdN*lnT zC=2OzqX*GL=wUPn4MC3}?FO?@HX4RTAnpA0#dCk8FQq%7&L{)5LfY0{LE6IwQ+ALx zW8o+w+EXES7ZyvQFe-|QqY`K!6&^&@+WlQjwm$CGj!j#xvTn}|XOOppyCK8r-&0>v=b#tS6f_mRf~KQa z(MI*niV*nhyQC=Ti7Sa-bo{3(X~$M6ISBfka4atS;Mb( zxciL`BM|AcTHUe!MIS?;ZbxaV1}*Wo@8C4@`eNzG5Y(8?6@t~+ zs(;TQO7L?hZ~#>)S4S&|TZeR87^*rbFisz41eUwt*ZrUF_bMPS{u~sQ7gzW{XJ{s) zUX0##V>6w!$i58rJ`{>>y8SYplu+FdM!0WfIvL&z{v(;r3~#S}v=T>e^*K=PE7&YX z_52=e`aU4ACvz$5=1ezZy7{_&YHqVVwhm}ZoV&HNbFW*ei*t7aecnBo<)C}4;Yc@6 zT~Rxvo2EaJ?tJwAz3z5GP!V@!7iU|UL$uY4el$n*kUnY3{k#cocVFx3By?*->*bKX zem}}BuKojp0jN5f9Kmm0n1UKmv$@$7SD$<%R2S(MN4GT9P#HJ0o6{roBu8%O_Uq=< zi||~;sho81$R^z-V_e7)U R51l2A^MY2m^BPBg`X3CU3&{Wg diff --git a/packages/randomness-service/package.json b/packages/randomness-service/package.json index 1a20d42e92..b135403144 100644 --- a/packages/randomness-service/package.json +++ b/packages/randomness-service/package.json @@ -7,6 +7,7 @@ "module": "./dist/index.es.js", "dependencies": { "@happychain/transaction-manager": "workspace:^", + "@happychain/common": "workspace:^", "neverthrow": "^8.1.0", "viem": "^2.21.53", "zod": "^3.23.8" diff --git a/packages/randomness-service/src/CommitmentManager.ts b/packages/randomness-service/src/CommitmentManager.ts index bacc5059bf..91c01f801d 100644 --- a/packages/randomness-service/src/CommitmentManager.ts +++ b/packages/randomness-service/src/CommitmentManager.ts @@ -1,23 +1,28 @@ import crypto from "node:crypto" +import type { UUID } from "@happychain/common" import type { Hex } from "viem" import { encodePacked, keccak256 } from "viem" interface Commitment { value: bigint commitment: Hex + transactionIntentId: UUID } export class CommitmentManager { private readonly map = new Map() - generateCommitmentForTimestamp(timestamp: bigint): Commitment { + generateCommitmentForTimestamp(): Omit { const value = this.generateRandomness() const commitment = this.hashValue(value) const commitmentObject = { value, commitment } - this.map.set(timestamp, commitmentObject) return commitmentObject } + setCommitmentForTimestamp(timestamp: bigint, commitment: Commitment): void { + this.map.set(timestamp, commitment) + } + getCommitmentForTimestamp(timestamp: bigint): Commitment | undefined { return this.map.get(timestamp) } diff --git a/packages/randomness-service/src/index.ts b/packages/randomness-service/src/index.ts index 1ae2c66287..e526796e07 100644 --- a/packages/randomness-service/src/index.ts +++ b/packages/randomness-service/src/index.ts @@ -1,4 +1,4 @@ -import { TransactionManager, TxmHookType } from "@happychain/transaction-manager" +import { TransactionManager, TransactionStatus } from "@happychain/transaction-manager" import type { LatestBlock, Transaction } from "@happychain/transaction-manager" import { webSocket } from "viem" import { privateKeyToAccount } from "viem/accounts" @@ -34,17 +34,14 @@ class RandomnessService { this.txm.start() this.txm.addTransactionCollector(this.onCollectTransactions.bind(this)) - this.txm.addHook(TxmHookType.TransactionStatusChanged, (event) => { - console.log(event) - }) } - private onCollectTransactions(block: LatestBlock): Transaction[] { + private async onCollectTransactions(block: LatestBlock): Promise { const transactions: Transaction[] = [] // We try to commit the ramdomness POST_COMMIT_MARGIN to be safe that the transaction is included before the PRECOMMIT_DELAY const commitmentTimestamp = block.timestamp + env.PRECOMMIT_DELAY + env.POST_COMMIT_MARGIN - const commitment = this.commitmentManager.generateCommitmentForTimestamp(commitmentTimestamp) + const commitment = this.commitmentManager.generateCommitmentForTimestamp() const commitmentTransaction = this.commitmentTransactionFactory.create( commitmentTimestamp, @@ -53,14 +50,24 @@ class RandomnessService { transactions.push(commitmentTransaction) + this.commitmentManager.setCommitmentForTimestamp(commitmentTimestamp, { + ...commitment, + transactionIntentId: commitmentTransaction.intentId, + }) + const revealValueCommitment = this.commitmentManager.getCommitmentForTimestamp(block.timestamp + env.TIME_BLOCK) if (revealValueCommitment) { - const revealValueTransaction = this.revealValueTransactionFactory.create( - block.timestamp + env.TIME_BLOCK, - revealValueCommitment.value, - ) - transactions.push(revealValueTransaction) + const transaction = await this.txm.getTransaction(revealValueCommitment.transactionIntentId) + + if (transaction?.status === TransactionStatus.Success) { + console.log(transaction) + const revealValueTransaction = this.revealValueTransactionFactory.create( + block.timestamp + env.TIME_BLOCK, + revealValueCommitment.value, + ) + transactions.push(revealValueTransaction) + } } return transactions diff --git a/packages/randomness-service/tsconfig.json b/packages/randomness-service/tsconfig.json index 8b56a30c17..4d863df25e 100644 --- a/packages/randomness-service/tsconfig.json +++ b/packages/randomness-service/tsconfig.json @@ -1,8 +1,4 @@ { "extends": ["../configs/tsconfig.base.json"], - "include": ["*.ts", "src"], - "compilerOptions": { - "experimentalDecorators": true, - "emitDecoratorMetadata": true - } + "include": ["*.ts", "src"] } diff --git a/packages/transaction-manager/lib/HookManager.ts b/packages/transaction-manager/lib/HookManager.ts index a8cf431e22..96555278e2 100644 --- a/packages/transaction-manager/lib/HookManager.ts +++ b/packages/transaction-manager/lib/HookManager.ts @@ -23,11 +23,6 @@ export class HookManager { eventBus.on(Topics.TransactionStatusChanged, this.onTransactionStatusChanged.bind(this)) } - /** - * Adds a hook to the hook manager. - * @param type - The type of hook to add. Defaults to All. - * @param handler - The handler function to add. - */ public async addHook({ type, handler }: { type?: TxmHookType; handler: TxmHookHandler }): Promise { const mapKey = type ?? "All" diff --git a/packages/transaction-manager/lib/Transaction.ts b/packages/transaction-manager/lib/Transaction.ts index 52432166c1..bcdd1260c2 100644 --- a/packages/transaction-manager/lib/Transaction.ts +++ b/packages/transaction-manager/lib/Transaction.ts @@ -116,8 +116,7 @@ export class Transaction { this.status = status eventBus.emit(Topics.TransactionStatusChanged, { - intentId: this.intentId, - status, + transaction: this, }) } diff --git a/packages/transaction-manager/lib/TransactionCollector.ts b/packages/transaction-manager/lib/TransactionCollector.ts index 075e090624..d36c038a95 100644 --- a/packages/transaction-manager/lib/TransactionCollector.ts +++ b/packages/transaction-manager/lib/TransactionCollector.ts @@ -14,8 +14,9 @@ export class TransactionCollector { private async onNewBlock(block: LatestBlock) { const { maxFeePerGas, maxPriorityFeePerGas } = this.txmgr.gasPriceOracle.suggestGasForNextBlock() - const transactionsBatch = this.txmgr.collectors - .flatMap((c) => c(block)) + const transactionUnsorted = await Promise.all(this.txmgr.collectors.map((c) => c(block))) + const transactionsBatch = transactionUnsorted + .flat() .sort((a, b) => (a.deadline ?? Number.POSITIVE_INFINITY) - (b.deadline ?? Number.POSITIVE_INFINITY)) const saveResult = await this.txmgr.transactionRepository.saveTransactions(transactionsBatch) diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index fc3dca7cc7..5192ad6ad4 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -76,7 +76,7 @@ export type TransactionManagerConfig = { gasEstimator?: GasEstimator } -export type TransactionOriginator = (block: LatestBlock) => Transaction[] +export type TransactionOriginator = (block: LatestBlock) => Promise export class TransactionManager { public readonly collectors: TransactionOriginator[] diff --git a/packages/transaction-manager/lib/index.ts b/packages/transaction-manager/lib/index.ts index f5cec82e01..48451d3dc9 100644 --- a/packages/transaction-manager/lib/index.ts +++ b/packages/transaction-manager/lib/index.ts @@ -1,4 +1,4 @@ -export { Transaction } from "./Transaction.js" +export { Transaction, TransactionStatus } from "./Transaction.js" export { TransactionManager } from "./TransactionManager.js" export { GasEstimator, EstimateGasErrorCause } from "./GasEstimator.js" export type { LatestBlock } from "./BlockMonitor.js" From c4b5d90d28452ccebec29f55dd1eef8049c70dc9 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Tue, 12 Nov 2024 11:18:30 +0100 Subject: [PATCH 6/9] chore(txm): remove console.log --- packages/randomness-service/src/CommitmentManager.ts | 2 +- packages/randomness-service/src/index.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/randomness-service/src/CommitmentManager.ts b/packages/randomness-service/src/CommitmentManager.ts index 91c01f801d..901e3dfa48 100644 --- a/packages/randomness-service/src/CommitmentManager.ts +++ b/packages/randomness-service/src/CommitmentManager.ts @@ -12,7 +12,7 @@ interface Commitment { export class CommitmentManager { private readonly map = new Map() - generateCommitmentForTimestamp(): Omit { + generateCommitment(): Omit { const value = this.generateRandomness() const commitment = this.hashValue(value) const commitmentObject = { value, commitment } diff --git a/packages/randomness-service/src/index.ts b/packages/randomness-service/src/index.ts index e526796e07..8454fde8d2 100644 --- a/packages/randomness-service/src/index.ts +++ b/packages/randomness-service/src/index.ts @@ -41,7 +41,7 @@ class RandomnessService { // We try to commit the ramdomness POST_COMMIT_MARGIN to be safe that the transaction is included before the PRECOMMIT_DELAY const commitmentTimestamp = block.timestamp + env.PRECOMMIT_DELAY + env.POST_COMMIT_MARGIN - const commitment = this.commitmentManager.generateCommitmentForTimestamp() + const commitment = this.commitmentManager.generateCommitment() const commitmentTransaction = this.commitmentTransactionFactory.create( commitmentTimestamp, @@ -61,7 +61,6 @@ class RandomnessService { const transaction = await this.txm.getTransaction(revealValueCommitment.transactionIntentId) if (transaction?.status === TransactionStatus.Success) { - console.log(transaction) const revealValueTransaction = this.revealValueTransactionFactory.create( block.timestamp + env.TIME_BLOCK, revealValueCommitment.value, From f5dfbf91e57175a70273cd9fbb5b7afcacb89a87 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Tue, 12 Nov 2024 11:21:44 +0100 Subject: [PATCH 7/9] chore(txm): added comment --- packages/transaction-manager/lib/TransactionManager.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index 5192ad6ad4..95bf4129a8 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -138,6 +138,12 @@ export class TransactionManager { this.blockTime = _config.blockTime || 2n } + /** + * Adds a collector to the transaction manager. + * A collector is a function that returns a list of transactions to be sent in the next block. + * It is important that the collector function is as fast as possible to avoid delays when sending transactions to the blockchain + * @param collector - The collector to add. + */ public addTransactionCollector(collector: TransactionOriginator): void { this.collectors.push(collector) } From 405cc6815993326c1436ab356d312d9d07d106f9 Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Tue, 3 Dec 2024 11:46:46 +0100 Subject: [PATCH 8/9] chore(txm): use function parameters instead of object in addHook method --- packages/transaction-manager/lib/HookManager.ts | 2 +- packages/transaction-manager/lib/TransactionManager.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/transaction-manager/lib/HookManager.ts b/packages/transaction-manager/lib/HookManager.ts index 96555278e2..ba1eee3f90 100644 --- a/packages/transaction-manager/lib/HookManager.ts +++ b/packages/transaction-manager/lib/HookManager.ts @@ -23,7 +23,7 @@ export class HookManager { eventBus.on(Topics.TransactionStatusChanged, this.onTransactionStatusChanged.bind(this)) } - public async addHook({ type, handler }: { type?: TxmHookType; handler: TxmHookHandler }): Promise { + public async addHook(handler: TxmHookHandler, type?: TxmHookType): Promise { const mapKey = type ?? "All" if (!this.hooks[mapKey]) { diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index 95bf4129a8..801b5b332a 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -153,8 +153,8 @@ export class TransactionManager { * @param type - The type of hook to add. Defaults to All. * @param handler - The handler function to add. */ - public async addHook(payload: { type?: TxmHookType; handler: TxmHookHandler }): Promise { - await this.hookManager.addHook(payload) + public async addHook(handler: TxmHookHandler, type?: TxmHookType): Promise { + await this.hookManager.addHook(handler, type) } public async getTransaction(txIntentId: UUID): Promise { From a520cec8e3741718ad4f99f52a3acc9c55df67ac Mon Sep 17 00:00:00 2001 From: Gabriel Martinez Rodriguez Date: Thu, 5 Dec 2024 12:41:27 +0100 Subject: [PATCH 9/9] chore(txm): added All to TxmHookType --- .../transaction-manager/lib/HookManager.ts | 19 +++++++++---------- .../lib/TransactionManager.ts | 4 ++-- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/transaction-manager/lib/HookManager.ts b/packages/transaction-manager/lib/HookManager.ts index ba1eee3f90..6846062df2 100644 --- a/packages/transaction-manager/lib/HookManager.ts +++ b/packages/transaction-manager/lib/HookManager.ts @@ -2,6 +2,7 @@ import { Topics, eventBus } from "./EventBus.js" import type { Transaction } from "./Transaction.js" export enum TxmHookType { + All = "All", TransactionStatusChanged = "TransactionStatusChanged", } @@ -13,29 +14,27 @@ export type TxmHookPayload = { export type TxmHookHandler = (event: TxmHookPayload) => void export class HookManager { - private hooks: Record + private hooks: Record constructor() { this.hooks = { - All: [], - TransactionStatusChanged: [], + [TxmHookType.All]: [], + [TxmHookType.TransactionStatusChanged]: [], } eventBus.on(Topics.TransactionStatusChanged, this.onTransactionStatusChanged.bind(this)) } - public async addHook(handler: TxmHookHandler, type?: TxmHookType): Promise { - const mapKey = type ?? "All" - - if (!this.hooks[mapKey]) { - this.hooks[mapKey] = [] + public async addHook(handler: TxmHookHandler, type: TxmHookType): Promise { + if (!this.hooks[type]) { + this.hooks[type] = [] } - this.hooks[mapKey].push(handler) + this.hooks[type].push(handler) } private async onTransactionStatusChanged(payload: { transaction: Transaction }): Promise { - this.hooks[TxmHookType.TransactionStatusChanged].concat(this.hooks.All).map((h) => + this.hooks[TxmHookType.TransactionStatusChanged].concat(this.hooks[TxmHookType.All]).map((h) => h({ type: TxmHookType.TransactionStatusChanged, transaction: payload.transaction, diff --git a/packages/transaction-manager/lib/TransactionManager.ts b/packages/transaction-manager/lib/TransactionManager.ts index 801b5b332a..d3f790cca6 100644 --- a/packages/transaction-manager/lib/TransactionManager.ts +++ b/packages/transaction-manager/lib/TransactionManager.ts @@ -150,10 +150,10 @@ export class TransactionManager { /** * Adds a hook to the hook manager. - * @param type - The type of hook to add. Defaults to All. + * @param type - The type of hook to add. * @param handler - The handler function to add. */ - public async addHook(handler: TxmHookHandler, type?: TxmHookType): Promise { + public async addHook(handler: TxmHookHandler, type: TxmHookType): Promise { await this.hookManager.addHook(handler, type) }