Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Multi-coins support for validators #4794

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (coins Coins) IsValid() bool {
// denominations.
//
// CONTRACT: Add will never return Coins where one Coin has a non-positive
// amount. In otherwords, IsValid will always return true.
// amount. In other words, IsValid will always return true.
func (coins Coins) Add(coinsB Coins) Coins {
return coins.safeAdd(coinsB)
}
Expand Down Expand Up @@ -395,6 +395,12 @@ func (coins Coins) IsAllLT(coinsB Coins) bool {
return coinsB.IsAllGT(coins)
}

// IsAnyLT returns True iff for any denom in coins, the denom is present at
// a smaller amount in coinsB.
func (coins Coins) IsAnyLT(coinsB Coins) bool {
return coinsB.IsAnyGT(coins)
}

// IsAllLTE returns true iff for every denom in coins, the denom is present at
// a smaller or equal amount in coinsB.
func (coins Coins) IsAllLTE(coinsB Coins) bool {
Expand Down Expand Up @@ -424,6 +430,22 @@ func (coins Coins) IsAnyGT(coinsB Coins) bool {
return false
}

// IsAnyGTInt returns true if for any denom in coins,
// the denom is present at a greater amount than amount.
func (coins Coins) IsAnyGTInt(amount Int) bool {
if len(coins) == 0 {
return false
}

for _, coin := range coins {
if coin.Amount.GT(amount) {
return true
}
}

return false
}

// IsAnyGTE returns true iff coins contains at least one denom that is present
// at a greater or equal amount in coinsB; it returns false otherwise.
//
Expand Down Expand Up @@ -506,6 +528,17 @@ func (coins Coins) AmountOf(denom string) Int {
}
}

// Returns the smallest amount of coin present inside coins
func (coins Coins) SmallestAmount() Int {
smallestAmount := ZeroInt()
for _, coin := range coins {
if coin.Amount.LTE(smallestAmount) {
smallestAmount = coin.Amount
}
}
return smallestAmount
}

// IsAllPositive returns true if there is at least one coin and all currencies
// have a positive value.
func (coins Coins) IsAllPositive() bool {
Expand Down Expand Up @@ -569,6 +602,48 @@ func removeZeroCoins(coins Coins) Coins {
return coins[:i]
}

// MinCoins returns a set of coins having the minimum values between coinsA and coinsB.
//
// e.g.
// MinCoins({2A, 3B}, {A}) = {A}
// MinCoins({2A, 3B}, {5C}) = {}
func MinCoins(coinsA Coins, coinsB Coins) Coins {
minCoins := Coins{}

for _, coin := range coinsA {
valueA := coin.Amount
valueB := coinsB.AmountOf(coin.Denom)
minValue := MinInt(valueA, valueB)

if minValue.GT(ZeroInt()) {
minCoins.Add(NewCoins(NewCoin(coin.Denom, minValue)))
}
}

return minCoins
}

// MaxCoins returns a set of coins having the maximum values between coinsA and coinsB.
//
// e.g.
// MaxCoins({2A, 3B}, {A}) = {2A, 3B}
// MaxCoins({2A, 3B}, {5C}) = {2A, 3B, 5C}
func MaxCoins(coinsA Coins, coinsB Coins) Coins {
minCoins := Coins{}

for _, coin := range coinsA {
valueA := coin.Amount
valueB := coinsB.AmountOf(coin.Denom)
maxValue := MaxInt(valueA, valueB)

if maxValue.GT(ZeroInt()) {
minCoins.Add(NewCoins(NewCoin(coin.Denom, maxValue)))
}
}

return minCoins
}

//-----------------------------------------------------------------------------
// Sort interface

Expand Down
Loading