2022-05-18 11:17:31 +00:00
|
|
|
package accounting
|
2020-08-04 14:46:12 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-24 17:05:09 +00:00
|
|
|
"math/big"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
internalclient "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/key"
|
|
|
|
commonCmd "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/internal/common"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/precision"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/accounting"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-01-24 17:05:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
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
|
|
|
)
|
|
|
|
|
2022-05-18 11:17:31 +00:00
|
|
|
const (
|
|
|
|
ownerFlag = "owner"
|
2020-10-12 14:31:00 +00:00
|
|
|
)
|
|
|
|
|
2020-10-12 14:19:57 +00:00
|
|
|
var accountingBalanceCmd = &cobra.Command{
|
|
|
|
Use: "balance",
|
2023-01-27 10:44:39 +00:00
|
|
|
Short: "Get internal balance of FrostFS account",
|
|
|
|
Long: `Get internal balance of FrostFS account`,
|
2024-03-11 14:11:49 +00:00
|
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
2022-05-31 17:00:41 +00:00
|
|
|
var idUser user.ID
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2022-05-24 08:22:23 +00:00
|
|
|
pk := key.GetOrGenerate(cmd)
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2022-05-18 11:17:31 +00:00
|
|
|
balanceOwner, _ := cmd.Flags().GetString(ownerFlag)
|
2021-04-21 12:27:32 +00:00
|
|
|
if balanceOwner == "" {
|
2022-05-31 17:00:41 +00:00
|
|
|
user.IDFromKey(&idUser, pk.PublicKey)
|
2021-03-15 11:23:04 +00:00
|
|
|
} else {
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.ExitOnErr(cmd, "can't decode owner ID wallet address: %w", idUser.DecodeString(balanceOwner))
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 08:38:45 +00:00
|
|
|
cli := internalclient.GetSDKClientByFlag(cmd, pk, commonflags.RPC)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2022-05-18 11:17:31 +00:00
|
|
|
var prm internalclient.BalanceOfPrm
|
|
|
|
prm.SetClient(cli)
|
2023-11-21 08:42:30 +00:00
|
|
|
prm.Account = idUser
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2023-05-24 13:51:57 +00:00
|
|
|
res, err := internalclient.BalanceOf(cmd.Context(), prm)
|
2023-01-16 09:20:16 +00:00
|
|
|
commonCmd.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)
|
2022-05-18 11:17:31 +00:00
|
|
|
ff.String(ownerFlag, "", "owner of balance account (omit to use owner from private key)")
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2022-08-22 11:04:00 +00:00
|
|
|
func prettyPrintDecimal(cmd *cobra.Command, decimal accounting.Decimal) {
|
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
|
|
|
}
|
|
|
|
}
|