github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/action/xrc20totalsupply.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 action 7 8 import ( 9 "fmt" 10 "math/big" 11 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/output" 17 ) 18 19 // Multi-language support 20 var ( 21 _totalSupplyCmdShorts = map[config.Language]string{ 22 config.English: "Get total supply", 23 config.Chinese: "获得总供应", 24 } 25 _totalSupplyCmdUses = map[config.Language]string{ 26 config.English: "totalSupply -c ALIAS|CONTRACT_ADDRESS", 27 config.Chinese: "totalSupply -c 别名|合同地址", 28 } 29 ) 30 31 // _xrc20TotalSupplyCmd represents total supply of the contract 32 var _xrc20TotalSupplyCmd = &cobra.Command{ 33 Use: config.TranslateInLang(_totalSupplyCmdUses, config.UILanguage), 34 Short: config.TranslateInLang(_totalSupplyCmdShorts, config.UILanguage), 35 RunE: func(cmd *cobra.Command, args []string) error { 36 cmd.SilenceUsage = true 37 err := totalSupply() 38 return output.PrintError(err) 39 }, 40 } 41 42 func totalSupply() error { 43 bytecode, err := _xrc20ABI.Pack("totalSupply") 44 if err != nil { 45 return output.NewError(output.ConvertError, "cannot generate bytecode from given command", err) 46 } 47 contract, err := xrc20Contract() 48 if err != nil { 49 return output.NewError(output.AddressError, "failed to get contract address", err) 50 } 51 result, err := Read(contract, "0", bytecode) 52 if err != nil { 53 return output.NewError(0, "failed to read contract", err) 54 } 55 decimal, ok := new(big.Int).SetString(result, 16) 56 if !ok { 57 return errors.New("failed to set contract supply") 58 } 59 message := amountMessage{RawData: result, Decimal: decimal.String()} 60 fmt.Println(message.String()) 61 return err 62 }