Skip to content

Commit

Permalink
Merge pull request #1 from kehiy/main
Browse files Browse the repository at this point in the history
feat: --total flag, help user to stake all of account balance
  • Loading branch information
Ja7ad authored Mar 2, 2024
2 parents 3da903c + f1e5474 commit e7ef766
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Pactus bond tools for multiple stake by config, you can stake many validators in
- `validators` is list of validators for stake **note: if your address is first time for validator need set public key in `pub`**

2. run tools with `./staker -config ./cfg.json -password foobar`
- `-config` config file path default is `./cfg.json`
- `-password` is optional for wallet password
- `-server` is for custom node rpc address
- `--config` config file path default is `./cfg.json`
- `--password` is optional for wallet password
- `--server` is for custom node rpc address
- `--total` is a flag that ignore amount it config and it will stake whole of account balance
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module staker
module staker

go 1.21.1
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"log"
"os"
"os/exec"
"regexp"
"os/signal"
"strconv"
"sync"
"syscall"
)

var blanceRegex = regexp.MustCompile(`balance: (\d+\.\d+)`)

type Cfg struct {
PactusWalletExecPath string `json:"pactus_wallet_exec_path"`
WalletPath string `json:"wallet_path"`
Expand All @@ -30,6 +33,8 @@ func main() {
cfgPath := flag.String("config", "./cfg.json", "config file path")
password := flag.String("password", "", "pactus wallet password")
rpc := flag.String("server", "", "custom node rpc")
total := flag.Bool("total", false, "determine that all balance of account will be staked")

flag.Parse()

b, err := os.ReadFile(*cfgPath)
Expand All @@ -45,6 +50,22 @@ func main() {

amount := strconv.FormatFloat(cfg.Amount, 'g', -1, 64)

if *total {
args := make([]string, 0)
args = append(args, "address", "balance", cfg.WalletAddress)
out, err := exec.Command(cfg.PactusWalletExecPath, args...).Output()
if err != nil {
log.Fatalf("err: %s, msg: %s", err.Error(), string(out))
}

match := blanceRegex.FindStringSubmatch(string(out))
if len(match) > 1 {
amount = match[1]
} else {
log.Fatalf("err: can't find the address balance, msg: %s", string(out))
}
}

var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -53,6 +74,7 @@ func main() {
os.Exit(0)
}()


for _, val := range cfg.Validators {
args := make([]string, 0)
args = append(args, "--path", cfg.WalletPath, "tx", "bond")
Expand Down

0 comments on commit e7ef766

Please sign in to comment.