github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountactions.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package account
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"strconv"
    12  
    13  	"github.com/pkg/errors"
    14  	"github.com/rodaine/table"
    15  	"github.com/spf13/cobra"
    16  
    17  	"github.com/iotexproject/iotex-core/ioctl"
    18  	"github.com/iotexproject/iotex-core/ioctl/config"
    19  )
    20  
    21  type (
    22  	// allActionsByAddressResult is the struct of an Confirmation output
    23  	allActionsByAddressResult struct {
    24  		ActHash    string
    25  		BlkHeight  string
    26  		Sender     string
    27  		Recipient  string
    28  		ActType    string
    29  		Amount     string
    30  		TimeStamp  string
    31  		RecordType string
    32  	}
    33  
    34  	// allActionsByAddressResponse is the struct of an Confirmation output
    35  	allActionsByAddressResponse struct {
    36  		Count   string
    37  		Results []*allActionsByAddressResult
    38  	}
    39  )
    40  
    41  // Multi-language support
    42  var (
    43  	_actionsCmdShorts = map[config.Language]string{
    44  		config.English: "Show the list of actions for an account",
    45  		config.Chinese: "显示账户的操作列表",
    46  	}
    47  	_actionsCmdUses = map[config.Language]string{
    48  		config.English: "actions [ALIAS|ADDRESS] [SKIP]",
    49  		config.Chinese: "actions [别名|地址] [SKIP]",
    50  	}
    51  )
    52  
    53  // NewAccountActions represents the account sign command
    54  func NewAccountActions(client ioctl.Client) *cobra.Command {
    55  	use, _ := client.SelectTranslation(_actionsCmdUses)
    56  	short, _ := client.SelectTranslation(_actionsCmdShorts)
    57  	return &cobra.Command{
    58  		Use:   use,
    59  		Short: short,
    60  		Args:  cobra.RangeArgs(1, 2),
    61  		RunE: func(cmd *cobra.Command, args []string) error {
    62  			cmd.SilenceUsage = true
    63  			var skip uint64 = 0
    64  			var err error
    65  			if len(args) == 2 {
    66  				skip, err = strconv.ParseUint(args[1], 10, 64)
    67  				if err != nil {
    68  					return errors.Wrap(err, "failed to convert skip ")
    69  				}
    70  			}
    71  
    72  			addr, err := client.Address(args[0])
    73  			if err != nil {
    74  				return errors.Wrap(err, "failed to get address")
    75  			}
    76  			reqData := map[string]string{
    77  				"address": addr,
    78  				"offset":  fmt.Sprint(skip),
    79  			}
    80  			resp, err := client.QueryAnalyser(reqData)
    81  			if err != nil {
    82  				return err
    83  			}
    84  
    85  			var respData allActionsByAddressResponse
    86  			err = json.NewDecoder(resp.Body).Decode(&respData)
    87  			if err != nil {
    88  				return errors.Wrap(err, "failed to deserialize the response")
    89  			}
    90  			actions := respData.Results
    91  
    92  			cmd.Println("Total:", len(actions))
    93  			showFields := []interface{}{
    94  				"ActHash",
    95  				"TimeStamp",
    96  				"BlkHeight",
    97  				"ActCategory",
    98  				"ActType",
    99  				"Sender",
   100  				"Recipient",
   101  				"Amount",
   102  			}
   103  			tb := table.New(showFields...)
   104  			for _, actionInfo := range actions {
   105  				tb.AddRow(
   106  					actionInfo.ActHash,
   107  					actionInfo.TimeStamp,
   108  					actionInfo.BlkHeight,
   109  					actionInfo.RecordType,
   110  					actionInfo.ActType,
   111  					actionInfo.Sender,
   112  					actionInfo.Recipient,
   113  					actionInfo.Amount+" IOTX",
   114  				)
   115  			}
   116  			tb.Print()
   117  			return nil
   118  		},
   119  	}
   120  }