From 8ee9da775ec6d66de0417a6a8a336c6eadc0bdef Mon Sep 17 00:00:00 2001
From: Hanjun Kim <hallazzang@gmail.com>
Date: Thu, 25 Mar 2021 01:33:55 +0900
Subject: [PATCH] types: add Abs() method to sdk.Int (#8963)

Fixes #8962

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
---
 CHANGELOG.md      | 1 +
 types/int.go      | 7 +++++++
 types/int_test.go | 3 +++
 3 files changed, 11 insertions(+)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9970eb415e7a..497bdb70dff3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -82,6 +82,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
 * (x/auth) [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts
 * (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method.
 * (x/upgrade) [\#8743](https://github.com/cosmos/cosmos-sdk/pull/8743) Add tracking module versions as per ADR-041
+* (types) [\#8962](https://github.com/cosmos/cosmos-sdk/issues/8962) Add `Abs()` method to `sdk.Int`.
 
 ### Bug Fixes
 
diff --git a/types/int.go b/types/int.go
index c33ff040a51e..e8fada2e78ea 100644
--- a/types/int.go
+++ b/types/int.go
@@ -37,6 +37,8 @@ func mod(i *big.Int, i2 *big.Int) *big.Int { return new(big.Int).Mod(i, i2) }
 
 func neg(i *big.Int) *big.Int { return new(big.Int).Neg(i) }
 
+func abs(i *big.Int) *big.Int { return new(big.Int).Abs(i) }
+
 func min(i *big.Int, i2 *big.Int) *big.Int {
 	if i.Cmp(i2) == 1 {
 		return new(big.Int).Set(i2)
@@ -304,6 +306,11 @@ func (i Int) Neg() (res Int) {
 	return Int{neg(i.i)}
 }
 
+// Abs returns the absolute value of Int.
+func (i Int) Abs() Int {
+	return Int{abs(i.i)}
+}
+
 // return the minimum of the ints
 func MinInt(i1, i2 Int) Int {
 	return Int{min(i1.BigInt(), i2.BigInt())}
diff --git a/types/int_test.go b/types/int_test.go
index 2992f938b384..e992d67566f9 100644
--- a/types/int_test.go
+++ b/types/int_test.go
@@ -159,6 +159,8 @@ func (s *intTestSuite) TestArithInt() {
 			{sdk.MinInt(i1, i2), minint(n1, n2)},
 			{sdk.MaxInt(i1, i2), maxint(n1, n2)},
 			{i1.Neg(), -n1},
+			{i1.Abs(), n1},
+			{i1.Neg().Abs(), n1},
 		}
 
 		for tcnum, tc := range cases {
@@ -206,6 +208,7 @@ func (s *intTestSuite) TestImmutabilityAllInt() {
 		func(i *sdk.Int) { _ = i.MulRaw(rand.Int63()) },
 		func(i *sdk.Int) { _ = i.QuoRaw(rand.Int63()) },
 		func(i *sdk.Int) { _ = i.Neg() },
+		func(i *sdk.Int) { _ = i.Abs() },
 		func(i *sdk.Int) { _ = i.IsZero() },
 		func(i *sdk.Int) { _ = i.Sign() },
 		func(i *sdk.Int) { _ = i.Equal(randint()) },