@thru/thru-sdk 0.1.22 → 0.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @thru/thru-sdk
2
2
 
3
- Typed TypeScript/JavaScript client for talking to the Thru blockchain. It wraps the public gRPC-Web endpoints and bundles helpers for working with blocks, accounts, transactions, proofs, and typed identifiers.
3
+ Typed TypeScript/JavaScript client for talking to the Thru blockchain. The SDK exposes rich domain models (blocks, accounts, transactions, events, proofs) that hide the underlying protobuf transport.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,7 +10,7 @@ npm install @thru/thru-sdk
10
10
 
11
11
  ### TypeScript Configuration
12
12
 
13
- For optimal import resolution, we recommend using modern TypeScript module resolution in your `tsconfig.json`:
13
+ For optimal import resolution, use modern module resolution:
14
14
 
15
15
  ```json
16
16
  {
@@ -23,119 +23,141 @@ For optimal import resolution, we recommend using modern TypeScript module resol
23
23
  }
24
24
  ```
25
25
 
26
- **Why?** The SDK uses modern `exports` fields in `package.json` for better tree-shaking and bundler compatibility. The `bundler` resolution strategy fully supports these modern package exports, while the older `node` resolution may require importing from `dist` paths directly.
27
-
28
- If you're using a bundler (Vite, Webpack, esbuild, etc.) or modern build tools, `moduleResolution: "bundler"` is the recommended setting. For Node.js projects, you can also use `"node16"` or `"nodenext"`.
26
+ If you rely on Node’s ESM support without a bundler, use `"moduleResolution": "nodenext"`.
29
27
 
30
28
  ## Basic Usage
31
29
 
32
30
  ```ts
33
- import { createThruClient } from '@thru/thru-sdk';
31
+ import { createThruClient } from "@thru/thru-sdk";
32
+ import {
33
+ Account,
34
+ Block,
35
+ ChainEvent,
36
+ Transaction,
37
+ TransactionStatusSnapshot,
38
+ } from "@thru/thru-sdk";
34
39
 
35
- // Point at the default public alpha cluster or override with your own URL
36
40
  const thru = createThruClient({
37
- baseUrl: 'https://grpc-web.alphanet.thruput.org',
41
+ baseUrl: "https://grpc-web.alphanet.thruput.org",
38
42
  });
39
43
 
40
- // Fetch the cluster height and the latest finalized block
41
- const { finalized } = await thru.blocks.getBlockHeight();
42
- const latestBlock = await thru.blocks.get({ slot: finalized });
44
+ // Fetch the latest finalized block
45
+ const height = await thru.blocks.getBlockHeight();
46
+ const latestBlock: Block = await thru.blocks.get({ slot: height.finalized });
47
+ console.log(latestBlock.header.blockHash);
43
48
 
44
- // Retrieve an account by address (Base58-like strings starting with "ta")
45
- const account = await thru.accounts.get('taExampleAddress...');
49
+ // Fetch an account returns the Account domain object
50
+ const account: Account = await thru.accounts.get("taExampleAddress...");
51
+ console.log(account.meta?.balance);
46
52
 
47
- // Build, sign, and submit a transaction
53
+ // Build, sign, submit, and track a transaction
48
54
  const { rawTransaction, signature } = await thru.transactions.buildAndSign({
49
55
  feePayer: {
50
- publicKey: 'taFeePayerAddress...',
56
+ publicKey: "taFeePayerAddress...",
51
57
  privateKey: feePayerSecretKeyBytes,
52
58
  },
53
59
  program: programIdentifierBytes,
54
60
  });
55
61
  await thru.transactions.send(rawTransaction);
62
+
63
+ // Track the transaction – emits domain snapshots
64
+ for await (const update of thru.streaming.trackTransaction(signature)) {
65
+ console.log(update.status, update.executionResult?.consumedComputeUnits);
66
+ if (update.statusCode === ConsensusStatus.FINALIZED) break;
67
+ }
56
68
  ```
57
69
 
58
- ### Using Filters
70
+ ## Domain Models
59
71
 
60
- Many SDK functions support CEL (Common Expression Language) filters to query or stream data based on custom expressions evaluated server-side. Filters are constructed using the `create` function from `@bufbuild/protobuf` (already a dependency of `@thru/thru-sdk`).
72
+ The SDK revolves around immutable domain classes. They copy mutable buffers, expose clear invariants, and provide conversion helpers where needed.
61
73
 
62
- #### Constructing Filters
74
+ | API surface | Domain class |
75
+ | --- | --- |
76
+ | Blocks | `Block`, `BlockHeader`, `BlockFooter` |
77
+ | Accounts | `Account`, `AccountMeta`, `AccountData` |
78
+ | Transactions | `Transaction`, `TransactionStatusSnapshot`, `TrackTransactionUpdate` |
79
+ | Events | `ChainEvent` |
80
+ | Proofs | `StateProof` |
81
+ | Height | `HeightSnapshot` |
82
+ | Node version | `VersionInfo` |
83
+
84
+ All classes are exported from the root package for easy access:
63
85
 
64
86
  ```ts
65
- import { create } from "@bufbuild/protobuf";
66
- import {
67
- FilterSchema,
68
- FilterParamValueSchema,
69
- type Filter,
70
- type FilterParamValue
71
- } from "@thru/thru-sdk";
87
+ import { Block, Account, ChainEvent } from "@thru/thru-sdk";
88
+ ```
72
89
 
73
- // Create a filter parameter value
74
- const paramValue = create(FilterParamValueSchema, {
75
- kind: {
76
- case: "bytesValue", // or "stringValue", "boolValue", "intValue", "doubleValue"
77
- value: new Uint8Array(32), // the actual value
78
- },
79
- });
90
+ ## Streaming APIs
80
91
 
81
- // Create the filter with a CEL expression
82
- const filter = create(FilterSchema, {
83
- expression: "meta.owner.value == params.owner_bytes",
84
- params: {
85
- owner_bytes: paramValue,
86
- },
87
- });
88
- ```
92
+ Every streaming endpoint yields an async iterable of domain models:
89
93
 
90
- #### Filter Parameter Value Types
94
+ ```ts
95
+ // Blocks
96
+ for await (const { block } of thru.streaming.streamBlocks()) {
97
+ console.log(block.header.slot);
98
+ }
91
99
 
92
- Filter parameters support these types:
93
- - `{ case: "stringValue", value: string }` - for string parameters
94
- - `{ case: "bytesValue", value: Uint8Array }` - for byte array parameters
95
- - `{ case: "boolValue", value: boolean }` - for boolean parameters
96
- - `{ case: "intValue", value: bigint }` - for integer parameters
97
- - `{ case: "doubleValue", value: number }` - for floating-point parameters
100
+ // Account updates
101
+ for await (const { update } of thru.streaming.streamAccountUpdates("taAddress")) {
102
+ if (update.kind === "snapshot") {
103
+ console.log(update.snapshot.account.meta?.balance);
104
+ }
105
+ }
98
106
 
99
- #### Functions That Accept Filters
107
+ // Events
108
+ for await (const { event } of thru.streaming.streamEvents()) {
109
+ console.log((event as ChainEvent).timestampNs);
110
+ }
100
111
 
101
- **Query Functions:**
102
- - `thru.accounts.list({ filter })` - List accounts with filtering
103
- - `thru.blocks.list({ filter })` - List blocks with filtering
104
- - `thru.transactions.listForAccount(account, { filter })` - List transactions for an account with filtering
112
+ // Transaction tracking
113
+ for await (const update of thru.streaming.trackTransaction(signature)) {
114
+ console.log(update.status, update.executionResult?.consumedComputeUnits);
115
+ }
116
+ ```
105
117
 
106
- **Streaming Functions:**
107
- - `thru.streaming.streamBlocks({ filter })` - Stream blocks with filtering
108
- - `thru.streaming.streamAccountUpdates(address, { filter })` - Stream account updates with filtering
109
- - `thru.streaming.streamTransactions({ filter })` - Stream transactions with filtering
110
- - `thru.streaming.streamEvents({ filter })` - Stream events with filtering
118
+ ## Filters
111
119
 
112
- #### Example: Filtering Accounts by Owner
120
+ Server-side filtering is supported everywhere via CEL expressions:
113
121
 
114
122
  ```ts
115
- // List accounts owned by a specific public key
116
- const ownerBytes = new Uint8Array(32); // your owner pubkey bytes
123
+ import { create } from "@bufbuild/protobuf";
124
+ import {
125
+ FilterSchema,
126
+ FilterParamValueSchema,
127
+ } from "@thru/thru-sdk";
117
128
 
129
+ const ownerBytes = new Uint8Array(32);
118
130
  const ownerParam = create(FilterParamValueSchema, {
119
- kind: {
120
- case: "bytesValue",
121
- value: ownerBytes,
122
- },
131
+ kind: { case: "bytesValue", value: ownerBytes },
123
132
  });
124
133
 
125
134
  const filter = create(FilterSchema, {
126
135
  expression: "meta.owner.value == params.owner_bytes",
127
- params: {
128
- owner_bytes: ownerParam,
129
- },
136
+ params: { owner_bytes: ownerParam },
130
137
  });
131
138
 
132
- const response = await thru.accounts.list({ filter });
139
+ const accounts = await thru.accounts.list({ filter });
133
140
  ```
134
141
 
135
- ### Modules at a Glance
142
+ Accepted parameter kinds:
143
+ - `stringValue`
144
+ - `bytesValue`
145
+ - `boolValue`
146
+ - `intValue`
147
+ - `doubleValue`
148
+
149
+ Functions that take filters:
150
+ - List APIs: `thru.accounts.list`, `thru.blocks.list`, `thru.transactions.listForAccount`
151
+ - Streams: `thru.streaming.streamBlocks`, `thru.streaming.streamAccountUpdates`, `thru.streaming.streamTransactions`, `thru.streaming.streamEvents`
152
+
153
+ ## Modules Overview
154
+
155
+ - `thru.blocks` — fetch/stream blocks and height snapshots
156
+ - `thru.accounts` — read account state or build create-account transactions
157
+ - `thru.transactions` — build, sign, submit, track, and inspect transactions
158
+ - `thru.events` — query event history
159
+ - `thru.proofs` — generate state proofs
160
+ - `thru.streaming` — streaming wrappers for blocks, accounts, transactions, events
161
+ - `thru.helpers` — address, signature, and block-hash conversion helpers
136
162
 
137
- - `thru.blocks` query finalized or raw blocks and stream height information
138
- - `thru.accounts` — fetch account state, list owned accounts, and generate create-account transactions
139
- - `thru.transactions` — build transactions locally, sign them, submit, and inspect status
140
- - `thru.events` / `thru.proofs` — retrieve on-chain events and generate state proofs
141
- - `thru.helpers` — convert to/from Thru identifiers (addresses, signatures, block hashes) and derive program addresses
163
+ The public surface is fully domain-based; reaching for lower-level protobuf structures is no longer necessary.