Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbono3 committed Sep 1, 2021
1 parent 06d9ae5 commit ab7d6fb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
28 changes: 21 additions & 7 deletions types/valuerenderer/valuerenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"unicode"


"github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

Expand Down Expand Up @@ -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)
Expand Down
28 changes: 24 additions & 4 deletions types/valuerenderer/valuerenderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 {
Expand All @@ -67,18 +80,25 @@ func TestFormatDec(t *testing.T) {
d valuerenderer.DefaultValueRenderer
)
// TODO add more cases and error cases

tt := []struct {
name string
input types.Dec
expRes string
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},
}
Expand Down

0 comments on commit ab7d6fb

Please sign in to comment.