github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountactions.go (about) 1 // Copyright (c) 2019 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 "bytes" 10 "encoding/json" 11 "fmt" 12 "net/http" 13 "strconv" 14 15 "github.com/rodaine/table" 16 "github.com/spf13/cobra" 17 18 "github.com/iotexproject/iotex-core/ioctl/config" 19 "github.com/iotexproject/iotex-core/ioctl/output" 20 "github.com/iotexproject/iotex-core/ioctl/util" 21 ) 22 23 type ( 24 allActionsByAddressResult struct { 25 ActHash string 26 BlkHeight string 27 Sender string 28 Recipient string 29 ActType string 30 Amount string 31 TimeStamp string 32 RecordType string 33 } 34 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 // _accountActionsCmd represents the account sign command 54 var _accountActionsCmd = &cobra.Command{ 55 Use: config.TranslateInLang(_actionsCmdUses, config.UILanguage), 56 Short: config.TranslateInLang(_actionsCmdShorts, config.UILanguage), 57 Args: cobra.RangeArgs(1, 2), 58 RunE: func(cmd *cobra.Command, args []string) error { 59 cmd.SilenceUsage = true 60 err := accountActions(args) 61 return output.PrintError(err) 62 }, 63 } 64 65 func accountActions(args []string) error { 66 var skip uint64 = 0 67 var err error 68 if len(args) == 2 { 69 skip, err = strconv.ParseUint(args[1], 10, 64) 70 if err != nil { 71 return output.NewError(output.ConvertError, "failed to convert skip ", nil) 72 } 73 } 74 75 addr, err := util.Address(args[0]) 76 if err != nil { 77 return output.NewError(output.AddressError, "failed to get address", err) 78 } 79 reqData := map[string]string{ 80 "address": addr, 81 "offset": fmt.Sprint(skip), 82 } 83 jsonData, err := json.Marshal(reqData) 84 if err != nil { 85 return output.NewError(output.ConvertError, "failed to pack in json", nil) 86 } 87 resp, err := http.Post(config.ReadConfig.AnalyserEndpoint+"/api.ActionsService.GetActionsByAddress", "application/json", 88 bytes.NewBuffer(jsonData)) 89 if err != nil { 90 return output.NewError(output.NetworkError, "failed to send request", nil) 91 } 92 93 var respData allActionsByAddressResponse 94 err = json.NewDecoder(resp.Body).Decode(&respData) 95 if err != nil { 96 return output.NewError(output.SerializationError, "failed to deserialize the response", nil) 97 } 98 actions := respData.Results 99 100 fmt.Println("Total:", len(actions)) 101 showFields := []interface{}{ 102 "ActHash", 103 "TimeStamp", 104 "BlkHeight", 105 "ActCategory", 106 "ActType", 107 "Sender", 108 "Recipient", 109 "Amount", 110 } 111 tb := table.New(showFields...) 112 for _, actionInfo := range actions { 113 tb.AddRow( 114 actionInfo.ActHash, 115 actionInfo.TimeStamp, 116 actionInfo.BlkHeight, 117 actionInfo.RecordType, 118 actionInfo.ActType, 119 actionInfo.Sender, 120 actionInfo.Recipient, 121 actionInfo.Amount+" IOTX", 122 ) 123 } 124 tb.Print() 125 return nil 126 }