2020-08-22 18:20:12 +03:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-17 16:59:46 +03:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-22 18:20:12 +03:00
|
|
|
|
2023-03-07 16:38:26 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/accounting"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client/balance"
|
|
|
|
accountingSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/accounting"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2020-08-22 18:20:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type morphExecutor struct {
|
2022-01-31 12:24:25 +03:00
|
|
|
client *balance.Client
|
2020-08-22 18:20:12 +03:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:24:25 +03:00
|
|
|
func NewExecutor(client *balance.Client) accountingSvc.ServiceExecutor {
|
2020-08-22 18:20:12 +03:00
|
|
|
return &morphExecutor{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 11:24:40 +03:00
|
|
|
func (s *morphExecutor) Balance(_ context.Context, body *accounting.BalanceRequestBody) (*accounting.BalanceResponseBody, error) {
|
2022-05-17 16:59:46 +03:00
|
|
|
idV2 := body.GetOwnerID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, errors.New("missing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
var id user.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid account: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-31 20:00:41 +03:00
|
|
|
amount, err := s.client.BalanceOf(id)
|
2020-08-22 18:20:12 +03:00
|
|
|
if err != nil {
|
2020-09-01 17:33:26 +03:00
|
|
|
return nil, err
|
2020-08-22 18:20:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-28 16:05:53 +03:00
|
|
|
balancePrecision, err := s.client.Decimals()
|
2020-08-22 18:20:12 +03:00
|
|
|
if err != nil {
|
2020-09-01 17:33:26 +03:00
|
|
|
return nil, err
|
2020-08-22 18:20:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
dec := new(accounting.Decimal)
|
2022-01-24 20:03:23 +03:00
|
|
|
dec.SetValue(amount.Int64())
|
|
|
|
dec.SetPrecision(balancePrecision)
|
2020-08-22 18:20:12 +03:00
|
|
|
|
|
|
|
res := new(accounting.BalanceResponseBody)
|
|
|
|
res.SetBalance(dec)
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|