github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/cmd/commands/cli/command_stake.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package cli
    19  
    20  import (
    21  	"fmt"
    22  	"math/big"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/mysteriumnetwork/node/cmd/commands/cli/clio"
    27  	"github.com/mysteriumnetwork/node/identity"
    28  )
    29  
    30  func (c *cliApp) stake(args []string) (err error) {
    31  	var usage = strings.Join([]string{
    32  		"Usage: stake <action> [args]",
    33  		"Available actions:",
    34  		"  " + usageIncreaseStake,
    35  		"  " + usageDecreaseStake,
    36  	}, "\n")
    37  
    38  	if len(args) == 0 {
    39  		clio.Info(usage)
    40  		return errWrongArgumentCount
    41  	}
    42  
    43  	action := args[0]
    44  	actionArgs := args[1:]
    45  
    46  	switch action {
    47  	case "increase":
    48  		return c.increaseStake(actionArgs)
    49  	case "decrease":
    50  		return c.decreaseStake(actionArgs)
    51  	default:
    52  		fmt.Println(usage)
    53  		return errUnknownSubCommand(args[0])
    54  	}
    55  }
    56  
    57  const usageIncreaseStake = "increase <identity>"
    58  const usageDecreaseStake = "decrease <identity> <amount>"
    59  
    60  func (c *cliApp) decreaseStake(args []string) (err error) {
    61  	if len(args) != 2 {
    62  		clio.Info("Usage: " + usageDecreaseStake)
    63  		return errWrongArgumentCount
    64  	}
    65  
    66  	res, ok := new(big.Int).SetString(args[1], 10)
    67  	if !ok {
    68  		return fmt.Errorf("could not parse amount: %v", args[1])
    69  	}
    70  
    71  	err = c.tequilapi.DecreaseStake(identity.FromAddress(args[0]), res)
    72  	if err != nil {
    73  		return fmt.Errorf("could not decrease stake: %w", err)
    74  	}
    75  	return nil
    76  }
    77  
    78  func (c *cliApp) increaseStake(args []string) (err error) {
    79  	if len(args) != 1 {
    80  		clio.Info("Usage: " + usageIncreaseStake)
    81  		return errWrongArgumentCount
    82  	}
    83  
    84  	hermesID, err := c.config.GetHermesID()
    85  	if err != nil {
    86  		return fmt.Errorf("could not get Hermes ID: %w", err)
    87  	}
    88  	clio.Info("Waiting for settlement to complete")
    89  	errChan := make(chan error)
    90  
    91  	go func() {
    92  		errChan <- c.tequilapi.SettleIntoStake(identity.FromAddress(args[0]), identity.FromAddress(hermesID), true)
    93  	}()
    94  
    95  	timeout := time.After(time.Minute * 2)
    96  	for {
    97  		select {
    98  		case <-timeout:
    99  			fmt.Println()
   100  			return errTimeout
   101  		case <-time.After(time.Millisecond * 500):
   102  			fmt.Print(".")
   103  		case err := <-errChan:
   104  			fmt.Println()
   105  			if err != nil {
   106  				return fmt.Errorf("settlement failed: %w", err)
   107  			}
   108  			clio.Info("settlement succeeded")
   109  			return nil
   110  		}
   111  	}
   112  }