From 2fd5802c48471cc18df820d697e81cd60918d94e Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 17 Dec 2021 13:36:07 +0300 Subject: [PATCH] [#105] pool: Add balance command Signed-off-by: Alex Vanin --- pool/pool.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pool/pool.go b/pool/pool.go index 6a2e6283..6c2d0367 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -14,6 +14,7 @@ import ( "time" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neofs-sdk-go/accounting" "github.com/nspcc-dev/neofs-sdk-go/client" apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status" "github.com/nspcc-dev/neofs-sdk-go/container" @@ -122,6 +123,7 @@ func (pb *Builder) Build(ctx context.Context, options *BuilderOptions) (Pool, er type Pool interface { Object Container + Accounting Connection() (client.Client, *session.Token, error) OwnerID() *owner.ID WaitForContainerPresence(context.Context, *cid.ID, *ContainerPollingParams) error @@ -149,6 +151,10 @@ type Container interface { AnnounceContainerUsedSpace(ctx context.Context, announce []container.UsedSpaceAnnouncement, opts ...CallOption) error } +type Accounting interface { + Balance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) +} + type clientPack struct { client client.Client healthy bool @@ -891,6 +897,26 @@ func (p *pool) AnnounceContainerUsedSpace(ctx context.Context, announce []contai return err } +func (p *pool) Balance(ctx context.Context, o *owner.ID, opts ...CallOption) (*accounting.Decimal, error) { + cfg := cfgFromOpts(opts...) + cp, options, err := p.conn(ctx, cfg) + if err != nil { + return nil, err + } + + res, err := cp.client.GetBalance(ctx, o, options...) + if err == nil { + // reflect status failures in err + err = apistatus.ErrFromStatus(res.Status()) + } + + if err != nil { + return nil, err + } + + return res.Amount(), nil +} + func (p *pool) WaitForContainerPresence(ctx context.Context, cid *cid.ID, pollParams *ContainerPollingParams) error { conn, _, err := p.Connection() if err != nil {