Skip to content

Commit

Permalink
fix: early exit if there are no expand-by-levels in datanode AMM mark…
Browse files Browse the repository at this point in the history
…et-depth calc
  • Loading branch information
wwestgarth committed Sep 24, 2024
1 parent 2c65a87 commit 7f0cc6b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 0 additions & 1 deletion core/execution/amm/estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,4 @@ func TestEstimateBoundsOneSided(t *testing.T) {
assert.True(t, metrics.LiquidationPriceAtLower.IsZero())
assert.True(t, metrics.PositionSizeAtLower.IsZero())
assert.True(t, metrics.LossOnCommitmentAtLower.IsZero())

}
5 changes: 4 additions & 1 deletion datanode/service/market_depth_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,11 @@ func (m *MarketDepth) getReference(ctx context.Context, marketID string) (num.De
}

func (m *MarketDepth) expandByLevels(pool entities.AMMPool, levels []*level, priceFactor num.Decimal) ([]*types.Order, []bool, error) {
// get positions
if len(levels) == 0 {
return nil, nil, nil
}

// get position
pos, err := m.getAMMPosition(pool.MarketID.String(), pool.AmmPartyID.String())
if err != nil {
return nil, nil, err
Expand Down
28 changes: 28 additions & 0 deletions datanode/service/market_depth_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,34 @@ func TestFairgroundAMM(t *testing.T) {
assert.Equal(t, "12956", mds.service.GetBestAskPrice(marketID).String())
}

func TestAMMDepthOutOfRange(t *testing.T) {
ctx := context.Background()

cfg := service.MarketDepthConfig{
AmmFullExpansionPercentage: 200,
AmmEstimatedStepPercentage: 0,
AmmMaxEstimatedSteps: 0,
}
mds := getServiceWithConfig(t, cfg)
defer mds.ctrl.Finish()

marketID := vgcrypto.RandomHash()

ensureLiveOrders(t, mds, marketID)
ensureDecimalPlaces(t, mds, 1, 1)

// set the mid-price to a value no where near the AMM's prices
mds.marketData.EXPECT().GetMarketDataByID(gomock.Any(), gomock.Any()).Times(1).Return(entities.MarketData{MidPrice: num.DecimalFromInt64(10)}, nil)

// data node is starting from network history, initialise market-depth based on whats aleady there
ensureAMMs(t, mds, marketID)
mds.service.Initialise(ctx)

assert.Equal(t, 0, int(mds.service.GetTotalAMMVolume(marketID)))
assert.Equal(t, 0, int(mds.service.GetAMMVolume(marketID, true)))
assert.Equal(t, 0, int(mds.service.GetAMMVolume(marketID, false)))
}

func ensureLiveOrders(t *testing.T, mds *MDS, marketID string) {
t.Helper()
mds.orders.EXPECT().GetLiveOrders(gomock.Any()).Return([]entities.Order{
Expand Down

0 comments on commit 7f0cc6b

Please sign in to comment.