2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-01-24 17:05:09 +00:00
|
|
|
"math/big"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2022-01-24 17:05:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2021-10-28 14:48:46 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-05-18 11:03:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-01-24 17:05:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/util/precision"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/accounting"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
2021-09-27 15:36:14 +00:00
|
|
|
"github.com/spf13/viper"
|
2020-08-04 14:46:12 +00:00
|
|
|
)
|
|
|
|
|
2020-10-12 14:31:00 +00:00
|
|
|
var (
|
|
|
|
balanceOwner string
|
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// accountingCmd represents the accounting command
|
|
|
|
var accountingCmd = &cobra.Command{
|
|
|
|
Use: "accounting",
|
|
|
|
Short: "Operations with accounts and balances",
|
|
|
|
Long: `Operations with accounts and balances`,
|
2021-09-27 15:36:14 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
_ = viper.BindPFlag(commonflags.WalletPath, flags.Lookup(commonflags.WalletPath))
|
|
|
|
_ = viper.BindPFlag(commonflags.Account, flags.Lookup(commonflags.Account))
|
|
|
|
_ = viper.BindPFlag(commonflags.RPC, flags.Lookup(commonflags.RPC))
|
2021-09-27 15:36:14 +00:00
|
|
|
},
|
2020-10-12 14:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var accountingBalanceCmd = &cobra.Command{
|
|
|
|
Use: "balance",
|
|
|
|
Short: "Get internal balance of NeoFS account",
|
|
|
|
Long: `Get internal balance of NeoFS account`,
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-10-28 14:48:46 +00:00
|
|
|
var oid *owner.ID
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
if balanceOwner == "" {
|
2022-01-21 12:15:10 +00:00
|
|
|
oid = owner.NewIDFromPublicKey(&key.PublicKey)
|
2021-03-15 11:23:04 +00:00
|
|
|
} else {
|
|
|
|
oid, err = ownerFromString(balanceOwner)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "", err)
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.BalanceOfPrm
|
|
|
|
|
|
|
|
prepareAPIClientWithKey(cmd, key, &prm)
|
2022-01-21 16:53:06 +00:00
|
|
|
prm.SetAccount(*oid)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
|
|
|
res, err := internalclient.BalanceOf(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-10-12 14:19:57 +00:00
|
|
|
|
|
|
|
// print to stdout
|
2021-10-28 14:48:46 +00:00
|
|
|
prettyPrintDecimal(cmd, res.Balance())
|
2020-08-04 14:46:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
func initAccountingBalanceCmd() {
|
|
|
|
ff := accountingBalanceCmd.Flags()
|
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
ff.StringP(commonflags.WalletPath, commonflags.WalletPathShorthand, commonflags.WalletPathDefault, commonflags.WalletPathUsage)
|
|
|
|
ff.StringP(commonflags.Account, commonflags.AccountShorthand, commonflags.AccountDefault, commonflags.AccountUsage)
|
|
|
|
ff.StringP(commonflags.RPC, commonflags.RPCShorthand, commonflags.RPCDefault, commonflags.RPCUsage)
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
accountingBalanceCmd.Flags().StringVar(&balanceOwner, "owner", "", "owner of balance account (omit to use owner from private key)")
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(accountingCmd)
|
2020-10-12 14:19:57 +00:00
|
|
|
accountingCmd.AddCommand(accountingBalanceCmd)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
|
|
// and all subcommands, e.g.:
|
|
|
|
// accountingCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
|
|
// is called directly, e.g.:
|
|
|
|
// accountingCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
2020-10-12 14:31:00 +00:00
|
|
|
|
2021-09-27 15:36:14 +00:00
|
|
|
initAccountingBalanceCmd()
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func prettyPrintDecimal(cmd *cobra.Command, decimal *accounting.Decimal) {
|
2020-10-12 14:19:57 +00:00
|
|
|
if decimal == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
if viper.GetBool(commonflags.Verbose) {
|
2021-06-20 23:02:56 +00:00
|
|
|
cmd.Println("value:", decimal.Value())
|
|
|
|
cmd.Println("precision:", decimal.Precision())
|
2020-10-12 14:19:57 +00:00
|
|
|
} else {
|
2022-01-24 17:05:09 +00:00
|
|
|
amountF8 := precision.Convert(decimal.Precision(), 8, big.NewInt(decimal.Value()))
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2022-01-24 17:05:09 +00:00
|
|
|
cmd.Println(fixedn.ToString(amountF8, 8))
|
2020-10-12 14:19:57 +00:00
|
|
|
}
|
|
|
|
}
|