Skip to content

Commit

Permalink
refactor(engine): refactor bot engine interface (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jan 27, 2024
1 parent 01bb8e0 commit 4cbbebe
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 157 deletions.
49 changes: 5 additions & 44 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import (
"os"
"strings"

"github.com/kehiy/RoboPac/client"
"github.com/kehiy/RoboPac/config"
"github.com/kehiy/RoboPac/engine"
"github.com/kehiy/RoboPac/engine/commands"
"github.com/kehiy/RoboPac/log"
"github.com/kehiy/RoboPac/store"
"github.com/kehiy/RoboPac/wallet"
cobra "github.com/spf13/cobra"
)

Expand All @@ -24,55 +20,21 @@ func REPLCommand(parentCmd *cobra.Command) {
}
parentCmd.AddCommand(connect)

localnetOpt := connect.Flags().StringP("localnet", "l", "localhost:8080", "your local-net node address")
envOpt := connect.Flags().StringP("env", "e", ".env.local", "your local/test env file for config")

connect.Run = func(cmd *cobra.Command, args []string) {
log.Info("initializing repl...")

config, err := config.Load(*envOpt)
if err != nil {
log.Panic("can't load config env", "err", err, "path", *envOpt)
}

cm := client.NewClientMgr()
c, err := client.NewClient(*localnetOpt)
if err != nil {
log.Panic("can't make a new local-net client", "err", err, "addr", *localnetOpt)
}

cm.AddClient("local-net", c)

// initializing logger global instance.
log.InitGlobalLogger()

// new subLogger for engine.
eSl := log.NewSubLogger("engine")

// new subLogger for store.
sSl := log.NewSubLogger("store")

// new subLogger for store.
wSl := log.NewSubLogger("wallet")

// load or create wallet.
wallet := wallet.Open(config, wSl)
if wallet == nil {
log.Panic("wallet could not be opened, wallet is nil", "path", config.WalletPath)
}

log.Info("wallet opened successfully", "address", wallet.Address())
log.Info("initializing repl...")

// load store.
store, err := store.LoadStore(config, sSl)
config, err := config.Load(*envOpt)
if err != nil {
log.Panic("could not load store", "err", err, "path", config.StorePath)
log.Panic("can't load config env", "err", err, "path", *envOpt)
}

log.Info("store loaded successfully", "path", config.StorePath)

// starting botEngine.
botEngine, err := engine.NewBotEngine(eSl, cm, wallet, store)
botEngine, err := engine.NewBotEngine(config)
if err != nil {
log.Panic("could not start discord bot", "err", err)
}
Expand All @@ -93,8 +55,7 @@ func REPLCommand(parentCmd *cobra.Command) {
return
}

q := commands.ParseQuery(input)
response, err := commands.Execute(q, botEngine)
response, err := botEngine.Run(input)
if err != nil {
cmd.PrintErr(err)
}
Expand Down
53 changes: 6 additions & 47 deletions cmd/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import (
"os/signal"
"syscall"

"github.com/kehiy/RoboPac/client"
"github.com/kehiy/RoboPac/config"
"github.com/kehiy/RoboPac/discord"
"github.com/kehiy/RoboPac/engine"
"github.com/kehiy/RoboPac/log"
"github.com/kehiy/RoboPac/store"
"github.com/kehiy/RoboPac/wallet"
"github.com/spf13/cobra"
)

Expand All @@ -23,58 +20,21 @@ func RunCommand(parentCmd *cobra.Command) {
parentCmd.AddCommand(run)

run.Run = func(_ *cobra.Command, _ []string) {
// load configuration.
config, err := config.Load()
if err != nil {
log.Panic("error loading configuration", "err", err)
}

// starting client manager for RPC.
cm := client.NewClientMgr()

for _, rn := range config.RPCNodes {
c, err := client.NewClient(rn)
if err != nil {
log.Error("can't make new client", "RPC Node address", rn)
continue
}
log.Info("connecting to RPC Node", "addr", rn)
cm.AddClient(rn, c)
}

// initializing logger global instance.
log.InitGlobalLogger()

// new subLogger for engine.
eSl := log.NewSubLogger("engine")

// new subLogger for store.
sSl := log.NewSubLogger("store")

// new subLogger for store.
wSl := log.NewSubLogger("wallet")

// load or create wallet.
wallet := wallet.Open(config, wSl)
if wallet == nil {
log.Panic("wallet could not be opened, wallet is nil", "path", config.WalletPath)
}

log.Info("wallet opened successfully", "address", wallet.Address())

// load store.
store, err := store.LoadStore(config, sSl)
// load configuration.
config, err := config.Load()
if err != nil {
log.Panic("could not load store", "err", err, "path", config.StorePath)
log.Panic("error loading configuration", "err", err)
}

log.Info("store loaded successfully", "path", config.StorePath)

// starting botEngine.
botEngine, err := engine.NewBotEngine(eSl, cm, wallet, store)
botEngine, err := engine.NewBotEngine(config)
if err != nil {
log.Panic("could not start bot engine", "err", err)
log.Panic("could not start discord bot", "err", err)
}

botEngine.Start()

discordBot, err := discord.NewDiscordBot(botEngine, config.DiscordBotCfg.DiscordToken,
Expand All @@ -90,7 +50,6 @@ func RunCommand(parentCmd *cobra.Command) {

// gracefully shutdown the bot.
discordBot.Stop()
cm.Stop()
botEngine.Stop()
}
}
8 changes: 5 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type Config struct {
WalletAddress string
WalletPath string
WalletPassword string
RPCNodes []string
NetworkNodes []string
LocalNode string
StorePath string
DiscordBotCfg DiscordBotConfig
}
Expand All @@ -34,7 +35,8 @@ func Load(filePaths ...string) (*Config, error) {
WalletAddress: os.Getenv("WALLET_ADDRESS"),
WalletPath: os.Getenv("WALLET_PATH"),
WalletPassword: os.Getenv("WALLET_PASSWORD"),
RPCNodes: strings.Split(os.Getenv("RPC_NODES"), ","),
LocalNode: os.Getenv("LOCAL_NODE"),
NetworkNodes: strings.Split(os.Getenv("NETWORK_NODES"), ","),
StorePath: os.Getenv("STORE_PATH"),
DiscordBotCfg: DiscordBotConfig{
DiscordToken: os.Getenv("DISCORD_TOKEN"),
Expand All @@ -61,7 +63,7 @@ func (cfg *Config) BasicCheck() error {
return fmt.Errorf("WALLET_PATH does not exist")
}

if len(cfg.RPCNodes) == 0 {
if len(cfg.NetworkNodes) == 0 {
return fmt.Errorf("RPCNODES is not set or incorrect")
}

Expand Down
4 changes: 2 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBasicCheck(t *testing.T) {
WalletAddress: "test_wallet_address",
WalletPath: tempWalletPath, // Use the temporary directory
WalletPassword: "test_password",
RPCNodes: []string{"http://127.0.0.1:8545"},
NetworkNodes: []string{"http://127.0.0.1:8545"},
StorePath: tempStorePath, // Use the temporary directory
DiscordBotCfg: DiscordBotConfig{
DiscordToken: "MTEabc123",
Expand All @@ -39,7 +39,7 @@ func TestBasicCheck(t *testing.T) {
WalletAddress: "test_wallet_address",
WalletPath: "/valid/path",
WalletPassword: "test_password",
RPCNodes: []string{},
NetworkNodes: []string{},
StorePath: "/valid/storepath",
DiscordBotCfg: DiscordBotConfig{
DiscordToken: "MTEabc123",
Expand Down
4 changes: 2 additions & 2 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

type DiscordBot struct {
Session *discordgo.Session
BotEngine engine.Engine
BotEngine engine.IEngine
GuildID string
}

func NewDiscordBot(botEngine engine.Engine, token, guildID string) (*DiscordBot, error) {
func NewDiscordBot(botEngine engine.IEngine, token, guildID string) (*DiscordBot, error) {
s, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
Expand Down
38 changes: 16 additions & 22 deletions engine/commands/execute.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package commands

import (
"errors"

"github.com/kehiy/RoboPac/engine"
)

func Execute(q Query, be engine.Engine) (interface{}, error) {
switch q.Cmd {
case "health":
return be.NetworkHealth(q.Tokens)
case "node-info":
return be.NodeInfo(q.Tokens)
case "claim":
return be.Claim(q.Tokens)
case "me":
return be.ClaimerInfo(q.Tokens)
case "network":
return be.NetworkStatus(q.Tokens)
default:
return nil, errors.New("invalid command")
}
}
// func Execute(q Query, be engine.IEngine) (interface{}, error) {
// switch q.Cmd {
// case "health":
// return be.NetworkHealth(q.Tokens)
// case "node-info":
// return be.NodeInfo(q.Tokens)
// case "claim":
// return be.Claim(q.Tokens)
// case "me":
// return be.ClaimerInfo(q.Tokens)
// case "network":
// return be.NetworkStatus(q.Tokens)
// default:
// return nil, errors.New("invalid command")
// }
// }
Loading

0 comments on commit 4cbbebe

Please sign in to comment.