-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (54 loc) · 2.16 KB
/
main.go
File metadata and controls
66 lines (54 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/trufnetwork/kwil-db/core/crypto"
"github.com/trufnetwork/kwil-db/core/crypto/auth"
"github.com/trufnetwork/sdk-go/core/tnclient"
)
func main() {
ctx := context.Background()
// 1. Configure your signer (replace with your 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}
// 2. Connect to a TN gateway (local or mainnet)
endpoint := "https://gateway.mainnet.truf.network" // or http://localhost:8484
tnClient, err := tnclient.NewClient(ctx, endpoint, tnclient.WithSigner(signer))
if err != nil {
log.Fatalf("failed to create TN client: %v", err)
}
// 3. Load the generic Action API
actions, err := tnClient.LoadActions()
if err != nil {
log.Fatalf("failed to load Action API: %v", err)
}
// ---------------------------------------------------------
// Example: call a read-only stored procedure with arguments
// ---------------------------------------------------------
// This example calls the `get_divergence_index_change` procedure that
// expects the following positional arguments:
// 1. $from INT — starting unix timestamp (inclusive)
// 2. $to INT — ending unix timestamp (inclusive)
// 3. $frozen_at INT? — optional timestamp when the stream was frozen
// 4. $base_time INT? — optional base time for index normalisation
// 5. $time_interval INT — comparison interval in seconds
from := int(time.Now().AddDate(0, 0, -7).Unix()) // one week ago
to := int(time.Now().Unix()) // now
timeInterval := 31_536_000 // one year in seconds
// nil placeholders are used for optional parameters we want to skip
args := []any{from, to, nil, nil, timeInterval}
queryResult, err := actions.CallProcedure(ctx, "get_divergence_index_change", args)
if err != nil {
log.Fatalf("procedure call failed: %v", err)
}
// Print the returned rows in a simple CSV-like format
fmt.Printf("Columns: %v\n", queryResult.ColumnNames)
for _, row := range queryResult.Values {
fmt.Println(row)
}
}