Skip to content

Commit e7ef766

Browse files
authored
Merge pull request #1 from kehiy/main
feat: --total flag, help user to stake all of account balance
2 parents 3da903c + f1e5474 commit e7ef766

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Pactus bond tools for multiple stake by config, you can stake many validators in
2929
- `validators` is list of validators for stake **note: if your address is first time for validator need set public key in `pub`**
3030

3131
2. run tools with `./staker -config ./cfg.json -password foobar`
32-
- `-config` config file path default is `./cfg.json`
33-
- `-password` is optional for wallet password
34-
- `-server` is for custom node rpc address
32+
- `--config` config file path default is `./cfg.json`
33+
- `--password` is optional for wallet password
34+
- `--server` is for custom node rpc address
35+
- `--total` is a flag that ignore amount it config and it will stake whole of account balance

go.mod

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module staker
1+
module staker
2+
3+
go 1.21.1

main.go

+22
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import (
77
"log"
88
"os"
99
"os/exec"
10+
"regexp"
1011
"os/signal"
1112
"strconv"
1213
"sync"
1314
"syscall"
1415
)
1516

17+
var blanceRegex = regexp.MustCompile(`balance: (\d+\.\d+)`)
18+
1619
type Cfg struct {
1720
PactusWalletExecPath string `json:"pactus_wallet_exec_path"`
1821
WalletPath string `json:"wallet_path"`
@@ -30,6 +33,8 @@ func main() {
3033
cfgPath := flag.String("config", "./cfg.json", "config file path")
3134
password := flag.String("password", "", "pactus wallet password")
3235
rpc := flag.String("server", "", "custom node rpc")
36+
total := flag.Bool("total", false, "determine that all balance of account will be staked")
37+
3338
flag.Parse()
3439

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

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

53+
if *total {
54+
args := make([]string, 0)
55+
args = append(args, "address", "balance", cfg.WalletAddress)
56+
out, err := exec.Command(cfg.PactusWalletExecPath, args...).Output()
57+
if err != nil {
58+
log.Fatalf("err: %s, msg: %s", err.Error(), string(out))
59+
}
60+
61+
match := blanceRegex.FindStringSubmatch(string(out))
62+
if len(match) > 1 {
63+
amount = match[1]
64+
} else {
65+
log.Fatalf("err: can't find the address balance, msg: %s", string(out))
66+
}
67+
}
68+
4869
var wg sync.WaitGroup
4970
ctx, cancel := context.WithCancel(context.Background())
5071

@@ -53,6 +74,7 @@ func main() {
5374
os.Exit(0)
5475
}()
5576

77+
5678
for _, val := range cfg.Validators {
5779
args := make([]string, 0)
5880
args = append(args, "--path", cfg.WalletPath, "tx", "bond")

0 commit comments

Comments
 (0)