Skip to content

Commit

Permalink
Merge branch 'amm-module' into fix/amt-in-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
kingcre committed Aug 21, 2023
2 parents 89175df + 23600f6 commit 8d66433
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 107 deletions.
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10471,6 +10471,9 @@ paths:
format: uint64
liquidity:
type: string
position_id:
type: string
format: uint64
total_share:
type: object
properties:
Expand Down Expand Up @@ -10645,6 +10648,9 @@ paths:
format: uint64
liquidity:
type: string
position_id:
type: string
format: uint64
total_share:
type: object
properties:
Expand Down Expand Up @@ -19416,6 +19422,9 @@ definitions:
format: uint64
liquidity:
type: string
position_id:
type: string
format: uint64
total_share:
type: object
properties:
Expand Down Expand Up @@ -19550,6 +19559,9 @@ definitions:
format: uint64
liquidity:
type: string
position_id:
type: string
format: uint64
total_share:
type: object
properties:
Expand Down Expand Up @@ -19605,6 +19617,9 @@ definitions:
format: uint64
liquidity:
type: string
position_id:
type: string
format: uint64
total_share:
type: object
properties:
Expand Down
3 changes: 2 additions & 1 deletion proto/crescent/liquidamm/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,6 @@ message PublicPositionResponse {
uint64 last_rewards_auction_id = 8;
string liquidity = 9
[(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", (gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin total_share = 10 [(gogoproto.nullable) = false];
uint64 position_id = 10; // underlying x/amm position's id
cosmos.base.v1beta1.Coin total_share = 11 [(gogoproto.nullable) = false];
}
3 changes: 3 additions & 0 deletions x/amm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func (s *KeeperTestSuite) TestQueryPool() {
s.Require().EqualValues(1, resp.Pool.Id)
s.AssertEqual(utils.ParseCoin("71750980uatom"), resp.Pool.Balance0)
s.AssertEqual(utils.ParseCoin("1390716293uusd"), resp.Pool.Balance1)
s.Require().Equal("cosmos1srphgsfqllr85ndknjme24txux8m0sz0hhpnnksn2339d3a788rsawjx77", resp.Pool.RewardsPool)
s.AssertEqual(utils.ParseDec("1"), resp.Pool.MinOrderQuantity)
s.AssertEqual(sdk.NewInt(12470981864), resp.Pool.TotalLiquidity)
},
},
{
Expand Down
4 changes: 3 additions & 1 deletion x/amm/types/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ func NewPoolResponse(pool Pool, poolState PoolState, balances sdk.Coins) PoolRes
MarketId: pool.MarketId,
Balance0: sdk.NewCoin(pool.Denom0, balances.AmountOf(pool.Denom0)),
Balance1: sdk.NewCoin(pool.Denom1, balances.AmountOf(pool.Denom1)),
TickSpacing: pool.TickSpacing,
ReserveAddress: pool.ReserveAddress,
RewardsPool: pool.RewardsPool,
TickSpacing: pool.TickSpacing,
MinOrderQuantity: pool.MinOrderQuantity,
CurrentTick: poolState.CurrentTick,
CurrentPrice: poolState.CurrentPrice,
CurrentLiquidity: poolState.CurrentLiquidity,
Expand Down
42 changes: 19 additions & 23 deletions x/exchange/keeper/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,29 @@ func (k Keeper) RunBatchMatching(ctx sdk.Context, market types.Market) (err erro
PriceLimit: &bestBuyPrice,
}, escrow)

var lastPrice sdk.Dec
marketState := k.MustGetMarketState(ctx, market.Id)
defer func() {
// If there was an error, exit early.
if err != nil {
return
}
// If there was no matching, exit early, too.
if lastPrice.IsNil() {
return
}

// Apply the match results.
memOrders := append(append(([]*types.MemOrder)(nil), buyObs.Orders()...), sellObs.Orders()...)
if err = k.finalizeMatching(ctx, market, memOrders, escrow); err != nil {
return
}
marketState.LastPrice = &lastPrice
marketState.LastMatchingHeight = ctx.BlockHeight()
k.SetMarketState(ctx, market.Id, marketState)
}()

mCtx := types.NewMatchingContext(market, false)
var (
lastPrice sdk.Dec
matched bool
)
if marketState.LastPrice == nil {
lastPrice = mCtx.RunSinglePriceAuction(buyObs, sellObs)
lastPrice, matched = mCtx.RunSinglePriceAuction(buyObs, sellObs)
} else {
lastPrice = mCtx.BatchMatchOrderBookSides(buyObs, sellObs, *marketState.LastPrice)
lastPrice, matched = mCtx.BatchMatchOrderBookSides(buyObs, sellObs, *marketState.LastPrice)
}
// If there was no matching, exit early.
if !matched {
return
}

// Apply the match results.
memOrders := append(append(([]*types.MemOrder)(nil), buyObs.Orders()...), sellObs.Orders()...)
if err = k.finalizeMatching(ctx, market, memOrders, escrow); err != nil {
return
}
marketState.LastPrice = &lastPrice
marketState.LastMatchingHeight = ctx.BlockHeight()
k.SetMarketState(ctx, market.Id, marketState)
return nil
}
11 changes: 7 additions & 4 deletions x/exchange/types/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (ctx *MatchingContext) ExecuteOrder(
return
}

func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookSide) (matchPrice sdk.Dec) {
func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookSide) (matchPrice sdk.Dec, matched bool) {
buyLevelIdx, sellLevelIdx := 0, 0
var buyLastPrice, sellLastPrice sdk.Dec
for buyLevelIdx < len(buyObs.levels) && sellLevelIdx < len(sellObs.levels) {
Expand Down Expand Up @@ -251,11 +251,12 @@ func (ctx *MatchingContext) RunSinglePriceAuction(buyObs, sellObs *MemOrderBookS
sellLevelIdx++
}
}
matched = true
}
return
}

func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBookSide, lastPrice sdk.Dec) (newLastPrice sdk.Dec) {
func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBookSide, lastPrice sdk.Dec) (newLastPrice sdk.Dec, matched bool) {
// Phase 1: Match orders with price below(or equal to) the last price and
// price above(or equal to) the last price.
// The execution price is the last price.
Expand All @@ -274,10 +275,11 @@ func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBo
if sellFull {
sellLevelIdx++
}
matched = true
}
// If there's no more levels to match, return earlier.
if buyLevelIdx >= len(buyObs.levels) || sellLevelIdx >= len(sellObs.levels) {
return lastPrice
return lastPrice, matched
}

// Phase 2: Match orders in traditional exchange's manner.
Expand Down Expand Up @@ -306,8 +308,9 @@ func (ctx *MatchingContext) BatchMatchOrderBookSides(buyObs, sellObs *MemOrderBo
if sellFull {
sellLevelIdx++
}
matched = true
}
return newLastPrice
return newLastPrice, matched
}

func PayReceiveDenoms(baseDenom, quoteDenom string, isBuy bool) (payDenom, receiveDenom string) {
Expand Down
10 changes: 7 additions & 3 deletions x/liquidamm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ func (k Querier) PublicPositions(c context.Context, req *types.QueryPublicPositi
var publicPositions []types.PublicPositionResponse
pageRes, err := query.Paginate(publicPositionStore, req.Pagination, func(key, value []byte) error {
publicPosition := publicPositionGetter(key, value)
position, found := k.GetAMMPosition(ctx, publicPosition)
ammPosition, found := k.GetAMMPosition(ctx, publicPosition)
if !found {
position.Liquidity = utils.ZeroInt
ammPosition.Liquidity = utils.ZeroInt
ammPosition.Id = 0
}
shareDenom := types.ShareDenom(publicPosition.Id)
publicPositions = append(publicPositions, types.PublicPositionResponse{
Expand All @@ -84,8 +85,9 @@ func (k Querier) PublicPositions(c context.Context, req *types.QueryPublicPositi
MinBidAmount: publicPosition.MinBidAmount,
FeeRate: publicPosition.FeeRate,
LastRewardsAuctionId: publicPosition.LastRewardsAuctionId,
Liquidity: position.Liquidity,
Liquidity: ammPosition.Liquidity,
TotalShare: k.bankKeeper.GetSupply(ctx, shareDenom),
PositionId: ammPosition.Id,
})
return nil
})
Expand Down Expand Up @@ -116,6 +118,7 @@ func (k Querier) PublicPosition(c context.Context, req *types.QueryPublicPositio
ammPosition, found := k.GetAMMPosition(ctx, publicPosition)
if !found { // sanity check
ammPosition.Liquidity = utils.ZeroInt
ammPosition.Id = 0
}
shareDenom := types.ShareDenom(publicPosition.Id)
resp := types.PublicPositionResponse{
Expand All @@ -129,6 +132,7 @@ func (k Querier) PublicPosition(c context.Context, req *types.QueryPublicPositio
LastRewardsAuctionId: publicPosition.LastRewardsAuctionId,
Liquidity: ammPosition.Liquidity,
TotalShare: k.bankKeeper.GetSupply(ctx, shareDenom),
PositionId: ammPosition.Id,
}
return &types.QueryPublicPositionResponse{PublicPosition: resp}, nil
}
Expand Down
13 changes: 13 additions & 0 deletions x/liquidamm/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ func (s *KeeperTestSuite) TestQueryPublicPosition() {
s.App.BankKeeper.GetSupply(s.Ctx, types.ShareDenom(publicPosition.Id)),
publicPosition.TotalShare)
s.Require().Equal(sdk.NewInt(43138144377), publicPosition.Liquidity)
s.Require().Equal(uint64(2), publicPosition.PositionId)
},
},
{
"no amm position",
&types.QueryPublicPositionRequest{
PublicPositionId: 2,
},
"",
func(resp *types.QueryPublicPositionResponse) {
publicPosition := resp.PublicPosition
s.Require().Equal(uint64(2), publicPosition.Id)
s.Require().Equal(uint64(0), publicPosition.PositionId)
},
},
} {
Expand Down
Loading

0 comments on commit 8d66433

Please sign in to comment.