Skip to content

Commit

Permalink
use map to prevent unnecessary second iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Jun 20, 2023
1 parent 28f7ee9 commit bba673d
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions x/pool-incentives/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper

import (
"context"
"time"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -142,7 +141,9 @@ func (q Querier) IncentivizedPools(ctx context.Context, _ *types.QueryIncentiviz
// While there are exceptions, typically the number of incentivizedPools
// equals to the number of incentivized gauges / number of lockable durations.
incentivizedPools := make([]types.IncentivizedPool, 0, len(distrInfo.Records)/len(lockableDurations))
incentivizedPoolIDs := make(map[uint64]struct{})

// Loop over the distribution records and fill in the incentivized pools struct.
for _, record := range distrInfo.Records {
for _, lockableDuration := range lockableDurations {
poolId, err := q.Keeper.GetPoolIdFromGaugeId(sdkCtx, record.GaugeId, lockableDuration)
Expand All @@ -154,6 +155,7 @@ func (q Querier) IncentivizedPools(ctx context.Context, _ *types.QueryIncentiviz
}

incentivizedPools = append(incentivizedPools, incentivizedPool)
incentivizedPoolIDs[poolId] = struct{}{}
}
}
}
Expand All @@ -168,23 +170,22 @@ func (q Querier) IncentivizedPools(ctx context.Context, _ *types.QueryIncentiviz

// Iterate over all migration records.
for _, record := range migrationRecords.BalancerToConcentratedPoolLinks {
linkedClPoolIsIncentivized := false
var duration time.Duration

// If the linked concentrated liquidity pool is in the list of incentivized pools,
// note this and add its balancer counterpart to the list of incentivized pools.
for _, incentivizedPool := range incentivizedPools {
if incentivizedPool.PoolId == record.ClPoolId {
linkedClPoolIsIncentivized = true
duration = incentivizedPool.LockableDuration
continue
}
// If the cl pool is not in the list of incentivized pools, skip it.
_, incentivized := incentivizedPoolIDs[record.ClPoolId]
if !incentivized {
continue
}

if !linkedClPoolIsIncentivized {
continue
// Determine the duration of the incentivized pool.
duration := lockableDurations[0]
for _, pool := range incentivizedPools {
if pool.PoolId == record.ClPoolId {
duration = pool.LockableDuration
break
}
}

// Add the indirectly incentivized balancer pools to the list of incentivized pools.
gaugeId, err := q.Keeper.GetPoolGaugeId(sdkCtx, record.BalancerPoolId, duration)
if err == nil {
incentivizedPool := types.IncentivizedPool{
Expand Down

0 comments on commit bba673d

Please sign in to comment.