2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
v2accounting "github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-11-09 08:07:49 +00:00
|
|
|
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"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-11-09 08:07:49 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmBalanceGet groups parameters of BalanceGet operation.
|
|
|
|
type PrmBalanceGet struct {
|
2022-03-03 11:04:53 +00:00
|
|
|
prmCommonMeta
|
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
accountSet bool
|
|
|
|
account user.ID
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAccount sets identifier of the NeoFS account for which the balance is requested.
|
2022-04-11 06:30:22 +00:00
|
|
|
// Required parameter.
|
|
|
|
func (x *PrmBalanceGet) SetAccount(id user.ID) {
|
|
|
|
x.account = id
|
|
|
|
x.accountSet = true
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
amount accounting.Decimal
|
2021-11-16 18:17:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// Amount returns current amount of funds on the NeoFS account as decimal number.
|
2022-08-05 07:56: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`,
|
2022-07-04 15:33:33 +00:00
|
|
|
// If PrmInit.ResolveNeoFSFailures has been called, unsuccessful
|
2022-01-11 12:44:22 +00:00
|
|
|
// 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)
|
2022-04-11 06:30:22 +00:00
|
|
|
case !prm.accountSet:
|
2021-12-02 12:21:54 +00:00
|
|
|
panic("account not set")
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 12:21:54 +00:00
|
|
|
// form request body
|
2022-04-11 06:30:22 +00:00
|
|
|
var accountV2 refs.OwnerID
|
|
|
|
prm.account.WriteToV2(&accountV2)
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2022-04-11 06:30:22 +00:00
|
|
|
var body v2accounting.BalanceRequestBody
|
|
|
|
body.SetOwnerID(&accountV2)
|
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)
|
2022-03-03 11:04:53 +00:00
|
|
|
cc.meta = prm.prmCommonMeta
|
2021-12-02 12:21:54 +00:00
|
|
|
cc.req = &req
|
|
|
|
cc.statusRes = &res
|
|
|
|
cc.call = func() (responseV2, error) {
|
2022-03-07 11:39:49 +00:00
|
|
|
return rpcapi.Balance(&c.c, &req, client.WithContext(ctx))
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
cc.result = func(r responseV2) {
|
|
|
|
resp := r.(*v2accounting.BalanceResponse)
|
2022-03-12 16:15:26 +00:00
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
const fieldBalance = "balance"
|
|
|
|
|
2022-07-07 10:51:05 +00:00
|
|
|
bal := resp.GetBody().GetBalance()
|
|
|
|
if bal == nil {
|
2022-08-05 07:56:49 +00:00
|
|
|
cc.err = newErrMissingResponseField(fieldBalance)
|
2022-07-07 10:51:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-05 07:56:49 +00:00
|
|
|
cc.err = res.amount.ReadFromV2(*bal)
|
2022-07-07 10:51:05 +00:00
|
|
|
if cc.err != nil {
|
2022-08-05 07:56:49 +00:00
|
|
|
cc.err = newErrInvalidResponseField(fieldBalance, cc.err)
|
2022-03-12 16:15:26 +00:00
|
|
|
}
|
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
|
|
|
}
|