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

[ioctl] Add stake2 transferownership command #4250

Merged
merged 6 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ioctl/cmd/action/stake2.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func init() {
Stake2Cmd.AddCommand(_stake2EndorseCmd)
Stake2Cmd.AddCommand(_stake2UnEndorseCmd)
Stake2Cmd.AddCommand(_stake2ActivateCmd)
Stake2Cmd.AddCommand(_stake2TransferOwnershipCmd)
Stake2Cmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint", config.ReadConfig.Endpoint, config.TranslateInLang(_stake2FlagEndpointUsages, config.UILanguage))
Stake2Cmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure, config.TranslateInLang(_stake2FlagInsecureUsages, config.UILanguage))
}
Expand Down
92 changes: 92 additions & 0 deletions ioctl/cmd/action/stake2transfer_ownership.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2024 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package action

import (
"encoding/hex"

"github.com/spf13/cobra"

"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)

// Multi-language support
var (
_stake2TransferOwnershipCmdUses = map[config.Language]string{
config.English: "transferownership (ALIAS|NEWOWNER_ADDRESS) [DATA]" +
" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "transferownership (别名|新所有人地址) [DATA]" +
" [-s 签署人] [-n NONCE] [-l GAS限制] [-p GAS价格] [-P 密码] [-y]",
}
_stake2TransferOwnershipCmdShorts = map[config.Language]string{
config.English: "Transfer Candidate Ownership on IoTeX blockchain",
config.Chinese: "在IoTeX区块链上转让节点所有权",
}
)

var _stake2TransferOwnershipCmd = &cobra.Command{
Use: config.TranslateInLang(_stake2TransferOwnershipCmdUses, config.UILanguage),
Short: config.TranslateInLang(_stake2TransferOwnershipCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := stake2TransferOwnership(args)
return output.PrintError(err)
},
}

func init() {
RegisterWriteCommand(_stake2TransferOwnershipCmd)
}

func stake2TransferOwnership(args []string) error {
ownerAddrStr, err := util.Address(args[0])
if err != nil {
return output.NewError(output.AddressError, "failed to get operator address", err)
}

var payload []byte
if len(args) == 2 {
payload, err = hex.DecodeString(args[1])
if err != nil {
return output.NewError(output.ConvertError, "failed to decode data", err)
}
}

sender, err := Signer()
if err != nil {
return output.NewError(output.AddressError, "failed to get signed address", err)
}

gasLimit := _gasLimitFlag.Value().(uint64)
if gasLimit == 0 {
gasLimit = action.CandidateTransferOwnershipBaseIntrinsicGas
}

gasPriceRau, err := gasPriceInRau()
if err != nil {
return output.NewError(0, "failed to get gas price", err)
}
nonce, err := nonce(sender)
if err != nil {
return output.NewError(0, "failed to get nonce ", err)
}

cto, err := action.NewCandidateTransferOwnership(nonce, gasLimit, gasPriceRau, ownerAddrStr, payload)
if err != nil {
return output.NewError(output.InstantiationError, "failed to make a candidateTransferOwnership instance", err)
}
return SendAction(
(&action.EnvelopeBuilder{}).
SetNonce(nonce).
SetGasPrice(gasPriceRau).
SetGasLimit(gasLimit).
SetAction(cto).Build(),
sender)
}
Loading