Skip to content

Commit

Permalink
utilize bytes.Buffer to build keys
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Mar 20, 2023
1 parent 5214e1d commit 0c1bc13
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
6 changes: 3 additions & 3 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ func (s *IntegrationTestSuite) TestConcentratedLiquidity() {
positionsAddress3 := node.QueryConcentratedPositions(address3)

// assert number of positions per address
s.Require().Equal(len(positionsAddress1), 2)
s.Require().Equal(len(positionsAddress2), 1)
s.Require().Equal(len(positionsAddress3), 2)
s.Require().Equal(2, len(positionsAddress1))
s.Require().Equal(1, len(positionsAddress2))
s.Require().Equal(2, len(positionsAddress3))

// Assert returned positions:
validateCLPosition := func(position model.Position, poolId uint64, lowerTick, upperTick int64) {
Expand Down
22 changes: 15 additions & 7 deletions x/concentrated-liquidity/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package concentrated_liquidity

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strconv"
Expand Down Expand Up @@ -163,37 +164,44 @@ func ParseFullPositionFromBytes(key, value []byte) (model.Position, error) {
// - upper tick
// - join time
// - freeze duration
address, err := sdk.AccAddressFromHex(fullPositionKeyComponents[1])
address, err := sdk.AccAddressFromHex(fmt.Sprintf("%x", []byte(fullPositionKeyComponents[1])))
if err != nil {
return model.Position{}, err
}

poolId, err := strconv.ParseUint(fullPositionKeyComponents[2], 10, 64)
var poolId uint64
err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[2])), binary.BigEndian, &poolId)
if err != nil {
return model.Position{}, err
}

lowerTick, err := strconv.ParseInt(fullPositionKeyComponents[3], 10, 64)
var lowerTick int64
err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[3])), binary.BigEndian, &lowerTick)
if err != nil {
return model.Position{}, err
}

upperTick, err := strconv.ParseInt(fullPositionKeyComponents[4], 10, 64)
var upperTick int64
err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[4])), binary.BigEndian, &upperTick)
if err != nil {
return model.Position{}, err
}

joinTime, err := osmoutils.ParseTimeString(fullPositionKeyComponents[5])
var joinTimeUnix int64
err = binary.Read(bytes.NewReader([]byte(fullPositionKeyComponents[5])), binary.BigEndian, &joinTimeUnix)
if err != nil {
return model.Position{}, err
}
joinTime := time.Unix(0, joinTimeUnix).UTC()

freezeDuration, err := strconv.ParseUint(fullPositionKeyComponents[6], 10, 64)
var freezeDuration time.Duration
err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[6])), binary.BigEndian, &freezeDuration)
if err != nil {
return model.Position{}, err
}

positionId, err := strconv.ParseUint(fullPositionKeyComponents[7], 10, 64)
var positionId uint64
err = binary.Read(bytes.NewBuffer([]byte(fullPositionKeyComponents[7])), binary.BigEndian, &positionId)
if err != nil {
return model.Position{}, err
}
Expand Down
85 changes: 55 additions & 30 deletions x/concentrated-liquidity/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package types

import (
"encoding/hex"
"bytes"
"encoding/binary"
"fmt"
"strconv"
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"

"github.com/osmosis-labs/osmosis/osmoutils"
)

const (
Expand Down Expand Up @@ -118,41 +115,69 @@ func keyTickPrefixByPoolIdPrealloc(poolId uint64, preAllocBytes int) []byte {

// KeyFullPosition uses pool Id, owner, lower tick, upper tick, joinTime, freezeDuration, and positionId for keys
func KeyFullPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64, joinTime time.Time, freezeDuration time.Duration, positionId uint64) []byte {
joinTimeKey := osmoutils.FormatTimeString(joinTime)

var builder strings.Builder
builder.Grow(len(PositionPrefix) + 9*len(KeySeparator) + len(addr.Bytes())*2 + 5*20) // pre-allocating the buffer

builder.Write(PositionPrefix)
builder.WriteString(KeySeparator)
builder.WriteString(hex.EncodeToString(addr.Bytes()))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatUint(poolId, 10))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatInt(lowerTick, 10))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatInt(upperTick, 10))
builder.WriteString(KeySeparator)
builder.WriteString(joinTimeKey)
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatUint(uint64(freezeDuration), 10))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatUint(positionId, 10))

return []byte(builder.String())
var buf bytes.Buffer

buf.Grow(len(PositionPrefix) + 7*len(KeySeparator) + len(addr.Bytes()) + 7*8) // pre-allocating the buffer

buf.Write(PositionPrefix)
buf.WriteString(KeySeparator)
buf.Write(addr.Bytes())
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, poolId)
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, lowerTick)
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, upperTick)
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, joinTime.UTC().UnixNano())
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, uint64(freezeDuration))
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, positionId)

return buf.Bytes()
}

// KeyPosition uses pool Id, owner, lower tick and upper tick for keys
func KeyPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64) []byte {
return []byte(fmt.Sprintf("%s%s%x%s%d%s%d%s%d", PositionPrefix, KeySeparator, addr.Bytes(), KeySeparator, poolId, KeySeparator, lowerTick, KeySeparator, upperTick))
var buf bytes.Buffer
buf.Grow(len(PositionPrefix) + 4*len(KeySeparator) + len(addr.Bytes()) + 4*8) // pre-allocating the buffer

buf.Write(PositionPrefix)
buf.WriteString(KeySeparator)
buf.Write(addr.Bytes())
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, poolId)
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, lowerTick)
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, upperTick)

return buf.Bytes()
}

func KeyAddressAndPoolId(addr sdk.AccAddress, poolId uint64) []byte {
return []byte(fmt.Sprintf("%s%s%x%s%d", PositionPrefix, KeySeparator, addr.Bytes(), KeySeparator, poolId))
var buf bytes.Buffer
buf.Grow(len(PositionPrefix) + 2*len(KeySeparator) + len(addr.Bytes()) + 2*8) // pre-allocating the buffer

buf.Write(PositionPrefix)
buf.WriteString(KeySeparator)
buf.Write(addr.Bytes())
buf.WriteString(KeySeparator)
binary.Write(&buf, binary.BigEndian, poolId)

return buf.Bytes()
}

func KeyUserPositions(addr sdk.AccAddress) []byte {
return []byte(fmt.Sprintf("%s%s%x", PositionPrefix, KeySeparator, addr.Bytes()))
var buf bytes.Buffer
buf.Grow(len(PositionPrefix) + len(KeySeparator) + len(addr.Bytes()) + 8) // pre-allocating the buffer

buf.Write(PositionPrefix)
buf.WriteString(KeySeparator)
buf.Write(addr.Bytes())

return buf.Bytes()
}

func KeyPool(poolId uint64) []byte {
Expand Down

0 comments on commit 0c1bc13

Please sign in to comment.