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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions core/contractsapi/stream_procedures.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func (s *Action) GetRecord(ctx context.Context, input types.GetRecordInput) ([]t
args = append(args, transformOrNil(input.To, func(date int) any { return date }))
args = append(args, transformOrNil(input.FrozenAt, func(date int) any { return date }))

results, err := s.call(ctx, "get_record", args)
prefix := ""
if input.Prefix != nil {
prefix = *input.Prefix
}

results, err := s.call(ctx, prefix + "get_record", args)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down Expand Up @@ -193,7 +198,12 @@ func (s *Action) GetIndex(ctx context.Context, input types.GetIndexInput) ([]typ
args = append(args, transformOrNil(input.FrozenAt, func(date int) any { return date }))
args = append(args, transformOrNil(input.BaseDate, func(date int) any { return date }))

results, err := s.call(ctx, "get_index", args)
prefix := ""
if input.Prefix != nil {
prefix = *input.Prefix
}

results, err := s.call(ctx, prefix + "get_index", args)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
1 change: 1 addition & 0 deletions core/types/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type GetRecordInput struct {
To *int
FrozenAt *int
BaseDate *int
Prefix *string
}

type GetIndexInput = GetRecordInput
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Before running the example:
## Running the Example

```bash
go mod tidy
go run main.go
```

Expand Down
1 change: 1 addition & 0 deletions examples/complex_stream_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ defer func() {
2. Adjust the `endpoint` to match your TN node (local or mainnet)
3. Run the example:
```bash
go mod tidy
go run main.go
```

Expand Down
2 changes: 1 addition & 1 deletion examples/complex_stream_example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.24.1

require (
github.com/kwilteam/kwil-db/core v0.4.2-0.20250506000241-da9d3ddea45e
github.com/trufnetwork/sdk-go v0.3.1
github.com/trufnetwork/sdk-go main
)

require (
Expand Down
1 change: 1 addition & 0 deletions examples/custom_procedure_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Feel free to replace the procedure name and arguments with your own.
3. In this directory run:

```bash
go mod tidy
go run .
```

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_procedure_example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.24.1

require (
github.com/kwilteam/kwil-db/core v0.4.2-0.20250506000241-da9d3ddea45e
github.com/trufnetwork/sdk-go v0.3.1
github.com/trufnetwork/sdk-go main
)

require (
Expand Down
56 changes: 56 additions & 0 deletions examples/custom_procedure_with_prefix_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Custom Index with Prefix Integration

## Overview

This example demonstrates the retrieval of custom indexes with prefix functionality from existing streams within the TRUF.NETWORK (TN) SDK framework.

> **Note:** Data provider partnership is required to integrate custom methods with prefix functionality into standard operations as shown in this implementation.

## Objectives

This implementation illustrates:
- Establishing connections to TN nodes (local or mainnet environments)
- Retrieving indexed data from predefined streams using standard methods enhanced with prefix capabilities

## Core Components

- TN client initialization and configuration
- Stream connection establishment
- Stream index retrieval operations
- Time-based index query

## System Requirements

- Go 1.20 or later
- TRUF.NETWORK SDK
- Active TN node access (local or mainnet)
- Valid stream for data retrieval operations

## Setup Instructions

Prior to execution:
1. Replace `"your-private-key"` with your authenticated private key
2. Configure the `endpoint` to match your designated TN node
3. Verify stream ID and data provider specifications

## Execution

```bash
go mod tidy
go run .
```

## Implementation Notes

- This example utilizes a preconfigured AI Index stream
- Stream parameters should be adjusted to match specific implementation requirements
- Production environments require robust error handling mechanisms
- Prefix functionality is currently supported for `get_record` and `get_index` operations only

## Extension Possibilities

This framework can be extended to:

- Interface with multiple stream sources
- Implement custom time range parameters for index queries
- Integrate advanced data processing and analytics capabilities
29 changes: 29 additions & 0 deletions examples/custom_procedure_with_prefix_example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module github.com/trufnetwork/sdk-go/examples/custom_procedure_example

go 1.24.1

require (
github.com/kwilteam/kwil-db/core v0.4.2-0.20250506000241-da9d3ddea45e
github.com/trufnetwork/sdk-go main
)

require (
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
github.com/decred/slog v1.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jrick/logrotate v1.1.2 // indirect
github.com/kwilteam/kwil-db v0.10.2 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
)
56 changes: 56 additions & 0 deletions examples/custom_procedure_with_prefix_example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/crypto/blake256 v1.1.0 h1:zPMNGQCm0g4QTY27fOCorQW7EryeQ/U0x++OzVrdms8=
github.com/decred/dcrd/crypto/blake256 v1.1.0/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 h1:NMZiJj8QnKe1LgsbDayM4UoHwbvwDRwnI3hwNaAHRnc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0/go.mod h1:ZXNYxsqcloTdSy/rNShjYzMhyjf0LaoftYK0p+A3h40=
github.com/decred/slog v1.2.0 h1:soHAxV52B54Di3WtKLfPum9OFfWqwtf/ygf9njdfnPM=
github.com/decred/slog v1.2.0/go.mod h1:kVXlGnt6DHy2fV5OjSeuvCJ0OmlmTF6LFpEPMu/fOY0=
github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc=
github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jrick/logrotate v1.1.2 h1:6ePk462NCX7TfKtNp5JJ7MbA2YIslkpfgP03TlTYMN0=
github.com/jrick/logrotate v1.1.2/go.mod h1:f9tdWggSVK3iqavGpyvegq5IhNois7KXmasU6/N96OQ=
github.com/kwilteam/kwil-db v0.10.2 h1:EEEJ5h2hYnPLsAVSHVSsH1OegzgDU/EKSeer41bJg8s=
github.com/kwilteam/kwil-db v0.10.2/go.mod h1:46IO8tO8gCBgETv90Lwscx1wDtYOBMxVHhlO8OAjfdc=
github.com/kwilteam/kwil-db/core v0.4.2-0.20250506000241-da9d3ddea45e h1:v0spmBGP7AGA/NBoW3CSbXUd+AGZMIzguNuS6Oppd/c=
github.com/kwilteam/kwil-db/core v0.4.2-0.20250506000241-da9d3ddea45e/go.mod h1:clG9Pk1NNs83dbIZZZn/6I+F4C9xntYTHbMFjL1HQ0w=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/trufnetwork/sdk-go v0.3.2-0.20250619042418-ab1467f1b8a4 h1:J8Y90u9CZCy/12GEJpw+i2WjQaBRxZonZiDqRucNwR0=
github.com/trufnetwork/sdk-go v0.3.2-0.20250619042418-ab1467f1b8a4/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=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
golang.org/x/net v0.35.0/go.mod h1:EglIi67kWsHKlRzzVMUD93VMSWGFOMSZgxFjparz1Qk=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
76 changes: 76 additions & 0 deletions examples/custom_procedure_with_prefix_example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"context"
"fmt"
"log"
"time"

"github.com/kwilteam/kwil-db/core/crypto"
"github.com/kwilteam/kwil-db/core/crypto/auth"
"github.com/trufnetwork/sdk-go/core/tnclient"
"github.com/trufnetwork/sdk-go/core/types"
)

func main() {
ctx := context.Background()

// 1. Set up local node connection
// Replace with your actual private key
pk, err := crypto.Secp256k1PrivateKeyFromHex("your-private-key")
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}
signer := &auth.EthPersonalSigner{Key: *pk}

// Choose endpoint: Use "http://localhost:8484" for local node or "https://gateway.mainnet.truf.network" for mainnet
endpoint := "https://gateway.mainnet.truf.network"
tnClient, err := tnclient.NewClient(
ctx,
endpoint,
tnclient.WithSigner(signer),
)
if err != nil {
log.Fatalf("Failed to create TN client: %v", err)
}

// 2. AI Index stream details
dataProvider := "0x4710a8d8f0d845da110086812a32de6d90d7ff5c"
streamId := "stai0000000000000000000000000000"

// 3. Prepare index retrieval parameters
now := time.Now()
dayAgo := now.AddDate(0, 0, -1)
fromTime := int(dayAgo.Unix())
toTime := int(now.Unix())

// 4. Load composed actions and retrieve indexes
// Note: AI Index is a composed stream that aggregates data from multiple primitive streams
composedActions, err := tnClient.LoadComposedActions()
if err != nil {
log.Fatalf("Failed to load primitive actions: %v", err)
}

// 5. Add prefix for specific data provider action
prefix := "truflation_"
records, err := composedActions.GetIndex(ctx, types.GetIndexInput{
DataProvider: dataProvider,
StreamId: streamId,
From: &fromTime,
To: &toTime,
Prefix: &prefix,
})
if err != nil {
log.Fatalf("Failed to retrieve indexes: %v", err)
}

// 5. Display retrieved records
fmt.Println("\nTruflation AI Index:")
fmt.Println("----------------------------")
for _, record := range records {
fmt.Printf("Event Time: %d, Value: %s\n",
record.EventTime,
record.Value.String(),
)
}
}