From 5022362c1ac9b9cbfb77d8431c8f55c2f5e79efd Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Sat, 22 Aug 2020 18:20:12 +0300 Subject: [PATCH] [#11] accounting: Implement ServiceExecutor on Neo:Morph client Signed-off-by: Leonard Lyubich --- pkg/services/accounting/morph/executor.go | 54 +++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 pkg/services/accounting/morph/executor.go diff --git a/pkg/services/accounting/morph/executor.go b/pkg/services/accounting/morph/executor.go new file mode 100644 index 000000000..a79a2a323 --- /dev/null +++ b/pkg/services/accounting/morph/executor.go @@ -0,0 +1,54 @@ +package accounting + +import ( + "context" + + "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/pkg/errors" +) + +type morphExecutor struct { + // TODO: use client wrapper + 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) { + id := body.GetOwnerID() + + idBytes, err := id.StableMarshal(nil) + if err != nil { + return nil, errors.Wrap(err, "could not marshal wallet owner ID") + } + + argsBalance := balance.GetBalanceOfArgs{} + argsBalance.SetWallet(idBytes) + + vBalance, err := s.client.BalanceOf(argsBalance) + if err != nil { + return nil, errors.Wrap(err, "could not call BalanceOf method") + } + + argsDecimals := balance.DecimalsArgs{} + + vDecimals, err := s.client.Decimals(argsDecimals) + if err != nil { + return nil, errors.Wrap(err, "could not call decimals method") + } + + dec := new(accounting.Decimal) + dec.SetValue(vBalance.Amount()) + dec.SetPrecision(uint32(vDecimals.Decimals())) + + res := new(accounting.BalanceResponseBody) + res.SetBalance(dec) + + return res, nil +}