2022-07-28 16:57:40 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2022-12-15 10:03:54 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-rest-gw/gen/models"
|
|
|
|
"github.com/TrueCloudLab/frostfs-rest-gw/gen/restapi/operations"
|
|
|
|
"github.com/TrueCloudLab/frostfs-rest-gw/internal/util"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2022-07-28 16:57:40 +00:00
|
|
|
"github.com/go-openapi/runtime/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Balance handler that get balance from NeoFS.
|
|
|
|
func (a *API) Balance(params operations.GetBalanceParams) middleware.Responder {
|
|
|
|
var ownerID user.ID
|
|
|
|
if err := ownerID.DecodeString(params.Address); err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("parse address", err)
|
|
|
|
return operations.NewGetBalanceBadRequest().WithPayload(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.PrmBalanceGet
|
|
|
|
prm.SetAccount(ownerID)
|
|
|
|
|
|
|
|
neofsBalance, err := a.pool.Balance(params.HTTPRequest.Context(), prm)
|
|
|
|
if err != nil {
|
|
|
|
resp := a.logAndGetErrorResponse("get balance", err)
|
|
|
|
return operations.NewGetBalanceBadRequest().WithPayload(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp models.Balance
|
|
|
|
resp.Address = util.NewString(params.Address)
|
|
|
|
resp.Value = util.NewString(strconv.FormatInt(neofsBalance.Value(), 10))
|
|
|
|
resp.Precision = util.NewInteger(int64(neofsBalance.Precision()))
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
return operations.NewGetBalanceOK().
|
|
|
|
WithPayload(&resp).
|
|
|
|
WithAccessControlAllowOrigin("*")
|
2022-07-28 16:57:40 +00:00
|
|
|
}
|