2020-08-22 15:20:12 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-09-01 14:33:26 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-22 15:20:12 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/accounting"
|
2020-09-01 14:33:26 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
|
2020-08-22 15:20:12 +00:00
|
|
|
accountingSvc "github.com/nspcc-dev/neofs-node/pkg/services/accounting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type morphExecutor struct {
|
2020-09-01 14:33:26 +00:00
|
|
|
client *wrapper.Wrapper
|
2020-08-22 15:20:12 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 14:33:26 +00:00
|
|
|
func NewExecutor(client *wrapper.Wrapper) 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-01 14:33:26 +00:00
|
|
|
id, err := owner.IDFromV2(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-09-01 14:33:26 +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-09-01 14:33:26 +00:00
|
|
|
precision, 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)
|
2020-09-01 14:33:26 +00:00
|
|
|
dec.SetValue(amount)
|
|
|
|
dec.SetPrecision(precision)
|
2020-08-22 15:20:12 +00:00
|
|
|
|
|
|
|
res := new(accounting.BalanceResponseBody)
|
|
|
|
res.SetBalance(dec)
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|