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

[v0.11 feature] Built-in Reward Share #1581

Merged
merged 11 commits into from
Dec 26, 2023
86 changes: 86 additions & 0 deletions app/cmd/cli/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func init() {
rootCmd.AddCommand(nodesCmd)
nodesCmd.AddCommand(nodeUnstakeCmd)
nodesCmd.AddCommand(nodeUnjailCmd)
nodesCmd.AddCommand(stakeNewCmd)
}

var nodesCmd = &cobra.Command{
Expand Down Expand Up @@ -116,3 +117,88 @@ Will prompt the user for the <fromAddr> account passphrase.`,
fmt.Println(resp)
},
}

// stakeNewCmd is an upgraded version of `nodesCmd` that captures newer
// on-chain functionality in a cleaner way
var stakeNewCmd = &cobra.Command{
msmania marked this conversation as resolved.
Show resolved Hide resolved
Use: "stakeNew <OperatorPublicKey> <OutputAddress> <SignerAddress> <Stake> <ChainIDs> <ServiceURL> <RewardDelegators> <NetworkID> <Fee> [Memo]",
Short: "Stake a node in the network",
Long: `Stake a node in the network, promoting it to a servicer or a validator.

The command takes the following parameters.

OperatorPublicKey Public key to use as the node's operator account
OutputAddress Address to use as the node's output account
SignerAddress Address to sign the transaction
Stake Amount to stake in uPOKT
ChainIDs Comma-separated chain IDs to host on the node
ServiceURL Relay endpoint of the node. Must include the port number.
RewardDelegators Addresses to share rewards
NetworkID Network ID to submit a transaction to e.g. mainnet or testnet
Fee Transaction fee in uPOKT
Memo Optional. Text to include in the transaction. No functional effect.

Example:
$ pocket nodes stakeNew \
e237efc54a93ed61689959e9afa0d4bd49fa11c0b946c35e6bebaccb052ce3fc \
fe818527cd743866c1db6bdeb18731d04891df78 \
1164b9c95638fc201f35eca2af4c35fe0a81b6cf \
8000000000000 \
DEAD,BEEF \
https://x.com:443 \
'{"1000000000000000000000000000000000000000":1,"2000000000000000000000000000000000000000":2}' \
mainnet \
10000 \
"new stake with delegators!"
`,
Args: cobra.MinimumNArgs(9),
Run: func(cmd *cobra.Command, args []string) {
app.InitConfig(datadir, tmNode, persistentPeers, seeds, remoteCLIURL)

operatorPubKey := args[0]
outputAddr := args[1]
signerAddr := args[2]
stakeAmount := args[3]
chains := args[4]
serviceUrl := args[5]
delegators := args[6]
networkId := args[7]
fee := args[8]
memo := ""
if len(args) >= 10 {
memo = args[9]
}

fmt.Println("Enter Passphrase:")
passphrase := app.Credentials(pwd)

rawStakeTx, err := BuildStakeTx(
operatorPubKey,
outputAddr,
stakeAmount,
chains,
serviceUrl,
delegators,
networkId,
fee,
memo,
signerAddr,
passphrase,
)
if err != nil {
fmt.Println(err)
return
}
txBytes, err := json.Marshal(rawStakeTx)
if err != nil {
fmt.Println("Fail to build a transaction:", err)
return
}
resp, err := QueryRPC(SendRawTxPath, txBytes)
if err != nil {
fmt.Println("Fail to submit a transaction:", err)
return
}
fmt.Println(resp)
},
}
84 changes: 84 additions & 0 deletions app/cmd/cli/txUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"

"github.com/pokt-network/pocket-core/app"
"github.com/pokt-network/pocket-core/app/cmd/rpc"
Expand Down Expand Up @@ -251,6 +253,88 @@ func StakeNode(chains []string, serviceURL, operatorPubKey, output, passphrase,
}, nil
}

func BuildStakeTx(
operatorPubKeyStr,
outputAddrStr,
stakeAmountStr,
chains,
serviceUrl,
delegatorsStr,
networkId,
feeStr,
memo,
signerAddrStr,
passphrase string,
) (*rpc.SendRawTxParams, error) {
keybase, err := app.GetKeybase()
if err != nil {
return nil, err
}

signerAddr, err := sdk.AddressFromHex(signerAddrStr)
if err != nil {
return nil, err
}

operatorPubkey, err := crypto.NewPublicKey(operatorPubKeyStr)
if err != nil {
return nil, err
}

outputAddr, err := sdk.AddressFromHex(outputAddrStr)
if err != nil {
return nil, err
}

stakeAmount, ok := sdk.NewIntFromString(stakeAmountStr)
if !ok {
return nil, errors.New("Invalid stake amount: " + stakeAmountStr)
}

fee, err := strconv.ParseInt(feeStr, 10, 64)
if err != nil {
return nil, err
}

msg := &nodeTypes.MsgStake{
PublicKey: operatorPubkey,
Chains: strings.Split(chains, ","),
Value: stakeAmount,
ServiceUrl: serviceUrl,
Output: outputAddr,
}

if len(delegatorsStr) > 0 {
if json.Unmarshal([]byte(delegatorsStr), &msg.RewardDelegators); err != nil {
return nil, err
}
}

if err = msg.ValidateBasic(); err != nil {
return nil, err
}

txBz, err := newTxBz(
app.Codec(),
msg,
signerAddr,
networkId,
keybase,
passphrase,
fee,
memo,
false,
)
if err != nil {
return nil, err
}

return &rpc.SendRawTxParams{
Addr: signerAddrStr,
RawHexBytes: hex.EncodeToString(txBz),
}, nil
}

// UnstakeNode - start unstaking message to node
func UnstakeNode(operatorAddr, fromAddr, passphrase, chainID string, fees int64, isBefore8 bool) (*rpc.SendRawTxParams, error) {
fa, err := sdk.AddressFromHex(fromAddr)
Expand Down
Loading