Skip to content

Commit 2872cda

Browse files
committed
fix: populate all fields to prevent 0 div panic
1 parent a72ff4b commit 2872cda

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

datanode/service/market_depth_amm.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,16 @@ func definitionFromEntity(ent entities.AMMPool, position int64, priceFactor num.
518518

519519
assetHigh, _ := num.UintFromDecimal(high.ToDecimal().Mul(priceFactor))
520520
assetBase, _ := num.UintFromDecimal(base.ToDecimal().Mul(priceFactor))
521-
assetlow, _ := num.UintFromDecimal(low.ToDecimal().Mul(priceFactor))
521+
assetLow, _ := num.UintFromDecimal(low.ToDecimal().Mul(priceFactor))
522522

523523
return &ammDefn{
524524
position: num.DecimalFromInt64(position),
525525
lower: &curve{
526526
low: low,
527527
high: base,
528-
assetLow: assetlow,
528+
assetLow: assetLow,
529529
assetHigh: assetBase,
530+
sqrtLow: num.UintOne().Sqrt(assetLow),
530531
sqrtHigh: num.UintOne().Sqrt(assetBase),
531532
isLower: true,
532533
l: ent.LowerVirtualLiquidity,
@@ -537,6 +538,7 @@ func definitionFromEntity(ent entities.AMMPool, position int64, priceFactor num.
537538
high: high,
538539
assetLow: assetBase,
539540
assetHigh: assetHigh,
541+
sqrtLow: num.UintOne().Sqrt(assetBase),
540542
sqrtHigh: num.UintOne().Sqrt(assetHigh),
541543
l: ent.UpperVirtualLiquidity,
542544
pv: ent.UpperTheoreticalPosition,

datanode/service/market_depth_amm_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,33 @@ func TestAMMSmallBounds(t *testing.T) {
250250
assert.Equal(t, 0, int(mds.service.GetVolumeAtPrice(marketID, types.SideSell, 2002)))
251251
}
252252

253+
func TestEstimatedStepOverAMMBound(t *testing.T) {
254+
ctx := context.Background()
255+
mds := getServiceWithConfig(t,
256+
service.MarketDepthConfig{
257+
AmmFullExpansionPercentage: 5,
258+
AmmEstimatedStepPercentage: 7.6, // make this a werid number so our estimated steps are not nice multiplies of 10
259+
AmmMaxEstimatedSteps: 5,
260+
},
261+
)
262+
defer mds.ctrl.Finish()
263+
264+
marketID := vgcrypto.RandomHash()
265+
ensureLiveOrders(t, mds, marketID)
266+
ensureDecimalPlaces(t, mds)
267+
mds.pos.EXPECT().GetByMarketAndParty(gomock.Any(), gomock.Any(), gomock.Any()).Return(entities.Position{OpenVolume: 0}, nil)
268+
mds.marketData.EXPECT().GetMarketDataByID(gomock.Any(), gomock.Any()).Times(1).Return(entities.MarketData{MidPrice: num.DecimalFromInt64(2000)}, nil)
269+
270+
// data node is starting from network history, initialise market-depth based on whats aleady there
271+
ensureAMMs(t, mds, marketID)
272+
mds.service.Initialise(ctx)
273+
274+
assert.Equal(t, "1999", mds.service.GetBestBidPrice(marketID).String())
275+
assert.Equal(t, "2001", mds.service.GetBestAskPrice(marketID).String())
276+
assert.Equal(t, 3, int(mds.service.GetVolumeAtPrice(marketID, types.SideBuy, 1999)))
277+
assert.Equal(t, 3, int(mds.service.GetVolumeAtPrice(marketID, types.SideSell, 2001)))
278+
}
279+
253280
func ensureLiveOrders(t *testing.T, mds *MDS, marketID string) {
254281
t.Helper()
255282
mds.orders.EXPECT().GetLiveOrders(gomock.Any()).Return([]entities.Order{

0 commit comments

Comments
 (0)