From 69bbce07cf254cf17d071bdfc1fa1393bfcdd198 Mon Sep 17 00:00:00 2001 From: Jeff Thompson Date: Thu, 31 Aug 2023 07:42:35 +0200 Subject: [PATCH] chore: Make callCreateReply use Client.NewRequest directly. Remove execCall. Signed-off-by: Jeff Thompson --- framework/call.go | 56 --------------------------- framework/gnokey.go | 92 ++++++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 107 deletions(-) diff --git a/framework/call.go b/framework/call.go index f8675b34..85c2e890 100644 --- a/framework/call.go +++ b/framework/call.go @@ -6,38 +6,11 @@ import ( "github.com/gnolang/gno/tm2/pkg/amino" rpc_client "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" "github.com/gnolang/gno/tm2/pkg/crypto/keys" "github.com/gnolang/gno/tm2/pkg/errors" "github.com/gnolang/gno/tm2/pkg/std" ) -// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/common.go -type BaseOptions struct { - Home string - Remote string - Quiet bool - InsecurePasswordStdin bool - Config string -} - -// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/root.go -type baseCfg struct { - BaseOptions -} - -// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/maketx.go -type makeTxCfg struct { - rootCfg *baseCfg - - gasWanted int64 - gasFee string - memo string - - broadcast bool - chainID string -} - // From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/query.go type queryCfg struct { remote string @@ -76,35 +49,6 @@ type broadcastCfg struct { tx *std.Tx } -// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/call.go -type callCfg struct { - rootCfg *makeTxCfg - - send string - pkgPath string - funcName string - args commands.StringArr -} - -// From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/call.go -func execCall(cfg *callCfg, nameOrBech32 string, password string) error { - client := NewClient(cfg.rootCfg.rootCfg.Remote, cfg.rootCfg.chainID) - client.SetAccount(nameOrBech32, password) - if err := client.SetKeyBaseFromDir(cfg.rootCfg.rootCfg.Home); err != nil { - return err - } - r := client.NewRequest("call") - r.StringOption("pkgpath", cfg.pkgPath) - r.StringOption("func", cfg.funcName) - r.StringOption("gas-fee", cfg.rootCfg.gasFee) - r.Int64Option("gas-wanted", cfg.rootCfg.gasWanted) - for _, arg := range cfg.args { - r.Argument(arg) - } - - return r.Send() -} - // From https://github.com/gnolang/gno/blob/master/tm2/pkg/crypto/keys/client/query.go func queryHandler(cfg *queryCfg) (*ctypes.ResultABCIQuery, error) { remote := cfg.remote diff --git a/framework/gnokey.go b/framework/gnokey.go index 3774b247..9b941f09 100644 --- a/framework/gnokey.go +++ b/framework/gnokey.go @@ -3,8 +3,6 @@ package gnomobile import ( "encoding/json" "fmt" - - "github.com/gnolang/gno/tm2/pkg/crypto/keys" ) type PromiseBlock interface { @@ -13,37 +11,32 @@ type PromiseBlock interface { } type accountAndTxCfg struct { - TxCfg *makeTxCfg + Client *Client - KeyName string - Password string - Mnemonic string + GasWanted int64 + GasFee string + Mnemonic string } func CreateDefaultAccount(rootDir string) error { - cfg := getAccountAndTxCfg(rootDir) - - kb, err := keys.NewKeyBaseFromDir(cfg.TxCfg.rootCfg.Home) + cfg, err := getAccountAndTxCfg(rootDir) if err != nil { return err } - keyList, err := kb.List() + + keyCount, err := cfg.Client.GetKeyCount() if err != nil { return err } - if len(keyList) > 0 { + if keyCount > 0 { // Assume the account with cfg.KeyName is already created. return nil } - _, err = kb.CreateAccount(cfg.KeyName, cfg.Mnemonic, "", cfg.Password, uint32(0), uint32(0)) - if err != nil { - return err - } - return nil + return cfg.Client.CreateAccount(cfg.Mnemonic, "", 0, 0) } -func getAccountAndTxCfg(rootDir string) *accountAndTxCfg { +func getAccountAndTxCfg(rootDir string) (*accountAndTxCfg, error) { dataDir := rootDir + "/data" remote := "testnet.gno.berty.io:26657" chainID := "dev" @@ -51,48 +44,42 @@ func getAccountAndTxCfg(rootDir string) *accountAndTxCfg { password := "password" mnemonic := "enable until hover project know foam join table educate room better scrub clever powder virus army pitch ranch fix try cupboard scatter dune fee" - return &accountAndTxCfg{ - TxCfg: &makeTxCfg{ - rootCfg: &baseCfg{ - BaseOptions: BaseOptions{ - Home: dataDir, - Remote: remote, - }, - }, - gasWanted: 2000000, - gasFee: "1000000ugnot", - - broadcast: true, - chainID: chainID, - }, - KeyName: keyName, - Password: password, - Mnemonic: mnemonic, + client := NewClient(remote, chainID) + client.SetAccount(keyName, password) + if err := client.SetKeyBaseFromDir(dataDir); err != nil { + return nil, err } + + return &accountAndTxCfg{ + Client: client, + GasWanted: 2000000, + GasFee: "1000000ugnot", + Mnemonic: mnemonic, + }, nil } func callCreateThread(cfg *accountAndTxCfg, boardId string, title string, body string) error { - callCfg := &callCfg{ - rootCfg: cfg.TxCfg, - pkgPath: "gno.land/r/demo/boards", - funcName: "CreateThread", - args: []string{boardId, title, body}, - } - return execCall(callCfg, cfg.KeyName, cfg.Password) + return cfg.Client.NewRequest("call"). + StringOption("pkgpath", "gno.land/r/demo/boards"). + StringOption("func", "CreateThread"). + StringOption("gas-fee", cfg.GasFee). + Int64Option("gas-wanted", cfg.GasWanted). + Argument(boardId). + Argument(title). + Argument(body). + Send() } func callCreateReply(cfg *accountAndTxCfg, boardId string, threadId string, postId string, body string) error { - callCfg := &callCfg{ - rootCfg: cfg.TxCfg, - pkgPath: "gno.land/r/demo/boards", - funcName: "CreateReply", - args: []string{boardId, threadId, postId, body}, - } - return execCall(callCfg, cfg.KeyName, cfg.Password) + return cfg.Client.Call("gno.land/r/demo/boards", "CreateReply", []string{"boardId", "threadId", "postId", "body"}, cfg.GasFee, cfg.GasWanted, "") } func ExportJsonConfig(rootDir string) string { - config, err := json.Marshal(getAccountAndTxCfg(rootDir)) + cfg, err := getAccountAndTxCfg(rootDir) + if err != nil { + return fmt.Sprintf("Error: unable make config: %s", err.Error()) + } + config, err := json.Marshal(cfg) if err != nil { return fmt.Sprintf("Error: unable load config: %s", err.Error()) } @@ -100,9 +87,12 @@ func ExportJsonConfig(rootDir string) string { } func CreateReply(message string, rootDir string) string { - cfg := getAccountAndTxCfg(rootDir) + cfg, err := getAccountAndTxCfg(rootDir) + if err != nil { + return fmt.Sprintf("Error: unable to get config: %s", err.Error()) + } - err := callCreateReply(cfg, "2", "1", "1", message) + err = callCreateReply(cfg, "2", "1", "1", message) if err != nil { return fmt.Sprintf("Error: unable to exec call command: %s", err.Error())