2020-08-22 15:20:12 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
2022-01-31 09:24:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance"
|
2020-08-22 15:20:12 +00:00
|
|
|
accountingSvc "github.com/nspcc-dev/neofs-node/pkg/services/accounting"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
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) {
|
2020-09-16 11:58:58 +00:00
|
|
|
amount, err := s.client.BalanceOf(owner.NewIDFromV2(body.GetOwnerID()))
|
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
|
|
|
|
}
|