From 80f10dab7bf68b131763a203339c195e9d67a877 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 1 Sep 2020 17:33:26 +0300 Subject: [PATCH] [#16] Fix balanceOf wrapper for api request Signed-off-by: Alex Vanin --- cmd/neofs-node/accounting.go | 6 +++- pkg/morph/client/balance/wrapper/balanceOf.go | 24 +++++++++++--- pkg/morph/client/balance/wrapper/decimals.go | 1 - pkg/services/accounting/morph/executor.go | 32 +++++++------------ 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/cmd/neofs-node/accounting.go b/cmd/neofs-node/accounting.go index 6b8a3700d..e99918905 100644 --- a/cmd/neofs-node/accounting.go +++ b/cmd/neofs-node/accounting.go @@ -6,6 +6,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper" accountingTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/accounting/grpc" accountingService "github.com/nspcc-dev/neofs-node/pkg/services/accounting" accounting "github.com/nspcc-dev/neofs-node/pkg/services/accounting/morph" @@ -25,6 +26,9 @@ func initAccountingService(c *cfg) { balanceClient, err := balance.New(staticClient) fatalOnErr(err) + balanceMorphWrapper, err := wrapper.New(balanceClient) + fatalOnErr(err) + metaHdr := new(session.ResponseMetaHeader) xHdr := new(session.XHeader) xHdr.SetKey("test X-Header key") @@ -36,7 +40,7 @@ func initAccountingService(c *cfg) { accountingService.NewSignService( c.key, accountingService.NewExecutionService( - accounting.NewExecutor(balanceClient), + accounting.NewExecutor(balanceMorphWrapper), metaHdr, ), ), diff --git a/pkg/morph/client/balance/wrapper/balanceOf.go b/pkg/morph/client/balance/wrapper/balanceOf.go index add725d32..48b7a413d 100644 --- a/pkg/morph/client/balance/wrapper/balanceOf.go +++ b/pkg/morph/client/balance/wrapper/balanceOf.go @@ -1,11 +1,25 @@ package wrapper -// OwnerID represents the container owner identifier. -// FIXME: correct the definition. -type OwnerID struct{} +import ( + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" +) // BalanceOf receives the amount of funds in the client's account // through the Balance contract call, and returns it. -func (w *Wrapper) BalanceOf(ownerID OwnerID) (int64, error) { - panic("implement me") +func (w *Wrapper) BalanceOf(id *owner.ID) (int64, error) { + v, err := owner.ScriptHashBE(id) + if err != nil { + return 0, err + } + + args := balance.GetBalanceOfArgs{} + args.SetWallet(v) + + result, err := w.client.BalanceOf(args) + if err != nil { + return 0, err + } + + return result.Amount(), nil } diff --git a/pkg/morph/client/balance/wrapper/decimals.go b/pkg/morph/client/balance/wrapper/decimals.go index ce13b20ef..2910622f5 100644 --- a/pkg/morph/client/balance/wrapper/decimals.go +++ b/pkg/morph/client/balance/wrapper/decimals.go @@ -17,6 +17,5 @@ func (w *Wrapper) Decimals() (uint32, error) { return 0, errors.Wrap(err, "could not invoke smart contract") } - // FIXME: avoid narrowing cast return uint32(values.Decimals()), nil } diff --git a/pkg/services/accounting/morph/executor.go b/pkg/services/accounting/morph/executor.go index a79a2a323..9b6e73aaa 100644 --- a/pkg/services/accounting/morph/executor.go +++ b/pkg/services/accounting/morph/executor.go @@ -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)