-
Notifications
You must be signed in to change notification settings - Fork 9
/
client_queries.go
118 lines (95 loc) · 3.63 KB
/
client_queries.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package gnoclient
import (
"fmt"
"github.com/gnolang/gno/tm2/pkg/amino"
rpcclient "github.com/gnolang/gno/tm2/pkg/bft/rpc/client"
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
)
// QueryCfg contains configuration options for performing queries.
type QueryCfg struct {
Path string // Query path
Data []byte // Query data
rpcclient.ABCIQueryOptions // ABCI query options
}
// Query performs a generic query on the blockchain.
func (c Client) Query(cfg QueryCfg) (*ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return nil, err
}
qres, err := c.RPCClient.ABCIQueryWithOptions(cfg.Path, cfg.Data, cfg.ABCIQueryOptions)
if err != nil {
return nil, errors.Wrap(err, "query error")
}
if qres.Response.Error != nil {
return qres, errors.Wrap(qres.Response.Error, "deliver transaction failed: log:%s", qres.Response.Log)
}
return qres, nil
}
// QueryAccount retrieves account information for a given address.
func (c Client) QueryAccount(addr crypto.Address) (*std.BaseAccount, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return nil, nil, err
}
path := fmt.Sprintf("auth/accounts/%s", crypto.AddressToBech32(addr))
data := []byte{}
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return nil, nil, errors.Wrap(err, "query account")
}
if qres.Response.Data == nil || len(qres.Response.Data) == 0 || string(qres.Response.Data) == "null" {
return nil, nil, std.ErrUnknownAddress("unknown address: " + crypto.AddressToBech32(addr))
}
var qret struct{ BaseAccount std.BaseAccount }
err = amino.UnmarshalJSON(qres.Response.Data, &qret)
if err != nil {
return nil, nil, err
}
return &qret.BaseAccount, qres, nil
}
func (c Client) QueryAppVersion() (string, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return "", nil, err
}
path := ".app/version"
data := []byte{}
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return "", nil, errors.Wrap(err, "query app version")
}
version := string(qres.Response.Value)
return version, qres, nil
}
// Render calls the Render function for pkgPath with optional args. The pkgPath should
// include the prefix like "gno.land/". This is similar to using a browser URL
// <testnet>/<pkgPath>:<args> where <pkgPath> doesn't have the prefix like "gno.land/".
func (c Client) Render(pkgPath string, args string) (string, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return "", nil, err
}
path := "vm/qrender"
data := []byte(fmt.Sprintf("%s\n%s", pkgPath, args))
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return "", nil, errors.Wrap(err, "query render")
}
return string(qres.Response.Data), qres, nil
}
// QEval evaluates the given expression with the realm code at pkgPath. The pkgPath should
// include the prefix like "gno.land/". The expression is usually a function call like
// "GetBoardIDFromName(\"testboard\")". The return value is a typed expression like
// "(1 gno.land/r/demo/boards.BoardID)\n(true bool)".
func (c Client) QEval(pkgPath string, expression string) (string, *ctypes.ResultABCIQuery, error) {
if err := c.validateRPCClient(); err != nil {
return "", nil, err
}
path := "vm/qeval"
data := []byte(fmt.Sprintf("%s\n%s", pkgPath, expression))
qres, err := c.RPCClient.ABCIQuery(path, data)
if err != nil {
return "", nil, errors.Wrap(err, "query qeval")
}
return string(qres.Response.Data), qres, nil
}