From ab7d6fba0c7795742c205a915678d586f37d65a1 Mon Sep 17 00:00:00 2001 From: Andrei Ivasko Date: Wed, 1 Sep 2021 19:23:25 +0300 Subject: [PATCH] all tests pass --- types/valuerenderer/valuerenderer.go | 28 +++++++++++++++++------ types/valuerenderer/valuerenderer_test.go | 28 +++++++++++++++++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/types/valuerenderer/valuerenderer.go b/types/valuerenderer/valuerenderer.go index f4d98595ad60..736031881564 100644 --- a/types/valuerenderer/valuerenderer.go +++ b/types/valuerenderer/valuerenderer.go @@ -8,6 +8,7 @@ import ( "strings" "unicode" + "github.com/cosmos/cosmos-sdk/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" @@ -124,22 +125,35 @@ func (dvr DefaultValueRenderer) Format(x interface{}) (string, error) { } metadata := dvr.denomQuerier(coin.Denom) - var srcExp, dstExp int64 + var coinExp, displayExp int64 for _, denomUnit := range metadata.DenomUnits { // TODO test 23000000 mregen 3 => "regen" exp 0 if denomUnit.Denom == coin.Denom { - srcExp = int64(denomUnit.Exponent) + coinExp = int64(denomUnit.Exponent) } if denomUnit.Denom == metadata.Display { - dstExp = int64(denomUnit.Exponent) + displayExp = int64(denomUnit.Exponent) } } - exp := int64(math.Abs(float64(dstExp - srcExp))) - - amount := types.NewDecFromIntWithPrec(coin.Amount, exp).TruncateInt64() - + expSub := float64(displayExp - coinExp) + var amount int64 + + switch { + // negative , convert mregen to regen less zeroes + case math.Signbit(expSub): + // TODO or should i use math package? + amount = types.NewDecFromIntWithPrec(coin.Amount, int64(math.Abs(expSub))).TruncateInt64() // use Dec or just golang built in methods + // positive, convert mregen to uregen + case !math.Signbit(expSub): + amount = coin.Amount.Mul(types.NewInt(int64(math.Pow(10, expSub)))).Int64() + // == 0, convert regen to regen, amount does not change + default: + amount = coin.Amount.Int64() + } + + newAmount, newDenom := p.Sprintf("%d", amount), metadata.Display sb.WriteString(newAmount) sb.WriteString(newDenom) diff --git a/types/valuerenderer/valuerenderer_test.go b/types/valuerenderer/valuerenderer_test.go index 637aa57d1c8a..30c68cfa5e21 100644 --- a/types/valuerenderer/valuerenderer_test.go +++ b/types/valuerenderer/valuerenderer_test.go @@ -37,7 +37,7 @@ func TestFormatCoin(t *testing.T) { false, }, { - "convert 23000000mregen to 1000regen", + "convert 23000000mregen to 23000regen", valuerenderer.NewDefaultValueRendererWithDenom("regen"), types.NewCoin("mregen", types.NewInt(int64(23000000))), "23,000regen", @@ -47,10 +47,23 @@ func TestFormatCoin(t *testing.T) { "convert 23000000mregen to 23000000000uregen", valuerenderer.NewDefaultValueRendererWithDenom("uregen"), types.NewCoin("mregen", types.NewInt(int64(23000000))), - "23000000000uregen", + "23,000,000,000uregen", + false, + }, + { + "convert 23000000regen to 23000000000mregen", + valuerenderer.NewDefaultValueRendererWithDenom("mregen"), + types.NewCoin("regen", types.NewInt(int64(23000000))), + "23,000,000,000mregen", + false, + }, + { + "convert 23000regen to 23000regen", + valuerenderer.NewDefaultValueRendererWithDenom("regen"), + types.NewCoin("regen", types.NewInt(int64(23000))), + "23,000regen", false, }, - } for _, tc := range tt { @@ -67,6 +80,7 @@ func TestFormatDec(t *testing.T) { d valuerenderer.DefaultValueRenderer ) // TODO add more cases and error cases + tt := []struct { name string input types.Dec @@ -74,11 +88,17 @@ func TestFormatDec(t *testing.T) { expErr bool }{ { - "Decimal, no error", + "10 thousands decimal", types.NewDecFromIntWithPrec(types.NewInt(1000000), 2), // 10000.000000000000000000 "10,000.000000000000000000", false, }, + { + "10 mil decimal", + types.NewInt(10000000).ToDec(), + "10,000,000.000000000000000000", + false, + }, //{"invalid string input panic", "qwerty", "", true, true}, }