Skip to content

Commit

Permalink
Merge branch 'main' into feat/bump_ics_to_v4
Browse files Browse the repository at this point in the history
  • Loading branch information
pr0n00gler committed Jan 26, 2024
2 parents 42fea50 + f7b7ea2 commit bd0dafb
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 150 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test_trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
fi
- name: Repository Dispatch
if: ${{ env.CONTINUE == 'true' }}
uses: peter-evans/repository-dispatch@v2
uses: peter-evans/repository-dispatch@v3
with:
token: ${{ secrets.PAT_TOKEN }}
repository: neutron-org/neutron-tests
Expand Down
217 changes: 112 additions & 105 deletions docs/static/openapi.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion proto/neutron/dex/limit_order_expiration.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "google/protobuf/timestamp.proto";
option go_package = "github.com/neutron-org/neutron/v2/x/dex/types";

message LimitOrderExpiration {
// see limitOrderTranche.proto for details on goodTilDate
// see limitOrderTranche.proto for details on expiration_time
google.protobuf.Timestamp expiration_time = 1 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
Expand Down
11 changes: 5 additions & 6 deletions proto/neutron/dex/limit_order_tranche.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ message LimitOrderTranche {
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "total_taker_denom"
];
// expiration_time is represented as an RFC 3339 formatted date.
// LimitOrders with expiration_time set are valid as long as blockTime <= expiration_time.
// JIT orders also use expiration_time to handle deletion, but represent a special case.
// All JIT orders have an expiration_time of 0001-01-01T00:00:00Z, and an exception is made to
// still treat these orders as live. Order deletion still functions the
// same, and the orders will be deleted at the end of the block.
// LimitOrders with expiration_time set are valid as long as blockTime <= expiration_time

// JIT orders also use expiration_time to handle deletion but represent a special case
// All JIT orders have a expiration_time of 0 and an exception is made to still treat these orders as live
// Order deletion still functions the same and the orders will be deleted at the end of the block
google.protobuf.Timestamp expiration_time = 6 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = true
Expand Down
73 changes: 43 additions & 30 deletions x/dex/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@ func (k Keeper) InitPool(
centerTickIndexNormalized int64,
fee uint64,
) (pool *types.Pool, err error) {
poolMetadata := types.PoolMetadata{PairId: pairID, Tick: centerTickIndexNormalized, Fee: fee}
poolID := k.initializePoolMetadata(ctx, pairID, centerTickIndexNormalized, fee)

// Get current poolID
poolID := k.GetPoolCount(ctx)
poolMetadata.Id = poolID
k.storePoolIDRef(ctx, poolID, pairID, centerTickIndexNormalized, fee)

// Store poolMetadata
k.SetPoolMetadata(ctx, poolMetadata)
return types.NewPool(pairID, centerTickIndexNormalized, fee, poolID)
}

// Create a reference so poolID can be looked up by poolMetadata
func (k Keeper) storePoolIDRef(
ctx sdk.Context,
poolID uint64,
pairID *types.PairID,
centerTickIndexNormalized int64,
fee uint64,
) {
poolIDBz := sdk.Uint64ToBigEndian(poolID)
poolIDKey := types.PoolIDKey(pairID, centerTickIndexNormalized, fee)

poolIDStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolIDKeyPrefix))
poolIDStore.Set(poolIDKey, poolIDBz)
}

// Update poolCount
k.SetPoolCount(ctx, poolID+1)

return types.NewPool(pairID, centerTickIndexNormalized, fee, poolID)
func (k Keeper) incrementPoolCount(ctx sdk.Context) {
currentCount := k.GetPoolCount(ctx)
k.SetPoolCount(ctx, currentCount+1)
}

// GetNextPoolId get ID for the next pool to be created
Expand All @@ -62,20 +66,29 @@ func (k Keeper) GetPool(
pairID *types.PairID,
centerTickIndexNormalized int64,
fee uint64,
) (*types.Pool, bool) {
feeInt64 := utils.MustSafeUint64ToInt64(fee)
) (pool *types.Pool, found bool) {
poolID, found := k.GetPoolIDByParams(ctx, pairID, centerTickIndexNormalized, fee)
if !found {
return nil, false
}

return k.getPoolByPoolID(ctx, poolID, pairID, centerTickIndexNormalized, fee)
}

func (k Keeper) getPoolByPoolID(
ctx sdk.Context,
poolID uint64,
pairID *types.PairID,
centerTickIndexNormalized int64,
fee uint64,
) (pool *types.Pool, found bool) {
feeInt64 := utils.MustSafeUint64ToInt64(fee)
id0To1 := &types.PoolReservesKey{
TradePairId: types.NewTradePairIDFromMaker(pairID, pairID.Token1),
TickIndexTakerToMaker: centerTickIndexNormalized + feeInt64,
Fee: fee,
}

poolID, found := k.GetPoolIDByParams(ctx, pairID, centerTickIndexNormalized, fee)
if !found {
return nil, false
}

upperTick, upperTickFound := k.GetPoolReserves(ctx, id0To1)
lowerTick, lowerTickFound := k.GetPoolReserves(ctx, id0To1.Counterpart())

Expand All @@ -85,7 +98,7 @@ func (k Keeper) GetPool(
case lowerTickFound && !upperTickFound:
upperTick = types.NewPoolReservesFromCounterpart(lowerTick)
case !lowerTickFound && !upperTickFound:
// Pool has already been initialized before so we can safely assume that pool creation doesn't throw an error
// Pool has already been initialized before, so we can safely assume that pool creation doesn't throw an error
return types.MustNewPool(pairID, centerTickIndexNormalized, fee, poolID), true
}

Expand All @@ -102,7 +115,7 @@ func (k Keeper) GetPoolByID(ctx sdk.Context, poolID uint64) (pool *types.Pool, f
return pool, false
}

return k.GetPool(ctx, poolMetadata.PairId, poolMetadata.Tick, poolMetadata.Fee)
return k.getPoolByPoolID(ctx, poolID, poolMetadata.PairId, poolMetadata.Tick, poolMetadata.Fee)
}

func (k Keeper) GetPoolIDByParams(
Expand All @@ -123,23 +136,23 @@ func (k Keeper) GetPoolIDByParams(
}

func (k Keeper) SetPool(ctx sdk.Context, pool *types.Pool) {
if pool.LowerTick0.HasToken() {
k.SetPoolReserves(ctx, pool.LowerTick0)
} else {
k.RemovePoolReserves(ctx, pool.LowerTick0.Key)
}
if pool.UpperTick1.HasToken() {
k.SetPoolReserves(ctx, pool.UpperTick1)
} else {
k.RemovePoolReserves(ctx, pool.UpperTick1.Key)
}
k.updatePoolReserves(ctx, pool.LowerTick0)
k.updatePoolReserves(ctx, pool.UpperTick1)

// TODO: this will create a bit of extra noise since not every Save is updating both ticks
// This should be solved upstream by better tracking of dirty ticks
ctx.EventManager().EmitEvent(types.CreateTickUpdatePoolReserves(*pool.LowerTick0))
ctx.EventManager().EmitEvent(types.CreateTickUpdatePoolReserves(*pool.UpperTick1))
}

func (k Keeper) updatePoolReserves(ctx sdk.Context, reserves *types.PoolReserves) {
if reserves.HasToken() {
k.SetPoolReserves(ctx, reserves)
} else {
k.RemovePoolReserves(ctx, reserves.Key)
}
}

// GetPoolCount get the total number of pools
func (k Keeper) GetPoolCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
Expand Down
20 changes: 20 additions & 0 deletions x/dex/keeper/pool_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ func (k Keeper) GetPoolMetadataByDenom(
return pm, nil
}

func (k Keeper) initializePoolMetadata(
ctx sdk.Context,
pairID *types.PairID,
centerTickIndexNormalized int64,
fee uint64,
) uint64 {
poolID := k.GetPoolCount(ctx)
poolMetadata := types.PoolMetadata{
Id: poolID,
PairId: pairID,
Tick: centerTickIndexNormalized,
Fee: fee,
}

k.SetPoolMetadata(ctx, poolMetadata)

k.incrementPoolCount(ctx)
return poolID
}

// RemovePoolMetadata removes a poolMetadata from the store
func (k Keeper) RemovePoolMetadata(ctx sdk.Context, id uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PoolMetadataKeyPrefix))
Expand Down
2 changes: 1 addition & 1 deletion x/dex/types/limit_order_expiration.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions x/dex/types/limit_order_tranche.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bd0dafb

Please sign in to comment.