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

test: fixing failing tests, adding more engine tests #29

Merged
merged 6 commits into from
Jan 28, 2024
Merged
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
74 changes: 15 additions & 59 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package engine

import (
"errors"
"fmt"
"strings"
"sync"
"time"

Expand All @@ -16,10 +14,6 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
)

const (
botCmdClaim = "claim"
)

type BotEngine struct {
Wallet wallet.IWallet
Store store.IStore
Expand Down Expand Up @@ -79,41 +73,7 @@ func newBotEngine(logger *log.SubLogger, cm *client.Mgr, w wallet.IWallet, s sto
}
}

// The input is always string.
//
// The input format is like: [Command] <Arguments ...>
//
// The output is always string, but format might be JSON. ???
func (be *BotEngine) Run(input string) (string, error) {
cmd, args := be.parseQuery(input)

switch cmd {
case botCmdClaim:
if len(args) != 3 {
return "", fmt.Errorf("expected to have 3 arguments, but it received %d", len(args))
}

_, err := be.Claim(args[0], args[1], args[2])
if err != nil {
return "", err
}
return "", nil

default:
return "", fmt.Errorf("unknown command: %s", cmd)
}
}

func (be *BotEngine) parseQuery(query string) (string, []string) {
subs := strings.Split(query, " ")
if len(subs) == 0 {
return "", nil
}

return subs[0], subs[1:]
}

func (be *BotEngine) NetworkHealth(_ []string) (*NetHealthResponse, error) {
func (be *BotEngine) NetworkHealth() (*NetHealthResponse, error) {
lastBlockTime, lastBlockHeight := be.Cm.GetLastBlockTime()
lastBlockTimeFormatted := time.Unix(int64(lastBlockTime), 0)
currentTime := time.Now()
Expand All @@ -134,7 +94,7 @@ func (be *BotEngine) NetworkHealth(_ []string) (*NetHealthResponse, error) {
}, nil
}

func (be *BotEngine) NetworkStatus(_ []string) (*NetStatus, error) {
func (be *BotEngine) NetworkStatus() (*NetStatus, error) {
netInfo, err := be.Cm.GetNetworkInfo()
if err != nil {
return nil, err
Expand All @@ -157,13 +117,7 @@ func (be *BotEngine) NetworkStatus(_ []string) (*NetStatus, error) {
}, nil
}

func (be *BotEngine) NodeInfo(tokens []string) (*NodeInfo, error) {
if len(tokens) != 1 {
return nil, errors.New("missing argument: validator address")
}

valAddress := tokens[0]

func (be *BotEngine) NodeInfo(valAddress string) (*NodeInfo, error) {
peerInfo, err := be.Cm.GetPeerInfo(valAddress)
if err != nil {
return nil, err
Expand Down Expand Up @@ -200,15 +154,11 @@ func (be *BotEngine) NodeInfo(tokens []string) (*NodeInfo, error) {
}, nil
}

func (be *BotEngine) ClaimerInfo(tokens []string) (*store.Claimer, error) {
func (be *BotEngine) ClaimerInfo(testNetValAddr string) (*store.Claimer, error) {
be.RLock()
defer be.RUnlock()

if len(tokens) != 1 {
return nil, errors.New("missing argument: Discord ID")
}

claimer := be.Store.ClaimerInfo(tokens[0])
claimer := be.Store.ClaimerInfo(testNetValAddr)
if claimer == nil {
return nil, errors.New("not found")
}
Expand All @@ -222,8 +172,10 @@ func (be *BotEngine) Claim(discordID string, testnetAddr string, mainnetAddr str

be.logger.Info("new claim request", "mainnetAddr", mainnetAddr, "testnetAddr", testnetAddr, "discordID", discordID)

// TODO:
// Check if has less balance of 500, return error
if utils.AtomicToCoin(be.Wallet.Balance()) <= 500 {
be.logger.Warn("bot wallet hasn't enough balance")
return "", errors.New("insufficient wallet balance")
}

claimer := be.Store.ClaimerInfo(testnetAddr)
if claimer == nil {
Expand All @@ -244,13 +196,16 @@ func (be *BotEngine) Claim(discordID string, testnetAddr string, mainnetAddr str
return "", err
}

txID, err := be.Wallet.BondTransaction(peerInfo.ConsensusKeys[0], mainnetAddr, "", claimer.TotalReward)
memo := "TestNet reward claim from RoboPac"
txID, err := be.Wallet.BondTransaction(peerInfo.ConsensusKeys[0], mainnetAddr, memo, claimer.TotalReward)
if err != nil {
return "", err
}

if txID == "" {
return "", errors.New("can't send bond transaction")
}

be.logger.Info("new bond transaction sent", "txID", txID)

err = be.Store.AddClaimTransaction(testnetAddr, txID)
Expand All @@ -259,7 +214,8 @@ func (be *BotEngine) Claim(discordID string, testnetAddr string, mainnetAddr str
"error", err,
"discordID", discordID,
"testnetAddr", testnetAddr,
"txID", txID)
"txID", txID,
)

return "", err
}
Expand Down
Loading
Loading