Skip to content

Commit 71ca0e5

Browse files
author
Alessio Treglia
committed
Port IsAllGT from safe-coins PR
1 parent 714168f commit 71ca0e5

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

types/coin.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,23 @@ func (coins Coins) safeAdd(coinsB Coins) Coins {
269269
}
270270
}
271271

272+
// ContainsDenomsOf returns true if coinsB' denom set
273+
// is subset of the receiver's denoms.
274+
func (coins Coins) ContainsDenomsOf(coinsB Coins) bool {
275+
// more denoms in B than in receiver
276+
if len(coinsB) > len(coins) {
277+
return false
278+
}
279+
280+
for _, coinB := range coinsB {
281+
if coins.AmountOf(coinB.Denom).IsZero() {
282+
return false
283+
}
284+
}
285+
286+
return true
287+
}
288+
272289
// Sub subtracts a set of coins from another.
273290
//
274291
// e.g.
@@ -297,12 +314,26 @@ func (coins Coins) SafeSub(coinsB Coins) (Coins, bool) {
297314
// IsAllGT returns true if for every denom in coins, the denom is present at a
298315
// greater amount in coinsB.
299316
func (coins Coins) IsAllGT(coinsB Coins) bool {
300-
diff, _ := coins.SafeSub(coinsB)
301-
if len(diff) == 0 {
317+
if len(coins) == 0 {
302318
return false
303319
}
304320

305-
return diff.IsAllPositive()
321+
if len(coinsB) == 0 {
322+
return true
323+
}
324+
325+
if !coins.ContainsDenomsOf(coinsB) {
326+
return false
327+
}
328+
329+
for _, coinB := range coinsB {
330+
amountA, amountB := coins.AmountOf(coinB.Denom), coinB.Amount
331+
if !amountA.GT(amountB) {
332+
return false
333+
}
334+
}
335+
336+
return true
306337
}
307338

308339
// IsAllGTE returns true iff for every denom in coins, the denom is present at

0 commit comments

Comments
 (0)