[#122] Reduce precision from balance contract to Fixed8.

Fixed8 won't overflow int64 for values less than 92 billion
that is suitable for GAS.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-28 16:05:53 +03:00 committed by Alex Vanin
parent 03f52bca01
commit d08c1c76c1

View file

@ -7,12 +7,15 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/accounting" "github.com/nspcc-dev/neofs-api-go/v2/accounting"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
accountingSvc "github.com/nspcc-dev/neofs-node/pkg/services/accounting" accountingSvc "github.com/nspcc-dev/neofs-node/pkg/services/accounting"
"github.com/nspcc-dev/neofs-node/pkg/util/precision"
) )
type morphExecutor struct { type morphExecutor struct {
client *wrapper.Wrapper client *wrapper.Wrapper
} }
const fixed8Precision = 8
func NewExecutor(client *wrapper.Wrapper) accountingSvc.ServiceExecutor { func NewExecutor(client *wrapper.Wrapper) accountingSvc.ServiceExecutor {
return &morphExecutor{ return &morphExecutor{
client: client, client: client,
@ -25,14 +28,21 @@ func (s *morphExecutor) Balance(ctx context.Context, body *accounting.BalanceReq
return nil, err return nil, err
} }
precision, err := s.client.Decimals() balancePrecision, err := s.client.Decimals()
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Convert amount to Fixed8 precision. This way it will definitely fit
// int64 value.
// Max Fixed8 decimal integer value that fit into int64: 92 233 720 368.
// Max Fixed12 decimal integer value that fit into int64: 9 223 372.
// Max Fixed16 decimal integer value that fit into int64: 922.
fixed8Amount := precision.Convert(balancePrecision, fixed8Precision, amount)
dec := new(accounting.Decimal) dec := new(accounting.Decimal)
dec.SetValue(amount) dec.SetValue(fixed8Amount.Int64())
dec.SetPrecision(precision) dec.SetPrecision(fixed8Precision)
res := new(accounting.BalanceResponseBody) res := new(accounting.BalanceResponseBody)
res.SetBalance(dec) res.SetBalance(dec)