-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: special asset pairs bugfix and tests (#2205)
* weighted coin sorting * ++ * test weightedDecCoins total * add edge case resilience to WeightedDecCoins.Add * ++ * ++ * string methods and WeightedDecCoins Add test * lint * WeidhgtedDecCoins Sub test and edge case clarification * fix matching denom WeightedNormalPair sorting bug * test WeightedNormalPairs before() * test WeightedSpecialPairs before() * canCombine tests * WeightedSpecialPairs add test * ++ * ++ * more zero-value cases * Update x/leverage/types/string.go Co-authored-by: Robert Zaremba <[email protected]> * fix suggestion * improve wdc Sub per suggestion * move string funcs to util/sdkutil * move string funcs to util/sdkutil * simplify position stringers * stringer tests * fix test * suggestion++ Co-authored-by: Robert Zaremba <[email protected]> * suggestion++ Co-authored-by: Robert Zaremba <[email protected]> * improve efficiency using strings.Join * provide examples * lint * add negative examples to test * differing weight cases * reduce repeated verbose test cases --------- Co-authored-by: Robert Zaremba <[email protected]>
- Loading branch information
1 parent
fa4f814
commit b0ab4c4
Showing
9 changed files
with
1,135 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package sdkutil | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// FormatDec formats a sdk.Dec as a string with no trailing zeroes after the decimal point, | ||
// omitting the decimal point as well for whole numbers. | ||
// e.g. 4.000 -> 4 and 3.500 -> 3.5 | ||
func FormatDec(d sdk.Dec) string { | ||
dStr := d.String() | ||
parts := strings.Split(dStr, ".") | ||
if len(parts) != 2 { | ||
return dStr | ||
} | ||
integer, decimal := parts[0], parts[1] | ||
decimal = strings.TrimRight(decimal, "0") // no need for trailing zeros after the "." | ||
if decimal == "" { | ||
return integer | ||
} | ||
return fmt.Sprint(integer, ".", decimal) | ||
} | ||
|
||
// FormatDecCoin formats a sdk.DecCoin with no trailing zeroes after the decimal point in its amount, | ||
// omitting the decimal point as well for whole numbers. Also places a space between amount and denom. | ||
// e.g. 4.000uumee -> 4 uumee and 3.500ibc/abcd -> 3.5 ibc/abcd | ||
func FormatDecCoin(c sdk.DecCoin) string { | ||
return fmt.Sprintf("%s %s", FormatDec(c.Amount), c.Denom) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package sdkutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func TestFormatDec(t *testing.T) { | ||
type testCase struct { | ||
input string | ||
output string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
"0", | ||
"0", | ||
}, | ||
{ | ||
"1.00", | ||
"1", | ||
}, | ||
{ | ||
"1.23", | ||
"1.23", | ||
}, | ||
{ | ||
"1.500000", | ||
"1.5", | ||
}, | ||
{ | ||
"1234.567800000", | ||
"1234.5678", | ||
}, | ||
{ | ||
"-250.02130", | ||
"-250.0213", | ||
}, | ||
{ | ||
"-0.73190", | ||
"-0.7319", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
assert.Equal(t, | ||
tc.output, | ||
FormatDec(sdk.MustNewDecFromStr(tc.input)), | ||
) | ||
} | ||
} | ||
|
||
func TestFormatDecCoin(t *testing.T) { | ||
type testCase struct { | ||
amount string | ||
denom string | ||
output string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
"1.00", | ||
"AAAA", | ||
"1 AAAA", | ||
}, | ||
{ | ||
"1.23", | ||
"BBBB", | ||
"1.23 BBBB", | ||
}, | ||
{ | ||
"1.500000", | ||
"ibc/CCCC", | ||
"1.5 ibc/CCCC", | ||
}, | ||
{ | ||
"1234.567800000", | ||
"u/DDDD", | ||
"1234.5678 u/DDDD", | ||
}, | ||
{ | ||
"0", | ||
"EEEE", | ||
"0 EEEE", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
assert.Equal(t, | ||
tc.output, | ||
FormatDecCoin(sdk.NewDecCoinFromDec(tc.denom, sdk.MustNewDecFromStr(tc.amount))), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/umee-network/umee/v6/util/sdkutil" | ||
) | ||
|
||
func (ap *AccountPosition) String() string { | ||
special := []string{} | ||
normal := []string{} | ||
for _, wsp := range ap.specialPairs { | ||
special = append(special, wsp.String()) | ||
} | ||
for _, wnp := range ap.normalPairs { | ||
normal = append(normal, wnp.String()) | ||
} | ||
for _, c := range ap.unpairedCollateral { | ||
normal = append(normal, fmt.Sprintf("[%s, -]", c)) | ||
} | ||
for _, b := range ap.unpairedBorrows { | ||
normal = append(normal, fmt.Sprintf("[-, %s]", b)) | ||
} | ||
sep := "\n " | ||
return fmt.Sprint( | ||
"special:", sep, | ||
strings.Join(special, sep), | ||
"\nnormal:", sep, | ||
strings.Join(normal, sep), | ||
) | ||
} | ||
|
||
// String represents a WeightedNormalPair in the form [WeightedDecCoin, WeightedDecCoin] | ||
// e.g. [10 uumee (0.35), 3.5 uumee (0.35)] | ||
func (wnp WeightedNormalPair) String() string { | ||
return fmt.Sprintf("[%s, %s]", wnp.Collateral, wnp.Borrow) | ||
} | ||
|
||
// String represents a WeightedSpecialPair in the form [weight, DecCoin, DecCoin] | ||
// e.g. {0.35, 10 uumee, 3.5 uumee} | ||
func (wsp WeightedSpecialPair) String() string { | ||
return fmt.Sprintf( | ||
"{%s, %s, %s}", | ||
sdkutil.FormatDec(wsp.SpecialWeight), | ||
sdkutil.FormatDecCoin(wsp.Collateral), | ||
sdkutil.FormatDecCoin(wsp.Borrow), | ||
) | ||
} | ||
|
||
// String represents a WeightedDecCoin in the form coin (weight) | ||
// e.g. 10 uumee (0.35) | ||
func (wdc WeightedDecCoin) String() string { | ||
return fmt.Sprintf( | ||
"%s (%s)", | ||
sdkutil.FormatDecCoin(wdc.Asset), | ||
sdkutil.FormatDec(wdc.Weight), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.