From 23dbaddfe9a458c8bec5c10d2a9dfa750c28f285 Mon Sep 17 00:00:00 2001 From: khanh <50263489+catShaark@users.noreply.github.com> Date: Mon, 11 Jul 2022 11:07:26 +0700 Subject: [PATCH] improve getToDistributedCoinsFromGauges (#2014) * mod getToDistributedCoinsFromGauges * mod getToDistributedCoinsFromGauges * rm unused function --- x/incentives/keeper/distribute.go | 11 ++++++++--- x/incentives/keeper/gauge.go | 9 --------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/x/incentives/keeper/distribute.go b/x/incentives/keeper/distribute.go index 2442bfc4687..1fcfe5215ed 100644 --- a/x/incentives/keeper/distribute.go +++ b/x/incentives/keeper/distribute.go @@ -21,9 +21,14 @@ func (k Keeper) getDistributedCoinsFromGauges(gauges []types.Gauge) sdk.Coins { } func (k Keeper) getToDistributeCoinsFromGauges(gauges []types.Gauge) sdk.Coins { - // TODO: Consider optimizing this in the future to only require one iteration over all gauges. - coins := k.getCoinsFromGauges(gauges) - distributed := k.getDistributedCoinsFromGauges(gauges) + coins := sdk.Coins{} + distributed := sdk.Coins{} + + for _, gauge := range gauges { + coins = coins.Add(gauge.Coins...) + distributed = distributed.Add(gauge.DistributedCoins...) + } + return coins.Sub(distributed) } diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index 59bc7436dc2..8dfe12d1ac7 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -38,15 +38,6 @@ func (k Keeper) getGaugesFromIterator(ctx sdk.Context, iterator db.Iterator) []t return gauges } -// Compute the total amount of coins in all the gauges. -func (k Keeper) getCoinsFromGauges(gauges []types.Gauge) sdk.Coins { - coins := sdk.Coins{} - for _, gauge := range gauges { - coins = coins.Add(gauge.Coins...) - } - return coins -} - // setGauge set the gauge inside store. func (k Keeper) setGauge(ctx sdk.Context, gauge *types.Gauge) error { store := ctx.KVStore(k.storeKey)