Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add liquidityProvisionMaxShapeSize network parameter #3060

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/vega/node/node_pre.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,10 @@ func (l *NodeCommand) setupNetParameters() error {
Param: netparams.MarketLiquidityProvidersFeeDistribitionTimeStep,
Watcher: l.executionEngine.OnMarketLiquidityProvidersFeeDistributionTimeStep,
},
netparams.WatchParam{
Param: netparams.MarketLiquidityProvisionShapesMaxSize,
Watcher: l.executionEngine.OnMarketLiquidityProvisionShapesMaxSizeUpdate,
},
)
}

Expand Down
126 changes: 125 additions & 1 deletion execution/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ type Engine struct {

broker Broker
time TimeService

npv netParamsValues
}

type netParamsValues struct {
shapesMaxSize int64
feeDistributionTimeStep time.Duration
timeWindowUpdate time.Duration
targetStakeScalingFactor float64
marketValueWindowLength time.Duration
suppliedStakeToObligationFactor float64
infrastructureFee float64
makerFee float64
scalingFactors *types.ScalingFactors
}

func defaultNetParamsValues() netParamsValues {
return netParamsValues{
shapesMaxSize: -1,
feeDistributionTimeStep: -1,
timeWindowUpdate: -1,
targetStakeScalingFactor: -1,
marketValueWindowLength: -1,
suppliedStakeToObligationFactor: -1,
infrastructureFee: -1,
makerFee: -1,
scalingFactors: nil,
}
}

// NewEngine takes stores and engines and returns
Expand All @@ -74,6 +102,7 @@ func NewEngine(
collateral: collateral,
idgen: NewIDGen(),
broker: broker,
npv: defaultNetParamsValues(),
}

// Add time change event handler
Expand Down Expand Up @@ -175,7 +204,6 @@ func (e *Engine) SubmitMarketWithLiquidityProvision(ctx context.Context, marketC
logging.LiquidityProvisionSubmission(*lp),
logging.PartyID(party),
logging.LiquidityID(lpID),

)
}

Expand Down Expand Up @@ -283,6 +311,61 @@ func (e *Engine) submitMarket(ctx context.Context, marketConfig *types.Market) e
// we ignore the response, this cannot fail as the asset
// is already proven to exists a few line before
_, _, _ = e.collateral.CreateMarketAccounts(ctx, marketConfig.Id, asset, e.Config.InsurancePoolInitialBalance)

if err := e.propagateInitialNetParams(ctx, mkt); err != nil {
return err
}

return nil
}

func (e *Engine) propagateInitialNetParams(ctx context.Context, mkt *Market) error {
if e.npv.shapesMaxSize != -1 {
if err := mkt.OnMarketLiquidityProvisionShapesMaxSizeUpdate(e.npv.shapesMaxSize); err != nil {
return err
}
}

if e.npv.targetStakeScalingFactor != -1 {
if err := mkt.OnMarketTargetStakeScalingFactorUpdate(e.npv.targetStakeScalingFactor); err != nil {
return err
}
}

if e.npv.infrastructureFee != -1 {
if err := mkt.OnFeeFactorsInfrastructureFeeUpdate(ctx, e.npv.infrastructureFee); err != nil {
return err
}
}

if e.npv.makerFee != -1 {
if err := mkt.OnFeeFactorsMakerFeeUpdate(ctx, e.npv.makerFee); err != nil {
return err
}
}

if e.npv.scalingFactors != nil {
if err := mkt.OnMarginScalingFactorsUpdate(ctx, e.npv.scalingFactors); err != nil {
return err
}
}

if e.npv.feeDistributionTimeStep != -1 {
mkt.OnMarketLiquidityProvidersFeeDistribitionTimeStep(e.npv.feeDistributionTimeStep)
}

if e.npv.timeWindowUpdate != -1 {
mkt.OnMarketTargetStakeTimeWindowUpdate(e.npv.timeWindowUpdate)
}

if e.npv.marketValueWindowLength != -1 {
mkt.OnMarketValueWindowLengthUpdate(e.npv.marketValueWindowLength)
}

if e.npv.suppliedStakeToObligationFactor != -1 {
mkt.OnSuppliedStakeToObligationFactorUpdate(e.npv.suppliedStakeToObligationFactor)
}

return nil
}

Expand Down Expand Up @@ -562,6 +645,9 @@ func (e *Engine) OnMarketMarginScalingFactorsUpdate(ctx context.Context, v inter
return err
}
}

e.npv.scalingFactors = scalingFactors

return nil
}

Expand All @@ -577,6 +663,9 @@ func (e *Engine) OnMarketFeeFactorsMakerFeeUpdate(ctx context.Context, f float64
return err
}
}

e.npv.makerFee = f

return nil
}

Expand All @@ -592,6 +681,9 @@ func (e *Engine) OnMarketFeeFactorsInfrastructureFeeUpdate(ctx context.Context,
return err
}
}

e.npv.infrastructureFee = f

return nil
}

Expand All @@ -605,6 +697,9 @@ func (e *Engine) OnSuppliedStakeToObligationFactorUpdate(_ context.Context, v fl
for _, mkt := range e.marketsCpy {
mkt.OnSuppliedStakeToObligationFactorUpdate(v)
}

e.npv.suppliedStakeToObligationFactor = v

return nil
}

Expand All @@ -618,6 +713,9 @@ func (e *Engine) OnMarketValueWindowLengthUpdate(_ context.Context, d time.Durat
for _, mkt := range e.marketsCpy {
mkt.OnMarketValueWindowLengthUpdate(d)
}

e.npv.marketValueWindowLength = d

return nil
}

Expand All @@ -633,6 +731,9 @@ func (e *Engine) OnMarketTargetStakeScalingFactorUpdate(_ context.Context, v flo
return err
}
}

e.npv.targetStakeScalingFactor = v

return nil
}

Expand All @@ -646,6 +747,9 @@ func (e *Engine) OnMarketTargetStakeTimeWindowUpdate(_ context.Context, d time.D
for _, mkt := range e.marketsCpy {
mkt.OnMarketTargetStakeTimeWindowUpdate(d)
}

e.npv.timeWindowUpdate = d

return nil
}

Expand All @@ -659,5 +763,25 @@ func (e *Engine) OnMarketLiquidityProvidersFeeDistributionTimeStep(_ context.Con
for _, mkt := range e.marketsCpy {
mkt.OnMarketLiquidityProvidersFeeDistribitionTimeStep(d)
}

e.npv.feeDistributionTimeStep = d

return nil
}

func (e *Engine) OnMarketLiquidityProvisionShapesMaxSizeUpdate(
_ context.Context, v int64) error {
if e.log.IsDebug() {
e.log.Debug("update liquidity provision max shape",
logging.Int64("max-shape", v),
)
}

for _, mkt := range e.marketsCpy {
mkt.OnMarketLiquidityProvisionShapesMaxSizeUpdate(v)
}

e.npv.shapesMaxSize = v

return nil
}
4 changes: 2 additions & 2 deletions execution/liquidity_provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,6 @@ func TestLiquidity_TooManyShapeLevels(t *testing.T) {
Sells: sells}

err := tm.market.SubmitLiquidityProvision(ctx, lps, "trader-A", "LPOrder01")
require.NoError(t, err)
assert.Equal(t, 1, tm.market.GetLPSCount())
require.EqualError(t, err, "SIDE_BUY shape size exceed max (100)")
assert.Equal(t, 0, tm.market.GetLPSCount())
}
4 changes: 4 additions & 0 deletions execution/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -2869,6 +2869,10 @@ func (m *Market) OnMarketTargetStakeScalingFactorUpdate(v float64) error {
return m.tsCalc.UpdateScalingFactor(v)
}

func (m *Market) OnMarketLiquidityProvisionShapesMaxSizeUpdate(v int64) error {
return m.liquidity.OnMarketLiquidityProvisionShapesMaxSizeUpdate(v)
}

// repriceFuncW is an adapter for getNewPeggedPrice.
func (m *Market) repriceFuncW(po *types.PeggedOrder) (uint64, error) {
return m.getNewPeggedPrice(
Expand Down
24 changes: 21 additions & 3 deletions liquidity/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ type Engine struct {

// undeployedProvisions flags that there are provisions within the engine that are not deployed
undeployedProvisions bool

// the maximum number of liquidity orders to be created on
// each shape
maxShapesSize int64
}

// NewEngine returns a new Liquidity Engine.
Expand All @@ -92,6 +96,7 @@ func NewEngine(
provisions: map[string]*types.LiquidityProvision{},
orders: map[string]map[string]*types.Order{},
liquidityOrders: map[string]map[string]*types.Order{},
maxShapesSize: 100, // set it to the same default than the netparams
}
}

Expand All @@ -105,6 +110,15 @@ func (e *Engine) OnSuppliedStakeToObligationFactorUpdate(v float64) {
e.stakeToObligationFactor = v
}

func (e *Engine) OnMarketLiquidityProvisionShapesMaxSizeUpdate(v int64) error {
if v < 0 {
return errors.New("shapes max size cannot be < 0")

}
e.maxShapesSize = v
return nil
}

func (e *Engine) stopLiquidityProvision(
ctx context.Context, party string, status types.LiquidityProvision_Status) ([]*types.Order, error) {
lp := e.provisions[party]
Expand Down Expand Up @@ -164,10 +178,11 @@ func (e *Engine) validateLiquidityProvisionSubmission(lp *types.LiquidityProvisi
if fee, err := strconv.ParseFloat(lp.Fee, 64); err != nil || fee <= 0 || len(lp.Fee) <= 0 || fee > 1.0 {
return errors.New("invalid liquidity provision fee")
}
if err := validateShape(lp.Buys, types.Side_SIDE_BUY); err != nil {

if err := validateShape(lp.Buys, types.Side_SIDE_BUY, e.maxShapesSize); err != nil {
return err
}
return validateShape(lp.Sells, types.Side_SIDE_SELL)
return validateShape(lp.Sells, types.Side_SIDE_SELL, e.maxShapesSize)
}

func (e *Engine) rejectLiquidityProvisionSubmission(ctx context.Context, lps *types.LiquidityProvisionSubmission, party, id string) {
Expand Down Expand Up @@ -562,10 +577,13 @@ func (e *Engine) createOrdersFromShape(party string, supplied []*supplied.Liquid
return newOrders, amendments
}

func validateShape(sh []*types.LiquidityOrder, side types.Side) error {
func validateShape(sh []*types.LiquidityOrder, side types.Side, maxSize int64) error {
if len(sh) <= 0 {
return fmt.Errorf("empty %v shape", side)
}
if len(sh) > int(maxSize) {
return fmt.Errorf("%v shape size exceed max (%v)", side, maxSize)
}
for _, lo := range sh {
if lo.Reference == types.PeggedReference_PEGGED_REFERENCE_UNSPECIFIED {
// We must specify a valid reference
Expand Down
1 change: 1 addition & 0 deletions netparams/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func defaultNetParams() map[string]value {
MarketValueWindowLength: NewDuration(DurationGT(0 * time.Second)).Mutable(true).MustUpdate(week),
MarketPriceMonitoringDefaultParameters: NewJSON(&proto.PriceMonitoringParameters{}, JSONProtoValidator()).Mutable(true).MustUpdate(`{"triggers": []}`),
MarketPriceMonitoringUpdateFrequency: NewDuration(DurationGT(0 * time.Second)).Mutable(true).MustUpdate("1m0s"),
MarketLiquidityProvisionShapesMaxSize: NewInt(IntGT(0)).Mutable(true).MustUpdate("100"),

// governance market proposal
GovernanceProposalMarketMinClose: NewDuration(DurationGT(0 * time.Second)).Mutable(true).MustUpdate("48h0m0s"),
Expand Down
2 changes: 2 additions & 0 deletions netparams/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
MarketValueWindowLength = "market.value.windowLength"
MarketPriceMonitoringDefaultParameters = "market.monitor.price.defaultParameters"
MarketPriceMonitoringUpdateFrequency = "market.monitor.price.updateFrequency"
MarketLiquidityProvisionShapesMaxSize = "market.liquidityProvision.shapes.maxSize"

GovernanceVoteAsset = "governance.vote.asset"

Expand Down Expand Up @@ -115,4 +116,5 @@ var AllKeys = map[string]struct{}{
GovernanceProposalUpdateNetParamMinProposerBalance: {},
GovernanceProposalUpdateNetParamMinVoterBalance: {},
BlockchainsEthereumConfig: {},
MarketLiquidityProvisionShapesMaxSize: {},
}