2020-08-22 15:20:12 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-17 13:59:46 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-22 15:20:12 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-api-go/v2/accounting"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client/balance"
|
|
|
|
accountingSvc "github.com/TrueCloudLab/frostfs-node/pkg/services/accounting"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/user"
|
2020-08-22 15:20:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type morphExecutor struct {
|
2022-01-31 09:24:25 +00:00
|
|
|
client *balance.Client
|
2020-08-22 15:20:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 09:24:25 +00:00
|
|
|
func NewExecutor(client *balance.Client) accountingSvc.ServiceExecutor {
|
2020-08-22 15:20:12 +00:00
|
|
|
return &morphExecutor{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *morphExecutor) Balance(ctx context.Context, body *accounting.BalanceRequestBody) (*accounting.BalanceResponseBody, error) {
|
2022-05-17 13:59:46 +00: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 17:00:41 +00:00
|
|
|
amount, err := s.client.BalanceOf(id)
|
2020-08-22 15:20:12 +00:00
|
|
|
if err != nil {
|
2020-09-01 14:33:26 +00:00
|
|
|
return nil, err
|
2020-08-22 15:20:12 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 13:05:53 +00:00
|
|
|
balancePrecision, err := s.client.Decimals()
|
2020-08-22 15:20:12 +00:00
|
|
|
if err != nil {
|
2020-09-01 14:33:26 +00:00
|
|
|
return nil, err
|
2020-08-22 15:20:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dec := new(accounting.Decimal)
|
2022-01-24 17:03:23 +00:00
|
|
|
dec.SetValue(amount.Int64())
|
|
|
|
dec.SetPrecision(balancePrecision)
|
2020-08-22 15:20:12 +00:00
|
|
|
|
|
|
|
res := new(accounting.BalanceResponseBody)
|
|
|
|
res.SetBalance(dec)
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|