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"
|
|
|
|
)
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmBalanceGet groups parameters of BalanceGet operation.
|
|
|
|
type PrmBalanceGet struct {
|
2021-12-02 12:21:54 +00:00
|
|
|
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.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *PrmBalanceGet) SetAccount(id owner.ID) {
|
2021-12-02 12:21:54 +00:00
|
|
|
x.ownerID = id
|
|
|
|
x.ownerSet = true
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// ResBalanceGet groups resulting values of BalanceGet operation.
|
|
|
|
type ResBalanceGet struct {
|
2021-11-16 18:17:25 +00:00
|
|
|
statusRes
|
|
|
|
|
|
|
|
amount *accounting.Decimal
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *ResBalanceGet) 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.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResBalanceGet) Amount() *accounting.Decimal {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.amount
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// BalanceGet requests current balance of the NeoFS account.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
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
|
|
|
//
|
2022-02-17 16:10:49 +00:00
|
|
|
// Immediately panics if parameters are set incorrectly (see PrmBalanceGet docs).
|
2021-12-02 12:21:54 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Return statuses:
|
|
|
|
// - global (see Client docs).
|
2022-02-17 16:10:49 +00:00
|
|
|
func (c *Client) BalanceGet(ctx context.Context, prm PrmBalanceGet) (*ResBalanceGet, error) {
|
2021-12-02 12:21:54 +00:00
|
|
|
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
|
2022-02-17 16:10:49 +00:00
|
|
|
res ResBalanceGet
|
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
|
|
|
}
|