Skip to content

Commit

Permalink
Add getter in osmoutils
Browse files Browse the repository at this point in the history
  • Loading branch information
mattverse committed Oct 25, 2022
1 parent 1bdb9e1 commit 016f275
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
13 changes: 13 additions & 0 deletions osmoutils/store_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ func MustGet(store store.KVStore, key []byte, result proto.Message) {
}
}

// GetIfFound gets ket from store by mutating result.
// returns a boolean indicating whether value exists for the given key and error
func GetIfFound(store store.KVStore, key []byte, result proto.Message) (found bool, err error) {
b := store.Get(key)
if b == nil {
return false, nil
}
if err := proto.Unmarshal(b, result); err != nil {
return true, err
}
return true, nil
}

// MustSetDec sets dec value to store at key. Panics on any error.
func MustSetDec(store store.KVStore, key []byte, value sdk.Dec) {
MustSet(store, key, &sdk.DecProto{
Expand Down
2 changes: 2 additions & 0 deletions x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ func (s *KeeperTestSuite) TestMint() {
s.Require().Equal(sdk.NewInt(5000), asset1)

// check position state
// 1517 is from the liquidity originally provided
position, err := s.App.ConcentratedLiquidityKeeper.GetPosition(s.Ctx, poolId, s.TestAccs[0], lowerTick, upperTick)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(1517), position.Liquidity)

// check tick state
// 1517 is from the liquidity originally provided
lowerTickInfo, err := s.App.ConcentratedLiquidityKeeper.GetTickInfo(s.Ctx, poolId, lowerTick)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(1517), lowerTickInfo.LiquidityGross)
Expand Down
8 changes: 3 additions & 5 deletions x/concentrated-liquidity/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package concentrated_liquidity

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"

"github.com/osmosis-labs/osmosis/v12/osmoutils"
types "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types"
Expand Down Expand Up @@ -36,12 +35,11 @@ func (k Keeper) GetPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress
positionStruct := Position{}
key := types.KeyPosition(poolId, owner, lowerTick, upperTick)

bz := store.Get(key)
if bz == nil {
found, err := osmoutils.GetIfFound(store, key, &positionStruct)
// return 0 values iuf key has not been initialized
if !found {
return Position{Liquidity: sdk.ZeroInt()}, nil
}

err = proto.Unmarshal(bz, &positionStruct)
if err != nil {
return positionStruct, err
}
Expand Down
9 changes: 3 additions & 6 deletions x/concentrated-liquidity/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
db "github.com/tendermint/tm-db"

"github.com/gogo/protobuf/proto"

"github.com/osmosis-labs/osmosis/v12/osmomath"
"github.com/osmosis-labs/osmosis/v12/osmoutils"
types "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types"
Expand Down Expand Up @@ -122,12 +120,11 @@ func (k Keeper) GetTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64) (ti
tickStruct := TickInfo{}
key := types.KeyTick(poolId, tickIndex)

bz := store.Get(key)
if bz == nil {
found, err := osmoutils.GetIfFound(store, key, &tickStruct)
// return 0 values if key has not been initialized
if !found {
return TickInfo{LiquidityGross: sdk.ZeroInt(), LiquidityNet: sdk.ZeroInt()}, err
}

err = proto.Unmarshal(bz, &tickStruct)
if err != nil {
return tickStruct, err
}
Expand Down

0 comments on commit 016f275

Please sign in to comment.