Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
# Truf Network (TN) SDK - Python
# TRUF NETWORK (TN) SDK - Python

Python SDK for interacting with the Truf Network. Uses C bindings to load the TN SDK (Go) library under the hood.
Python SDK for interacting with the TURF NETWORK, a decentralized platform for publishing, composing, and consuming economic data streams. This SDK uses C bindings to load the TN SDK (Go) library under the hood.

## Support

If you need help, don't hesitate to [open an issue](https://github.com/trufnetwork/sdk-py/issues).

## Requirements
- Go
- Python
- Go (for compiling C bindings)
- Python 3.8 or later

## Quick Start

### Installation

You can install the SDK as a dependency using pip with a git URL:

```bash
pip install trufnetwork-sdk-py@git+https://github.com/trufnetwork/sdk-py.git@main
```

Alternatively, if you are using a `pyproject.toml` file for dependency management, add the following:
```toml
[project]
dependencies = [
"trufnetwork-sdk-py@git+https://github.com/trufnetwork/sdk-py.git@main"
]
name = "my-truf-project"
version = "0.1.0"
```

Then install the dependencies:
```bash
pip install .
```

## Development

Expand All @@ -30,7 +59,7 @@ make
from trufnetwork_sdk_py.client import TNClient

# Connect to mainnet
client = TNClient("https://gateway.infra.truf.network", "YOUR_PRIVATE_KEY")
client = TNClient("https://gateway.mainnet.truf.network", "YOUR_PRIVATE_KEY")
```

### Example: Query AI Index Stream
Expand All @@ -42,7 +71,7 @@ from trufnetwork_sdk_py.client import TNClient
from datetime import datetime, timezone

# Connect to Truf Network mainnet
client = TNClient("https://gateway.infra.truf.network", "YOUR_PRIVATE_KEY")
client = TNClient("https://gateway.mainnet.truf.network", "YOUR_PRIVATE_KEY")

# AI Index stream details from explorer
stream_id = "st527bf3897aa3d6f5ae15a0af846db6"
Expand Down
70 changes: 18 additions & 52 deletions examples/main.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,29 @@
from datetime import datetime, timezone
from datetime import datetime, timezone, timedelta
from trufnetwork_sdk_py.client import TNClient
from trufnetwork_sdk_py.utils import generate_stream_id

TEST_PROVIDER_URL = "http://localhost:8484"
TEST_PRIVATE_KEY = "0000000000000000000000000000000000000000000000000000000000000001"
TEST_PROVIDER_URL = "http://localhost:8484" # Replace with mainnet URL https://gateway.mainnet.truf.network if you want to connect to mainnet
TEST_PRIVATE_KEY = "0000000000000000000000000000000000000000000000000000000000000001" # Replace with your private key

def main():
# initialize TN client
client = TNClient(TEST_PROVIDER_URL, TEST_PRIVATE_KEY)

# generate example stream id
primitive_stream_id = generate_stream_id("test_primitive_stream")
composed_stream_id = generate_stream_id("test_composed_stream")

# example of deploying a primitive stream
tx_hash = client.deploy_stream(primitive_stream_id)
print("primitive stream, transaction hash: ", tx_hash)

# example of deploying a composed stream
tx_hash = client.deploy_stream(composed_stream_id, "composed")
print("composed stream, transaction hash: ", tx_hash)

# example of inserting records to primitive stream
records_to_insert = [
{"date": date_string_to_unix("2023-01-01"), "value": 10.5},
{"date": date_string_to_unix("2023-01-02"), "value": 12.2},
{"date": date_string_to_unix("2023-01-03"), "value": 8.8},
]
insert_tx_hash = client.insert_records(primitive_stream_id, records_to_insert)
print("insert record, transaction hash: ", insert_tx_hash)

# get records from streams
retrieved_records = client.get_records(
primitive_stream_id, date_from=date_string_to_unix("2023-01-01"), date_to=date_string_to_unix("2023-01-03")
# example of reading AI Index stream
print("Reading AI Index Stream:")
ai_stream_id = "stai0000000000000000000000000000"
ai_data_provider = "0x4710a8d8f0d845da110086812a32de6d90d7ff5c"
now = datetime.now(timezone.utc)
week_ago = now - timedelta(days=7)
ai_records = client.get_records(
stream_id=ai_stream_id,
data_provider=ai_data_provider,
date_from=int(week_ago.timestamp()),
date_to=int(now.timestamp())
)
print("\nRecords:")
for _, record in enumerate(retrieved_records):
print("Record Time (UNIX): ", record["EventTime"])
print("Value: ", record["Value"])

# example of defining taxonomy for composed stream
child_streams = {
primitive_stream_id: 1 # stream_id : weight
}
tx_hash = client.set_taxonomy(composed_stream_id, child_streams)
print("\ndefine taxonomy, transaction hash: ", tx_hash)

taxonomy = client.describe_taxonomy(composed_stream_id)
print("\nTaxonomy:")
print(taxonomy)

# example of destroying/deleting a stream
client.destroy_stream(primitive_stream_id)
client.destroy_stream(composed_stream_id)

def date_string_to_unix(date_str, date_format="%Y-%m-%d"):
"""Convert a date string to a Unix timestamp (integer)."""
dt = datetime.strptime(date_str, date_format).replace(tzinfo=timezone.utc)
return int(dt.timestamp())
print("AI Index Records:")
for record in ai_records:
date = datetime.fromtimestamp(int(record["EventTime"]), tz=timezone.utc)
print(f"Date: {date.strftime('%Y-%m-%d')}, Value: {record['Value']}")

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9
github.com/kwilteam/kwil-db/core v0.4.2-0.20250506000241-da9d3ddea45e
github.com/pkg/errors v0.9.1
github.com/trufnetwork/sdk-go v0.3.1-0.20250508033514-75eed7527fb4
github.com/trufnetwork/sdk-go v0.3.1-0.20250605091523-f709f0f3a01e
google.golang.org/genproto v0.0.0-20250324211829-b45e905df463
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ github.com/trufnetwork/sdk-go v0.2.1-0.20250331144736-8f0c9d7c829e h1:0yxL7ehUDJ
github.com/trufnetwork/sdk-go v0.2.1-0.20250331144736-8f0c9d7c829e/go.mod h1:9EF6TLPTHyKiTN+wLNgSAzB88ZeZlKYX4mpZ01praJc=
github.com/trufnetwork/sdk-go v0.3.1-0.20250508033514-75eed7527fb4 h1:mMVm9sCzHFV/HX6IiNommN1UTEEnfVp6r4BL9aI070w=
github.com/trufnetwork/sdk-go v0.3.1-0.20250508033514-75eed7527fb4/go.mod h1:vcSgIR+ZRpkUJZ3DWaQVj0YSaPYkfcCPLd332Oo6RUM=
github.com/trufnetwork/sdk-go v0.3.1-0.20250605091523-f709f0f3a01e h1:BvLO1Voxr2HJP+XYSKFoPWEGT9Sa3ks8AlmvrfdC4Ok=
github.com/trufnetwork/sdk-go v0.3.1-0.20250605091523-f709f0f3a01e/go.mod h1:vcSgIR+ZRpkUJZ3DWaQVj0YSaPYkfcCPLd332Oo6RUM=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down