2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
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
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/accounting"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
|
|
|
)
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// GetBalancePrm groups parameters of GetBalance operation.
|
|
|
|
type GetBalancePrm struct {
|
|
|
|
ownerSet bool
|
|
|
|
ownerID owner.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAccount sets identifier of the NeoFS account for which the balance is requested.
|
|
|
|
// Required parameter. Must be a valid ID according to NeoFS API protocol.
|
|
|
|
func (x *GetBalancePrm) SetAccount(id owner.ID) {
|
|
|
|
x.ownerID = id
|
|
|
|
x.ownerSet = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBalanceRes groups resulting values of GetBalance operation.
|
|
|
|
type GetBalanceRes struct {
|
2021-11-16 18:17:25 +00:00
|
|
|
statusRes
|
|
|
|
|
|
|
|
amount *accounting.Decimal
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
func (x *GetBalanceRes) setAmount(v *accounting.Decimal) {
|
2021-11-16 18:17:25 +00:00
|
|
|
x.amount = v
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// Amount returns current amount of funds on the NeoFS account as decimal number.
|
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
|
|
|
func (x GetBalanceRes) Amount() *accounting.Decimal {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.amount
|
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// GetBalance requests current balance of the NeoFS account.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-11 12:44:22 +00:00
|
|
|
// Any client's internal or transport errors are returned as `error`,
|
|
|
|
// If WithNeoFSErrorParsing option has been provided, unsuccessful
|
|
|
|
// NeoFS status codes are returned as `error`, otherwise, are included
|
|
|
|
// in the returned result structure.
|
2021-12-02 12:21:54 +00:00
|
|
|
//
|
|
|
|
// Immediately panics if parameters are set incorrectly (see GetBalancePrm docs).
|
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
|
|
|
// Exactly one return value is non-nil. Server status return is returned in GetBalanceRes.
|
|
|
|
// Reflects all internal errors in second return value (transport problems, response processing, etc.).
|
|
|
|
func (c *Client) GetBalance(ctx context.Context, prm GetBalancePrm) (*GetBalanceRes, error) {
|
|
|
|
switch {
|
|
|
|
case ctx == nil:
|
|
|
|
panic(panicMsgMissingContext)
|
|
|
|
case !prm.ownerSet:
|
|
|
|
panic("account not set")
|
|
|
|
case !prm.ownerID.Valid():
|
|
|
|
panic("invalid account ID")
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// form request body
|
|
|
|
var body v2accounting.BalanceRequestBody
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
body.SetOwnerID(prm.ownerID.ToV2())
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// form request
|
|
|
|
var req v2accounting.BalanceRequest
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
req.SetBody(&body)
|
|
|
|
|
|
|
|
// init call context
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-11-16 18:17:25 +00:00
|
|
|
var (
|
2021-12-02 12:21:54 +00:00
|
|
|
cc contextCall
|
|
|
|
res GetBalanceRes
|
2021-11-16 18:17:25 +00:00
|
|
|
)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
c.initCallContext(&cc)
|
|
|
|
cc.req = &req
|
|
|
|
cc.statusRes = &res
|
|
|
|
cc.call = func() (responseV2, error) {
|
|
|
|
return rpcapi.Balance(c.Raw(), &req, client.WithContext(ctx))
|
|
|
|
}
|
|
|
|
cc.result = func(r responseV2) {
|
|
|
|
resp := r.(*v2accounting.BalanceResponse)
|
|
|
|
res.setAmount(accounting.NewDecimalFromV2(resp.GetBody().GetBalance()))
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// process call
|
|
|
|
if !cc.processCall() {
|
|
|
|
return nil, cc.err
|
|
|
|
}
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
return &res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|