2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-10-12 14:19:57 +00:00
|
|
|
"context"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
2020-10-12 14:19:57 +00:00
|
|
|
"math"
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2020-10-12 14:19:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
|
2020-10-12 14:31:00 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
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`,
|
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`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
var (
|
|
|
|
response *accounting.Decimal
|
2020-10-12 14:31:00 +00:00
|
|
|
oid *owner.ID
|
2020-10-12 14:19:57 +00:00
|
|
|
err error
|
2020-10-12 14:31:00 +00:00
|
|
|
|
|
|
|
ctx = context.Background()
|
2020-10-12 14:19:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
cli, err := getSDKClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:31:00 +00:00
|
|
|
switch balanceOwner {
|
|
|
|
case "":
|
2020-12-24 11:25:36 +00:00
|
|
|
response, err = cli.GetSelfBalance(ctx, globalCallOptions()...)
|
2020-10-12 14:31:00 +00:00
|
|
|
default:
|
|
|
|
oid, err = ownerFromString(balanceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
response, err = cli.GetBalance(ctx, oid, globalCallOptions()...)
|
2020-10-12 14:31:00 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:19:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// print to stdout
|
|
|
|
prettyPrintDecimal(response)
|
|
|
|
|
|
|
|
return nil
|
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
|
|
|
|
|
|
|
accountingBalanceCmd.Flags().StringVar(&balanceOwner, "owner", "", "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
|
|
|
|
|
|
|
func prettyPrintDecimal(decimal *accounting.Decimal) {
|
|
|
|
if decimal == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if verbose {
|
2020-11-16 09:43:52 +00:00
|
|
|
fmt.Println("value:", decimal.Value())
|
|
|
|
fmt.Println("precision:", decimal.Precision())
|
2020-10-12 14:19:57 +00:00
|
|
|
} else {
|
|
|
|
// divider = 10^{precision}; v:365, p:2 => 365 / 10^2 = 3.65
|
2020-11-16 09:43:52 +00:00
|
|
|
divider := math.Pow(10, float64(decimal.Precision()))
|
2020-10-12 14:19:57 +00:00
|
|
|
|
|
|
|
// %0.8f\n for precision 8
|
2020-11-16 09:43:52 +00:00
|
|
|
format := fmt.Sprintf("%%0.%df\n", decimal.Precision())
|
2020-10-12 14:19:57 +00:00
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
fmt.Printf(format, float64(decimal.Value())/divider)
|
2020-10-12 14:19:57 +00:00
|
|
|
}
|
|
|
|
}
|