Skip to content

Commit

Permalink
random fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Compagni Portis committed Sep 1, 2023
1 parent 53366fd commit 0a9c9ac
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 223 deletions.
9 changes: 4 additions & 5 deletions proto/duality/dex/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ option go_package = "github.com/duality-labs/duality/x/dex/types";
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
repeated TickLiquidity tickLiquidityList = 2 [(gogoproto.nullable) = true ];
repeated LimitOrderTranche inactiveLimitOrderTrancheList = 6 [(gogoproto.nullable) = true ];
repeated LimitOrderTrancheUser limitOrderTrancheUserList = 7 [(gogoproto.nullable) = true ];

repeated LimitOrderTranche inactiveLimitOrderTrancheList = 3 [(gogoproto.nullable) = true ];
repeated LimitOrderTrancheUser limitOrderTrancheUserList = 4 [(gogoproto.nullable) = true ];
repeated PoolMetadata poolMetadataList = 5 [(gogoproto.nullable) = false];
uint64 poolCount = 6;
// this line is used by starport scaffolding # genesis/proto/state
repeated PoolMetadata poolMetadataList = 8 [(gogoproto.nullable) = false];
uint64 poolCount = 9;
}

13 changes: 7 additions & 6 deletions proto/duality/dex/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ option go_package = "github.com/duality-labs/duality/x/dex/types";

// Query defines the gRPC querier service.
service Query {

// Parameters queries the parameters of the module.
rpc Params (QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/duality/dex/params";
Expand Down Expand Up @@ -127,17 +126,18 @@ service Query {

}

// this line is used by starport scaffolding # 2

// Queries a list of PoolMetadata items.
rpc PoolMetadata (QueryGetPoolMetadataRequest) returns (QueryGetPoolMetadataResponse) {
option (google.api.http).get = "/duality-labs/duality/dex/pool_metadata/{id}";
option (google.api.http).get = "/duality/dex/pool_metadata/{id}";

}
rpc PoolMetadataAll (QueryAllPoolMetadataRequest) returns (QueryAllPoolMetadataResponse) {
option (google.api.http).get = "/duality-labs/duality/dex/pool_metadata";
option (google.api.http).get = "/duality/dex/pool_metadata";

}

// this line is used by starport scaffolding # 2

}
// QueryParamsRequest is request type for the Query/Params RPC method.
message QueryParamsRequest {}
Expand Down Expand Up @@ -326,7 +326,6 @@ message QueryPoolResponse {
}


// this line is used by starport scaffolding # 3
message QueryGetPoolMetadataRequest {
uint64 id = 1;
}
Expand All @@ -344,3 +343,5 @@ message QueryAllPoolMetadataResponse {
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// this line is used by starport scaffolding # 3

50 changes: 25 additions & 25 deletions x/dex/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (k Keeper) InitPool(
) (pool *types.Pool, err error) {
poolMetadata := types.PoolMetadata{PairID: pairID, Tick: centerTickIndexNormalized, Fee: fee}

// Get current pool poolID
// Get current poolID
poolID := k.GetPoolCount(ctx)
poolMetadata.ID = poolID

Expand Down Expand Up @@ -103,21 +103,6 @@ func (k Keeper) GetPoolByID(ctx sdk.Context, poolID uint64) (pool *types.Pool, f
return k.GetPool(ctx, poolMetadata.PairID, poolMetadata.Tick, poolMetadata.Fee)
}

// GetPoolCount get the total number of pool
func (k Keeper) GetPoolCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.PoolCountKeyPrefix)
bz := store.Get(byteKey)

// Count doesn't exist: no element
if bz == nil {
return 0
}

// Parse bytes
return binary.BigEndian.Uint64(bz)
}

func (k Keeper) GetPoolIDByParams(
ctx sdk.Context,
pairID *types.PairID,
Expand All @@ -135,15 +120,6 @@ func (k Keeper) GetPoolIDByParams(
return poolID, true
}

// SetPoolCount set the total number of pool
func (k Keeper) SetPoolCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.PoolCountKeyPrefix)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}

func (k Keeper) SetPool(ctx sdk.Context, pool *types.Pool) {
if pool.LowerTick0.HasToken() {
k.SetPoolReserves(ctx, pool.LowerTick0)
Expand All @@ -161,3 +137,27 @@ func (k Keeper) SetPool(ctx sdk.Context, pool *types.Pool) {
ctx.EventManager().EmitEvent(types.CreateTickUpdatePoolReserves(*pool.LowerTick0))
ctx.EventManager().EmitEvent(types.CreateTickUpdatePoolReserves(*pool.UpperTick1))
}

// GetPoolCount get the total number of pools
func (k Keeper) GetPoolCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.PoolCountKeyPrefix)
bz := store.Get(byteKey)

// Count doesn't exist: no element
if bz == nil {
return 0
}

// Parse bytes
return binary.BigEndian.Uint64(bz)
}

// SetPoolCount set the total number of pools
func (k Keeper) SetPoolCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte{})
byteKey := types.KeyPrefix(types.PoolCountKeyPrefix)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
10 changes: 2 additions & 8 deletions x/dex/keeper/pool_reserves_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ import (

func createNPoolReserves(k *keeper.Keeper, ctx sdk.Context, n int) []*types.PoolReserves {
items := make([]*types.PoolReserves, n)
for i := range items {
pool, err := k.InitPool(ctx, types.MustNewPairID("TokenA", "TokenB"), int64(i), uint64(i))
if err != nil {
panic("failed to create pool")
}
pool.Deposit(sdk.NewInt(10), sdk.NewInt(0), sdk.ZeroInt(), true)
k.SetPool(ctx, pool)
pools := createNPools(k, ctx, n)
for i, pool := range pools {
items[i] = pool.LowerTick0
}

return items
}

Expand Down
6 changes: 3 additions & 3 deletions x/dex/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func (gs GenesisState) Validate() error {
inactiveLimitOrderTrancheKeyMap[index] = struct{}{}
}
// Check for duplicated ID in poolMetadata
poolMetadataIdMap := make(map[uint64]bool)
poolMetadataIDMap := make(map[uint64]bool)
poolMetadataCount := gs.GetPoolCount()
for _, elem := range gs.PoolMetadataList {
if _, ok := poolMetadataIdMap[elem.ID]; ok {
if _, ok := poolMetadataIDMap[elem.ID]; ok {
return fmt.Errorf("duplicated id for poolMetadata")
}
if elem.ID >= poolMetadataCount {
return fmt.Errorf("poolMetadata id should be lower or equal than the last id")
}
poolMetadataIdMap[elem.ID] = true
poolMetadataIDMap[elem.ID] = true
}
// this line is used by starport scaffolding # genesis/types/validate

Expand Down
77 changes: 38 additions & 39 deletions x/dex/types/genesis.pb.go

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

Loading

0 comments on commit 0a9c9ac

Please sign in to comment.