Skip to content

Commit

Permalink
Added custom percentage inputs to stake-rpl, api cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaspanf committed Nov 3, 2024
1 parent 40e092a commit 483ccbd
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 59 deletions.
25 changes: 17 additions & 8 deletions rocketpool-cli/node/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package node

import (
"fmt"
"strconv"
"strings"

"github.com/urfave/cli"

Expand Down Expand Up @@ -320,7 +322,7 @@ func RegisterCommands(app *cli.App, name string, aliases []string) {
Flags: []cli.Flag{
cli.StringFlag{
Name: "amount, a",
Usage: "The amount of RPL to stake (also accepts '5%', '10%', or '15%' for 8-ETH minipools, or 'all' for all of your RPL)",
Usage: "The amount of RPL to stake (also accepts custom percentages for 8-ETH minipools (eg. 3% of borrowed ETH as RPL), or 'all' for all of your RPL)",
},
cli.BoolFlag{
Name: "yes, y",
Expand All @@ -339,13 +341,20 @@ func RegisterCommands(app *cli.App, name string, aliases []string) {
}

// Validate flags
if c.String("amount") != "" &&
c.String("amount") != "5%" &&
c.String("amount") != "10%" &&
c.String("amount") != "15%" &&
c.String("amount") != "all" {
if _, err := cliutils.ValidatePositiveEthAmount("stake amount", c.String("amount")); err != nil {
return err
amount := c.String("amount")
if amount != "" {
if strings.HasSuffix(amount, "%") {
trimmedAmount := strings.TrimSuffix(amount, "%")
stakePercent, err := strconv.ParseFloat(trimmedAmount, 64)
if err != nil || stakePercent <= 0 {
return fmt.Errorf("invalid percentage value: %s", amount)
}

} else if amount != "all" {
// Validate it as a positive ETH amount if it's not a percentage or "all"
if _, err := cliutils.ValidatePositiveEthAmount("stake amount", amount); err != nil {
return err
}
}
}

Expand Down
74 changes: 53 additions & 21 deletions rocketpool-cli/node/stake-rpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/big"
"strconv"
"strings"

"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/urfave/cli"
Expand Down Expand Up @@ -161,35 +162,26 @@ func nodeStakeRpl(c *cli.Context) error {

}

// Get min/max per minipool RPL stake amounts
// Get RPL price
rplPrice, err := rp.RplPrice()
if err != nil {
return err
}

// Get stake amount
var amountWei *big.Int
if c.String("amount") == "5%" {
// Set RPL stake amount to 5% of borrowed ETH for one minipool
amountWei = rplPrice.FivePercentBorrowedRplStake

} else if c.String("amount") == "10%" {

// Set RPL stake amount to 10% of borrowed ETH for one minipool
amountWei = rplPrice.TenPercentBorrowedRplStake

} else if c.String("amount") == "15%" {

// Set RPL stake amount to 15% of borrowed ETH for one minipool
amountWei = rplPrice.FifteenPercentBorrowedRplStake
// Amount flag custom percentage input
if strings.HasSuffix(c.String("amount"), "%") {
amountWei, err = parsePercentageInput(c.String("amount"), rplPrice.RplPrice)
if err != nil {
return fmt.Errorf("Invalid stake amount '%s': %w", c.String("amount"), err)
}

} else if c.String("amount") == "all" {

// Set amount to node's entire RPL balance
amountWei = &rplBalance

} else if c.String("amount") != "" {

// Parse amount
stakeAmount, err := strconv.ParseFloat(c.String("amount"), 64)
if err != nil {
Expand All @@ -199,9 +191,11 @@ func nodeStakeRpl(c *cli.Context) error {

} else {
// Get the RPL stake amounts for 5,10,15% borrowed ETH per LEB8
fivePercentBorrowedRplStake := rplPrice.FivePercentBorrowedRplStake
tenPercentBorrowedRplStake := rplPrice.TenPercentBorrowedRplStake
fifteenPercentBorrowedRplStake := rplPrice.FifteenPercentBorrowedRplStake
fivePercentBorrowedPerMinipool := new(big.Int)
fivePercentBorrowedPerMinipool.SetString("50000000000000000", 10)
fivePercentBorrowedRplStake := borrowedRplStake(fivePercentBorrowedPerMinipool, rplPrice.RplPrice)
tenPercentBorrowedRplStake := new(big.Int).Mul(fivePercentBorrowedRplStake, big.NewInt(2))
fifteenPercentBorrowedRplStake := new(big.Int).Mul(fivePercentBorrowedRplStake, big.NewInt(3))

// Prompt for amount option
amountOptions := []string{
Expand All @@ -210,8 +204,12 @@ func nodeStakeRpl(c *cli.Context) error {
fmt.Sprintf("15%% of borrowed ETH (%.6f RPL) for one minipool?", math.RoundUp(eth.WeiToEth(fifteenPercentBorrowedRplStake), 6)),
fmt.Sprintf("Your entire RPL balance (%.6f RPL)?", math.RoundDown(eth.WeiToEth(&rplBalance), 6)),
"A custom amount",
"A custom percentage",
}
selected, _ := cliutils.Select("Please choose an amount of RPL to stake:", amountOptions)

var customAmount bool
var customPercentage bool
switch selected {
case 0:
amountWei = fivePercentBorrowedRplStake
Expand All @@ -221,18 +219,29 @@ func nodeStakeRpl(c *cli.Context) error {
amountWei = fifteenPercentBorrowedRplStake
case 3:
amountWei = &rplBalance
case 4:
customAmount = true
case 5:
customPercentage = true
}

// Prompt for custom amount
if amountWei == nil {
if customAmount == true {
inputAmount := cliutils.Prompt("Please enter an amount of RPL to stake:", "^\\d+(\\.\\d+)?$", "Invalid amount")
stakeAmount, err := strconv.ParseFloat(inputAmount, 64)
if err != nil {
return fmt.Errorf("Invalid stake amount '%s': %w", inputAmount, err)
}
amountWei = eth.EthToWei(stakeAmount)
}

// Prompt for custom percentage
if customPercentage == true {
inputPercentage := cliutils.Prompt("Please specify a percentage of borrowed ETH as RPL to stake: (e.g 3%, 3.5%, 15%, etc)", "^(0|[1-9][0-9]*)(\\.[0-9]+)?%$", "Invalid amount")
amountWei, err = parsePercentageInput(inputPercentage, rplPrice.RplPrice)
if err != nil {
return fmt.Errorf("Invalid stake amount '%s': %w", inputPercentage, err)
}
}
}

// Check allowance
Expand Down Expand Up @@ -337,3 +346,26 @@ func nodeStakeRpl(c *cli.Context) error {
return nil

}

func borrowedRplStake(borrowedPerMinipool *big.Int, rplPrice *big.Int) *big.Int {
percentBorrowedRplStake := big.NewInt(0)
percentBorrowedRplStake.Mul(eth.EthToWei(24), borrowedPerMinipool)
percentBorrowedRplStake.Div(percentBorrowedRplStake, rplPrice)
percentBorrowedRplStake.Add(percentBorrowedRplStake, big.NewInt(1))
amountWei := percentBorrowedRplStake

return amountWei

}

func parsePercentageInput(customInput string, rplPrice *big.Int) (*big.Int, error) {
trimmedInput := strings.TrimSuffix(customInput, "%")
stakePercent, err := strconv.ParseFloat(trimmedInput, 64)
if err != nil {
return nil, err
}
amountWei := borrowedRplStake(eth.EthToWei(stakePercent/100), rplPrice)

return amountWei, err

}
15 changes: 1 addition & 14 deletions rocketpool/api/network/rpl-price.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math/big"

"github.com/rocket-pool/rocketpool-go/network"
"github.com/rocket-pool/rocketpool-go/utils/eth"
"github.com/urfave/cli"
"golang.org/x/sync/errgroup"

Expand All @@ -29,7 +28,7 @@ func getRplPrice(c *cli.Context) (*api.RplPriceResponse, error) {
// Data
var wg errgroup.Group
var rplPrice *big.Int
_24Eth := eth.EthToWei(24)
// _24Eth := eth.EthToWei(24)

// Get RPL price set block
wg.Go(func() error {
Expand All @@ -52,18 +51,6 @@ func getRplPrice(c *cli.Context) (*api.RplPriceResponse, error) {
return nil, err
}

// RPL stake amounts for 5,10,15% borrowed ETH per LEB8
fivePercentBorrowedPerMinipool := new(big.Int)
fivePercentBorrowedPerMinipool.SetString("50000000000000000", 10)

fivePercentBorrowedRplStake := big.NewInt(0)
fivePercentBorrowedRplStake.Mul(_24Eth, fivePercentBorrowedPerMinipool)
fivePercentBorrowedRplStake.Div(fivePercentBorrowedRplStake, rplPrice)
fivePercentBorrowedRplStake.Add(fivePercentBorrowedRplStake, big.NewInt(1))
response.FivePercentBorrowedRplStake = fivePercentBorrowedRplStake
response.TenPercentBorrowedRplStake = new(big.Int).Mul(fivePercentBorrowedRplStake, big.NewInt(2))
response.FifteenPercentBorrowedRplStake = new(big.Int).Mul(fivePercentBorrowedRplStake, big.NewInt(3))

// Update & return response
response.RplPrice = rplPrice
return &response, nil
Expand Down
9 changes: 0 additions & 9 deletions shared/services/rocketpool/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ func (c *Client) RplPrice() (api.RplPriceResponse, error) {
if response.RplPrice == nil {
response.RplPrice = big.NewInt(0)
}
if response.FivePercentBorrowedRplStake == nil {
response.FivePercentBorrowedRplStake = big.NewInt(0)
}
if response.TenPercentBorrowedRplStake == nil {
response.TenPercentBorrowedRplStake = big.NewInt(0)
}
if response.FifteenPercentBorrowedRplStake == nil {
response.FifteenPercentBorrowedRplStake = big.NewInt(0)
}
return response, nil
}

Expand Down
11 changes: 4 additions & 7 deletions shared/types/api/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ type NodeFeeResponse struct {
}

type RplPriceResponse struct {
Status string `json:"status"`
Error string `json:"error"`
RplPrice *big.Int `json:"rplPrice"`
RplPriceBlock uint64 `json:"rplPriceBlock"`
FivePercentBorrowedRplStake *big.Int `json:"fivePercentBorrowedRplStake"`
TenPercentBorrowedRplStake *big.Int `json:"tenPercentRplStake"`
FifteenPercentBorrowedRplStake *big.Int `json:"fifteenPercentRplStake"`
Status string `json:"status"`
Error string `json:"error"`
RplPrice *big.Int `json:"rplPrice"`
RplPriceBlock uint64 `json:"rplPriceBlock"`
}

type NetworkStatsResponse struct {
Expand Down

0 comments on commit 483ccbd

Please sign in to comment.