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"
|
2020-08-25 11:51:53 +00:00
|
|
|
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/client"
|
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c Client) GetSelfBalance(ctx context.Context, opts ...CallOption) (*accounting.Decimal, error) {
|
2021-03-10 12:07:06 +00:00
|
|
|
return c.GetBalance(ctx, nil, opts...)
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 08:15:46 +00:00
|
|
|
func (c Client) GetBalance(ctx context.Context, owner *owner.ID, opts ...CallOption) (*accounting.Decimal, error) {
|
2020-08-25 11:51:53 +00:00
|
|
|
// check remote node version
|
2020-11-16 08:08:47 +00:00
|
|
|
switch c.remoteNode.Version.Major() {
|
2020-08-25 11:51:53 +00:00
|
|
|
case 2:
|
|
|
|
return c.getBalanceV2(ctx, owner, opts...)
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:07:06 +00:00
|
|
|
func (c Client) getBalanceV2(ctx context.Context, ownerID *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 {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:07:06 +00:00
|
|
|
if ownerID == nil {
|
|
|
|
w, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerID = new(owner.ID)
|
|
|
|
ownerID.SetNeo3Wallet(w)
|
|
|
|
}
|
|
|
|
|
2020-08-25 11:51:53 +00:00
|
|
|
reqBody := new(v2accounting.BalanceRequestBody)
|
2021-03-10 12:07:06 +00:00
|
|
|
reqBody.SetOwnerID(ownerID.ToV2())
|
2020-08-25 11:51:53 +00:00
|
|
|
|
|
|
|
req := new(v2accounting.BalanceRequest)
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2AccountingClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.Balance(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:36:18 +00:00
|
|
|
return accounting.NewDecimalFromV2(resp.GetBody().GetBalance()), nil
|
2020-08-25 11:51:53 +00:00
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errUnsupportedProtocol
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func v2AccountingClientFromOptions(opts *clientOptions) (cli *v2accounting.Client, err error) {
|
|
|
|
switch {
|
|
|
|
case opts.grpcOpts.v2AccountingClient != nil:
|
|
|
|
// return value from client cache
|
|
|
|
return opts.grpcOpts.v2AccountingClient, nil
|
|
|
|
|
|
|
|
case opts.grpcOpts.conn != nil:
|
|
|
|
cli, err = v2accounting.NewClient(v2accounting.WithGlobalOpts(
|
|
|
|
client.WithGRPCConn(opts.grpcOpts.conn)),
|
|
|
|
)
|
|
|
|
|
|
|
|
case opts.addr != "":
|
|
|
|
cli, err = v2accounting.NewClient(v2accounting.WithGlobalOpts(
|
2020-11-23 11:05:27 +00:00
|
|
|
client.WithNetworkAddress(opts.addr),
|
|
|
|
client.WithDialTimeout(opts.dialTimeout),
|
|
|
|
))
|
2020-08-25 11:51:53 +00:00
|
|
|
|
|
|
|
default:
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errOptionsLack("Accounting")
|
2020-08-25 11:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if client correct and save in cache
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.grpcOpts.v2AccountingClient = cli
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|