[#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

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/session" "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"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" "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" accountingTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/accounting/grpc"
accountingService "github.com/nspcc-dev/neofs-node/pkg/services/accounting" accountingService "github.com/nspcc-dev/neofs-node/pkg/services/accounting"
accounting "github.com/nspcc-dev/neofs-node/pkg/services/accounting/morph" accounting "github.com/nspcc-dev/neofs-node/pkg/services/accounting/morph"
@ -25,6 +26,9 @@ func initAccountingService(c *cfg) {
balanceClient, err := balance.New(staticClient) balanceClient, err := balance.New(staticClient)
fatalOnErr(err) fatalOnErr(err)
balanceMorphWrapper, err := wrapper.New(balanceClient)
fatalOnErr(err)
metaHdr := new(session.ResponseMetaHeader) metaHdr := new(session.ResponseMetaHeader)
xHdr := new(session.XHeader) xHdr := new(session.XHeader)
xHdr.SetKey("test X-Header key") xHdr.SetKey("test X-Header key")
@ -36,7 +40,7 @@ func initAccountingService(c *cfg) {
accountingService.NewSignService( accountingService.NewSignService(
c.key, c.key,
accountingService.NewExecutionService( accountingService.NewExecutionService(
accounting.NewExecutor(balanceClient), accounting.NewExecutor(balanceMorphWrapper),
metaHdr, metaHdr,
), ),
), ),

View file

@ -1,11 +1,25 @@
package wrapper package wrapper
// OwnerID represents the container owner identifier. import (
// FIXME: correct the definition. "github.com/nspcc-dev/neofs-api-go/pkg/owner"
type OwnerID struct{} "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
)
// BalanceOf receives the amount of funds in the client's account // BalanceOf receives the amount of funds in the client's account
// through the Balance contract call, and returns it. // through the Balance contract call, and returns it.
func (w *Wrapper) BalanceOf(ownerID OwnerID) (int64, error) { func (w *Wrapper) BalanceOf(id *owner.ID) (int64, error) {
panic("implement me") 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
} }

View file

@ -17,6 +17,5 @@ func (w *Wrapper) Decimals() (uint32, error) {
return 0, errors.Wrap(err, "could not invoke smart contract") return 0, errors.Wrap(err, "could not invoke smart contract")
} }
// FIXME: avoid narrowing cast
return uint32(values.Decimals()), nil return uint32(values.Decimals()), nil
} }

View file

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