-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.go
64 lines (56 loc) · 1.71 KB
/
watch.go
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
package sdk
import (
"context"
"encoding/hex"
"connectrpc.com/connect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/watch"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/watch/watchconnect"
)
type WatchServiceClient watchconnect.WatchServiceClient
func NewWatchServiceClient(u *UtxorpcClient) WatchServiceClient {
return u.NewWatchServiceClient()
}
func (u *UtxorpcClient) NewWatchServiceClient() WatchServiceClient {
return watchconnect.NewWatchServiceClient(
u.httpClient,
u.baseUrl,
connect.WithGRPC(),
)
}
func watchIntersect(blockHashStr string, blockIndex int64) []*watch.BlockRef {
var intersect []*watch.BlockRef
// Construct the BlockRef based on the provided parameters
blockRef := &watch.BlockRef{}
if blockHashStr != "" {
hash, err := hex.DecodeString(blockHashStr)
if err != nil {
return nil
}
blockRef.Hash = hash
}
// We assume blockIndex can be 0 or any positive number
if blockIndex > -1 {
blockRef.Index = uint64(blockIndex)
}
// Only add blockRef to intersect if at least one of blockHashStr or blockIndex is provided
if blockHashStr != "" || blockIndex > -1 {
intersect = []*watch.BlockRef{blockRef}
}
return intersect
}
func (u *UtxorpcClient) WatchTx(
blockHashStr string,
blockIndex int64,
) (*connect.ServerStreamForClient[watch.WatchTxResponse], error) {
ctx := context.Background()
req := &watch.WatchTxRequest{Intersect: watchIntersect(blockHashStr, blockIndex)}
return u.WatchTxWithContext(ctx, req)
}
func (u *UtxorpcClient) WatchTxWithContext(
ctx context.Context,
watchReq *watch.WatchTxRequest,
) (*connect.ServerStreamForClient[watch.WatchTxResponse], error) {
req := connect.NewRequest(watchReq)
u.AddHeadersToRequest(req)
return u.Watch.WatchTx(ctx, req)
}