frostfs-node/pkg/services/accounting/morph/executor.go
Leonard Lyubich 1c30414a6c [] Upgrade NeoFS SDK Go module with new IDs
Core changes:
 * avoid package-colliding variable naming
 * avoid using pointers to IDs where unnecessary
 * avoid using `idSDK` import alias pattern
 * use `EncodeToString` for protocol string calculation and `String` for
  printing

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2022-06-01 17:41:45 +03:00

55 lines
1.1 KiB
Go

package accounting
import (
"context"
"errors"
"fmt"
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
accountingSvc "github.com/nspcc-dev/neofs-node/pkg/services/accounting"
"github.com/nspcc-dev/neofs-sdk-go/user"
)
type morphExecutor struct {
client *balance.Client
}
func NewExecutor(client *balance.Client) accountingSvc.ServiceExecutor {
return &morphExecutor{
client: client,
}
}
func (s *morphExecutor) Balance(ctx context.Context, body *accounting.BalanceRequestBody) (*accounting.BalanceResponseBody, error) {
idV2 := body.GetOwnerID()
if idV2 == nil {
return nil, errors.New("missing account")
}
var id user.ID
err := id.ReadFromV2(*idV2)
if err != nil {
return nil, fmt.Errorf("invalid account: %w", err)
}
amount, err := s.client.BalanceOf(id)
if err != nil {
return nil, err
}
balancePrecision, err := s.client.Decimals()
if err != nil {
return nil, err
}
dec := new(accounting.Decimal)
dec.SetValue(amount.Int64())
dec.SetPrecision(balancePrecision)
res := new(accounting.BalanceResponseBody)
res.SetBalance(dec)
return res, nil
}