Skip to content

Commit

Permalink
remove iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed May 25, 2023
1 parent bbb4eaa commit e0ecfea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
1 change: 1 addition & 0 deletions x/lockup/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func (q Querier) NextLockID(goCtx context.Context, req *types.NextLockIDRequest)

// SyntheticLockupsByLockupID returns synthetic lockups by native lockup id.
// Deprecated: use SyntheticLockupByLockupID instead.
// nolint: staticcheck
func (q Querier) SyntheticLockupsByLockupID(goCtx context.Context, req *types.SyntheticLockupsByLockupIDRequest) (*types.SyntheticLockupsByLockupIDResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down
29 changes: 8 additions & 21 deletions x/lockup/keeper/synthetic_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,17 @@ func (k Keeper) GetSyntheticLockup(ctx sdk.Context, lockID uint64, synthdenom st
}

// GetSyntheticLockupByUnderlyingLockId gets the synthetic lockup object connected to the underlying lock ID.
// Error is returned if:
// - there are more than one synthetic lockup objects with the same underlying lock ID.
// - there is no synthetic lockup object with the given underlying lock ID.
// Error is returned if there is no synthetic lockup object with the given underlying lock ID.
func (k Keeper) GetSyntheticLockupByUnderlyingLockId(ctx sdk.Context, lockID uint64) (types.SyntheticLock, error) {
synthLock := types.SyntheticLock{}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, combineKeys(types.KeyPrefixSyntheticLockup, sdk.Uint64ToBigEndian(lockID)))
defer iterator.Close()

synthLocks := []types.SyntheticLock{}
for ; iterator.Valid(); iterator.Next() {
synthLock := types.SyntheticLock{}
err := proto.Unmarshal(iterator.Value(), &synthLock)
if err != nil {
return types.SyntheticLock{}, err
}
synthLocks = append(synthLocks, synthLock)
}
if len(synthLocks) > 1 {
return types.SyntheticLock{}, fmt.Errorf("synthetic lockup with same lock id should not exist")
}
if len(synthLocks) == 0 {
return types.SyntheticLock{}, nil
synthLockKey := combineKeys(types.KeyPrefixSyntheticLockup, sdk.Uint64ToBigEndian(lockID))
if !store.Has(synthLockKey) {
return types.SyntheticLock{}, fmt.Errorf("synthetic lock with ID %d does not exist", lockID)
}
return synthLocks[0], nil
bz := store.Get(synthLockKey)
err := proto.Unmarshal(bz, &synthLock)
return synthLock, err
}

// GetAllSyntheticLockupsByAddr gets all the synthetic lockups from all the locks owned by the given address.
Expand Down

0 comments on commit e0ecfea

Please sign in to comment.