Skip to content

Commit

Permalink
ioctl new endorse action (#4331)
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc authored and dustinxie committed Jul 11, 2024
1 parent 2b0511a commit 0c77e21
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
3 changes: 2 additions & 1 deletion ioctl/cmd/action/stake2.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func init() {
Stake2Cmd.AddCommand(_stake2RegisterCmd)
Stake2Cmd.AddCommand(_stake2ChangeCmd)
Stake2Cmd.AddCommand(_stake2EndorseCmd)
Stake2Cmd.AddCommand(_stake2UnEndorseCmd)
Stake2Cmd.AddCommand(_stake2IntentToRevokeCmd)
Stake2Cmd.AddCommand(_stake2RevokeCmd)
Stake2Cmd.AddCommand(_stake2ActivateCmd)
Stake2Cmd.AddCommand(_stake2TransferOwnershipCmd)
Stake2Cmd.AddCommand(_stake2MigrateCmd)
Expand Down
63 changes: 48 additions & 15 deletions ioctl/cmd/action/stake2endorse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@ var (
config.Chinese: "endorse 票索引" +
" [-s 签署人] [-n NONCE] [-l GAS 限制] [-p GAS 价格] [-P 密码] [-y]",
}
_stake2UnEndorseCmdUses = map[config.Language]string{
config.English: "unendorse BUCKET_INDEX" +
_stake2IntentToRevokeCmdUses = map[config.Language]string{
config.English: "intentToRevokeEndorsement BUCKET_INDEX" +
" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "unendorse 票索引" +
config.Chinese: "intentToRevokeEndorsement 票索引" +
" [-s 签署人] [-n NONCE] [-l GAS 限制] [-p GAS 价格] [-P 密码] [-y]",
}
_stake2RevokeCmdUses = map[config.Language]string{
config.English: "revokeEndorsement BUCKET_INDEX" +
" [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
config.Chinese: "revokeEndorsement 票索引" +
" [-s 签署人] [-n NONCE] [-l GAS 限制] [-p GAS 价格] [-P 密码] [-y]",
}

_stake2EndorseCmdShorts = map[config.Language]string{
config.English: "Endorse bucket's candidate on IoTeX blockchain",
config.Chinese: "在 IoTeX 区块链上背书候选人",
}
_stake2UnEndorseCmdShorts = map[config.Language]string{
config.English: "UnEndorse bucket's candidate on IoTeX blockchain",
_stake2IntentToRevokeCmdShorts = map[config.Language]string{
config.English: "IntentToRevoke Endorsement on IoTeX blockchain",
config.Chinese: "在 IoTeX 区块链上提交撤销背书意向",
}
_stake2RevokeCmdShorts = map[config.Language]string{
config.English: "Revoke Endorsement on IoTeX blockchain",
config.Chinese: "在 IoTeX 区块链上撤销背书",
}
)
Expand All @@ -47,30 +57,50 @@ var (
return output.PrintError(err)
},
}
_stake2UnEndorseCmd = &cobra.Command{
Use: config.TranslateInLang(_stake2UnEndorseCmdUses, config.UILanguage),
Short: config.TranslateInLang(_stake2UnEndorseCmdShorts, config.UILanguage),
_stake2IntentToRevokeCmd = &cobra.Command{
Use: config.TranslateInLang(_stake2IntentToRevokeCmdUses, config.UILanguage),
Short: config.TranslateInLang(_stake2IntentToRevokeCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := stake2IntentToRevoke(args)
return output.PrintError(err)
},
}
_stake2RevokeCmd = &cobra.Command{
Use: config.TranslateInLang(_stake2RevokeCmdUses, config.UILanguage),
Short: config.TranslateInLang(_stake2RevokeCmdShorts, config.UILanguage),
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := stake2UnEndorse(args)
err := stake2Revoke(args)
return output.PrintError(err)
},
}
)

func init() {
RegisterWriteCommand(_stake2EndorseCmd)
RegisterWriteCommand(_stake2UnEndorseCmd)
RegisterWriteCommand(_stake2IntentToRevokeCmd)
RegisterWriteCommand(_stake2RevokeCmd)
}

func stake2UnEndorse(args []string) error {
func stake2Revoke(args []string) error {
bucketIndex, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
}

return doEndorsement(bucketIndex, false)
return doEndorsement(bucketIndex, action.CandidateEndorsementOpRevoke)
}

func stake2IntentToRevoke(args []string) error {
bucketIndex, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
}

return doEndorsement(bucketIndex, action.CandidateEndorsementOpIntentToRevoke)
}

func stake2Endorse(args []string) error {
Expand All @@ -79,10 +109,10 @@ func stake2Endorse(args []string) error {
return output.NewError(output.ConvertError, "failed to convert bucket index", nil)
}

return doEndorsement(bucketIndex, true)
return doEndorsement(bucketIndex, action.CandidateEndorsementOpEndorse)
}

func doEndorsement(bucketIndex uint64, isEndorse bool) error {
func doEndorsement(bucketIndex uint64, op action.CandidateEndorsementOp) error {
sender, err := Signer()
if err != nil {
return output.NewError(output.AddressError, "failed to get signed address", err)
Expand All @@ -101,7 +131,10 @@ func doEndorsement(bucketIndex uint64, isEndorse bool) error {
if err != nil {
return output.NewError(0, "failed to get nonce ", err)
}
s2t := action.NewCandidateEndorsementLegacy(nonce, gasLimit, gasPriceRau, bucketIndex, isEndorse)
s2t, err := action.NewCandidateEndorsement(nonce, gasLimit, gasPriceRau, bucketIndex, op)
if err != nil {
return output.NewError(output.InstantiationError, "failed to make a candidate endorsement", err)
}
return SendAction(
(&action.EnvelopeBuilder{}).
SetNonce(nonce).
Expand Down

0 comments on commit 0c77e21

Please sign in to comment.