Skip to content

Commit

Permalink
chore: Make callCreateReply use Client.NewRequest directly. Remove ex…
Browse files Browse the repository at this point in the history
…ecCall.

Signed-off-by: Jeff Thompson <[email protected]>
  • Loading branch information
jefft0 committed Aug 31, 2023
1 parent cb964e6 commit 69bbce0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 107 deletions.
56 changes: 0 additions & 56 deletions framework/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
92 changes: 41 additions & 51 deletions framework/gnokey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package gnomobile
import (
"encoding/json"
"fmt"

"github.com/gnolang/gno/tm2/pkg/crypto/keys"
)

type PromiseBlock interface {
Expand All @@ -13,96 +11,88 @@ 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"
keyName := "jefft0"
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())
}
return string(config)
}

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())
Expand Down

0 comments on commit 69bbce0

Please sign in to comment.