From 320173d79ca05b21d4e036c3315cb44611bc952f Mon Sep 17 00:00:00 2001 From: lucanicoladebiasi Date: Wed, 1 Jan 2025 10:57:49 +0100 Subject: [PATCH] feat: 1484 node documented --- .../diagrams/v2/net/thor/accounts/accounts.md | 1 - .../net/thor/subscriptions/subscriptions.md | 20 +- .../thor/transactions/transactions-module.md | 97 ------- .../v2/net/thor/transactions/transactions.md | 274 +++++++++--------- .../src/thor/accounts/ExecuteCodesResponse.ts | 7 - .../NewTransactionSubscription.ts | 2 +- .../subscriptions/TransfersSubscription.ts | 51 +++- packages/net/src/thor/subscriptions/index.ts | 1 - packages/net/src/thor/transactions/Clause.ts | 8 +- .../src/thor/transactions/GetTxResponse.ts | 20 +- .../net/src/thor/transactions/ReceiptMeta.ts | 4 +- .../RetrieveRawTransactionByID.ts | 4 +- .../transactions/RetrieveTransactionByID.ts | 10 +- .../RetrieveTransactionReceipt.ts | 8 +- .../src/thor/transactions/SendTransaction.ts | 18 +- .../{subscriptions => transactions}/TXID.ts | 0 packages/net/src/thor/transactions/index.ts | 1 + .../accounts/InspectClauses.testnet.test.ts | 1 - .../thor/blocks/RetrieveBlock.testnet.test.ts | 35 +-- .../BeatsSubscription.solo.test.ts | 9 + .../BlocksSubscription.solo.test.ts | 5 + .../NewTransactionSubscription.solo.test.ts | 11 +- 22 files changed, 248 insertions(+), 339 deletions(-) delete mode 100644 docs/diagrams/v2/net/thor/transactions/transactions-module.md rename packages/net/src/thor/{subscriptions => transactions}/TXID.ts (100%) diff --git a/docs/diagrams/v2/net/thor/accounts/accounts.md b/docs/diagrams/v2/net/thor/accounts/accounts.md index b46a207ac..bf9020525 100644 --- a/docs/diagrams/v2/net/thor/accounts/accounts.md +++ b/docs/diagrams/v2/net/thor/accounts/accounts.md @@ -120,7 +120,6 @@ classDiagram } class ExecuteCodesResponse { constructor(json: ExecuteCodesResponseJSON) ExecuteCodesResponse - toJSON() ExecuteCodesResponseJSON } class ExecuteCodesResponseJSON { <> diff --git a/docs/diagrams/v2/net/thor/subscriptions/subscriptions.md b/docs/diagrams/v2/net/thor/subscriptions/subscriptions.md index e6f56cb2d..f9484eac0 100644 --- a/docs/diagrams/v2/net/thor/subscriptions/subscriptions.md +++ b/docs/diagrams/v2/net/thor/subscriptions/subscriptions.md @@ -20,6 +20,17 @@ classDiagram clauseIndex: number } } + namespace trasactions { + class TXID { + id: ThorId + constructor(json: TXIDJSON): TXID + toJSON() TXIDJSON + } + class TXIDJSON { + <> + id: string + } + } namespace ws { class WebSocketClient { <> @@ -176,15 +187,6 @@ classDiagram obsolete: boolean meta: LogMetaJSON } - class TXID { - id: ThorId - constructor(json: TXIDJSON): TXID - toJSON() TXIDJSON - } - class TXIDJSON { - <> - id: string - } WebSocketClient <|.. BeatsSubscription WebSocketClient <|.. BlocksSubscription WebSocketClient <|.. EventsSubscription diff --git a/docs/diagrams/v2/net/thor/transactions/transactions-module.md b/docs/diagrams/v2/net/thor/transactions/transactions-module.md deleted file mode 100644 index cc628faa4..000000000 --- a/docs/diagrams/v2/net/thor/transactions/transactions-module.md +++ /dev/null @@ -1,97 +0,0 @@ -```mermaid -classDiagram - class HttpClient { - <> - } - class ThorRequest~Request~ { - <> - ThorResponse~Request~ askTo(HttpClient httpClient) - } - class ThorResponse~Response~ { - <> - ThorRequest~Request~ request - Response response - } - namespace Request { - class RetrieveTransactionByID { - HexUInt id - RetrieveTransactionByID_Query query - } - class RetrieveTransactionByID_Query { - BlockId head - boolean pending - boolean raw - } - class RetrieveTransactionReceipt { - HexUInt id - RetrieveTransactionReceipt_Query query - } - class RetrieveTransactionReceipt_Query { - BlockId head - } - class SendTransaction { - } - class SendTransaction_Body { - HexUInt raw - } - } - namespace Response { - class SendTransactionResponse { - HexUInt id - } - class GetTxResponse { - HexUInt id - Address origin - Address delegator - number size - number chainTag - string BlockRef - number expiration - Clause[] clauses - number gasPriceCoef - VTHO gaa - HexUInt dependsOn - HexUInt nonce - TxMeta meta - } - class GetTxReceiptResponse { - number gasUsed - Address gasPayer - VTHO paid - VTHO reward - boolean reverted - TransactionOutput[] outputs - ReceiptMeta meta - } - } - class Clause { - Address to - VET value - HexUInt data - } - class TxMeta { - BlockID blockID - number blockBumber - number BlockTimeestamp - } - - HttpClient o-- ThorRequest - - ThorRequest <|.. RetrieveTransactionByID - ThorRequest <|.. RetrieveTransactionReceipt - ThorRequest <|.. SendTransaction - - ThorRequest --* ThorResponse - - ThorResponse <|.. GetTxResponse - ThorResponse <|.. GetTxReceiptResponse - ThorResponse <|.. SendTransactionResponse - - RetrieveTransactionByID --* RetrieveTransactionByID_Query - RetrieveTransactionReceipt --* RetrieveTransactionReceipt_Query - SendTransaction --* SendTransaction_Body - - GetTxResponse --* Clause - GetTxResponse --* TxMeta - -``` diff --git a/docs/diagrams/v2/net/thor/transactions/transactions.md b/docs/diagrams/v2/net/thor/transactions/transactions.md index a41477ca7..78cba088f 100644 --- a/docs/diagrams/v2/net/thor/transactions/transactions.md +++ b/docs/diagrams/v2/net/thor/transactions/transactions.md @@ -1,27 +1,47 @@ ```mermaid classDiagram + namespace http { + class HttpClient { + <> + get(httpPath: HttpPath) Promise~Response~ + post(httpPath: HttpPath, body?: unknown) Promise~Response~ + } + class HttpPath { + <> + path: string + } + } + namespace thor { + class ThorRequest~RequestClass~ { + <> + askTo(httpClient: HttpClient Promise~ThorResponse~ResponseClass~~ + } + class ThorResponse~ResponseClass~ { + <> + request: ThorRequest~RequestClass~ + response: ResponseClass + } + } class Clause { - to: Address + to?: Address value: VET data: HexUInt - constructor(json: EventJSON) - toJSON() EventJSON + constructor(json: ClauseJSON) Clause + toJSON() ClauseJSON } class ClauseJSON { - <> - to: string + to?: string value: string - data string + data: string } class Event { address: Address topics: ThorId[] - data HexUInt - constructor(json: EventJSON) + data: HexUInt + constructor(json: EventJSON) Event toJSON() EventJSON } class EventJSON { - <> address: string topics: string[] data: string @@ -29,18 +49,17 @@ classDiagram class GetRawTxResponse { raw: HexUInt meta: TxMeta - constructor(json: GetTawTxResponseJSON) - toJSON() GetTawTxResponseJSON + constructor(json: GetRawTxResponseJSON) GetRawTxResponse + toJSON() GetRawTxResponseJSON } class GetRawTxResponseJSON { - <> - raw: string; + raw: string meta: TxMetaJSON } class GetTxReceiptResponse { meta: ReceiptMeta - constructor(json: GetTxReceiptResponseJSON) - toJSON(): GetTxReceiptResponseJSON + constructor(json: GetTxReceiptResponseJSON) GetTxReceiptResponse + toJSON() GetTxReceiptResponseJSON } class GetTxReceiptResponseJSON { meta: ReceiptMetaJSON @@ -48,7 +67,7 @@ classDiagram class GetTxResponse { id: TxId origin: Address - delegator: Address|null + delegator?: Address size: UInt chainTag: UInt blockRef: BlockId @@ -56,17 +75,16 @@ classDiagram clauses: Clause[] gasPriceCoef: UInt gas: VTHO - dependsOn: TxId|null + dependsOn?: TxId nonce: Nonce meta: TxMeta - constructor(json: GetTxResponseJSON) + constructor(json: GetTxResponseJSON) GetTxResponse toJSON() GetTxResponseJSON } class GetTxResponseJSON { - <> id: string origin: string - delegator: string|null + delegator?: string size: number chainTag: number blockRef: string @@ -74,10 +92,22 @@ classDiagram clauses: ClauseJSON[] gasPriceCoef: number gas: number - dependsOn: string|null + dependsOn?: string nonce: string meta: TxMetaJSON } + class TxMeta { + blockID: BlockId + blockNumber: UInt + blockTimestamp: bigint + constructor(json: TxMetaJSON) TxMeta + toJSON() TxMetaJSON + } + class TxMetaJSON { + blockID: string + blockNumber: number + blockTimestamp: bigint + } class Receipt { gasUsed: VTHO gasPayer: Address @@ -85,26 +115,24 @@ classDiagram reward: VTHO reverted: boolean outputs: ReceiptOutput[] - constructor(json: ReceiptJSON) + constructor(json: ReceiptJSON) Receipt toJSON() ReceiptJSON } class ReceiptJSON { - <> - gasUsed: number; - gasPayer: string; - paid: string; - reward: string; - reverted: boolean; - outputs: ReceiptOutputJSON[]; + gasUsed: number + gasPayer: string + paid: string + reward: string + reverted: boolean + outputs: ReceiptOutputJSON[] } class ReceiptMeta { txID: TxId txOrigin: Address - constructor(json: ReceiptMetaJSON) + constructor(json: ReceiptMetaJSON) ReceiptMeta toJSON() ReceiptMetaJSON } class ReceiptMetaJSON { - <> txID: string txOrigin: string } @@ -112,142 +140,130 @@ classDiagram contractAddress: Address events: Event[] transfers: Transfer[] - constructor(json: ReceiptOutputJSON) - toJSON(): ReceiptOutputJSON + constructor(json: ReceiptOutputJSON) ReceiptOutput + toJSON() ReceiptOutputJSON } class ReceiptOutputJSON { - <> contractAddress: string events: EventJSON[] transfers: TransferJSON[] } class RetrieveRawTransactionByID { - path: RetrieveRawTransactionByIDPath; - query: RetrieveRawTransactionByIDQuery; - constructor(path: RetrieveRawTransactionByIDPath, query: RetrieveRawTransactionByIDQuery) - askTo(httpClient: HttpClient): Promise~ThorResponse~ RetrieveRawTransactionByID, GetRawTxResponse~~ - of(txId: TxId) RetrieveRawTransactionByID - withHead(head: BlockId|null) RetrieveRawTransactionByID - withPending(pending: boolean) RetrieveRawTransactionByID - } - class RetrieveRawTransactionByIDPath { - } - class RetrieveRawTransactionByIDQuery { - } - class RetrieveTransactionByID { - path: RetrieveTransactionByIDPath - query: RetrieveTransactionByIDQuery - constructor(path: RetrieveTransactionByIDPath, query: RetrieveTransactionByIDQuery) - askTo(httpClient: HttpClient) Promise~ThorResponse~ RetrieveTransactionByID, GetTxResponse~~ - of(txId: TxId) RetrieveTransactionByID - withHead(head: BlockId|null): RetrieveTransactionByID - withPending(pending: boolean): RetrieveTransactionByID + path: RetrieveRawTransactionByIDPath + query: RetrieveRawTransactionByIDQuery + askTo(httpClient: HttpClient) Promise~ThorResponse~ + of(txId: TxId) RetrieveRawTransactionByID$ + withHead(head?: BlockId) RetrieveTransactionByID$ + withPending(pending: boolean) RetrieveTransactionByID$ } class RetrieveTransactionByIDPath { txId: TxId - constructor(txId: TxId) } class RetrieveTransactionByIDQuery { - head: BlockId | null; + head?: BlockId pending: boolean - constructor(head: BlockId|null, pending: boolean) RetrieveTransactionByIDQuery } class RetrieveTransactionReceipt { path: RetrieveTransactionReceiptPath query: RetrieveTransactionReceiptQuery - constructor(path: RetrieveTransactionReceiptPath, query: RetrieveTransactionReceiptQuery) - askTo(httpClient: HttpClient): Promise~ThorResponse~ RetrieveTransactionReceipt, GetTxReceiptResponse~~ - of(txId: TxId): RetrieveTransactionReceipt - withHead(head: BlockId): RetrieveTransactionReceipt + askTo(httpPath: HttpPath) Promise~ThorResponse~GetTxReceiptResponse~~ + of(txId: TxId) RetrieveTransactionReceipt$ + withHead(head?: BlockId) RetrieveTransactionReceipt } class RetrieveTransactionReceiptPath { txId: TxId - constructor(txId: TxId) } class RetrieveTransactionReceiptQuery { - head: BlockId|null; - constructor(head: BlockId|null) + head?: BlockId } class SendTransaction { - PATH: HttpPath + PATH: HttpPath$ encoded: Uint8Array - askTo(httpClient: httpClient) - of(encoded: Uint8Array): SendTransaction + askTo(httpPath: HttpPath) Promise~ThorResponse~TXID~~ + of(encoded: Uint8Array) SendTransaction$ } class Transfer { - sender: Address; - recipient: Address; - amount: VET; - constructor(json: TransferJSON) - toJSON(): TransferJSON + sender: Address + recipient: Address + amount: VET + constructor(json: TransferJSON) Transfer + toJSON() TransferJSON } class TransferJSON { - <> - sender: string; - recipient: string; - amount: string; - } - class TxMeta { - blockID: BlockId; - blockNumber: UInt; - blockTimestamp: bigint; - constructor(json: TxMetaJSON) - toJSON(): TxMetaJSON + sender: string + recipient: string + amount: string } - class TxMetaJSON { - <> - blockID: string; - blockNumber: number; - blockTimestamp: bigint; + class TXID { + id: ThorId + constructor(json: TXIDJSON): TXID + toJSON() TXIDJSON } - class ThorRequest { + class TXIDJSON { <> - askTo: (httpClient: HttpClient) + id: string } - class HttpPath { - <> + class TxMeta { + blockID: BlockId + blockNumber: UInt + blockTimestamp: bigint + constructor(json: TxMetaJSON) TxMeta + toJSON() TxMetaJSON } - class HttpQuery { - <> + class TxMetaJSON { + blockID: string + blockNumber: number + blockTimestamp: bigint } - RetrieveRawTransactionByIDPath ..|> HttpPath - RetrieveTransactionByIDPath ..|> HttpPath - RetrieveTransactionReceiptPath ..|> HttpPath - RetrieveRawTransactionByIDQuery ..|> HttpQuery - RetrieveTransactionByIDQuery ..|> HttpQuery - RetrieveTransactionReceiptQuery ..|> HttpQuery - ThorRequest <|.. RetrieveRawTransactionByID - ThorRequest <|.. RetrieveTransactionByID - ThorRequest <|.. RetrieveTransactionReceipt - ThorRequest <|.. SendTransaction + Clause --> "new - toJSON" ClauseJSON + Event --> "new - toJSON" EventJSON + GetRawTxResponse *--> TxMeta + GetRawTxResponse --> "new - toJSON" GetRawTxResponseJSON + GetRawTxResponse <-- "askTo" RetrieveRawTransactionByID + GetRawTxResponseJSON *--> TxMetaJSON + GetTxReceiptResponse *--> ReceiptMeta + GetTxReceiptResponse --> "new - toJSON" GetTxReceiptResponseJSON + GetTxReceiptResponseJSON *--> ReceiptMetaJSON + GetTxResponse *--> "*" Clause + GetTxResponse --> "new - toJSON" GetTxResponseJSON + GetTxResponseJSON *--> "*" ClauseJSON + HttpPath <--* SendTransaction + HttpPath <|.. RetrieveTransactionByIDPath + HttpPath <|.. RetrieveTransactionReceiptPath + HttpQuery <|.. RetrieveTransactionByIDQuery + HttpQuery <|.. RetrieveTransactionReceiptQuery + Receipt *--> "*" ReceiptOutput + Receipt --> "new - toJSON" ReceiptJSON Receipt <|-- GetTxReceiptResponse - RetrieveTransactionByIDPath <|-- RetrieveRawTransactionByIDPath - RetrieveTransactionByIDQuery <|-- RetrieveRawTransactionByIDQuery - TxMeta <-- ReceiptMeta + ReceiptJSON *--> "*" ReceiptOutputJSON + ReceiptJSON <|-- GetTxReceiptResponseJSON + ReceiptMeta --> "new - toJSON" ReceiptMetaJSON + ReceiptOutput *--> "*" Event + ReceiptOutput *--> "*" Transfer + ReceiptOutput --> "new - toJSON" ReceiptOutputJSON + ReceiptOutputJSON *--> "*" EventJSON + ReceiptOutputJSON *--> "*" TransferJSON RetrieveRawTransactionByID *--> RetrieveRawTransactionByIDPath RetrieveRawTransactionByID *--> RetrieveRawTransactionByIDQuery - RetrieveTransactionByID *--> RetrieveTransactionByIDPath - RetrieveTransactionByID *--> RetrieveRawTransactionByIDQuery + RetrieveRawTransactionByID --> "askTo" GetRawTxResponse + RetrieveTransactionByID --> "askTo" GetTxResponse + RetrieveTransactionByIDPath <|-- RetrieveRawTransactionByIDPath + RetrieveTransactionByIDQuery <|-- RetrieveRawTransactionByIDQuery RetrieveTransactionReceipt *--> RetrieveTransactionReceiptPath RetrieveTransactionReceipt *--> RetrieveTransactionReceiptQuery - GetRawTxResponse *--> TxMeta - GetTxReceiptResponse *--> ReceiptMeta - GetTxResponse *--> Clause - GetTxResponse *--> TxMeta - Receipt *--> ReceiptOutput - ReceiptOutput *--> Event - ReceiptOutput *--> Transfer - ClauseJSON <-- Clause - EventJSON <-- Event - GetRawTxResponseJSON <-- GetRawTxResponse - GetTxReceiptResponseJSON <-- GetTxReceiptResponse - GetTxResponseJSON <-- GetTxResponse - ReceiptJSON <-- Receipt - ReceiptMetaJSON <-- ReceiptMeta - ReceiptOutputJSON <-- ReceiptOutput - TransferJSON <-- Transfer - TxMetaJSON <-- TxMeta - GetRawTxResponse <-- RetrieveRawTransactionByID - GetTxResponse <-- RetrieveTransactionByID - GetTxReceiptResponse <-- RetrieveTransactionReceipt + RetrieveTransactionReceipt --> "askTo" GetTxReceiptResponse + SendTransaction --> "askTo" TXID + ThorRequest <|.. RetrieveRawTransactionByID + ThorRequest <|.. RetrieveTransactionByID + ThorRequest <|.. RetrieveTransactionReceipt + ThorRequest <|.. SendTransaction + ThorResponse <-- "askTo" RetrieveRawTransactionByID + ThorResponse <-- "askTo" RetrieveTransactionByID + ThorResponse <-- "askTo" RetrieveTransactionReceipt + ThorResponse <-- "askTo" SendTransaction + Transfer --> "new - toJSON" TransferJSON + TXID --> "new - toJSON" TXIDJSON + TxMeta --> "new - toJSON" TxMetaJSON + TxMeta <|-- ReceiptMeta + TxMetaJSON <|-- ReceiptMetaJSON ``` diff --git a/packages/net/src/thor/accounts/ExecuteCodesResponse.ts b/packages/net/src/thor/accounts/ExecuteCodesResponse.ts index 4aa25cedf..bdd547304 100644 --- a/packages/net/src/thor/accounts/ExecuteCodesResponse.ts +++ b/packages/net/src/thor/accounts/ExecuteCodesResponse.ts @@ -52,13 +52,6 @@ class ExecuteCodesResponse extends Array { ) ); } - - toJSON(): ExecuteCodesResponseJSON { - return this.map( - (response: ExecuteCodeResponse): ExecuteCodeResponseJSON => - response.toJSON() - ) as ExecuteCodesResponseJSON; - } } interface ExecuteCodeResponseJSON { diff --git a/packages/net/src/thor/subscriptions/NewTransactionSubscription.ts b/packages/net/src/thor/subscriptions/NewTransactionSubscription.ts index 8ef59755c..51a58f203 100644 --- a/packages/net/src/thor/subscriptions/NewTransactionSubscription.ts +++ b/packages/net/src/thor/subscriptions/NewTransactionSubscription.ts @@ -1,6 +1,6 @@ import { type WebSocketClient, type WebSocketListener } from '../../ws'; import type { HttpPath } from '../../http'; -import { TXID, type TXIDJSON } from './TXID'; +import { TXID, type TXIDJSON } from '../transactions/TXID'; class NewTransactionSubscription implements WebSocketClient, WebSocketListener diff --git a/packages/net/src/thor/subscriptions/TransfersSubscription.ts b/packages/net/src/thor/subscriptions/TransfersSubscription.ts index 2ae670e0d..0be102bb7 100644 --- a/packages/net/src/thor/subscriptions/TransfersSubscription.ts +++ b/packages/net/src/thor/subscriptions/TransfersSubscription.ts @@ -4,11 +4,11 @@ import { type WebSocketClient, type WebSocketListener } from '../../ws'; import { type SubscriptionTransferResponse } from './SubscriptionTransferResponse'; class TransfersSubscription - implements WebSocketClient, WebSocketListener + implements WebSocketClient, WebSocketListener { static readonly PATH: HttpPath = { path: '/subscriptions/transfer' }; - private readonly messageListeners: Array< + private readonly listeners: Array< WebSocketListener > = []; @@ -24,10 +24,25 @@ class TransfersSubscription this.query = query; } - addListener( - listener: WebSocketListener - ): this { - this.messageListeners.push(listener); + addListener(listener: WebSocketListener): WebSocketClient { + this.listeners.push(listener); + return this; + } + + close(): WebSocketClient { + this.wsc.close(); + return this; + } + + open(): WebSocketClient { + this.wsc.addListener(this).open({ + path: TransfersSubscription.PATH.path + this.query.query + }); + return this; + } + + removeListener(listener: WebSocketListener): WebSocketClient { + this.listeners.splice(this.listeners.indexOf(listener), 1); return this; } @@ -39,9 +54,16 @@ class TransfersSubscription return this.wsc.baseURL; } - close(): this { - this.wsc.close(); - return this; + onClose(event: Event): void { + this.listeners.forEach((listener) => { + listener.onClose(event); + }); + } + + onError(event: Event): void { + this.listeners.forEach((listener) => { + listener.onError(event); + }); } onMessage(event: MessageEvent): void { @@ -52,16 +74,15 @@ class TransfersSubscription event.type, { data: json } ); - this.messageListeners.forEach((listener) => { + this.listeners.forEach((listener) => { listener.onMessage(message); }); } - open(): this { - this.wsc - .addListener(this) - .open({ path: TransfersSubscription.PATH.path + this.query.query }); - return this; + onOpen(event: Event): void { + this.listeners.forEach((listener) => { + listener.onOpen(event); + }); } } diff --git a/packages/net/src/thor/subscriptions/index.ts b/packages/net/src/thor/subscriptions/index.ts index eef21e6d0..4f8935c99 100644 --- a/packages/net/src/thor/subscriptions/index.ts +++ b/packages/net/src/thor/subscriptions/index.ts @@ -7,4 +7,3 @@ export * from './SubscriptionBlockResponse'; export * from './SubscriptionEventResponse'; export * from './SubscriptionTransferResponse'; export * from './TransfersSubscription'; -export * from './TXID'; diff --git a/packages/net/src/thor/transactions/Clause.ts b/packages/net/src/thor/transactions/Clause.ts index 922adb697..e3f95c030 100644 --- a/packages/net/src/thor/transactions/Clause.ts +++ b/packages/net/src/thor/transactions/Clause.ts @@ -1,19 +1,19 @@ import { Address, HexUInt, VET } from '@vechain/sdk-core'; class Clause { - readonly to: Address | null; + readonly to?: Address; readonly value: VET; readonly data: HexUInt; constructor(json: ClauseJSON) { - this.to = json.to === null ? null : Address.of(json.to); + this.to = json.to !== undefined ? Address.of(json.to) : undefined; this.value = VET.of(json.value); this.data = HexUInt.of(json.data); } toJSON(): ClauseJSON { return { - to: this.to === null ? null : this.to.toString(), + to: this.to?.toString(), value: HexUInt.of(this.value.wei).toString(), data: this.data.toString() } satisfies ClauseJSON; @@ -21,7 +21,7 @@ class Clause { } interface ClauseJSON { - to: string | null; + to?: string; value: string; data: string; } diff --git a/packages/net/src/thor/transactions/GetTxResponse.ts b/packages/net/src/thor/transactions/GetTxResponse.ts index 59e0ce5e2..903548f0b 100644 --- a/packages/net/src/thor/transactions/GetTxResponse.ts +++ b/packages/net/src/thor/transactions/GetTxResponse.ts @@ -9,7 +9,7 @@ import { UInt } from '../../../../core/src/vcdm/UInt'; class GetTxResponse { readonly id: TxId; readonly origin: Address; - readonly delegator: Address | null; + readonly delegator?: Address; readonly size: UInt; readonly chainTag: UInt; readonly blockRef: BlockId; @@ -17,7 +17,7 @@ class GetTxResponse { readonly clauses: Clause[]; readonly gasPriceCoef: UInt; readonly gas: VTHO; - readonly dependsOn: TxId | null; + readonly dependsOn?: TxId; readonly nonce: Nonce; readonly meta: TxMeta; @@ -25,7 +25,9 @@ class GetTxResponse { this.id = TxId.of(json.id); this.origin = Address.of(json.origin); this.delegator = - json.delegator === null ? null : Address.of(json.delegator); + json.delegator !== undefined + ? Address.of(json.delegator) + : undefined; this.size = UInt.of(json.size); this.chainTag = UInt.of(json.chainTag); this.blockRef = BlockId.of(json.blockRef); @@ -36,7 +38,7 @@ class GetTxResponse { this.gasPriceCoef = UInt.of(json.gasPriceCoef); this.gas = VTHO.of(json.gas); this.dependsOn = - json.dependsOn === null ? null : TxId.of(json.dependsOn); + json.dependsOn !== undefined ? TxId.of(json.dependsOn) : undefined; this.nonce = Nonce.of(json.nonce); this.meta = new TxMeta(json.meta); } @@ -45,8 +47,7 @@ class GetTxResponse { return { id: this.id.toString(), origin: this.origin.toString(), - delegator: - this.delegator === null ? null : this.delegator.toString(), + delegator: this.delegator?.toString(), size: this.size.valueOf(), chainTag: this.chainTag.valueOf(), blockRef: this.blockRef.toString(), @@ -54,8 +55,7 @@ class GetTxResponse { clauses: this.clauses.map((clause) => clause.toJSON()), gasPriceCoef: this.gasPriceCoef.valueOf(), gas: Number(this.gas.wei), - dependsOn: - this.dependsOn === null ? null : this.dependsOn.toString(), + dependsOn: this.dependsOn?.toString(), nonce: this.nonce.toString(), meta: this.meta.toJSON() } satisfies GetTxResponseJSON; @@ -65,7 +65,7 @@ class GetTxResponse { interface GetTxResponseJSON { id: string; origin: string; - delegator: string | null; + delegator?: string; size: number; chainTag: number; blockRef: string; @@ -73,7 +73,7 @@ interface GetTxResponseJSON { clauses: ClauseJSON[]; gasPriceCoef: number; gas: number; - dependsOn: string | null; + dependsOn?: string; nonce: string; meta: TxMetaJSON; } diff --git a/packages/net/src/thor/transactions/ReceiptMeta.ts b/packages/net/src/thor/transactions/ReceiptMeta.ts index 2d896a1b9..47216cf0d 100644 --- a/packages/net/src/thor/transactions/ReceiptMeta.ts +++ b/packages/net/src/thor/transactions/ReceiptMeta.ts @@ -4,8 +4,8 @@ import { Address } from '@vechain/sdk-core'; import { TxId } from '../../../../core/src/vcdm/BlockId'; class ReceiptMeta extends TxMeta { - txID: TxId; - txOrigin: Address; + readonly txID: TxId; + readonly txOrigin: Address; constructor(json: ReceiptMetaJSON) { super(json); diff --git a/packages/net/src/thor/transactions/RetrieveRawTransactionByID.ts b/packages/net/src/thor/transactions/RetrieveRawTransactionByID.ts index 6cd6092cb..308098d0a 100644 --- a/packages/net/src/thor/transactions/RetrieveRawTransactionByID.ts +++ b/packages/net/src/thor/transactions/RetrieveRawTransactionByID.ts @@ -42,11 +42,11 @@ class RetrieveRawTransactionByID static of(txId: TxId): RetrieveRawTransactionByID { return new RetrieveRawTransactionByID( new RetrieveRawTransactionByIDPath(txId), - new RetrieveRawTransactionByIDQuery(null, false) + new RetrieveRawTransactionByIDQuery(undefined, false) ); } - withHead(head: BlockId | null = null): RetrieveRawTransactionByID { + withHead(head?: BlockId): RetrieveRawTransactionByID { return new RetrieveRawTransactionByID( this.path, new RetrieveRawTransactionByIDQuery(head, this.query.pending) diff --git a/packages/net/src/thor/transactions/RetrieveTransactionByID.ts b/packages/net/src/thor/transactions/RetrieveTransactionByID.ts index cc2625b1e..1ae1abc32 100644 --- a/packages/net/src/thor/transactions/RetrieveTransactionByID.ts +++ b/packages/net/src/thor/transactions/RetrieveTransactionByID.ts @@ -35,11 +35,11 @@ class RetrieveTransactionByID static of(txId: TxId): RetrieveTransactionByID { return new RetrieveTransactionByID( new RetrieveTransactionByIDPath(txId), - new RetrieveTransactionByIDQuery(null, false) + new RetrieveTransactionByIDQuery(undefined, false) ); } - withHead(head: BlockId | null = null): RetrieveTransactionByID { + withHead(head?: BlockId): RetrieveTransactionByID { return new RetrieveTransactionByID( this.path, new RetrieveTransactionByIDQuery(head, this.query.pending) @@ -67,16 +67,16 @@ class RetrieveTransactionByIDPath implements HttpPath { } class RetrieveTransactionByIDQuery implements HttpQuery { - readonly head: BlockId | null; + readonly head?: BlockId; readonly pending: boolean; - constructor(head: BlockId | null, pending: boolean) { + constructor(head: BlockId | undefined, pending: boolean) { this.head = head; this.pending = pending; } get query(): string { - const head = this.head === null ? '' : `${this.head}&`; + const head = this.head === undefined ? '' : `${this.head}&`; return `?${head}pending=${this.pending}&raw=false`; } } diff --git a/packages/net/src/thor/transactions/RetrieveTransactionReceipt.ts b/packages/net/src/thor/transactions/RetrieveTransactionReceipt.ts index 60994af0a..d1e9acad3 100644 --- a/packages/net/src/thor/transactions/RetrieveTransactionReceipt.ts +++ b/packages/net/src/thor/transactions/RetrieveTransactionReceipt.ts @@ -42,11 +42,11 @@ class RetrieveTransactionReceipt static of(txId: TxId): RetrieveTransactionReceipt { return new RetrieveTransactionReceipt( new RetrieveTransactionReceiptPath(txId), - new RetrieveTransactionReceiptQuery(null) + new RetrieveTransactionReceiptQuery(undefined) ); } - withHead(head: BlockId | null = null): RetrieveTransactionReceipt { + withHead(head?: BlockId): RetrieveTransactionReceipt { return new RetrieveTransactionReceipt( this.path, new RetrieveTransactionReceiptQuery(head) @@ -67,9 +67,9 @@ class RetrieveTransactionReceiptPath implements HttpPath { } class RetrieveTransactionReceiptQuery implements HttpQuery { - readonly head: BlockId | null; + readonly head?: BlockId; - constructor(head: BlockId | null) { + constructor(head: BlockId | undefined) { this.head = head; } diff --git a/packages/net/src/thor/transactions/SendTransaction.ts b/packages/net/src/thor/transactions/SendTransaction.ts index 72c01bea7..4f4aa6fc3 100644 --- a/packages/net/src/thor/transactions/SendTransaction.ts +++ b/packages/net/src/thor/transactions/SendTransaction.ts @@ -2,10 +2,9 @@ import { HexUInt } from '@vechain/sdk-core'; import { type HttpClient, type HttpPath } from '../../http'; import { type ThorRequest } from '../ThorRequest'; import { type ThorResponse } from '../ThorResponse'; +import { TXID, type TXIDJSON } from './TXID'; -import { TxId } from '../../../../core/src/vcdm/BlockId'; - -class SendTransaction implements ThorRequest { +class SendTransaction implements ThorRequest { static readonly PATH: HttpPath = { path: '/transactions' }; readonly encoded: Uint8Array; @@ -16,7 +15,7 @@ class SendTransaction implements ThorRequest { async askTo( httpClient: HttpClient - ): Promise> { + ): Promise> { const response = await httpClient.post( SendTransaction.PATH, { query: '' }, @@ -24,12 +23,11 @@ class SendTransaction implements ThorRequest { raw: HexUInt.of(this.encoded).toString() } ); - const responseBody = - (await response.json()) as SendTransactionResponseJSON; + const json = (await response.json()) as TXIDJSON; return { request: this, - response: TxId.of(responseBody.id) - } satisfies ThorResponse; + response: new TXID(json) + } satisfies ThorResponse; } static of(encoded: Uint8Array): SendTransaction { @@ -37,8 +35,4 @@ class SendTransaction implements ThorRequest { } } -interface SendTransactionResponseJSON { - id: string; -} - export { SendTransaction }; diff --git a/packages/net/src/thor/subscriptions/TXID.ts b/packages/net/src/thor/transactions/TXID.ts similarity index 100% rename from packages/net/src/thor/subscriptions/TXID.ts rename to packages/net/src/thor/transactions/TXID.ts diff --git a/packages/net/src/thor/transactions/index.ts b/packages/net/src/thor/transactions/index.ts index 41cd0ca72..65504bc01 100644 --- a/packages/net/src/thor/transactions/index.ts +++ b/packages/net/src/thor/transactions/index.ts @@ -10,5 +10,6 @@ export * from './RetrieveRawTransactionByID'; export * from './RetrieveTransactionByID'; export * from './RetrieveTransactionReceipt'; export * from './SendTransaction'; +export * from './TXID'; export * from './Transfer'; export * from './TxMeta'; diff --git a/packages/net/tests/thor/accounts/InspectClauses.testnet.test.ts b/packages/net/tests/thor/accounts/InspectClauses.testnet.test.ts index 193f7481c..bb0593b2a 100644 --- a/packages/net/tests/thor/accounts/InspectClauses.testnet.test.ts +++ b/packages/net/tests/thor/accounts/InspectClauses.testnet.test.ts @@ -28,7 +28,6 @@ describe('InspectClauses testnet tests', () => { data: '0x' }, { - to: null, value: '0x0', data: '0x6080604052348015600f57600080fd5b50609f8061001e6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631820cabb146044575b600080fd5b348015604f57600080fd5b506056606c565b6040518082815260200191505060405180910390f35b62015180815600a165627a7a723058200ac7475da248e2fc26c057319e296e90c24d5f8b9bf956fb3b77545642cad3b10029' } diff --git a/packages/net/tests/thor/blocks/RetrieveBlock.testnet.test.ts b/packages/net/tests/thor/blocks/RetrieveBlock.testnet.test.ts index 4dfc3a4f0..320cccc7d 100644 --- a/packages/net/tests/thor/blocks/RetrieveBlock.testnet.test.ts +++ b/packages/net/tests/thor/blocks/RetrieveBlock.testnet.test.ts @@ -1,10 +1,5 @@ import { describe, test } from '@jest/globals'; -import { - FetchHttpClient, - RetrieveBlock, - RetrieveBlockPath, - ThorNetworks -} from '../../../src'; +import { FetchHttpClient, RetrieveBlock, ThorNetworks } from '../../../src'; import { Revision } from '@vechain/sdk-core'; describe('RetrieveBlock testnet tests', () => { @@ -14,32 +9,4 @@ describe('RetrieveBlock testnet tests', () => { ); console.log(JSON.stringify(r, null, 2)); }); - - test('explore', async () => { - const httpClient = FetchHttpClient.at(ThorNetworks.TESTNET); - const lastBlockNumber = await getBestBlockNumber(httpClient); - for ( - let blockNumber = 0; - blockNumber <= lastBlockNumber; - blockNumber++ - ) { - const block = ( - await RetrieveBlock.of(Revision.of(blockNumber)).askTo( - httpClient - ) - ).response; - block.transactions.forEach((tx) => { - console.log(`${block.number}/${tx}`); - }); - } - }); - - async function getBestBlockNumber( - httpClient: FetchHttpClient - ): Promise { - const r = await new RetrieveBlock( - new RetrieveBlockPath(Revision.BEST) - ).askTo(httpClient); - return r.response.number.valueOf(); - } }); diff --git a/packages/net/tests/thor/subscriptions/BeatsSubscription.solo.test.ts b/packages/net/tests/thor/subscriptions/BeatsSubscription.solo.test.ts index 68790d5fc..28572b9ad 100644 --- a/packages/net/tests/thor/subscriptions/BeatsSubscription.solo.test.ts +++ b/packages/net/tests/thor/subscriptions/BeatsSubscription.solo.test.ts @@ -23,6 +23,15 @@ describe('BlocksSubscription solo tests', () => { const data = message.data; console.log(JSON.stringify(data, null, 2)); done(); + }, + onOpen: () => { + console.log('WebSocket connection opened'); + }, + onClose: () => { + console.log(`WebSocket connection closed`); + }, + onError: (error) => { + console.error('WebSocket encountered an error:', error); } } satisfies WebSocketListener) .open(); diff --git a/packages/net/tests/thor/subscriptions/BlocksSubscription.solo.test.ts b/packages/net/tests/thor/subscriptions/BlocksSubscription.solo.test.ts index 92ce0007c..da30ba968 100644 --- a/packages/net/tests/thor/subscriptions/BlocksSubscription.solo.test.ts +++ b/packages/net/tests/thor/subscriptions/BlocksSubscription.solo.test.ts @@ -23,6 +23,11 @@ describe('BlocksSubscription solo tests', () => { const data = message.data; console.log(JSON.stringify(data, null, 2)); done(); + }, + onOpen: () => {}, + onClose: () => {}, + onError: (error) => { + console.error('WebSocket error:', error); } } satisfies WebSocketListener) .open(); diff --git a/packages/net/tests/thor/subscriptions/NewTransactionSubscription.solo.test.ts b/packages/net/tests/thor/subscriptions/NewTransactionSubscription.solo.test.ts index aa3af3ab7..d70ef1978 100644 --- a/packages/net/tests/thor/subscriptions/NewTransactionSubscription.solo.test.ts +++ b/packages/net/tests/thor/subscriptions/NewTransactionSubscription.solo.test.ts @@ -3,10 +3,8 @@ import { MozillaWebSocketClient, type WebSocketListener } from '../../../src/ws'; -import { - NewTransactionSubscription, - type TXID -} from '../../../src/thor/subscriptions'; +import { NewTransactionSubscription } from '../../../src/thor/subscriptions'; +import { type TXID } from '../../../src'; describe('NewTransactionSubscription solo tests', () => { let subscription: NewTransactionSubscription; @@ -23,7 +21,10 @@ describe('NewTransactionSubscription solo tests', () => { const data = message.data; console.log(JSON.stringify(data, null, 2)); done(); - } + }, + onClose: () => {}, + onError: () => {}, + onOpen: () => {} } satisfies WebSocketListener) .open(); }, 30000);