Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gnokey): add querying realm balances #2470

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 36 additions & 1 deletion tm2/pkg/crypto/keys/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package client
import (
"context"
"flag"
"strings"

"github.com/gnolang/gno/gnovm/pkg/gnolang"
"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/commands"
Expand All @@ -17,6 +19,8 @@ type QueryCfg struct {
Path string
}

const balancesQuery = "bank/balances/"

func NewQueryCmd(rootCfg *BaseCfg, io commands.IO) *commands.Command {
cfg := &QueryCfg{
RootCfg: rootCfg,
Expand Down Expand Up @@ -78,21 +82,52 @@ func QueryHandler(cfg *QueryCfg) (*ctypes.ResultABCIQuery, error) {
return nil, errors.New("missing remote url")
}

path := cfg.Path

if path == "" {
return nil, errors.New("missing path")
}

if strings.Contains(path, balancesQuery) {
path = handleBalanceQuery(path)
if path == "" {
return nil, errors.New("could not derive address from pkgpath")
}
}

data := []byte(cfg.Data)
opts2 := client.ABCIQueryOptions{
// Height: height, XXX
// Prove: false, XXX
}

cli, err := client.NewHTTPClient(remote)
if err != nil {
return nil, errors.Wrap(err, "new http client")
}

qres, err := cli.ABCIQueryWithOptions(
cfg.Path, data, opts2)
path, data, opts2)
if err != nil {
return nil, errors.Wrap(err, "querying")
}

return qres, nil
}

func handleBalanceQuery(path string) string {
// If the query is bank/balances & it contains a path, derive the address from the path
if strings.Contains(path, "gno.land/") {
i := strings.Index(path, balancesQuery)
pkgPath := path[i+len(balancesQuery):]

pkgAddr := gnolang.DerivePkgAddr(pkgPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tm2 should not import gnovm/ or gno.land/.

if len(pkgAddr.Bytes()) == 0 {
return ""
}

path = balancesQuery + pkgAddr.String()
}

return path
}
20 changes: 20 additions & 0 deletions tm2/pkg/crypto/keys/client/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"testing"

"github.com/gnolang/gno/gnovm/pkg/gnolang"
)

func TestHandleBalanceQuery(t *testing.T) {
pkgPath := "gno.land/r/demo/wugnot"
queryPath := balancesQuery + pkgPath

addr := gnolang.DerivePkgAddr(pkgPath)

parsedPath := handleBalanceQuery(queryPath)

if parsedPath != balancesQuery+addr.String() {
t.Fatalf("expected %s, got %s", balancesQuery+addr.String(), parsedPath)
}
}
Loading