From c79eb5f340ee64c919d751006663222aa7245004 Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Fri, 27 Sep 2024 05:57:54 +0800 Subject: [PATCH 1/7] feat: added fetchhistory wawtchtx and watchmempool --- examples/node/follow-tip.ts | 137 +++++++++++++++++++++++++++++------- src/cardano.ts | 118 +++++++++++++++++++++++++++++-- 2 files changed, 223 insertions(+), 32 deletions(-) diff --git a/examples/node/follow-tip.ts b/examples/node/follow-tip.ts index 5166f85..5e68780 100644 --- a/examples/node/follow-tip.ts +++ b/examples/node/follow-tip.ts @@ -1,39 +1,124 @@ -import { CardanoSyncClient, CardanoQueryClient } from "@saibdev/utxorpc-sdk"; +import { submit } from "@utxorpc/spec"; +import { SyncClient, QueryClient, Utxo, SubmitClient, WatchClient, ChainPoint } from "../../src/cardano"; async function test() { - let syncClient = new CardanoSyncClient({ - uri: "http://localhost:50051", + let syncClient = new SyncClient({ + uri: "https://preview.utxorpc-v0.demeter.run", + headers: { + "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", + } }); - let queryClient = new CardanoQueryClient({ - uri: "http://localhost:50051", + let queryClient = new QueryClient({ + uri: "https://preview.utxorpc-v0.demeter.run", + headers: { + "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", + } }); - const params = await queryClient.readParams(); - - console.log(params); + let submitClient = new SubmitClient({ + uri: "https://preview.utxorpc-v0.demeter.run", + headers: { + "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", + } + }); - const utxos = await queryClient.searchUtxosByAddress( - Buffer.from( - "705c87cbca3a88cbfee6f6ad820acea99f484b4830fc632610f2a30146", - "hex" - ) - ); - console.log( - utxos.map((utxo) => { - console.log(utxo.parsedValued?.script); - }) - ); + const watchClient = new WatchClient({ + uri: "https://preview.utxorpc-v0.demeter.run", + headers: { + "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", + } + }); - let tip = syncClient.followTip([ + const chainPoint: ChainPoint = { - slot: 54131816, - hash: "34c65aba4b299113a488b74e2efe3a3dd272d25b470d25f374b2c693d4386535", - }, - ]); + slot: 60692700, // Adjust this value as necessary + hash: "1113030bc4a8feb29b4f3e3928cd7193ee1b0ca49f35cf2af9cd881b86375d29", // Replace with the actual hash + }; - for await (const event of tip) { - console.log(event); + // Fetch history for the provided ChainPoint + try { + const block = await syncClient.fetchHistory(chainPoint); + console.log("Fetched Block:", JSON.stringify(block,null,2)); + } catch (error) { + console.error("Error fetching block history:", error); } + + // Call watchTx + // const asyncIterable = watchClient.watchTx(chainPoint); + + // try { + // for await (const response of asyncIterable) { + // console.log("Transaction Update:", response); + + // if (response.action.case == "apply") { + // console.log(response.action.value.chain.value); + // } + // if (response.action.case == "undo") { + // console.log(response.action.value.chain.value); + // } + // } + // } catch (error) { + // console.error("Error while watching transactions:", error); + // } + + // watchmempool test + + // const tx = await submitClient.submitTx( + // Buffer.from( + // "84a300d901028182582033dd8b84c4d4d4018c6a0fd83bf892a6dbf6a95afa49f5872e1b4e5f67b25e5f01018282581d60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de8567591a004c4b4082581d60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de8567591b0000000252d052cc021a0002990da100d9010281825820526fa19e3694cda4f3c0d2fb2d2bb8768925eccc49a89d5f12b1972644ac769858403548d4bdb305c0081acd1d53dda37a87abf313e1a816ca25bae99acbca96a69118bd50cb2454aeda6992ad9320ef346b3aeb860fde057bc1b4d8e514eb3f7b01f5f6", + // "hex" + // ) + // ) + // console.log("Transaction Hash:", tx); + // const asyncIterable = submitClient.watchMempoolForAddress( + // Buffer.from( + // "60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de856759", + // "hex" + // ) + // ); + + // try { + // for await (const response of asyncIterable) { + // console.log("Mempool Update:", response); + // if (response.tx) { + // console.log("Transaction:", response.tx); + // } + // } + // } catch (error) { + // console.error("Error while watching mempool:", error); + // } + + + + // const tx: any = await submitClient.readMempool(); + + // console.log(tx); + // const params = await queryClient.readParams(); + + // console.log(params); + + // const utxos = await queryClient.searchUtxosByAddress( + // Buffer.from( + // "705c87cbca3a88cbfee6f6ad820acea99f484b4830fc632610f2a30146", + // "hex" + // ) + // ); + // console.log( + // utxos.map((utxo) => { + // console.log(utxo.parsedValued?.script); + // }) + // ); + + // let tip = syncClient.followTip([ + // { + // slot: 54131816, + // hash: "34c65aba4b299113a488b74e2efe3a3dd272d25b470d25f374b2c693d4386535", + // }, + // ]); + + // for await (const event of tip) { + // console.log(event); + // } } test().catch(console.error); diff --git a/src/cardano.ts b/src/cardano.ts index e765140..2bf7693 100644 --- a/src/cardano.ts +++ b/src/cardano.ts @@ -1,9 +1,6 @@ -import { - PromiseClient, - createPromiseClient, -} from "@connectrpc/connect"; +import { PromiseClient, createPromiseClient } from "@connectrpc/connect"; -import { createGrpcTransport } from '@sdk/grpcTransport'; +import { createGrpcTransport } from "@sdk/grpcTransport"; import { PartialMessage } from "@bufbuild/protobuf"; @@ -16,6 +13,8 @@ import { queryConnect, submit, submitConnect, + watchConnect, + watch, cardano, } from "@utxorpc/spec"; @@ -122,6 +121,26 @@ export class SyncClient { const res = await this.inner.fetchBlock({ ref: [req] }); return anyChainToBlock(res.block[0])!; } + + async fetchHistory(p: ChainPoint): Promise { + const req = new sync.DumpHistoryRequest({ + startToken: new sync.BlockRef({ + index: BigInt(p.slot), + hash: Buffer.from(p.hash, "hex"), + }), + maxItems: 1, + }); + + const res = await this.inner.dumpHistory(req); + + if (res.block.length === 0) { + throw new Error("No block history found for the provided ChainPoint."); + } + + const block = anyChainToBlock(res.block[0]); + + return block!; + } } export class QueryClient { @@ -187,7 +206,9 @@ export class QueryClient { }); } - async searchUtxosByDelegationPart(delegationPart: Uint8Array): Promise { + async searchUtxosByDelegationPart( + delegationPart: Uint8Array + ): Promise { return this.searchUtxosByMatch({ address: { delegationPart: delegationPart, @@ -250,4 +271,89 @@ export class SubmitClient { yield change.stage; } } + + async *watchMempool( + pattern: PartialMessage + ): AsyncIterable { + const updates = this.inner.watchMempool({ + predicate: { + match: { chain: { value: pattern, case: "cardano" } }, + }, + }); + + for await (const response of updates) { + if (response.tx) { + yield response; + } + } + } + + async *watchMempoolForAddress( + address: Uint8Array + ): AsyncIterable { + const updates = this.inner.watchMempool({ + predicate: { + match: { + chain: { + value: { + hasAddress: { exactAddress: address }, + }, + case: "cardano", + }, + }, + }, + }); + + for await (const response of updates) { + if (response.tx) { + yield response; + } + } + } + + // async readMempool(): Promise { + // const request = new submit.ReadMempoolRequest(); + // const response = await this.inner.readMempool(request); + // return response; + // } + + // async evalTx(tx: TxCbor): Promise { + // const evalTxRequest = new submit.EvalTxRequest({ + // tx: [{ type: { case: "raw", value: tx } }], + // }); + + // const res = await this.inner.evalTx(evalTxRequest); + + // return res.report.map((report) => report); + // } +} + +export class WatchClient { + inner: PromiseClient; + + constructor(options: ClientBuilderOptions) { + let headerInterceptor = metadataInterceptor(options); + + const transport = createGrpcTransport({ + httpVersion: "2", + baseUrl: options.uri, + interceptors: [headerInterceptor], + }); + + this.inner = createPromiseClient(watchConnect.WatchService, transport); + } + + async *watchTx( + intersect?: ChainPoint[] + ): AsyncIterable { + const request: watch.WatchTxRequest = new watch.WatchTxRequest({ + intersect: intersect ? intersect.map(pointToBlockRef) : [], + }); + + const stream = this.inner.watchTx(request); + + for await (const response of stream) { + yield response; + } + } } From 2e80e90ab8033131305895f40bde49c9aeefb33a Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Wed, 2 Oct 2024 16:53:12 +0800 Subject: [PATCH 2/7] chore: cleanup --- examples/node/follow-tip.ts | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/examples/node/follow-tip.ts b/examples/node/follow-tip.ts index 5e68780..38859c0 100644 --- a/examples/node/follow-tip.ts +++ b/examples/node/follow-tip.ts @@ -3,31 +3,19 @@ import { SyncClient, QueryClient, Utxo, SubmitClient, WatchClient, ChainPoint } async function test() { let syncClient = new SyncClient({ - uri: "https://preview.utxorpc-v0.demeter.run", - headers: { - "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", - } + uri: "http://localhost:50051/" }); let queryClient = new QueryClient({ - uri: "https://preview.utxorpc-v0.demeter.run", - headers: { - "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", - } + uri: "http://localhost:50051/" }); let submitClient = new SubmitClient({ - uri: "https://preview.utxorpc-v0.demeter.run", - headers: { - "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", - } + uri: "http://localhost:50051/" }); const watchClient = new WatchClient({ - uri: "https://preview.utxorpc-v0.demeter.run", - headers: { - "dmtr-api-key": "dmtr_utxorpc1vc0m93rynmltysttwm7ns9m3n5cklws6", - } + uri: "http://localhost:50051/" }); const chainPoint: ChainPoint = From da28f5feed92b6be2c2c1e2e37cbc9f5ca4aa24b Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Wed, 2 Oct 2024 22:57:46 +0800 Subject: [PATCH 3/7] chore: further implementations --- src/cardano.ts | 171 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 132 insertions(+), 39 deletions(-) diff --git a/src/cardano.ts b/src/cardano.ts index 2bf7693..7f2941a 100644 --- a/src/cardano.ts +++ b/src/cardano.ts @@ -30,6 +30,42 @@ export type Utxo = GenericUtxo; export type TipEvent = GenericTipEvent; export type TxHash = Uint8Array; export type TxCbor = Uint8Array; +export type MempoolEvent = { + ref: Uint8Array; + stage: MempoolStage; + nativeBytes: Uint8Array; + parsedState?: cardano.Tx | undefined; +}; +export enum MempoolStage { + ACKNOWLEDGED = "ACKNOWLEDGED", + MEMPOOL = "MEMPOOL", + NETWORK = "NETWORK", + CONFIRMED = "CONFIRMED", +} + +export interface TxEvent { + ref: cardano.Tx | undefined; + stage: "apply" | "undo"; +} + +function toMempoolEvent(txInMempool): MempoolEvent { + return { + ref: txInMempool.ref, + stage: txInMempool.stage as MempoolStage, + nativeBytes: txInMempool.nativeBytes, + parsedState: txInMempool.parsedState && txInMempool.parsedState.case === "cardano" + ? txInMempool.parsedState.value + : undefined, + }; +} + +function toTxEvent(response): TxEvent { + return { + ref: response.action.value?.chain.value, + stage: response.action.case as "apply" | "undo", + }; +} + function anyChainToBlock(msg) { return msg.chain.case == "cardano" ? msg.chain.value : null; @@ -272,60 +308,58 @@ export class SubmitClient { } } - async *watchMempool( + async *watchMempoolByMatch( pattern: PartialMessage - ): AsyncIterable { - const updates = this.inner.watchMempool({ + ): AsyncIterable { + const stream = this.inner.watchMempool({ predicate: { match: { chain: { value: pattern, case: "cardano" } }, }, }); - for await (const response of updates) { + for await (const response of stream) { if (response.tx) { - yield response; + yield toMempoolEvent(response.tx); } } } + async *watchMempool(): AsyncIterable { + yield* this.watchMempoolByMatch({}); + } + async *watchMempoolForAddress( address: Uint8Array - ): AsyncIterable { - const updates = this.inner.watchMempool({ - predicate: { - match: { - chain: { - value: { - hasAddress: { exactAddress: address }, - }, - case: "cardano", - }, - }, - }, + ): AsyncIterable { + yield* this.watchMempoolByMatch({ + hasAddress: { exactAddress: address }, }); - - for await (const response of updates) { - if (response.tx) { - yield response; - } - } } - // async readMempool(): Promise { - // const request = new submit.ReadMempoolRequest(); - // const response = await this.inner.readMempool(request); - // return response; - // } - - // async evalTx(tx: TxCbor): Promise { - // const evalTxRequest = new submit.EvalTxRequest({ - // tx: [{ type: { case: "raw", value: tx } }], - // }); + async *watchMempoolForPaymentPart( + paymentPart: Uint8Array + ): AsyncIterable { + yield* this.watchMempoolByMatch({ + hasAddress: { paymentPart: paymentPart }, + }); + } - // const res = await this.inner.evalTx(evalTxRequest); + async *watchMempoolForDelegationPart( + delegationPart: Uint8Array + ): AsyncIterable { + yield* this.watchMempoolByMatch({ + hasAddress: { delegationPart: delegationPart }, + }); + } - // return res.report.map((report) => report); - // } + async *watchMempoolForAsset( + policyId?: Uint8Array, + assetName?: Uint8Array + ): AsyncIterable { + yield* this.watchMempoolByMatch({ + movesAsset: policyId ? { policyId } : { assetName }, + }); + } } export class WatchClient { @@ -343,17 +377,76 @@ export class WatchClient { this.inner = createPromiseClient(watchConnect.WatchService, transport); } - async *watchTx( + async *watchTxByMatch( + pattern: PartialMessage, intersect?: ChainPoint[] - ): AsyncIterable { + ): AsyncIterable { const request: watch.WatchTxRequest = new watch.WatchTxRequest({ intersect: intersect ? intersect.map(pointToBlockRef) : [], + predicate: { + match: { + chain: { + value: pattern, + case: "cardano", + }, + }, + }, }); const stream = this.inner.watchTx(request); for await (const response of stream) { - yield response; + switch (response.action.case) { + case "apply": + yield toTxEvent(response); + break; + + case "undo": + yield toTxEvent(response); + break; + } } } + + async *watchTx( + intersect?: ChainPoint[] + ): AsyncIterable { + const pattern = {}; + yield* this.watchTxByMatch(pattern, intersect); + } + + async *watchTxForAddress( + address: Uint8Array, + intersect?: ChainPoint[] + ): AsyncIterable { + const pattern = { hasAddress: { exactAddress: address } }; + yield* this.watchTxByMatch(pattern, intersect); + } + + async *watchTxForPaymentPart( + paymentPart: Uint8Array, + intersect?: ChainPoint[] + ): AsyncIterable { + const pattern = { hasAddress: { paymentPart } }; + yield* this.watchTxByMatch(pattern, intersect); + } + + async *watchTxForDelegationPart( + delegationPart: Uint8Array, + intersect?: ChainPoint[] + ): AsyncIterable { + const pattern = { hasAddress: { delegationPart } }; + yield* this.watchTxByMatch(pattern, intersect); + } + + async *watchTxForAsset( + policyId?: Uint8Array, + assetName?: Uint8Array, + intersect?: ChainPoint[] + ): AsyncIterable { + const pattern = policyId + ? { movesAsset: { policyId } } + : { movesAsset: { assetName } }; + yield* this.watchTxByMatch(pattern, intersect); + } } From 982eb358291594cd517f5b74edb236fa5d493ed5 Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Thu, 3 Oct 2024 01:53:19 +0800 Subject: [PATCH 4/7] chore: more abstraction --- src/cardano.ts | 47 +++++++++++++++++------------------------------ src/common.ts | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/src/cardano.ts b/src/cardano.ts index 7f2941a..bbd4cbb 100644 --- a/src/cardano.ts +++ b/src/cardano.ts @@ -20,53 +20,42 @@ import { import { ClientBuilderOptions, - metadataInterceptor, GenericTipEvent, GenericUtxo, + GenericTxEvent, + GenericTxInMempoolEvent, + metadataInterceptor, } from "./common.js"; export type ChainPoint = { slot: number | string; hash: string }; export type Utxo = GenericUtxo; export type TipEvent = GenericTipEvent; +export type TxEvent = GenericTxEvent; +export type MempoolEvent = GenericTxInMempoolEvent; export type TxHash = Uint8Array; export type TxCbor = Uint8Array; -export type MempoolEvent = { - ref: Uint8Array; - stage: MempoolStage; - nativeBytes: Uint8Array; - parsedState?: cardano.Tx | undefined; -}; -export enum MempoolStage { - ACKNOWLEDGED = "ACKNOWLEDGED", - MEMPOOL = "MEMPOOL", - NETWORK = "NETWORK", - CONFIRMED = "CONFIRMED", -} - -export interface TxEvent { - ref: cardano.Tx | undefined; - stage: "apply" | "undo"; -} function toMempoolEvent(txInMempool): MempoolEvent { return { - ref: txInMempool.ref, - stage: txInMempool.stage as MempoolStage, + txoRef: txInMempool.ref, + stage: txInMempool.stage, nativeBytes: txInMempool.nativeBytes, - parsedState: txInMempool.parsedState && txInMempool.parsedState.case === "cardano" - ? txInMempool.parsedState.value - : undefined, + Tx: + txInMempool.parsedState.case == "cardano" + ? txInMempool.parsedState.value + : undefined, }; } - function toTxEvent(response): TxEvent { return { - ref: response.action.value?.chain.value, - stage: response.action.case as "apply" | "undo", + action: response.action.case as "apply" | "undo", + Tx: + response.action.value.chain.case == "cardano" + ? response.action.value?.chain.value + : undefined, }; } - function anyChainToBlock(msg) { return msg.chain.case == "cardano" ? msg.chain.value : null; } @@ -408,9 +397,7 @@ export class WatchClient { } } - async *watchTx( - intersect?: ChainPoint[] - ): AsyncIterable { + async *watchTx(intersect?: ChainPoint[]): AsyncIterable { const pattern = {}; yield* this.watchTxByMatch(pattern, intersect); } diff --git a/src/common.ts b/src/common.ts index 30efbd3..a40f53a 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1,6 +1,4 @@ -import { - Interceptor, -} from "@connectrpc/connect"; +import { Interceptor } from "@connectrpc/connect"; export function metadataInterceptor( options?: ClientBuilderOptions @@ -21,6 +19,50 @@ export type GenericTipEvent = | { action: "undo"; block: Block } | { action: "reset"; point: Point }; +export type GenericTxEvent = + | { action: "apply"; Tx: Tx } + | { action: "undo"; Tx: Tx }; + +export enum GenericMempoolStage { + UNSPECIFIED = 0, + ACKNOWLEDGED = 1, + MEMPOOL = 2, + NETWORK = 3, + CONFIRMED = 4, +} + +export type GenericTxInMempoolEvent = + | { + stage: GenericMempoolStage.UNSPECIFIED; + txoRef: Uint8Array; + nativeBytes: Uint8Array; + Tx: Tx | undefined; + } + | { + stage: GenericMempoolStage.ACKNOWLEDGED; + txoRef: Uint8Array; + nativeBytes: Uint8Array; + Tx: Tx | undefined; + } + | { + stage: GenericMempoolStage.MEMPOOL; + txoRef: Uint8Array; + nativeBytes: Uint8Array; + Tx: Tx | undefined; + } + | { + stage: GenericMempoolStage.NETWORK; + txoRef: Uint8Array; + nativeBytes: Uint8Array; + Tx: Tx | undefined + } + | { + stage: GenericMempoolStage.CONFIRMED; + txoRef: Uint8Array; + nativeBytes: Uint8Array; + Tx: Tx | undefined; + }; + export type GenericUtxo = { txoRef: Ref; parsedValued: Parsed | undefined; From 984635285bc92ecd016b2049a63d0f366a170f54 Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Thu, 3 Oct 2024 02:18:32 +0800 Subject: [PATCH 5/7] chore: added parameter to fetchhistory --- src/cardano.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cardano.ts b/src/cardano.ts index bbd4cbb..64b53aa 100644 --- a/src/cardano.ts +++ b/src/cardano.ts @@ -147,13 +147,13 @@ export class SyncClient { return anyChainToBlock(res.block[0])!; } - async fetchHistory(p: ChainPoint): Promise { + async fetchHistory(p: ChainPoint, maxItems = 1): Promise { const req = new sync.DumpHistoryRequest({ startToken: new sync.BlockRef({ index: BigInt(p.slot), hash: Buffer.from(p.hash, "hex"), }), - maxItems: 1, + maxItems: maxItems, }); const res = await this.inner.dumpHistory(req); From 51b5462da2ea18bd1754fd0326a2296a4023b13f Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Thu, 3 Oct 2024 02:26:37 +0800 Subject: [PATCH 6/7] chore: sample cleanup --- examples/node/follow-tip.ts | 151 ++++++++++-------------------------- examples/node/package.json | 2 +- 2 files changed, 40 insertions(+), 113 deletions(-) diff --git a/examples/node/follow-tip.ts b/examples/node/follow-tip.ts index 38859c0..860827a 100644 --- a/examples/node/follow-tip.ts +++ b/examples/node/follow-tip.ts @@ -1,112 +1,39 @@ -import { submit } from "@utxorpc/spec"; -import { SyncClient, QueryClient, Utxo, SubmitClient, WatchClient, ChainPoint } from "../../src/cardano"; - -async function test() { - let syncClient = new SyncClient({ - uri: "http://localhost:50051/" - }); - - let queryClient = new QueryClient({ - uri: "http://localhost:50051/" - }); - - let submitClient = new SubmitClient({ - uri: "http://localhost:50051/" - }); - - const watchClient = new WatchClient({ - uri: "http://localhost:50051/" - }); - - const chainPoint: ChainPoint = - { - slot: 60692700, // Adjust this value as necessary - hash: "1113030bc4a8feb29b4f3e3928cd7193ee1b0ca49f35cf2af9cd881b86375d29", // Replace with the actual hash - }; - - // Fetch history for the provided ChainPoint - try { - const block = await syncClient.fetchHistory(chainPoint); - console.log("Fetched Block:", JSON.stringify(block,null,2)); - } catch (error) { - console.error("Error fetching block history:", error); - } - - // Call watchTx - // const asyncIterable = watchClient.watchTx(chainPoint); - - // try { - // for await (const response of asyncIterable) { - // console.log("Transaction Update:", response); - - // if (response.action.case == "apply") { - // console.log(response.action.value.chain.value); - // } - // if (response.action.case == "undo") { - // console.log(response.action.value.chain.value); - // } - // } - // } catch (error) { - // console.error("Error while watching transactions:", error); - // } - - // watchmempool test - - // const tx = await submitClient.submitTx( - // Buffer.from( - // "84a300d901028182582033dd8b84c4d4d4018c6a0fd83bf892a6dbf6a95afa49f5872e1b4e5f67b25e5f01018282581d60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de8567591a004c4b4082581d60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de8567591b0000000252d052cc021a0002990da100d9010281825820526fa19e3694cda4f3c0d2fb2d2bb8768925eccc49a89d5f12b1972644ac769858403548d4bdb305c0081acd1d53dda37a87abf313e1a816ca25bae99acbca96a69118bd50cb2454aeda6992ad9320ef346b3aeb860fde057bc1b4d8e514eb3f7b01f5f6", - // "hex" - // ) - // ) - // console.log("Transaction Hash:", tx); - // const asyncIterable = submitClient.watchMempoolForAddress( - // Buffer.from( - // "60916c769efc6e2a3339594818a1d0c3998c29e3a6303d8711de856759", - // "hex" - // ) - // ); - - // try { - // for await (const response of asyncIterable) { - // console.log("Mempool Update:", response); - // if (response.tx) { - // console.log("Transaction:", response.tx); - // } - // } - // } catch (error) { - // console.error("Error while watching mempool:", error); - // } - - - - // const tx: any = await submitClient.readMempool(); - - // console.log(tx); - // const params = await queryClient.readParams(); - - // console.log(params); - - // const utxos = await queryClient.searchUtxosByAddress( - // Buffer.from( - // "705c87cbca3a88cbfee6f6ad820acea99f484b4830fc632610f2a30146", - // "hex" - // ) - // ); - // console.log( - // utxos.map((utxo) => { - // console.log(utxo.parsedValued?.script); - // }) - // ); - - // let tip = syncClient.followTip([ - // { - // slot: 54131816, - // hash: "34c65aba4b299113a488b74e2efe3a3dd272d25b470d25f374b2c693d4386535", - // }, - // ]); - - // for await (const event of tip) { - // console.log(event); - // } -} -test().catch(console.error); +import { SyncClient, QueryClient } from "../../src/cardano"; + + async function test() { + let syncClient = new SyncClient({ + uri: "http://localhost:50051", + }); + + let queryClient = new QueryClient({ + uri: "http://localhost:50051", + }); + + const params = await queryClient.readParams(); + + console.log(params); + + const utxos = await queryClient.searchUtxosByAddress( + Buffer.from( + "705c87cbca3a88cbfee6f6ad820acea99f484b4830fc632610f2a30146", + "hex" + ) + ); + console.log( + utxos.map((utxo) => { + console.log(utxo.parsedValued?.script); + }) + ); + + let tip = syncClient.followTip([ + { + slot: 54131816, + hash: "34c65aba4b299113a488b74e2efe3a3dd272d25b470d25f374b2c693d4386535", + }, + ]); + + for await (const event of tip) { + console.log(event); + } + } + test().catch(console.error); \ No newline at end of file diff --git a/examples/node/package.json b/examples/node/package.json index aae47de..4bcc4d2 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -9,7 +9,7 @@ "license": "ISC", "description": "", "dependencies": { - "@saibdev/utxorpc-sdk": "0.6.4" + "@utxorpc/sdk": "^0.6.2" }, "devDependencies": { "tsx": "^4.19.0", From c836007a0abcc433315f1179db841583491c2970 Mon Sep 17 00:00:00 2001 From: johnquinnvictaboada Date: Thu, 3 Oct 2024 03:16:32 +0800 Subject: [PATCH 7/7] chore: architecture corrections --- src/cardano.ts | 12 ++++++------ src/common.ts | 53 ++++++++++---------------------------------------- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/cardano.ts b/src/cardano.ts index 64b53aa..6319cb7 100644 --- a/src/cardano.ts +++ b/src/cardano.ts @@ -15,7 +15,7 @@ import { submitConnect, watchConnect, watch, - cardano, + cardano } from "@utxorpc/spec"; import { @@ -35,7 +35,7 @@ export type MempoolEvent = GenericTxInMempoolEvent; export type TxHash = Uint8Array; export type TxCbor = Uint8Array; -function toMempoolEvent(txInMempool): MempoolEvent { +function toMempoolEvent(txInMempool: submit.TxInMempool): MempoolEvent { return { txoRef: txInMempool.ref, stage: txInMempool.stage, @@ -46,17 +46,17 @@ function toMempoolEvent(txInMempool): MempoolEvent { : undefined, }; } -function toTxEvent(response): TxEvent { +function toTxEvent(response: watch.WatchTxResponse): TxEvent { return { action: response.action.case as "apply" | "undo", Tx: - response.action.value.chain.case == "cardano" + response.action.value?.chain.case == "cardano" ? response.action.value?.chain.value : undefined, }; } -function anyChainToBlock(msg) { +function anyChainToBlock(msg: sync.AnyChainBlock) { return msg.chain.case == "cardano" ? msg.chain.value : null; } @@ -67,7 +67,7 @@ function pointToBlockRef(p: ChainPoint) { }); } -function blockRefToPoint(r) { +function blockRefToPoint(r: sync.BlockRef) { return { slot: r.index.toString(), hash: Buffer.from(r.hash).toString("hex"), diff --git a/src/common.ts b/src/common.ts index a40f53a..a7b0fed 100644 --- a/src/common.ts +++ b/src/common.ts @@ -1,5 +1,5 @@ import { Interceptor } from "@connectrpc/connect"; - +import { submit } from "@utxorpc/spec"; export function metadataInterceptor( options?: ClientBuilderOptions ): Interceptor { @@ -20,48 +20,15 @@ export type GenericTipEvent = | { action: "reset"; point: Point }; export type GenericTxEvent = - | { action: "apply"; Tx: Tx } - | { action: "undo"; Tx: Tx }; - -export enum GenericMempoolStage { - UNSPECIFIED = 0, - ACKNOWLEDGED = 1, - MEMPOOL = 2, - NETWORK = 3, - CONFIRMED = 4, -} - -export type GenericTxInMempoolEvent = - | { - stage: GenericMempoolStage.UNSPECIFIED; - txoRef: Uint8Array; - nativeBytes: Uint8Array; - Tx: Tx | undefined; - } - | { - stage: GenericMempoolStage.ACKNOWLEDGED; - txoRef: Uint8Array; - nativeBytes: Uint8Array; - Tx: Tx | undefined; - } - | { - stage: GenericMempoolStage.MEMPOOL; - txoRef: Uint8Array; - nativeBytes: Uint8Array; - Tx: Tx | undefined; - } - | { - stage: GenericMempoolStage.NETWORK; - txoRef: Uint8Array; - nativeBytes: Uint8Array; - Tx: Tx | undefined - } - | { - stage: GenericMempoolStage.CONFIRMED; - txoRef: Uint8Array; - nativeBytes: Uint8Array; - Tx: Tx | undefined; - }; + | { action: "apply"; Tx: Tx | undefined } + | { action: "undo"; Tx: Tx | undefined }; + +export type GenericTxInMempoolEvent = { + stage: submit.Stage; + txoRef: Uint8Array; + nativeBytes: Uint8Array; + Tx: Tx | undefined; +}; export type GenericUtxo = { txoRef: Ref;