-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(osmoutils): coin math helpers (#6427)
* feat(osmoutils): coin math helpers * updates * go mod
- Loading branch information
Showing
5 changed files
with
280 additions
and
1 deletion.
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
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,138 @@ | ||
package coins | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
"github.com/osmosis-labs/osmosis/osmomath" | ||
) | ||
|
||
// Mutative helpers that mutate the input coins | ||
|
||
// MulIntMut multiplies the coins by the given integer | ||
// Mutates the input coins | ||
func MulIntMut(coins sdk.Coins, num osmomath.Int) { | ||
for i := range coins { | ||
coins[i].Amount = coins[i].Amount.Mul(num) | ||
} | ||
} | ||
|
||
// MulRawMut multiplies the coins by the given integer | ||
// Mutates the input coins | ||
func MulRawMut(coins sdk.Coins, num int64) sdk.Coins { | ||
for i := range coins { | ||
coins[i].Amount = coins[i].Amount.MulRaw(num) | ||
} | ||
return coins | ||
} | ||
|
||
// MulDecMut multiplies the coins by the given decimal | ||
// Mutates the input coins | ||
func MulDecMut(coins sdk.Coins, num osmomath.Dec) { | ||
for i := range coins { | ||
coins[i].Amount = coins[i].Amount.ToLegacyDec().Mul(num).TruncateInt() | ||
} | ||
} | ||
|
||
// QuoIntMut divides the coins by the given integer | ||
// Mutates the input coins | ||
func QuoIntMut(coins sdk.Coins, num osmomath.Int) { | ||
for i := range coins { | ||
coins[i].Amount = coins[i].Amount.Quo(num) | ||
} | ||
} | ||
|
||
// QuoRawMut divides the coins by the given integer | ||
// Mutates the input coins | ||
func QuoRawMut(coins sdk.Coins, num int64) { | ||
for i := range coins { | ||
coins[i].Amount = coins[i].Amount.QuoRaw(num) | ||
} | ||
} | ||
|
||
// QuoIntMut divides the coins by the given decimal | ||
// Mutates the input coins | ||
func QuoDecMut(coins sdk.Coins, num osmomath.Dec) { | ||
for i := range coins { | ||
coins[i].Amount = coins[i].Amount.ToLegacyDec().Quo(num).TruncateInt() | ||
} | ||
} | ||
|
||
// Non-mutative coin helpers that reallocate and return new coins | ||
|
||
// MulInt multiplies the coins by the given integer | ||
// Does not mutate the input coins and returns new coins. | ||
func MulInt(coins sdk.Coins, num osmomath.Int) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
|
||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount.Mul(num) | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
|
||
return newCoins | ||
} | ||
|
||
// MulRaw multiplies the coins by the given integer | ||
// Does not mutate the input coins and returns new coins. | ||
func MulRaw(coins sdk.Coins, num int64) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
|
||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount.MulRaw(num) | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
|
||
return newCoins | ||
} | ||
|
||
// MulDec multiplies the coins by the given decimal | ||
// Does not mutate the input coins and returns new coins. | ||
func MulDec(coins sdk.Coins, num osmomath.Dec) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
|
||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount.ToLegacyDec().Mul(num).TruncateInt() | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
|
||
return newCoins | ||
} | ||
|
||
// QuoInt divides the coins by the given integer | ||
// Does not mutate the input coins and returns new coins. | ||
func QuoInt(coins sdk.Coins, num osmomath.Int) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
|
||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount.Quo(num) | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
|
||
return newCoins | ||
} | ||
|
||
// QuoRaw divides the coins by the given integer | ||
// Does not mutate the input coins and returns new coins. | ||
func QuoRaw(coins sdk.Coins, num int64) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
|
||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount.QuoRaw(num) | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
|
||
return newCoins | ||
} | ||
|
||
// QuoDec divides the coins by the given integer | ||
// Does not mutate the input coins and returns new coins. | ||
func QuoDec(coins sdk.Coins, num osmomath.Dec) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
|
||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount.ToLegacyDec().Quo(num).TruncateInt() | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
|
||
return newCoins | ||
} |
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,135 @@ | ||
package coins_test | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/osmosis-labs/osmosis/osmomath" | ||
"github.com/osmosis-labs/osmosis/osmoutils/coins" | ||
) | ||
|
||
var ( | ||
defaultCoins = sdk.NewCoins( | ||
sdk.NewCoin("foo", sdk.NewInt(100)), | ||
sdk.NewCoin("bar", sdk.NewInt(200)), | ||
) | ||
|
||
defaultMultiplier = osmomath.NewInt(2) | ||
|
||
defaultMulExpectedResult = sdk.NewCoins( | ||
sdk.NewCoin(defaultCoins[0].Denom, defaultCoins[0].Amount.Mul(defaultMultiplier)), | ||
sdk.NewCoin(defaultCoins[1].Denom, defaultCoins[1].Amount.Mul(defaultMultiplier)), | ||
) | ||
|
||
defaultQuoExpectedResult = sdk.NewCoins( | ||
sdk.NewCoin(defaultCoins[0].Denom, defaultCoins[0].Amount.Quo(defaultMultiplier)), | ||
sdk.NewCoin(defaultCoins[1].Denom, defaultCoins[1].Amount.Quo(defaultMultiplier)), | ||
) | ||
) | ||
|
||
// makes a deep copy to avoid accidentally mutating the input to a test. | ||
func deepCopy(coins sdk.Coins) sdk.Coins { | ||
newCoins := make(sdk.Coins, len(coins)) | ||
for i := range coins { | ||
newCoins[i].Amount = coins[i].Amount | ||
newCoins[i].Denom = coins[i].Denom | ||
} | ||
return newCoins | ||
} | ||
|
||
// Basic multiplication test. | ||
func TestMul(t *testing.T) { | ||
t.Run("test mutative multiplication", func(t *testing.T) { | ||
t.Run("MulIntMut", func(t *testing.T) { | ||
defaulCoins := deepCopy(defaultCoins) | ||
coins.MulIntMut(defaulCoins, defaultMultiplier) | ||
require.Equal(t, defaultMulExpectedResult, defaulCoins) | ||
}) | ||
|
||
t.Run("MulIntRawMut", func(t *testing.T) { | ||
defaulCoins := deepCopy(defaultCoins) | ||
coins.MulRawMut(defaulCoins, defaultMultiplier.Int64()) | ||
require.Equal(t, defaultMulExpectedResult, defaulCoins) | ||
}) | ||
|
||
t.Run("MulDecMut", func(t *testing.T) { | ||
defaulCoins := deepCopy(defaultCoins) | ||
coins.MulDecMut(defaulCoins, osmomath.NewDecFromInt(defaultMultiplier)) | ||
require.Equal(t, defaultMulExpectedResult, defaulCoins) | ||
}) | ||
}) | ||
|
||
// Make a deep copy of the default coins for the input. | ||
// Validate that the copy input coins are not mutated. | ||
t.Run("test non-mutative multiplication", func(t *testing.T) { | ||
t.Run("MulInt", func(t *testing.T) { | ||
defaulCoinsCopy := deepCopy(defaultCoins) | ||
result := coins.MulInt(defaulCoinsCopy, defaultMultiplier) | ||
require.Equal(t, defaultMulExpectedResult, result) | ||
require.Equal(t, defaultCoins, defaulCoinsCopy) | ||
}) | ||
|
||
t.Run("MulIntRaw", func(t *testing.T) { | ||
defaulCoinsCopy := deepCopy(defaultCoins) | ||
result := coins.MulRaw(defaulCoinsCopy, defaultMultiplier.Int64()) | ||
require.Equal(t, defaultMulExpectedResult, result) | ||
require.Equal(t, defaultCoins, defaulCoinsCopy) | ||
}) | ||
|
||
t.Run("MulDec", func(t *testing.T) { | ||
defaulCoinsCopy := deepCopy(defaultCoins) | ||
result := coins.MulDec(defaulCoinsCopy, osmomath.NewDecFromInt(defaultMultiplier)) | ||
require.Equal(t, defaultMulExpectedResult, result) | ||
require.Equal(t, defaultCoins, defaulCoinsCopy) | ||
}) | ||
}) | ||
} | ||
|
||
func TestQuo(t *testing.T) { | ||
t.Run("test mutative division", func(t *testing.T) { | ||
t.Run("QuoIntMut", func(t *testing.T) { | ||
defaulCoins := deepCopy(defaultCoins) | ||
coins.QuoIntMut(defaulCoins, defaultMultiplier) | ||
require.Equal(t, defaultQuoExpectedResult, defaulCoins) | ||
}) | ||
|
||
t.Run("QuoIntRawMut", func(t *testing.T) { | ||
defaulCoins := deepCopy(defaultCoins) | ||
coins.QuoRawMut(defaulCoins, defaultMultiplier.Int64()) | ||
require.Equal(t, defaultQuoExpectedResult, defaulCoins) | ||
}) | ||
|
||
t.Run("QuoDecMut", func(t *testing.T) { | ||
defaulCoins := deepCopy(defaultCoins) | ||
coins.QuoDecMut(defaulCoins, osmomath.NewDecFromInt(defaultMultiplier)) | ||
require.Equal(t, defaultQuoExpectedResult, defaulCoins) | ||
}) | ||
}) | ||
|
||
// Make a deep copy of the default coins for the input. | ||
// Validate that the copy input coins are not mutated. | ||
t.Run("test non-mutative division", func(t *testing.T) { | ||
t.Run("QuoInt", func(t *testing.T) { | ||
defaulCoinsCopy := deepCopy(defaultCoins) | ||
result := coins.QuoInt(defaulCoinsCopy, defaultMultiplier) | ||
require.Equal(t, defaultQuoExpectedResult, result) | ||
require.Equal(t, defaultCoins, defaulCoinsCopy) | ||
}) | ||
|
||
t.Run("QuoIntRaw", func(t *testing.T) { | ||
defaulCoinsCopy := deepCopy(defaultCoins) | ||
result := coins.QuoRaw(defaulCoinsCopy, defaultMultiplier.Int64()) | ||
require.Equal(t, defaultQuoExpectedResult, result) | ||
require.Equal(t, defaultCoins, defaulCoinsCopy) | ||
}) | ||
|
||
t.Run("QuoDec", func(t *testing.T) { | ||
defaulCoinsCopy := deepCopy(defaultCoins) | ||
result := coins.QuoDec(defaulCoinsCopy, osmomath.NewDecFromInt(defaultMultiplier)) | ||
require.Equal(t, defaultQuoExpectedResult, result) | ||
require.Equal(t, defaultCoins, defaulCoinsCopy) | ||
}) | ||
}) | ||
} |