Co-authored-by: Julien Robert <julien@rbrt.fr>
This commit is contained in:
co-authored by
Julien Robert
parent
ce54aab92e
commit
d9c53bfefc
+2
-1
@@ -4,8 +4,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/collections/colltest"
|
||||
"cosmossdk.io/math"
|
||||
)
|
||||
|
||||
func TestIntValue(t *testing.T) {
|
||||
colltest.TestValueCodec(t, IntValue, NewInt(10005994859))
|
||||
colltest.TestValueCodec(t, IntValue, math.NewInt(10005994859))
|
||||
}
|
||||
|
||||
+14
-12
@@ -7,6 +7,8 @@ import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
)
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -14,7 +16,7 @@ import (
|
||||
|
||||
// NewCoin returns a new coin with a denomination and amount. It will panic if
|
||||
// the amount is negative or if the denomination is invalid.
|
||||
func NewCoin(denom string, amount Int) Coin {
|
||||
func NewCoin(denom string, amount math.Int) Coin {
|
||||
coin := Coin{
|
||||
Denom: denom,
|
||||
Amount: amount,
|
||||
@@ -30,7 +32,7 @@ func NewCoin(denom string, amount Int) Coin {
|
||||
// NewInt64Coin returns a new coin with a denomination and amount. It will panic
|
||||
// if the amount is negative.
|
||||
func NewInt64Coin(denom string, amount int64) Coin {
|
||||
return NewCoin(denom, NewInt(amount))
|
||||
return NewCoin(denom, math.NewInt(amount))
|
||||
}
|
||||
|
||||
// String provides a human-readable representation of a coin
|
||||
@@ -113,7 +115,7 @@ func (coin Coin) Add(coinB Coin) Coin {
|
||||
}
|
||||
|
||||
// AddAmount adds an amount to the Coin.
|
||||
func (coin Coin) AddAmount(amount Int) Coin {
|
||||
func (coin Coin) AddAmount(amount math.Int) Coin {
|
||||
return Coin{coin.Denom, coin.Amount.Add(amount)}
|
||||
}
|
||||
|
||||
@@ -143,7 +145,7 @@ func (coin Coin) SafeSub(coinB Coin) (Coin, error) {
|
||||
}
|
||||
|
||||
// SubAmount subtracts an amount from the Coin.
|
||||
func (coin Coin) SubAmount(amount Int) Coin {
|
||||
func (coin Coin) SubAmount(amount math.Int) Coin {
|
||||
res := Coin{coin.Denom, coin.Amount.Sub(amount)}
|
||||
if res.IsNegative() {
|
||||
panic("negative coin amount")
|
||||
@@ -337,7 +339,7 @@ func (coins Coins) safeAdd(coinsB Coins) (coalesced Coins) {
|
||||
}
|
||||
|
||||
for denom, cL := range uniqCoins { //#nosec
|
||||
comboCoin := Coin{Denom: denom, Amount: NewInt(0)}
|
||||
comboCoin := Coin{Denom: denom, Amount: math.NewInt(0)}
|
||||
for _, c := range cL {
|
||||
comboCoin = comboCoin.Add(c)
|
||||
}
|
||||
@@ -400,7 +402,7 @@ func (coins Coins) SafeSub(coinsB ...Coin) (Coins, bool) {
|
||||
// {2A, 3B} * 2 = {4A, 6B}
|
||||
// {2A} * 0 panics
|
||||
// Note, if IsValid was true on Coins, IsValid stays true.
|
||||
func (coins Coins) MulInt(x Int) Coins {
|
||||
func (coins Coins) MulInt(x math.Int) Coins {
|
||||
coins, ok := coins.SafeMulInt(x)
|
||||
if !ok {
|
||||
panic("multiplying by zero is an invalid operation on coins")
|
||||
@@ -411,7 +413,7 @@ func (coins Coins) MulInt(x Int) Coins {
|
||||
|
||||
// SafeMulInt performs the same arithmetic as MulInt but returns false
|
||||
// if the `multiplier` is zero because it makes IsValid return false.
|
||||
func (coins Coins) SafeMulInt(x Int) (Coins, bool) {
|
||||
func (coins Coins) SafeMulInt(x math.Int) (Coins, bool) {
|
||||
if x.IsZero() {
|
||||
return nil, false
|
||||
}
|
||||
@@ -434,7 +436,7 @@ func (coins Coins) SafeMulInt(x Int) (Coins, bool) {
|
||||
// {2A} / 0 = panics
|
||||
// Note, if IsValid was true on Coins, IsValid stays true,
|
||||
// unless the `divisor` is greater than the smallest coin amount.
|
||||
func (coins Coins) QuoInt(x Int) Coins {
|
||||
func (coins Coins) QuoInt(x math.Int) Coins {
|
||||
coins, ok := coins.SafeQuoInt(x)
|
||||
if !ok {
|
||||
panic("dividing by zero is an invalid operation on coins")
|
||||
@@ -445,7 +447,7 @@ func (coins Coins) QuoInt(x Int) Coins {
|
||||
|
||||
// SafeQuoInt performs the same arithmetic as QuoInt but returns an error
|
||||
// if the division cannot be done.
|
||||
func (coins Coins) SafeQuoInt(x Int) (Coins, bool) {
|
||||
func (coins Coins) SafeQuoInt(x math.Int) (Coins, bool) {
|
||||
if x.IsZero() {
|
||||
return nil, false
|
||||
}
|
||||
@@ -681,18 +683,18 @@ func (coins Coins) Empty() bool {
|
||||
}
|
||||
|
||||
// AmountOf returns the amount of a denom from coins
|
||||
func (coins Coins) AmountOf(denom string) Int {
|
||||
func (coins Coins) AmountOf(denom string) math.Int {
|
||||
mustValidateDenom(denom)
|
||||
return coins.AmountOfNoDenomValidation(denom)
|
||||
}
|
||||
|
||||
// AmountOfNoDenomValidation returns the amount of a denom from coins
|
||||
// without validating the denomination.
|
||||
func (coins Coins) AmountOfNoDenomValidation(denom string) Int {
|
||||
func (coins Coins) AmountOfNoDenomValidation(denom string) math.Int {
|
||||
if ok, c := coins.Find(denom); ok {
|
||||
return c.Amount
|
||||
}
|
||||
return ZeroInt()
|
||||
return math.ZeroInt()
|
||||
}
|
||||
|
||||
// Find returns true and coin if the denom exists in coins. Otherwise it returns false
|
||||
|
||||
+24
-21
@@ -4,6 +4,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
cosmossdk_io_math "cosmossdk.io/math"
|
||||
fmt "fmt"
|
||||
_ "github.com/cosmos/cosmos-proto"
|
||||
_ "github.com/cosmos/cosmos-sdk/types/tx/amino"
|
||||
@@ -30,8 +31,8 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
|
||||
// NOTE: The amount field is an Int which implements the custom method
|
||||
// signatures required by gogoproto.
|
||||
type Coin struct {
|
||||
Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"`
|
||||
Amount Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Int" json:"amount"`
|
||||
Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"`
|
||||
Amount cosmossdk_io_math.Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=cosmossdk.io/math.Int" json:"amount"`
|
||||
}
|
||||
|
||||
func (m *Coin) Reset() { *m = Coin{} }
|
||||
@@ -78,8 +79,8 @@ func (m *Coin) GetDenom() string {
|
||||
// NOTE: The amount field is an Dec which implements the custom method
|
||||
// signatures required by gogoproto.
|
||||
type DecCoin struct {
|
||||
Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"`
|
||||
Amount Dec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=Dec" json:"amount"`
|
||||
Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty"`
|
||||
Amount cosmossdk_io_math.LegacyDec `protobuf:"bytes,2,opt,name=amount,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"amount"`
|
||||
}
|
||||
|
||||
func (m *DecCoin) Reset() { *m = DecCoin{} }
|
||||
@@ -123,7 +124,7 @@ func (m *DecCoin) GetDenom() string {
|
||||
|
||||
// IntProto defines a Protobuf wrapper around an Int object.
|
||||
type IntProto struct {
|
||||
Int Int `protobuf:"bytes,1,opt,name=int,proto3,customtype=Int" json:"int"`
|
||||
Int cosmossdk_io_math.Int `protobuf:"bytes,1,opt,name=int,proto3,customtype=cosmossdk.io/math.Int" json:"int"`
|
||||
}
|
||||
|
||||
func (m *IntProto) Reset() { *m = IntProto{} }
|
||||
@@ -160,7 +161,7 @@ var xxx_messageInfo_IntProto proto.InternalMessageInfo
|
||||
|
||||
// DecProto defines a Protobuf wrapper around a Dec object.
|
||||
type DecProto struct {
|
||||
Dec Dec `protobuf:"bytes,1,opt,name=dec,proto3,customtype=Dec" json:"dec"`
|
||||
Dec cosmossdk_io_math.LegacyDec `protobuf:"bytes,1,opt,name=dec,proto3,customtype=cosmossdk.io/math.LegacyDec" json:"dec"`
|
||||
}
|
||||
|
||||
func (m *DecProto) Reset() { *m = DecProto{} }
|
||||
@@ -205,27 +206,29 @@ func init() {
|
||||
func init() { proto.RegisterFile("cosmos/base/v1beta1/coin.proto", fileDescriptor_189a96714eafc2df) }
|
||||
|
||||
var fileDescriptor_189a96714eafc2df = []byte{
|
||||
// 312 bytes of a gzipped FileDescriptorProto
|
||||
// 341 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4b, 0xce, 0x2f, 0xce,
|
||||
0xcd, 0x2f, 0xd6, 0x4f, 0x4a, 0x2c, 0x4e, 0xd5, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34, 0xd4,
|
||||
0x4f, 0xce, 0xcf, 0xcc, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x86, 0xc8, 0xeb, 0x81,
|
||||
0xe4, 0xf5, 0xa0, 0xf2, 0x52, 0x22, 0xe9, 0xf9, 0xe9, 0xf9, 0x60, 0x79, 0x7d, 0x10, 0x0b, 0xa2,
|
||||
0x54, 0x4a, 0x12, 0xa2, 0x34, 0x1e, 0x22, 0x01, 0xd5, 0x07, 0x91, 0x12, 0x4c, 0xcc, 0xcd, 0xcc,
|
||||
0xcb, 0xd7, 0x07, 0x93, 0x10, 0x21, 0xa5, 0x28, 0x2e, 0x16, 0xe7, 0xfc, 0xcc, 0x3c, 0x21, 0x11,
|
||||
0xcb, 0xd7, 0x07, 0x93, 0x10, 0x21, 0xa5, 0x1c, 0x2e, 0x16, 0xe7, 0xfc, 0xcc, 0x3c, 0x21, 0x11,
|
||||
0x2e, 0xd6, 0x94, 0xd4, 0xbc, 0xfc, 0x5c, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, 0x08, 0x47,
|
||||
0xc8, 0x8c, 0x8b, 0x2d, 0x31, 0x37, 0xbf, 0x34, 0xaf, 0x44, 0x82, 0x09, 0x24, 0xec, 0x24, 0x77,
|
||||
0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xcc, 0x9e, 0x79, 0x25, 0x97, 0xb6, 0xe8, 0x72, 0x41,
|
||||
0x4d, 0xf7, 0xcc, 0x2b, 0x59, 0xf1, 0x7c, 0x83, 0x16, 0x63, 0x10, 0x54, 0xb5, 0x15, 0xcb, 0x8b,
|
||||
0x05, 0xf2, 0x8c, 0x4a, 0x11, 0x5c, 0xec, 0x2e, 0xa9, 0xc9, 0x78, 0x8c, 0x37, 0x44, 0x33, 0x5e,
|
||||
0x12, 0x66, 0xbc, 0x4b, 0x6a, 0x32, 0x92, 0xf1, 0x2e, 0xa9, 0xc9, 0x68, 0x26, 0x9b, 0x73, 0x71,
|
||||
0x78, 0xe6, 0x95, 0x04, 0x80, 0x83, 0x46, 0x9b, 0x8b, 0x39, 0x33, 0xaf, 0x04, 0x62, 0x30, 0xc2,
|
||||
0x04, 0x0c, 0x07, 0x06, 0x81, 0x54, 0x81, 0x34, 0xba, 0xa4, 0x26, 0xc3, 0x35, 0xa6, 0xa4, 0x26,
|
||||
0xa3, 0x6b, 0xc4, 0xb4, 0x1a, 0xa4, 0xca, 0xc9, 0xe5, 0xc6, 0x43, 0x39, 0x86, 0x86, 0x47, 0x72,
|
||||
0x0c, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7,
|
||||
0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0xa5, 0x94, 0x9e, 0x59, 0x92,
|
||||
0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0x0b, 0x0d, 0x75, 0x28, 0xa5, 0x5b, 0x9c, 0x92, 0xad, 0x5f,
|
||||
0x52, 0x59, 0x90, 0x5a, 0x9c, 0xc4, 0x06, 0x0e, 0x74, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff,
|
||||
0x33, 0xcc, 0xd5, 0x87, 0xef, 0x01, 0x00, 0x00,
|
||||
0xc8, 0x83, 0x8b, 0x2d, 0x31, 0x37, 0xbf, 0x34, 0xaf, 0x44, 0x82, 0x09, 0x24, 0xec, 0x64, 0x70,
|
||||
0xe2, 0x9e, 0x3c, 0xc3, 0xad, 0x7b, 0xf2, 0xa2, 0x10, 0x63, 0x8b, 0x53, 0xb2, 0xf5, 0x32, 0xf3,
|
||||
0xf5, 0x73, 0x13, 0x4b, 0x32, 0xf4, 0x3c, 0xf3, 0x4a, 0x2e, 0x6d, 0xd1, 0xe5, 0x82, 0xda, 0xe7,
|
||||
0x99, 0x57, 0xb2, 0xe2, 0xf9, 0x06, 0x2d, 0xc6, 0x20, 0xa8, 0x7e, 0x2b, 0x96, 0x17, 0x0b, 0xe4,
|
||||
0x19, 0x95, 0x0a, 0xb8, 0xd8, 0x5d, 0x52, 0x93, 0xf1, 0x58, 0xe8, 0x89, 0x66, 0xa1, 0x21, 0xd4,
|
||||
0x42, 0x69, 0x4c, 0x0b, 0x7d, 0x52, 0xd3, 0x13, 0x93, 0x2b, 0x5d, 0x52, 0x93, 0x91, 0xac, 0x75,
|
||||
0x49, 0x4d, 0x46, 0xb3, 0xd1, 0x93, 0x8b, 0xc3, 0x33, 0xaf, 0x24, 0x00, 0x1c, 0x88, 0xb6, 0x5c,
|
||||
0xcc, 0x99, 0x79, 0x25, 0x10, 0x0b, 0x9d, 0xb4, 0x49, 0xf0, 0x4a, 0x10, 0x48, 0x9f, 0x92, 0x3f,
|
||||
0x17, 0x87, 0x4b, 0x6a, 0x32, 0xc4, 0x28, 0x67, 0x2e, 0xe6, 0x94, 0xd4, 0x64, 0xa8, 0x51, 0x64,
|
||||
0x38, 0x12, 0xa4, 0xdb, 0xc9, 0xe5, 0xc6, 0x43, 0x39, 0x86, 0x86, 0x47, 0x72, 0x0c, 0x27, 0x1e,
|
||||
0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17,
|
||||
0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0xa5, 0x94, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4,
|
||||
0x97, 0x9c, 0x9f, 0x0b, 0x8d, 0x49, 0x28, 0xa5, 0x5b, 0x9c, 0x92, 0xad, 0x5f, 0x52, 0x59, 0x90,
|
||||
0x5a, 0x9c, 0xc4, 0x06, 0x8e, 0x48, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xd2, 0x0a, 0x5e,
|
||||
0xb8, 0x43, 0x02, 0x00, 0x00,
|
||||
}
|
||||
|
||||
func (this *Coin) Equal(that interface{}) bool {
|
||||
|
||||
@@ -3,6 +3,8 @@ package types
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
)
|
||||
|
||||
func coinName(suffix int) string {
|
||||
@@ -18,10 +20,10 @@ func BenchmarkCoinsAdditionIntersect(b *testing.B) {
|
||||
coinsB := Coins(make([]Coin, numCoinsB))
|
||||
|
||||
for i := 0; i < numCoinsA; i++ {
|
||||
coinsA[i] = NewCoin(coinName(i), NewInt(int64(i)))
|
||||
coinsA[i] = NewCoin(coinName(i), math.NewInt(int64(i)))
|
||||
}
|
||||
for i := 0; i < numCoinsB; i++ {
|
||||
coinsB[i] = NewCoin(coinName(i), NewInt(int64(i)))
|
||||
coinsB[i] = NewCoin(coinName(i), math.NewInt(int64(i)))
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
@@ -49,10 +51,10 @@ func BenchmarkCoinsAdditionNoIntersect(b *testing.B) {
|
||||
coinsB := Coins(make([]Coin, numCoinsB))
|
||||
|
||||
for i := 0; i < numCoinsA; i++ {
|
||||
coinsA[i] = NewCoin(coinName(numCoinsB+i), NewInt(int64(i)))
|
||||
coinsA[i] = NewCoin(coinName(numCoinsB+i), math.NewInt(int64(i)))
|
||||
}
|
||||
for i := 0; i < numCoinsB; i++ {
|
||||
coinsB[i] = NewCoin(coinName(i), NewInt(int64(i)))
|
||||
coinsB[i] = NewCoin(coinName(i), math.NewInt(int64(i)))
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
@@ -84,7 +86,7 @@ func BenchmarkSumOfCoinAdds(b *testing.B) {
|
||||
|
||||
for i := 0; i < numAdds; i++ {
|
||||
intersectCoins := make([]Coin, numIntersectingCoins)
|
||||
num := NewInt(int64(i))
|
||||
num := math.NewInt(int64(i))
|
||||
for j := 0; j < numIntersectingCoins; j++ {
|
||||
intersectCoins[j] = NewCoin(coinName(j+1_000_000_000), num)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
)
|
||||
|
||||
func TestCoinTestSuite(t *testing.T) {
|
||||
@@ -15,7 +17,7 @@ type coinInternalSuite struct {
|
||||
}
|
||||
|
||||
func (s *coinInternalSuite) TestIsSorted() {
|
||||
v := NewInt(1)
|
||||
v := math.NewInt(1)
|
||||
cases := []struct {
|
||||
coins Coins
|
||||
expected bool
|
||||
|
||||
@@ -137,10 +137,10 @@ func (i intValueCodec) Encode(value math.Int) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (i intValueCodec) Decode(b []byte) (math.Int, error) {
|
||||
v := new(Int)
|
||||
v := new(math.Int)
|
||||
err := v.Unmarshal(b)
|
||||
if err != nil {
|
||||
return Int{}, err
|
||||
return math.Int{}, err
|
||||
}
|
||||
return *v, nil
|
||||
}
|
||||
@@ -149,16 +149,16 @@ func (i intValueCodec) EncodeJSON(value math.Int) ([]byte, error) {
|
||||
return value.MarshalJSON()
|
||||
}
|
||||
|
||||
func (i intValueCodec) DecodeJSON(b []byte) (Int, error) {
|
||||
v := new(Int)
|
||||
func (i intValueCodec) DecodeJSON(b []byte) (math.Int, error) {
|
||||
v := new(math.Int)
|
||||
err := v.UnmarshalJSON(b)
|
||||
if err != nil {
|
||||
return Int{}, err
|
||||
return math.Int{}, err
|
||||
}
|
||||
return *v, nil
|
||||
}
|
||||
|
||||
func (i intValueCodec) Stringify(value Int) string {
|
||||
func (i intValueCodec) Stringify(value math.Int) string {
|
||||
return value.String()
|
||||
}
|
||||
|
||||
|
||||
+12
-12
@@ -13,17 +13,17 @@ import (
|
||||
// Decimal Coin
|
||||
|
||||
// NewDecCoin creates a new DecCoin instance from an Int.
|
||||
func NewDecCoin(denom string, amount Int) DecCoin {
|
||||
func NewDecCoin(denom string, amount math.Int) DecCoin {
|
||||
coin := NewCoin(denom, amount)
|
||||
|
||||
return DecCoin{
|
||||
Denom: coin.Denom,
|
||||
Amount: NewDecFromInt(coin.Amount),
|
||||
Amount: math.LegacyNewDecFromInt(coin.Amount),
|
||||
}
|
||||
}
|
||||
|
||||
// NewDecCoinFromDec creates a new DecCoin instance from a Dec.
|
||||
func NewDecCoinFromDec(denom string, amount Dec) DecCoin {
|
||||
func NewDecCoinFromDec(denom string, amount math.LegacyDec) DecCoin {
|
||||
mustValidateDenom(denom)
|
||||
|
||||
if amount.IsNegative() {
|
||||
@@ -44,14 +44,14 @@ func NewDecCoinFromCoin(coin Coin) DecCoin {
|
||||
|
||||
return DecCoin{
|
||||
Denom: coin.Denom,
|
||||
Amount: NewDecFromInt(coin.Amount),
|
||||
Amount: math.LegacyNewDecFromInt(coin.Amount),
|
||||
}
|
||||
}
|
||||
|
||||
// NewInt64DecCoin returns a new DecCoin with a denomination and amount. It will
|
||||
// panic if the amount is negative or denom is invalid.
|
||||
func NewInt64DecCoin(denom string, amount int64) DecCoin {
|
||||
return NewDecCoin(denom, NewInt(amount))
|
||||
return NewDecCoin(denom, math.NewInt(amount))
|
||||
}
|
||||
|
||||
// IsZero returns if the DecCoin amount is zero.
|
||||
@@ -109,7 +109,7 @@ func (coin DecCoin) Sub(coinB DecCoin) DecCoin {
|
||||
// change. Note, the change may be zero.
|
||||
func (coin DecCoin) TruncateDecimal() (Coin, DecCoin) {
|
||||
truncated := coin.Amount.TruncateInt()
|
||||
change := coin.Amount.Sub(NewDecFromInt(truncated))
|
||||
change := coin.Amount.Sub(math.LegacyNewDecFromInt(truncated))
|
||||
return NewCoin(coin.Denom, truncated), NewDecCoinFromDec(coin.Denom, change)
|
||||
}
|
||||
|
||||
@@ -359,7 +359,7 @@ func (coins DecCoins) IsAnyNegative() bool {
|
||||
// MulDec multiplies all the coins by a decimal.
|
||||
//
|
||||
// CONTRACT: No zero coins will be returned.
|
||||
func (coins DecCoins) MulDec(d Dec) DecCoins {
|
||||
func (coins DecCoins) MulDec(d math.LegacyDec) DecCoins {
|
||||
var res DecCoins
|
||||
for _, coin := range coins {
|
||||
product := DecCoin{
|
||||
@@ -379,7 +379,7 @@ func (coins DecCoins) MulDec(d Dec) DecCoins {
|
||||
// returns nil DecCoins if d is zero.
|
||||
//
|
||||
// CONTRACT: No zero coins will be returned.
|
||||
func (coins DecCoins) MulDecTruncate(d Dec) DecCoins {
|
||||
func (coins DecCoins) MulDecTruncate(d math.LegacyDec) DecCoins {
|
||||
if d.IsZero() {
|
||||
return DecCoins{}
|
||||
}
|
||||
@@ -402,7 +402,7 @@ func (coins DecCoins) MulDecTruncate(d Dec) DecCoins {
|
||||
// QuoDec divides all the decimal coins by a decimal. It panics if d is zero.
|
||||
//
|
||||
// CONTRACT: No zero coins will be returned.
|
||||
func (coins DecCoins) QuoDec(d Dec) DecCoins {
|
||||
func (coins DecCoins) QuoDec(d math.LegacyDec) DecCoins {
|
||||
if d.IsZero() {
|
||||
panic("invalid zero decimal")
|
||||
}
|
||||
@@ -426,7 +426,7 @@ func (coins DecCoins) QuoDec(d Dec) DecCoins {
|
||||
// panics if d is zero.
|
||||
//
|
||||
// CONTRACT: No zero coins will be returned.
|
||||
func (coins DecCoins) QuoDecTruncate(d Dec) DecCoins {
|
||||
func (coins DecCoins) QuoDecTruncate(d math.LegacyDec) DecCoins {
|
||||
if d.IsZero() {
|
||||
panic("invalid zero decimal")
|
||||
}
|
||||
@@ -452,7 +452,7 @@ func (coins DecCoins) Empty() bool {
|
||||
}
|
||||
|
||||
// AmountOf returns the amount of a denom from deccoins
|
||||
func (coins DecCoins) AmountOf(denom string) Dec {
|
||||
func (coins DecCoins) AmountOf(denom string) math.LegacyDec {
|
||||
mustValidateDenom(denom)
|
||||
|
||||
switch len(coins) {
|
||||
@@ -628,7 +628,7 @@ func ParseDecCoin(coinStr string) (coin DecCoin, err error) {
|
||||
|
||||
amountStr, denomStr := matches[1], matches[2]
|
||||
|
||||
amount, err := NewDecFromStr(amountStr)
|
||||
amount, err := math.LegacyNewDecFromStr(amountStr)
|
||||
if err != nil {
|
||||
return DecCoin{}, errors.Wrap(err, fmt.Sprintf("failed to parse decimal coin amount: %s", amountStr))
|
||||
}
|
||||
|
||||
+41
-41
@@ -73,9 +73,9 @@ func (s *decCoinTestSuite) TestDecCoinIsPositive() {
|
||||
}
|
||||
|
||||
func (s *decCoinTestSuite) TestAddDecCoin() {
|
||||
decCoinA1 := sdk.NewDecCoinFromDec(testDenom1, sdk.NewDecWithPrec(11, 1))
|
||||
decCoinA2 := sdk.NewDecCoinFromDec(testDenom1, sdk.NewDecWithPrec(22, 1))
|
||||
decCoinB1 := sdk.NewDecCoinFromDec(testDenom2, sdk.NewDecWithPrec(11, 1))
|
||||
decCoinA1 := sdk.NewDecCoinFromDec(testDenom1, math.LegacyNewDecWithPrec(11, 1))
|
||||
decCoinA2 := sdk.NewDecCoinFromDec(testDenom1, math.LegacyNewDecWithPrec(22, 1))
|
||||
decCoinB1 := sdk.NewDecCoinFromDec(testDenom2, math.LegacyNewDecWithPrec(11, 1))
|
||||
|
||||
// regular add
|
||||
res := decCoinA1.Add(decCoinA1)
|
||||
@@ -376,36 +376,36 @@ func (s *decCoinTestSuite) TestParseDecCoins() {
|
||||
expectedErr bool
|
||||
}{
|
||||
{"", nil, false},
|
||||
{"4stake", sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecFromInt(math.NewInt(4)))}, false},
|
||||
{"4stake", sdk.DecCoins{sdk.NewDecCoinFromDec("stake", math.LegacyNewDecFromInt(math.NewInt(4)))}, false},
|
||||
{"5.5atom,4stake", sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5500000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("atom", math.LegacyNewDecWithPrec(5500000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(4)),
|
||||
}, false},
|
||||
{"0.0stake", sdk.DecCoins{}, false}, // remove zero coins
|
||||
{"10.0btc,1.0atom,20.0btc", nil, true},
|
||||
{
|
||||
"0.004STAKE",
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec("STAKE", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision))},
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec("STAKE", math.LegacyNewDecWithPrec(4000000000000000, math.LegacyPrecision))},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"0.004stake",
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision))},
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec("stake", math.LegacyNewDecWithPrec(4000000000000000, math.LegacyPrecision))},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"5.04atom,0.004stake",
|
||||
sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("atom", math.LegacyNewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDecWithPrec(4000000000000000, math.LegacyPrecision)),
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"0.0stake,0.004stake,5.04atom", // remove zero coins
|
||||
sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("atom", math.LegacyNewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDecWithPrec(4000000000000000, math.LegacyPrecision)),
|
||||
},
|
||||
false,
|
||||
},
|
||||
@@ -430,8 +430,8 @@ func (s *decCoinTestSuite) TestDecCoinsString() {
|
||||
{sdk.DecCoins{}, ""},
|
||||
{
|
||||
sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("atom", sdk.NewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", sdk.NewDecWithPrec(4000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("atom", math.LegacyNewDecWithPrec(5040000000000000000, math.LegacyPrecision)),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDecWithPrec(4000000000000000, math.LegacyPrecision)),
|
||||
},
|
||||
"5.040000000000000000atom,0.004000000000000000stake",
|
||||
},
|
||||
@@ -474,8 +474,8 @@ func (s *decCoinTestSuite) TestDecCoinsIntersect() {
|
||||
}
|
||||
|
||||
func (s *decCoinTestSuite) TestDecCoinsTruncateDecimal() {
|
||||
decCoinA := sdk.NewDecCoinFromDec("bar", sdk.MustNewDecFromStr("5.41"))
|
||||
decCoinB := sdk.NewDecCoinFromDec("foo", sdk.MustNewDecFromStr("6.00"))
|
||||
decCoinA := sdk.NewDecCoinFromDec("bar", math.LegacyMustNewDecFromStr("5.41"))
|
||||
decCoinB := sdk.NewDecCoinFromDec("foo", math.LegacyMustNewDecFromStr("6.00"))
|
||||
|
||||
testCases := []struct {
|
||||
input sdk.DecCoins
|
||||
@@ -486,7 +486,7 @@ func (s *decCoinTestSuite) TestDecCoinsTruncateDecimal() {
|
||||
{
|
||||
sdk.DecCoins{decCoinA, decCoinB},
|
||||
sdk.Coins{sdk.NewInt64Coin(decCoinA.Denom, 5), sdk.NewInt64Coin(decCoinB.Denom, 6)},
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec(decCoinA.Denom, sdk.MustNewDecFromStr("0.41"))},
|
||||
sdk.DecCoins{sdk.NewDecCoinFromDec(decCoinA.Denom, math.LegacyMustNewDecFromStr("0.41"))},
|
||||
},
|
||||
{
|
||||
sdk.DecCoins{decCoinB},
|
||||
@@ -509,8 +509,8 @@ func (s *decCoinTestSuite) TestDecCoinsTruncateDecimal() {
|
||||
}
|
||||
|
||||
func (s *decCoinTestSuite) TestDecCoinsQuoDecTruncate() {
|
||||
x := sdk.MustNewDecFromStr("1.00")
|
||||
y := sdk.MustNewDecFromStr("10000000000000000000.00")
|
||||
x := math.LegacyMustNewDecFromStr("1.00")
|
||||
y := math.LegacyMustNewDecFromStr("10000000000000000000.00")
|
||||
|
||||
testCases := []struct {
|
||||
coins sdk.DecCoins
|
||||
@@ -520,7 +520,7 @@ func (s *decCoinTestSuite) TestDecCoinsQuoDecTruncate() {
|
||||
}{
|
||||
{sdk.DecCoins{}, math.LegacyZeroDec(), sdk.DecCoins(nil), true},
|
||||
{sdk.DecCoins{sdk.NewDecCoinFromDec("foo", x)}, y, sdk.DecCoins(nil), false},
|
||||
{sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)}, math.LegacyNewDec(2), sdk.DecCoins{sdk.NewDecCoinFromDec("foo", sdk.MustNewDecFromStr("2.5"))}, false},
|
||||
{sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)}, math.LegacyNewDec(2), sdk.DecCoins{sdk.NewDecCoinFromDec("foo", math.LegacyMustNewDecFromStr("2.5"))}, false},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
@@ -847,7 +847,7 @@ func (s *decCoinTestSuite) TestDecCoins_MulDec() {
|
||||
testCases := []struct {
|
||||
name string
|
||||
coins sdk.DecCoins
|
||||
multiplier sdk.Dec
|
||||
multiplier math.LegacyDec
|
||||
expectedResult sdk.DecCoins
|
||||
}{
|
||||
{"No Coins", sdk.DecCoins{}, math.LegacyNewDec(1), sdk.DecCoins(nil)},
|
||||
@@ -899,39 +899,39 @@ func (s *decCoinTestSuite) TestDecCoins_MulDecTruncate() {
|
||||
testCases := []struct {
|
||||
name string
|
||||
coins sdk.DecCoins
|
||||
multiplier sdk.Dec
|
||||
multiplier math.LegacyDec
|
||||
expectedResult sdk.DecCoins
|
||||
expectedPanic bool
|
||||
}{
|
||||
{"No Coins", sdk.DecCoins{}, math.LegacyNewDec(1), sdk.DecCoins(nil), false},
|
||||
|
||||
{"Multiple coins - zero multiplier", sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(10, 3)},
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(30, 2)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(10, 3)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(30, 2)},
|
||||
}, math.LegacyNewDec(0), sdk.DecCoins{}, false},
|
||||
|
||||
{"Multiple coins - positive multiplier", sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(15, 1)},
|
||||
}, math.LegacyNewDec(1), sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(3, 0)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(3, 0)},
|
||||
}, false},
|
||||
|
||||
{"Multiple coins - positive multiplier", sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(15, 1)},
|
||||
}, math.LegacyNewDec(-2), sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(-6, 0)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(-6, 0)},
|
||||
}, false},
|
||||
|
||||
{"Multiple coins - Different denom", sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom2, sdk.NewDecWithPrec(3333, 4)},
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom2, sdk.NewDecWithPrec(333, 4)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom2, math.LegacyNewDecWithPrec(3333, 4)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(15, 1)},
|
||||
sdk.DecCoin{testDenom2, math.LegacyNewDecWithPrec(333, 4)},
|
||||
}, math.LegacyNewDec(10), sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(30, 0)},
|
||||
sdk.DecCoin{testDenom2, sdk.NewDecWithPrec(3666, 3)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(30, 0)},
|
||||
sdk.DecCoin{testDenom2, math.LegacyNewDecWithPrec(3666, 3)},
|
||||
}, false},
|
||||
}
|
||||
|
||||
@@ -952,7 +952,7 @@ func (s *decCoinTestSuite) TestDecCoins_QuoDec() {
|
||||
testCases := []struct {
|
||||
name string
|
||||
coins sdk.DecCoins
|
||||
input sdk.Dec
|
||||
input math.LegacyDec
|
||||
expectedResult sdk.DecCoins
|
||||
panics bool
|
||||
}{
|
||||
@@ -967,14 +967,14 @@ func (s *decCoinTestSuite) TestDecCoins_QuoDec() {
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDec(3)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDec(4)},
|
||||
}, math.LegacyNewDec(2), sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(35, 1)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(35, 1)},
|
||||
}, false},
|
||||
|
||||
{"Multiple coins - negative input", sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDec(3)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDec(4)},
|
||||
}, math.LegacyNewDec(-2), sdk.DecCoins{
|
||||
sdk.DecCoin{testDenom1, sdk.NewDecWithPrec(-35, 1)},
|
||||
sdk.DecCoin{testDenom1, math.LegacyNewDecWithPrec(-35, 1)},
|
||||
}, false},
|
||||
|
||||
{"Multiple coins - Different input", sdk.DecCoins{
|
||||
@@ -1144,11 +1144,11 @@ func (s *decCoinTestSuite) TestDecCoin_ParseDecCoin() {
|
||||
|
||||
{"Precision over limit", "9.11111111111111111111stake", empty, true},
|
||||
|
||||
{"Valid upper case denom", "9.3STAKE", sdk.DecCoin{"STAKE", sdk.NewDecWithPrec(93, 1)}, false},
|
||||
{"Valid upper case denom", "9.3STAKE", sdk.DecCoin{"STAKE", math.LegacyNewDecWithPrec(93, 1)}, false},
|
||||
|
||||
{"Valid input - amount and denom separated by space", "9.3 stake", sdk.DecCoin{"stake", sdk.NewDecWithPrec(93, 1)}, false},
|
||||
{"Valid input - amount and denom separated by space", "9.3 stake", sdk.DecCoin{"stake", math.LegacyNewDecWithPrec(93, 1)}, false},
|
||||
|
||||
{"Valid input - amount and denom concatenated", "9.3stake", sdk.DecCoin{"stake", sdk.NewDecWithPrec(93, 1)}, false},
|
||||
{"Valid input - amount and denom concatenated", "9.3stake", sdk.DecCoin{"stake", math.LegacyNewDecWithPrec(93, 1)}, false},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
|
||||
+4
-4
@@ -8,14 +8,14 @@ import (
|
||||
|
||||
// denomUnits contains a mapping of denomination mapped to their respective unit
|
||||
// multipliers (e.g. 1atom = 10^-6uatom).
|
||||
var denomUnits = map[string]Dec{}
|
||||
var denomUnits = map[string]math.LegacyDec{}
|
||||
|
||||
// baseDenom is the denom of smallest unit registered
|
||||
var baseDenom string
|
||||
|
||||
// RegisterDenom registers a denomination with a corresponding unit. If the
|
||||
// denomination is already registered, an error will be returned.
|
||||
func RegisterDenom(denom string, unit Dec) error {
|
||||
func RegisterDenom(denom string, unit math.LegacyDec) error {
|
||||
if err := ValidateDenom(denom); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func RegisterDenom(denom string, unit Dec) error {
|
||||
|
||||
// GetDenomUnit returns a unit for a given denomination if it exists. A boolean
|
||||
// is returned if the denomination is registered.
|
||||
func GetDenomUnit(denom string) (Dec, bool) {
|
||||
func GetDenomUnit(denom string) (math.LegacyDec, bool) {
|
||||
if err := ValidateDenom(denom); err != nil {
|
||||
return math.LegacyZeroDec(), false
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func ConvertCoin(coin Coin, denom string) (Coin, error) {
|
||||
return NewCoin(denom, coin.Amount), nil
|
||||
}
|
||||
|
||||
return NewCoin(denom, NewDecFromInt(coin.Amount).Mul(srcUnit).Quo(dstUnit).TruncateInt()), nil
|
||||
return NewCoin(denom, math.LegacyNewDecFromInt(coin.Amount).Mul(srcUnit).Quo(dstUnit).TruncateInt()), nil
|
||||
}
|
||||
|
||||
// ConvertDecCoin attempts to convert a decimal coin to a given denomination. If the given
|
||||
|
||||
@@ -46,35 +46,35 @@ func (s *internalDenomTestSuite) TestRegisterDenom() {
|
||||
|
||||
// reset registration
|
||||
baseDenom = ""
|
||||
denomUnits = map[string]Dec{}
|
||||
denomUnits = map[string]math.LegacyDec{}
|
||||
}
|
||||
|
||||
func (s *internalDenomTestSuite) TestConvertCoins() {
|
||||
atomUnit := math.LegacyOneDec() // 1 (base denom unit)
|
||||
s.Require().NoError(RegisterDenom(atom, atomUnit))
|
||||
|
||||
matomUnit := NewDecWithPrec(1, 3) // 10^-3 (milli)
|
||||
matomUnit := math.LegacyNewDecWithPrec(1, 3) // 10^-3 (milli)
|
||||
s.Require().NoError(RegisterDenom(matom, matomUnit))
|
||||
|
||||
uatomUnit := NewDecWithPrec(1, 6) // 10^-6 (micro)
|
||||
uatomUnit := math.LegacyNewDecWithPrec(1, 6) // 10^-6 (micro)
|
||||
s.Require().NoError(RegisterDenom(uatom, uatomUnit))
|
||||
|
||||
natomUnit := NewDecWithPrec(1, 9) // 10^-9 (nano)
|
||||
natomUnit := math.LegacyNewDecWithPrec(1, 9) // 10^-9 (nano)
|
||||
s.Require().NoError(RegisterDenom(natom, natomUnit))
|
||||
|
||||
res, err := GetBaseDenom()
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(res, natom)
|
||||
s.Require().Equal(NormalizeCoin(NewCoin(uatom, NewInt(1))), NewCoin(natom, NewInt(1000)))
|
||||
s.Require().Equal(NormalizeCoin(NewCoin(matom, NewInt(1))), NewCoin(natom, NewInt(1000000)))
|
||||
s.Require().Equal(NormalizeCoin(NewCoin(atom, NewInt(1))), NewCoin(natom, NewInt(1000000000)))
|
||||
s.Require().Equal(NormalizeCoin(NewCoin(uatom, math.NewInt(1))), NewCoin(natom, math.NewInt(1000)))
|
||||
s.Require().Equal(NormalizeCoin(NewCoin(matom, math.NewInt(1))), NewCoin(natom, math.NewInt(1000000)))
|
||||
s.Require().Equal(NormalizeCoin(NewCoin(atom, math.NewInt(1))), NewCoin(natom, math.NewInt(1000000000)))
|
||||
|
||||
coins, err := ParseCoinsNormalized("1atom,1matom,1uatom")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(coins, Coins{
|
||||
Coin{natom, NewInt(1000000000)},
|
||||
Coin{natom, NewInt(1000000)},
|
||||
Coin{natom, NewInt(1000)},
|
||||
Coin{natom, math.NewInt(1000000000)},
|
||||
Coin{natom, math.NewInt(1000000)},
|
||||
Coin{natom, math.NewInt(1000)},
|
||||
})
|
||||
|
||||
testCases := []struct {
|
||||
@@ -83,20 +83,20 @@ func (s *internalDenomTestSuite) TestConvertCoins() {
|
||||
result Coin
|
||||
expErr bool
|
||||
}{
|
||||
{NewCoin("foo", ZeroInt()), atom, Coin{}, true},
|
||||
{NewCoin(atom, ZeroInt()), "foo", Coin{}, true},
|
||||
{NewCoin(atom, ZeroInt()), "FOO", Coin{}, true},
|
||||
{NewCoin("foo", math.ZeroInt()), atom, Coin{}, true},
|
||||
{NewCoin(atom, math.ZeroInt()), "foo", Coin{}, true},
|
||||
{NewCoin(atom, math.ZeroInt()), "FOO", Coin{}, true},
|
||||
|
||||
{NewCoin(atom, NewInt(5)), matom, NewCoin(matom, NewInt(5000)), false}, // atom => matom
|
||||
{NewCoin(atom, NewInt(5)), uatom, NewCoin(uatom, NewInt(5000000)), false}, // atom => uatom
|
||||
{NewCoin(atom, NewInt(5)), natom, NewCoin(natom, NewInt(5000000000)), false}, // atom => natom
|
||||
{NewCoin(atom, math.NewInt(5)), matom, NewCoin(matom, math.NewInt(5000)), false}, // atom => matom
|
||||
{NewCoin(atom, math.NewInt(5)), uatom, NewCoin(uatom, math.NewInt(5000000)), false}, // atom => uatom
|
||||
{NewCoin(atom, math.NewInt(5)), natom, NewCoin(natom, math.NewInt(5000000000)), false}, // atom => natom
|
||||
|
||||
{NewCoin(uatom, NewInt(5000000)), matom, NewCoin(matom, NewInt(5000)), false}, // uatom => matom
|
||||
{NewCoin(uatom, NewInt(5000000)), natom, NewCoin(natom, NewInt(5000000000)), false}, // uatom => natom
|
||||
{NewCoin(uatom, NewInt(5000000)), atom, NewCoin(atom, NewInt(5)), false}, // uatom => atom
|
||||
{NewCoin(uatom, math.NewInt(5000000)), matom, NewCoin(matom, math.NewInt(5000)), false}, // uatom => matom
|
||||
{NewCoin(uatom, math.NewInt(5000000)), natom, NewCoin(natom, math.NewInt(5000000000)), false}, // uatom => natom
|
||||
{NewCoin(uatom, math.NewInt(5000000)), atom, NewCoin(atom, math.NewInt(5)), false}, // uatom => atom
|
||||
|
||||
{NewCoin(matom, NewInt(5000)), natom, NewCoin(natom, NewInt(5000000000)), false}, // matom => natom
|
||||
{NewCoin(matom, NewInt(5000)), uatom, NewCoin(uatom, NewInt(5000000)), false}, // matom => uatom
|
||||
{NewCoin(matom, math.NewInt(5000)), natom, NewCoin(natom, math.NewInt(5000000000)), false}, // matom => natom
|
||||
{NewCoin(matom, math.NewInt(5000)), uatom, NewCoin(uatom, math.NewInt(5000000)), false}, // matom => uatom
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
@@ -113,35 +113,35 @@ func (s *internalDenomTestSuite) TestConvertCoins() {
|
||||
|
||||
// reset registration
|
||||
baseDenom = ""
|
||||
denomUnits = map[string]Dec{}
|
||||
denomUnits = map[string]math.LegacyDec{}
|
||||
}
|
||||
|
||||
func (s *internalDenomTestSuite) TestConvertDecCoins() {
|
||||
atomUnit := math.LegacyOneDec() // 1 (base denom unit)
|
||||
s.Require().NoError(RegisterDenom(atom, atomUnit))
|
||||
|
||||
matomUnit := NewDecWithPrec(1, 3) // 10^-3 (milli)
|
||||
matomUnit := math.LegacyNewDecWithPrec(1, 3) // 10^-3 (milli)
|
||||
s.Require().NoError(RegisterDenom(matom, matomUnit))
|
||||
|
||||
uatomUnit := NewDecWithPrec(1, 6) // 10^-6 (micro)
|
||||
uatomUnit := math.LegacyNewDecWithPrec(1, 6) // 10^-6 (micro)
|
||||
s.Require().NoError(RegisterDenom(uatom, uatomUnit))
|
||||
|
||||
natomUnit := NewDecWithPrec(1, 9) // 10^-9 (nano)
|
||||
natomUnit := math.LegacyNewDecWithPrec(1, 9) // 10^-9 (nano)
|
||||
s.Require().NoError(RegisterDenom(natom, natomUnit))
|
||||
|
||||
res, err := GetBaseDenom()
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(res, natom)
|
||||
s.Require().Equal(NormalizeDecCoin(NewDecCoin(uatom, NewInt(1))), NewDecCoin(natom, NewInt(1000)))
|
||||
s.Require().Equal(NormalizeDecCoin(NewDecCoin(matom, NewInt(1))), NewDecCoin(natom, NewInt(1000000)))
|
||||
s.Require().Equal(NormalizeDecCoin(NewDecCoin(atom, NewInt(1))), NewDecCoin(natom, NewInt(1000000000)))
|
||||
s.Require().Equal(NormalizeDecCoin(NewDecCoin(uatom, math.NewInt(1))), NewDecCoin(natom, math.NewInt(1000)))
|
||||
s.Require().Equal(NormalizeDecCoin(NewDecCoin(matom, math.NewInt(1))), NewDecCoin(natom, math.NewInt(1000000)))
|
||||
s.Require().Equal(NormalizeDecCoin(NewDecCoin(atom, math.NewInt(1))), NewDecCoin(natom, math.NewInt(1000000000)))
|
||||
|
||||
coins, err := ParseCoinsNormalized("0.1atom,0.1matom,0.1uatom")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(coins, Coins{
|
||||
Coin{natom, NewInt(100000000)},
|
||||
Coin{natom, NewInt(100000)},
|
||||
Coin{natom, NewInt(100)},
|
||||
Coin{natom, math.NewInt(100000000)},
|
||||
Coin{natom, math.NewInt(100000)},
|
||||
Coin{natom, math.NewInt(100)},
|
||||
})
|
||||
|
||||
testCases := []struct {
|
||||
@@ -150,21 +150,21 @@ func (s *internalDenomTestSuite) TestConvertDecCoins() {
|
||||
result DecCoin
|
||||
expErr bool
|
||||
}{
|
||||
{NewDecCoin("foo", ZeroInt()), atom, DecCoin{}, true},
|
||||
{NewDecCoin(atom, ZeroInt()), "foo", DecCoin{}, true},
|
||||
{NewDecCoin(atom, ZeroInt()), "FOO", DecCoin{}, true},
|
||||
{NewDecCoin("foo", math.ZeroInt()), atom, DecCoin{}, true},
|
||||
{NewDecCoin(atom, math.ZeroInt()), "foo", DecCoin{}, true},
|
||||
{NewDecCoin(atom, math.ZeroInt()), "FOO", DecCoin{}, true},
|
||||
|
||||
// 0.5atom
|
||||
{NewDecCoinFromDec(atom, NewDecWithPrec(5, 1)), matom, NewDecCoin(matom, NewInt(500)), false}, // atom => matom
|
||||
{NewDecCoinFromDec(atom, NewDecWithPrec(5, 1)), uatom, NewDecCoin(uatom, NewInt(500000)), false}, // atom => uatom
|
||||
{NewDecCoinFromDec(atom, NewDecWithPrec(5, 1)), natom, NewDecCoin(natom, NewInt(500000000)), false}, // atom => natom
|
||||
{NewDecCoinFromDec(atom, math.LegacyNewDecWithPrec(5, 1)), matom, NewDecCoin(matom, math.NewInt(500)), false}, // atom => matom
|
||||
{NewDecCoinFromDec(atom, math.LegacyNewDecWithPrec(5, 1)), uatom, NewDecCoin(uatom, math.NewInt(500000)), false}, // atom => uatom
|
||||
{NewDecCoinFromDec(atom, math.LegacyNewDecWithPrec(5, 1)), natom, NewDecCoin(natom, math.NewInt(500000000)), false}, // atom => natom
|
||||
|
||||
{NewDecCoin(uatom, NewInt(5000000)), matom, NewDecCoin(matom, NewInt(5000)), false}, // uatom => matom
|
||||
{NewDecCoin(uatom, NewInt(5000000)), natom, NewDecCoin(natom, NewInt(5000000000)), false}, // uatom => natom
|
||||
{NewDecCoin(uatom, NewInt(5000000)), atom, NewDecCoin(atom, NewInt(5)), false}, // uatom => atom
|
||||
{NewDecCoin(uatom, math.NewInt(5000000)), matom, NewDecCoin(matom, math.NewInt(5000)), false}, // uatom => matom
|
||||
{NewDecCoin(uatom, math.NewInt(5000000)), natom, NewDecCoin(natom, math.NewInt(5000000000)), false}, // uatom => natom
|
||||
{NewDecCoin(uatom, math.NewInt(5000000)), atom, NewDecCoin(atom, math.NewInt(5)), false}, // uatom => atom
|
||||
|
||||
{NewDecCoin(matom, NewInt(5000)), natom, NewDecCoin(natom, NewInt(5000000000)), false}, // matom => natom
|
||||
{NewDecCoin(matom, NewInt(5000)), uatom, NewDecCoin(uatom, NewInt(5000000)), false}, // matom => uatom
|
||||
{NewDecCoin(matom, math.NewInt(5000)), natom, NewDecCoin(natom, math.NewInt(5000000000)), false}, // matom => natom
|
||||
{NewDecCoin(matom, math.NewInt(5000)), uatom, NewDecCoin(uatom, math.NewInt(5000000)), false}, // matom => uatom
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
@@ -181,24 +181,24 @@ func (s *internalDenomTestSuite) TestConvertDecCoins() {
|
||||
|
||||
// reset registration
|
||||
baseDenom = ""
|
||||
denomUnits = map[string]Dec{}
|
||||
denomUnits = map[string]math.LegacyDec{}
|
||||
}
|
||||
|
||||
func (s *internalDenomTestSuite) TestDecOperationOrder() {
|
||||
dec, err := NewDecFromStr("11")
|
||||
dec, err := math.LegacyNewDecFromStr("11")
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(RegisterDenom("unit1", dec))
|
||||
dec, err = NewDecFromStr("100000011")
|
||||
dec, err = math.LegacyNewDecFromStr("100000011")
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(RegisterDenom("unit2", dec))
|
||||
|
||||
coin, err := ConvertCoin(NewCoin("unit1", NewInt(100000011)), "unit2")
|
||||
coin, err := ConvertCoin(NewCoin("unit1", math.NewInt(100000011)), "unit2")
|
||||
s.Require().NoError(err)
|
||||
s.Require().Equal(coin, NewCoin("unit2", NewInt(11)))
|
||||
s.Require().Equal(coin, NewCoin("unit2", math.NewInt(11)))
|
||||
|
||||
// reset registration
|
||||
baseDenom = ""
|
||||
denomUnits = map[string]Dec{}
|
||||
denomUnits = map[string]math.LegacyDec{}
|
||||
}
|
||||
|
||||
func (s *internalDenomTestSuite) TestSetBaseDenomError() {
|
||||
@@ -207,5 +207,5 @@ func (s *internalDenomTestSuite) TestSetBaseDenomError() {
|
||||
|
||||
// reset registration
|
||||
baseDenom = ""
|
||||
denomUnits = map[string]Dec{}
|
||||
denomUnits = map[string]math.LegacyDec{}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
abci "github.com/cometbft/cometbft/abci/types"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
testdata "github.com/cosmos/cosmos-sdk/testutil/testdata"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
@@ -90,7 +92,7 @@ func (s *eventsTestSuite) TestEmitTypedEvent() {
|
||||
s.Run("deterministic key-value order", func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
em := sdk.NewEventManager()
|
||||
coin := sdk.NewCoin("fakedenom", sdk.NewInt(1999999))
|
||||
coin := sdk.NewCoin("fakedenom", math.NewInt(1999999))
|
||||
s.Require().NoError(em.EmitTypedEvent(&coin))
|
||||
s.Require().Len(em.Events(), 1)
|
||||
attrs := em.Events()[0].Attributes
|
||||
@@ -104,7 +106,7 @@ func (s *eventsTestSuite) TestEmitTypedEvent() {
|
||||
func (s *eventsTestSuite) TestEventManagerTypedEvents() {
|
||||
em := sdk.NewEventManager()
|
||||
|
||||
coin := sdk.NewCoin("fakedenom", sdk.NewInt(1999999))
|
||||
coin := sdk.NewCoin("fakedenom", math.NewInt(1999999))
|
||||
cat := testdata.Cat{
|
||||
Moniker: "Garfield",
|
||||
Lives: 6,
|
||||
|
||||
+3
-1
@@ -1,9 +1,11 @@
|
||||
package types
|
||||
|
||||
import "cosmossdk.io/math"
|
||||
|
||||
// map coins is a map representation of sdk.Coins
|
||||
// intended solely for use in bulk additions.
|
||||
// All serialization and iteration should be done after conversion to sdk.Coins.
|
||||
type MapCoins map[string]Int
|
||||
type MapCoins map[string]math.Int
|
||||
|
||||
func NewMapCoins(coins Coins) MapCoins {
|
||||
m := make(MapCoins, len(coins))
|
||||
|
||||
@@ -1,41 +1,9 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
sdkmath "cosmossdk.io/math"
|
||||
)
|
||||
|
||||
// Type aliases to the SDK's math sub-module
|
||||
//
|
||||
// Deprecated: Functionality of this package has been moved to it's own module:
|
||||
// cosmossdk.io/math
|
||||
//
|
||||
// Please use the above module instead of this package.
|
||||
type (
|
||||
Int = sdkmath.Int
|
||||
)
|
||||
|
||||
var (
|
||||
NewInt = sdkmath.NewInt
|
||||
ZeroInt = sdkmath.ZeroInt
|
||||
)
|
||||
|
||||
func (ip IntProto) String() string {
|
||||
return ip.Int.String()
|
||||
}
|
||||
|
||||
type (
|
||||
Dec = sdkmath.LegacyDec
|
||||
)
|
||||
|
||||
var (
|
||||
NewDecWithPrec = sdkmath.LegacyNewDecWithPrec
|
||||
NewDecFromInt = sdkmath.LegacyNewDecFromInt
|
||||
NewDecFromStr = sdkmath.LegacyNewDecFromStr
|
||||
MustNewDecFromStr = sdkmath.LegacyMustNewDecFromStr
|
||||
)
|
||||
|
||||
var _ CustomProtobufType = (*Dec)(nil)
|
||||
|
||||
func (dp DecProto) String() string {
|
||||
return dp.Dec.String()
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
sdkmath "cosmossdk.io/math"
|
||||
"cosmossdk.io/math"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
@@ -42,19 +42,19 @@ func RandStringOfLength(r *rand.Rand, n int) string {
|
||||
}
|
||||
|
||||
// RandPositiveInt get a rand positive math.Int
|
||||
func RandPositiveInt(r *rand.Rand, max sdkmath.Int) (sdkmath.Int, error) {
|
||||
if !max.GTE(sdkmath.OneInt()) {
|
||||
return sdkmath.Int{}, errors.New("max too small")
|
||||
func RandPositiveInt(r *rand.Rand, max math.Int) (math.Int, error) {
|
||||
if !max.GTE(math.OneInt()) {
|
||||
return math.Int{}, errors.New("max too small")
|
||||
}
|
||||
|
||||
max = max.Sub(sdkmath.OneInt())
|
||||
max = max.Sub(math.OneInt())
|
||||
|
||||
return sdkmath.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(sdkmath.OneInt()), nil
|
||||
return math.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(math.OneInt()), nil
|
||||
}
|
||||
|
||||
// RandomAmount generates a random amount
|
||||
// Note: The range of RandomAmount includes max, and is, in fact, biased to return max as well as 0.
|
||||
func RandomAmount(r *rand.Rand, max sdkmath.Int) sdkmath.Int {
|
||||
func RandomAmount(r *rand.Rand, max math.Int) math.Int {
|
||||
randInt := big.NewInt(0)
|
||||
|
||||
switch r.Intn(10) {
|
||||
@@ -66,12 +66,12 @@ func RandomAmount(r *rand.Rand, max sdkmath.Int) sdkmath.Int {
|
||||
randInt = big.NewInt(0).Rand(r, max.BigInt()) // up to max - 1
|
||||
}
|
||||
|
||||
return sdkmath.NewIntFromBigInt(randInt)
|
||||
return math.NewIntFromBigInt(randInt)
|
||||
}
|
||||
|
||||
// RandomDecAmount generates a random decimal amount
|
||||
// Note: The range of RandomDecAmount includes max, and is, in fact, biased to return max as well as 0.
|
||||
func RandomDecAmount(r *rand.Rand, max sdkmath.LegacyDec) sdkmath.LegacyDec {
|
||||
func RandomDecAmount(r *rand.Rand, max math.LegacyDec) math.LegacyDec {
|
||||
randInt := big.NewInt(0)
|
||||
|
||||
switch r.Intn(10) {
|
||||
@@ -83,7 +83,7 @@ func RandomDecAmount(r *rand.Rand, max sdkmath.LegacyDec) sdkmath.LegacyDec {
|
||||
randInt = big.NewInt(0).Rand(r, max.BigInt())
|
||||
}
|
||||
|
||||
return sdkmath.LegacyNewDecFromBigIntWithPrec(randInt, sdkmath.LegacyPrecision)
|
||||
return math.LegacyNewDecFromBigIntWithPrec(randInt, math.LegacyPrecision)
|
||||
}
|
||||
|
||||
// RandTimestamp generates a random timestamp
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
@@ -21,6 +23,6 @@ func (s *stakingTestSuite) SetupSuite() {
|
||||
}
|
||||
|
||||
func (s *stakingTestSuite) TestTokensToConsensusPower() {
|
||||
s.Require().Equal(int64(0), sdk.TokensToConsensusPower(sdk.NewInt(999_999), sdk.DefaultPowerReduction))
|
||||
s.Require().Equal(int64(1), sdk.TokensToConsensusPower(sdk.NewInt(1_000_000), sdk.DefaultPowerReduction))
|
||||
s.Require().Equal(int64(0), sdk.TokensToConsensusPower(math.NewInt(999_999), sdk.DefaultPowerReduction))
|
||||
s.Require().Equal(int64(1), sdk.TokensToConsensusPower(math.NewInt(1_000_000), sdk.DefaultPowerReduction))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user