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
|
|
|
|
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-05-18 11:17:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
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"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
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",
|
|
|
|
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) {
|
2022-05-17 13:59:46 +00:00
|
|
|
var oid 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-17 13:59:46 +00:00
|
|
|
user.IDFromKey(&oid, pk.PublicKey)
|
2021-03-15 11:23:04 +00:00
|
|
|
} else {
|
2022-05-17 13:59:46 +00:00
|
|
|
common.ExitOnErr(cmd, "can't decode owner ID wallet address: %w", oid.DecodeString(balanceOwner))
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 11:17:31 +00:00
|
|
|
cli, err := internalclient.GetSDKClientByFlag(pk, commonflags.RPC)
|
|
|
|
common.ExitOnErr(cmd, "create API client: %w", err)
|
2021-10-28 14:48:46 +00:00
|
|
|
|
2022-05-18 11:17:31 +00:00
|
|
|
var prm internalclient.BalanceOfPrm
|
|
|
|
prm.SetClient(cli)
|
2022-05-17 13:59:46 +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)
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|