2020-08-25 11:51:53 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/accounting"
|
2020-08-31 08:15:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2021-03-12 13:07:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/client"
|
2020-08-25 11:51:53 +00:00
|
|
|
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
2021-03-12 13:07:52 +00:00
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2020-08-25 11:51:53 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// Accounting contains methods related to balance querying.
|
|
|
|
type Accounting interface {
|
|
|
|
// GetBalance returns balance of provided account.
|
|
|
|
GetBalance(context.Context, *owner.ID, ...CallOption) (*accounting.Decimal, error)
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
func (c *clientImpl) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
|
2020-08-25 11:51:53 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-08-25 11:51:53 +00:00
|
|
|
for i := range opts {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOptions)
|
2021-03-10 12:07:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 11:51:53 +00:00
|
|
|
reqBody := new(v2accounting.BalanceRequestBody)
|
2021-03-12 13:07:52 +00:00
|
|
|
reqBody.SetOwnerID(owner.ToV2())
|
2020-08-25 11:51:53 +00:00
|
|
|
|
|
|
|
req := new(v2accounting.BalanceRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
err := v2signature.SignServiceMessage(callOptions.key, req)
|
2020-08-25 11:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.Balance(c.Raw(), req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
2020-08-25 11:51:53 +00:00
|
|
|
if err != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
return accounting.NewDecimalFromV2(resp.GetBody().GetBalance()), nil
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|