Skip to content

Commit 89d59d0

Browse files
committed
docs: Enhance SDK documentation and examples
- Update stream lifecycle documentation with correct timestamp handling - Improve stream deletion and resource management explanations - Refactor code examples to match current SDK implementation - Add detailed guidance on stream creation, management, and deletion - Ensure consistency across API reference, examples, and core documentation - Clarify stream types, permissions, and best practices - Improve error handling and transaction management guidance Key improvements: - Corrected date/timestamp usage in examples - Added comprehensive stream deletion examples - Updated API reference with precise method descriptions - Enhanced documentation for primitive and composed streams - Provided clearer explanations of stream lifecycle and permissions
1 parent 90336b5 commit 89d59d0

File tree

16 files changed

+989
-251
lines changed

16 files changed

+989
-251
lines changed

README.md

Lines changed: 234 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ go get github.com/trufnetwork/sdk-go
4141
task single:start
4242
```
4343

44-
**Note:** Setting up a local node as described above will initialize an empty database. This setup is primarily for testing the technology or development purposes. If you are a node operator and wish to sync with the Truf Network to access real data, please follow the [Node Operator Guide](https://github.com/trufnetwork/node/blob/main/docs/node-operator-guide.md) for instructions on connecting to the network and syncing data.
44+
**Note:** Setting up a local node as described above will initialize an empty database. This setup is primarily for testing the technology or development purposes. If you are a node operator and wish to sync with the TRUF.NETWORK to access real data, please follow the [Node Operator Guide](https://github.com/trufnetwork/node/blob/main/docs/node-operator-guide.md) for instructions on connecting to the network and syncing data.
4545

4646
4. **Verify Node Synchronization**
4747

@@ -176,7 +176,7 @@ func main() {
176176
}
177177

178178
// Now you can perform operations on the mainnet
179-
fmt.Println("Connected to Truf Network Mainnet")
179+
fmt.Println("Connected to TRUF.NETWORK Mainnet")
180180
}
181181
```
182182

@@ -229,3 +229,235 @@ For additional support or questions, please [open an issue](https://github.com/t
229229

230230
The SDK-Go repository is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE.md) for more details.
231231

232+
## Stream Creation and Management
233+
234+
The TN SDK provides comprehensive support for creating and managing both primitive and composed streams.
235+
236+
### Primitive Streams
237+
238+
Primitive streams are raw data sources that can represent various types of data points. To create a primitive stream:
239+
240+
```go
241+
// Generate a unique stream ID
242+
primitiveStreamId := util.GenerateStreamId("my-market-data-stream")
243+
244+
// Deploy the primitive stream
245+
deployTx, err := tnClient.DeployStream(ctx, primitiveStreamId, types.StreamTypePrimitive)
246+
247+
// Insert records into the primitive stream
248+
primitiveActions, err := tnClient.LoadPrimitiveActions()
249+
insertTx, err := primitiveActions.InsertRecords(ctx, []types.InsertRecordInput{
250+
{
251+
DataProvider: dataProviderAddress,
252+
StreamId: primitiveStreamId.String(),
253+
EventTime: int(time.Now().Unix()),
254+
Value: 100.5,
255+
},
256+
})
257+
```
258+
259+
### Composed Streams
260+
261+
Composed streams aggregate and process data from multiple primitive or other composed streams. They use a taxonomy to define how child streams are combined:
262+
263+
```go
264+
// Deploy a composed stream
265+
composedStreamId := util.GenerateStreamId("my-composite-index")
266+
deployTx, err := tnClient.DeployStream(ctx, composedStreamId, types.StreamTypeComposed)
267+
268+
// Load composed actions
269+
composedActions, err := tnClient.LoadComposedActions()
270+
271+
// Set taxonomy (define how child streams are combined)
272+
taxonomyTx, err := composedActions.InsertTaxonomy(ctx, types.Taxonomy{
273+
ParentStream: tnClient.OwnStreamLocator(composedStreamId),
274+
TaxonomyItems: []types.TaxonomyItem{
275+
{
276+
ChildStream: tnClient.OwnStreamLocator(primitiveStreamId1),
277+
Weight: 0.6, // 60% weight
278+
},
279+
{
280+
ChildStream: tnClient.OwnStreamLocator(primitiveStreamId2),
281+
Weight: 0.4, // 40% weight
282+
},
283+
},
284+
})
285+
```
286+
287+
### Complex Stream Creation Example
288+
289+
For a comprehensive example demonstrating stream creation, taxonomy setup, and data retrieval, see the `examples/complex_stream_example/main.go` file. This example shows:
290+
291+
- Deploying primitive streams
292+
- Inserting records into primitive streams
293+
- Creating a composed stream
294+
- Setting up stream taxonomy
295+
- Retrieving composed stream records
296+
297+
Key steps include:
298+
1. Generating unique stream IDs
299+
2. Deploying primitive and composed streams
300+
3. Inserting records into primitive streams
301+
4. Defining stream taxonomy
302+
5. Retrieving composed stream records
303+
304+
This example provides a practical walkthrough of creating and managing streams in the TRUF.NETWORK ecosystem.
305+
306+
### Stream Locators and Data Providers
307+
308+
#### Stream Locators
309+
310+
A `StreamLocator` is a unique identifier for a stream that consists of two key components:
311+
1. `StreamId`: A unique identifier for the stream
312+
2. `DataProvider`: The Ethereum address of the stream's creator/owner
313+
314+
The `OwnStreamLocator()` method is a convenience function that automatically creates a `StreamLocator` using:
315+
- The provided `StreamId`
316+
- The current client's Ethereum address
317+
318+
Example:
319+
```go
320+
// Creates a StreamLocator with:
321+
// - The given stream ID
322+
// - The current client's address as the data provider
323+
streamLocator := tnClient.OwnStreamLocator(myStreamId)
324+
```
325+
326+
This is particularly useful when you're creating and managing your own streams, as it automatically uses your client's address.
327+
328+
#### Data Providers
329+
330+
A `DataProvider` is the Ethereum address responsible for creating and managing a stream. When inserting records or performing operations on a stream, you need to specify the data provider's address.
331+
332+
To get the current client's address, use:
333+
```go
334+
// Get the current client's Ethereum address
335+
dataProviderAddress := tnClient.Address()
336+
337+
// Get the address as a string for use in stream operations
338+
dataProviderAddressString := dataProviderAddress.Address()
339+
```
340+
341+
Key differences:
342+
- `tnClient.Address()` returns an `EthereumAddress` object
343+
- `dataProviderAddress.Address()` returns the address as a string, which is used in stream operations
344+
345+
### Example of Stream Creation with Locators and Providers
346+
347+
```go
348+
// Generate a stream ID
349+
streamId := util.GenerateStreamId("my-stream")
350+
351+
// Deploy the stream using the current client's address
352+
deployTx, err := tnClient.DeployStream(ctx, streamId, types.StreamTypePrimitive)
353+
354+
// Create a stream locator
355+
streamLocator := tnClient.OwnStreamLocator(streamId)
356+
357+
// Get the data provider address
358+
dataProvider := tnClient.Address()
359+
360+
// Insert a record using the data provider address
361+
insertTx, err := primitiveActions.InsertRecords(ctx, []types.InsertRecordInput{
362+
{
363+
DataProvider: dataProvider.Address(),
364+
StreamId: streamId.String(),
365+
EventTime: int(time.Now().Unix()),
366+
Value: 100.5,
367+
},
368+
})
369+
```
370+
371+
This approach ensures that:
372+
- Streams are uniquely identified
373+
- Records are correctly attributed to their creator
374+
- Stream operations are performed with the correct addressing
375+
376+
### Stream Deletion and Resource Management
377+
378+
#### Why Delete Streams?
379+
380+
Stream deletion is crucial for:
381+
- Cleaning up unused or test streams
382+
- Managing resource consumption
383+
- Maintaining a clean and organized stream ecosystem
384+
385+
#### Stream Deletion Process
386+
387+
Streams can be deleted using the `DestroyStream()` method:
388+
389+
```go
390+
// Destroy a specific stream
391+
destroyTx, err := tnClient.DestroyStream(ctx, streamId)
392+
if err != nil {
393+
// Handle deletion error
394+
log.Printf("Failed to destroy stream: %v", err)
395+
}
396+
397+
// Wait for the destroy transaction to be mined
398+
_, err = tnClient.WaitForTx(ctx, destroyTx, time.Second*5)
399+
if err != nil {
400+
log.Printf("Error waiting for stream destruction: %v", err)
401+
}
402+
```
403+
404+
#### Best Practices for Stream Deletion
405+
406+
1. **Cleanup in Reverse Order**
407+
- Delete composed streams before their child primitive streams
408+
- Ensures proper resource management and prevents orphaned references
409+
410+
2. **Error Handling**
411+
- Always check for errors during stream deletion
412+
- Log and handle potential issues gracefully
413+
414+
3. **Deferred Deletion**
415+
- Use `defer` for automatic cleanup in test or example scenarios
416+
- Ensures resources are freed even if an error occurs
417+
418+
Example of Deferred Stream Deletion:
419+
```go
420+
func main() {
421+
// Defer stream destruction
422+
defer func() {
423+
streamIds := []util.StreamId{
424+
composedStreamId,
425+
primitiveStreamId1,
426+
primitiveStreamId2,
427+
}
428+
429+
for _, streamId := range streamIds {
430+
destroyTx, err := tnClient.DestroyStream(ctx, streamId)
431+
if err != nil {
432+
log.Printf("Failed to destroy stream %s: %v", streamId, err)
433+
continue
434+
}
435+
436+
// Wait for the destroy transaction
437+
_, err = tnClient.WaitForTx(ctx, destroyTx, time.Second*5)
438+
if err != nil {
439+
log.Printf("Error waiting for destroy transaction: %v", err)
440+
}
441+
}
442+
}()
443+
444+
// Rest of the stream creation and management code
445+
}
446+
```
447+
448+
#### Considerations
449+
450+
- Stream deletion is a permanent action
451+
- Deleted streams cannot be recovered
452+
- Ensure you have the necessary permissions to delete a stream
453+
- In production, implement additional safeguards before deletion
454+
455+
### When to Delete Streams
456+
457+
- After completing testing
458+
- When streams are no longer needed
459+
- To free up resources
460+
- As part of a stream lifecycle management strategy
461+
462+
By following these guidelines, you can effectively manage stream resources in the TRUF.NETWORK ecosystem.
463+
Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
11
# API Reference
22

3-
The Truf Node SDK offers a comprehensive set of APIs for interacting with the TN, allowing developers to create, manage, and consume data streams. This document provides detailed descriptions of all available methods in the SDK, along with examples of their usage.
3+
## TRUF.NETWORK SDK Overview
4+
5+
The TRUF.NETWORK SDK provides a comprehensive toolkit for developers to interact with decentralized data streams. It enables seamless creation, management, and consumption of economic and financial data streams.
6+
7+
### Key Features
8+
- Stream creation and management
9+
- Primitive and composed stream support
10+
- Flexible data retrieval
11+
- Advanced permission management
12+
- Secure, blockchain-backed data streams
413

514
## Interfaces
6-
- [Client](client.md)
7-
- [Primitive Stream](primitive-stream.md)
8-
- [Composed Stream](composed-stream.md)
915

10-
Other utilities are in the [util](util.md) documentation.
16+
The SDK is structured around several key interfaces:
1117

12-
## Example Usage
18+
- [Client](client.md): Primary entry point for network interactions
19+
- [Primitive Stream](primitive-stream.md): Raw data stream management
20+
- [Composed Stream](composed-stream.md): Aggregated data stream handling
21+
22+
## Core Concepts
23+
24+
### Streams
25+
- **Primitive Streams**: Direct data sources with raw data points
26+
- **Composed Streams**: Aggregated streams combining multiple data sources
1327

14-
Below is an example demonstrating how to use the TN SDK to deploy, initialize, and read from a primitive stream.
28+
### Data Management
29+
- Secure, immutable data recording
30+
- Flexible querying and indexing
31+
- Granular access control
32+
33+
## Example Usage
1534

1635
```go
1736
package main
1837

1938
import (
2039
"context"
21-
"fmt"
22-
"github.com/golang-sql/civil"
23-
"github.com/kwilteam/kwil-db/core/crypto"
24-
"github.com/kwilteam/kwil-db/core/crypto/auth"
2540
"github.com/trufnetwork/sdk-go/core/tnclient"
2641
"github.com/trufnetwork/sdk-go/core/types"
2742
"github.com/trufnetwork/sdk-go/core/util"
28-
"time"
2943
)
3044

3145
func main() {
32-
// handle errors appropriately in a real application
3346
ctx := context.Background()
3447

35-
// Create TN client
36-
pk, _ := crypto.Secp256k1PrivateKeyFromHex("<your-private-key-hex>")
37-
signer := &auth.EthPersonalSigner{Key: *pk}
38-
tnClient, _ := tnclient.NewClient(ctx, "<https://tsn-provider-url.com>", tnclient.WithSigner(signer))
39-
40-
// Generate a stream ID
41-
streamId := util.GenerateStreamId("example-stream")
42-
43-
// Deploy a new primitive stream
44-
deployTxHash, _ := tnClient.DeployStream(ctx, streamId, types.StreamTypePrimitive)
45-
46-
// Wait for the transaction to be mined
47-
txRes, _ := tnClient.WaitForTx(ctx, deployTxHash, time.Second)
48-
49-
// Load the deployed stream
50-
stream, _ := tnClient.LoadPrimitiveStream(tnClient.OwnStreamLocator(streamId))
51-
52-
// Initialize the stream
53-
txHashInit, _ := stream.InitializeStream(ctx)
54-
55-
// Wait for the initialization transaction to be mined
56-
txResInit, _ := tnClient.WaitForTx(ctx, txHashInit, time.Second)
57-
fmt.Println("Initialize transaction result:", txResInit)
58-
59-
// Insert records into the stream
60-
txHashInsert, _ := stream.InsertRecords(ctx, []types.InsertRecordInput{
61-
{
62-
Value: 1,
63-
DateValue: civil.Date{Year: 2023, Month: 1, Day: 1},
64-
},
65-
})
48+
// Initialize client with mainnet endpoint
49+
tnClient, err := tnclient.NewClient(
50+
ctx,
51+
"https://gateway.mainnet.truf.network",
52+
tnclient.WithSigner(mySigner),
53+
)
54+
if err != nil {
55+
// Handle client initialization error
56+
}
57+
58+
// Deploy a primitive stream
59+
streamId := util.GenerateStreamId("my-economic-stream")
60+
deployTx, err := tnClient.DeployStream(
61+
ctx,
62+
streamId,
63+
types.StreamTypePrimitive,
64+
)
65+
// Handle deployment and further stream operations
66+
}
67+
```
6668

67-
// Wait for the insert transaction to be mined
68-
_, _ = tnClient.WaitForTx(ctx, txHashInsert, time.Second)
69+
## Getting Started
6970

70-
// Read records from the stream
71-
records, _ := stream.GetRecord(ctx, types.GetRecordInput{
72-
DateFrom: civil.ParseDate("2023-01-01"),
73-
DateTo: civil.ParseDate("2023-01-31"),
74-
})
75-
fmt.Println("Records:", records)
76-
}
71+
1. Install the SDK
72+
2. Configure your network endpoint
73+
3. Initialize a client
74+
4. Create and manage streams
7775

78-
```
76+
## Support and Community
7977

80-
Please refer to the test files in the SDK repository for more examples and detailed usage patterns. These tests provide comprehensive examples of various stream operations and error-handling scenarios.
78+
- [GitHub Repository](https://github.com/trufnetwork/sdk-go)
79+
- [Issue Tracker](https://github.com/trufnetwork/sdk-go/issues)
80+
- [Documentation](https://docs.truf.network)

0 commit comments

Comments
 (0)