[#16] Fix balanceOf wrapper for api request

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-01 17:33:26 +03:00
parent 58cb90966a
commit 80f10dab7b
4 changed files with 36 additions and 27 deletions

View file

@ -3,49 +3,41 @@ package accounting
import (
"context"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
accountingSvc "github.com/nspcc-dev/neofs-node/pkg/services/accounting"
"github.com/pkg/errors"
)
type morphExecutor struct {
// TODO: use client wrapper
client *balance.Client
client *wrapper.Wrapper
}
func NewExecutor(client *balance.Client) accountingSvc.ServiceExecutor {
func NewExecutor(client *wrapper.Wrapper) accountingSvc.ServiceExecutor {
return &morphExecutor{
client: client,
}
}
func (s *morphExecutor) Balance(ctx context.Context, body *accounting.BalanceRequestBody) (*accounting.BalanceResponseBody, error) {
id := body.GetOwnerID()
idBytes, err := id.StableMarshal(nil)
id, err := owner.IDFromV2(body.GetOwnerID())
if err != nil {
return nil, errors.Wrap(err, "could not marshal wallet owner ID")
return nil, err
}
argsBalance := balance.GetBalanceOfArgs{}
argsBalance.SetWallet(idBytes)
vBalance, err := s.client.BalanceOf(argsBalance)
amount, err := s.client.BalanceOf(id)
if err != nil {
return nil, errors.Wrap(err, "could not call BalanceOf method")
return nil, err
}
argsDecimals := balance.DecimalsArgs{}
vDecimals, err := s.client.Decimals(argsDecimals)
precision, err := s.client.Decimals()
if err != nil {
return nil, errors.Wrap(err, "could not call decimals method")
return nil, err
}
dec := new(accounting.Decimal)
dec.SetValue(vBalance.Amount())
dec.SetPrecision(uint32(vDecimals.Decimals()))
dec.SetValue(amount)
dec.SetPrecision(precision)
res := new(accounting.BalanceResponseBody)
res.SetBalance(dec)