@@ -2,8 +2,9 @@ package dataverse
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
5
6
"fmt"
6
-
7
+ cgschema "github.com/axone-protocol/axone-contract-schema/go/cognitarium-schema/v5"
7
8
dvschema "github.com/axone-protocol/axone-contract-schema/go/dataverse-schema/v5"
8
9
"google.golang.org/grpc"
9
10
)
@@ -14,18 +15,20 @@ type Client interface {
14
15
}
15
16
16
17
type client struct {
17
- dataverseClient dvschema.QueryClient
18
- cognitariumAddr string
18
+ dataverseClient dvschema.QueryClient
19
+ cognitariumClient cgschema.QueryClient
20
+ cognitariumAddr string
19
21
}
20
22
21
- func NewDataverseClient (ctx context.Context , dataverseClient dvschema.QueryClient ) (Client , error ) {
23
+ func NewDataverseClient (ctx context.Context , dataverseClient dvschema.QueryClient , cognitariumClient cgschema. QueryClient ) (Client , error ) {
22
24
cognitariumAddr , err := getCognitariumAddr (ctx , dataverseClient )
23
25
if err != nil {
24
26
return nil , fmt .Errorf ("failed to get cognitarium address: %w" , err )
25
27
}
26
28
27
29
return & client {
28
30
dataverseClient ,
31
+ cognitariumClient ,
29
32
cognitariumAddr ,
30
33
}, nil
31
34
}
@@ -39,11 +42,45 @@ func NewClient(ctx context.Context,
39
42
return nil , fmt .Errorf ("failed to create dataverse client: %w" , err )
40
43
}
41
44
42
- return NewDataverseClient (ctx , dataverseClient )
45
+ cognitariumAddr , err := getCognitariumAddr (ctx , dataverseClient )
46
+ if err != nil {
47
+ return nil , fmt .Errorf ("failed to get cognitarium address: %w" , err )
48
+ }
49
+
50
+ cognitariumClient , err := cgschema .NewQueryClient (grpcAddr , cognitariumAddr , opts ... )
51
+ if err != nil {
52
+ return nil , fmt .Errorf ("failed to create cognitarium client: %w" , err )
53
+ }
54
+
55
+ return & client {
56
+ dataverseClient ,
57
+ cognitariumClient ,
58
+ cognitariumAddr ,
59
+ }, nil
43
60
}
44
61
45
- func (c * client ) GetResourceGovAddr (_ context.Context , _ string ) (string , error ) {
46
- panic ("not implemented" )
62
+ func (c * client ) GetResourceGovAddr (_ context.Context , resourceDID string ) (string , error ) {
63
+ query := buildGetResourceGovAddrRequest (resourceDID )
64
+ queryB , _ := json .Marshal (query )
65
+ fmt .Printf ("query : %s" , queryB )
66
+ response , err := c .cognitariumClient .Select (context .Background (), & cgschema.QueryMsg_Select {Query : query })
67
+ if err != nil {
68
+ return "" , err
69
+ }
70
+
71
+ if len (response .Results .Bindings ) != 1 {
72
+ return "" , fmt .Errorf ("could not find governance code" )
73
+ }
74
+
75
+ codeBinding , ok := response .Results .Bindings [0 ]["code" ]
76
+ if ! ok {
77
+ return "" , fmt .Errorf ("could not find governance code" )
78
+ }
79
+ code , ok := codeBinding .ValueType .(cgschema.URI )
80
+ if ! ok {
81
+ return "" , fmt .Errorf ("could not decode governance code" )
82
+ }
83
+ return string (* code .Value .Full ), nil
47
84
}
48
85
49
86
func (c * client ) ExecGov (_ context.Context , _ string , _ string ) (interface {}, error ) {
0 commit comments