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

View file

@ -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
}

View file

@ -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
}

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)