2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/rpc/client"
|
2021-11-09 08:07:49 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/accounting"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Accounting contains methods related to balance querying.
|
|
|
|
type Accounting interface {
|
|
|
|
// GetBalance returns balance of provided account.
|
2021-11-16 18:17:25 +00:00
|
|
|
GetBalance(context.Context, *owner.ID, ...CallOption) (*BalanceOfRes, error)
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
type BalanceOfRes struct {
|
|
|
|
statusRes
|
|
|
|
|
|
|
|
amount *accounting.Decimal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *BalanceOfRes) setAmount(v *accounting.Decimal) {
|
|
|
|
x.amount = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x BalanceOfRes) Amount() *accounting.Decimal {
|
|
|
|
return x.amount
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *clientImpl) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*BalanceOfRes, error) {
|
2021-11-09 08:07:49 +00:00
|
|
|
// apply all available options
|
|
|
|
callOptions := c.defaultCallOptions()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](callOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqBody := new(v2accounting.BalanceRequestBody)
|
|
|
|
reqBody.SetOwnerID(owner.ToV2())
|
|
|
|
|
|
|
|
req := new(v2accounting.BalanceRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(callOptions.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := rpcapi.Balance(c.Raw(), req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("transport error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
var (
|
|
|
|
res = new(BalanceOfRes)
|
|
|
|
procPrm processResponseV2Prm
|
|
|
|
procRes processResponseV2Res
|
|
|
|
)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
procPrm.callOpts = callOptions
|
|
|
|
procPrm.resp = resp
|
|
|
|
|
|
|
|
procRes.statusRes = res
|
|
|
|
|
|
|
|
// process response in general
|
|
|
|
if c.processResponseV2(&procRes, procPrm) {
|
|
|
|
if procRes.cliErr != nil {
|
|
|
|
return nil, procRes.cliErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
res.setAmount(accounting.NewDecimalFromV2(resp.GetBody().GetBalance()))
|
|
|
|
|
|
|
|
return res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|