Skip to content

Commit 030f019

Browse files
committed
feat: allow AMM base price to be automatically amended by an oracle price
1 parent 260cc2e commit 030f019

34 files changed

+3474
-2402
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- [11714](https://github.com/vegaprotocol/vega/issues/11714) - Improve `AMM` performance by caching best prices and volumes.
1818
- [11642](https://github.com/vegaprotocol/vega/issues/11642) - `AMMs` with empty price levels are now allowed.
1919
- [11685](https://github.com/vegaprotocol/vega/issues/11685) - Automated purchase support added.
20+
- [11732](https://github.com/vegaprotocol/vega/issues/11732) - `AMMs` can now have their base price automatically updated by a market's data source.
2021
- [11726](https://github.com/vegaprotocol/vega/issues/11726) - Combined `AMM` uncrossing orders for better performance when uncrossing the book.
2122
- [11711](https://github.com/vegaprotocol/vega/issues/11711) - Manage closed team membership by updating the allow list.
2223
- [11722](https://github.com/vegaprotocol/vega/issues/11722) - Expose active protocol automated purchase identifier in market data API.

commands/amend_amm.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,26 @@ func checkAmendAMM(cmd *commandspb.AmendAMM) Errors {
6868
}
6969
}
7070

71+
if cmd.MinimumPriceChangeTrigger != nil {
72+
if minPriceChange, err := num.DecimalFromString(*cmd.MinimumPriceChangeTrigger); err != nil {
73+
errs.AddForProperty("submit_amm.mimimum_price_change_trigger", ErrIsNotValid)
74+
} else if minPriceChange.LessThan(num.DecimalZero()) {
75+
errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositiveOrZero)
76+
}
77+
}
78+
7179
if cmd.ConcentratedLiquidityParameters != nil {
80+
var haveLower, haveUpper, emptyBase bool
7281
hasUpdate = true
7382
var base, lowerBound, upperBound *big.Int
74-
if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
83+
if len(cmd.ConcentratedLiquidityParameters.Base) == 0 {
84+
emptyBase = true
85+
} else if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
7586
errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsNotValidNumber)
7687
} else if base.Cmp(big.NewInt(0)) <= 0 {
7788
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrMustBePositive)
7889
}
7990

80-
var haveLower, haveUpper bool
8191
if cmd.ConcentratedLiquidityParameters.LowerBound != nil {
8292
haveLower = true
8393
if lowerBound, _ = big.NewInt(0).SetString(*cmd.ConcentratedLiquidityParameters.LowerBound, 10); lowerBound == nil {
@@ -124,11 +134,19 @@ func checkAmendAMM(cmd *commandspb.AmendAMM) Errors {
124134
}
125135

126136
if len(cmd.SlippageTolerance) <= 0 {
127-
errs.AddForProperty("submit_amm.slippage_tolerance", ErrIsRequired)
137+
errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsRequired)
128138
} else if slippageTolerance, err := num.DecimalFromString(cmd.SlippageTolerance); err != nil {
129-
errs.AddForProperty("submit_amm.slippage_tolerance", ErrIsNotValidNumber)
139+
errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsNotValidNumber)
130140
} else if slippageTolerance.LessThan(num.DecimalZero()) {
131-
errs.AddForProperty("submit_amm.slippage_tolerance", ErrMustBePositive)
141+
errs.AddForProperty("amend_amm.slippage_tolerance", ErrMustBePositive)
142+
}
143+
144+
if cmd.ConcentratedLiquidityParameters.DataSourceId == nil && emptyBase {
145+
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
146+
}
147+
148+
if cmd.ConcentratedLiquidityParameters.DataSourceId != nil && !IsVegaID(*cmd.ConcentratedLiquidityParameters.DataSourceId) {
149+
errs.AddForProperty("amend_amm.data_source_id", ErrShouldBeAValidVegaID)
132150
}
133151
}
134152

commands/amend_amm_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestCheckAmendAMM(t *testing.T) {
125125
Base: "",
126126
},
127127
},
128-
errStr: "amend_amm.concentrated_liquidity_parameters.base (is not a valid number)",
128+
errStr: "amend_amm.concentrated_liquidity_parameters.base (is required)",
129129
},
130130
{
131131
submission: commandspb.AmendAMM{

commands/submit_amm.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,28 @@ func checkSubmitAMM(cmd *commandspb.SubmitAMM) Errors {
6464
errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositive)
6565
}
6666

67+
if cmd.MinimumPriceChangeTrigger != nil {
68+
if minPriceChange, err := num.DecimalFromString(*cmd.MinimumPriceChangeTrigger); err != nil {
69+
errs.AddForProperty("submit_amm.mimimum_price_change_trigger", ErrIsNotValid)
70+
} else if minPriceChange.LessThan(num.DecimalZero()) {
71+
errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositiveOrZero)
72+
}
73+
}
74+
6775
if cmd.ConcentratedLiquidityParameters == nil {
6876
errs.FinalAddForProperty("submit_amm.concentrated_liquidity_parameters", ErrIsRequired)
6977
} else {
78+
var emptyLower, emptyUpper, emptyBase bool
7079
var base, lowerBound, upperBound *big.Int
7180

7281
if len(cmd.ConcentratedLiquidityParameters.Base) <= 0 {
73-
errs.FinalAddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
82+
emptyBase = true
7483
} else if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
7584
errs.FinalAddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrIsNotValidNumber)
7685
} else if base.Cmp(big.NewInt(0)) <= 0 {
7786
errs.AddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrMustBePositive)
7887
}
7988

80-
var emptyLower, emptyUpper bool
8189
if cmd.ConcentratedLiquidityParameters.LowerBound == nil {
8290
emptyLower = true
8391
} else if len(*cmd.ConcentratedLiquidityParameters.LowerBound) <= 0 {
@@ -139,6 +147,14 @@ func checkSubmitAMM(cmd *commandspb.SubmitAMM) Errors {
139147
if base != nil && upperBound != nil && base.Cmp(upperBound) >= 0 {
140148
errs.AddForProperty("submit_amm.concentrated_liquidity_parameters.base", errors.New("should be a smaller value than upper_bound"))
141149
}
150+
151+
if cmd.ConcentratedLiquidityParameters.DataSourceId == nil && emptyBase {
152+
errs.AddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
153+
}
154+
155+
if cmd.ConcentratedLiquidityParameters.DataSourceId != nil && !IsVegaID(*cmd.ConcentratedLiquidityParameters.DataSourceId) {
156+
errs.AddForProperty("submit_amm.data_source_id", ErrShouldBeAValidVegaID)
157+
}
142158
}
143159

144160
return errs

core/events/amm_pool.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,23 @@ func NewAMMPoolEvent(
5454
fees num.Decimal,
5555
lowerCurve *AMMCurve,
5656
upperCurve *AMMCurve,
57+
minimumPriceChangeTrigger num.Decimal,
5758
) *AMMPool {
5859
return &AMMPool{
5960
Base: newBase(ctx, AMMPoolEvent),
6061
pool: &eventspb.AMM{
61-
Id: poolID,
62-
PartyId: party,
63-
MarketId: market,
64-
AmmPartyId: ammParty,
65-
Commitment: commitment.String(),
66-
Parameters: p.ToProtoEvent(),
67-
Status: status,
68-
StatusReason: statusReason,
69-
ProposedFee: fees.String(),
70-
LowerCurve: lowerCurve.ToProtoEvent(),
71-
UpperCurve: upperCurve.ToProtoEvent(),
62+
Id: poolID,
63+
PartyId: party,
64+
MarketId: market,
65+
AmmPartyId: ammParty,
66+
Commitment: commitment.String(),
67+
Parameters: p.ToProtoEvent(),
68+
Status: status,
69+
StatusReason: statusReason,
70+
ProposedFee: fees.String(),
71+
LowerCurve: lowerCurve.ToProtoEvent(),
72+
UpperCurve: upperCurve.ToProtoEvent(),
73+
MinimumPriceChangeTrigger: minimumPriceChangeTrigger.String(),
7274
},
7375
}
7476
}

core/execution/amm/engine.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ func (e *Engine) Create(
695695
e.positionFactor,
696696
e.maxCalculationLevels,
697697
e.allowedEmptyAMMLevels,
698+
submit.SlippageTolerance,
699+
submit.MinimumPriceChangeTrigger,
698700
)
699701
if err != nil {
700702
return nil, err
@@ -727,7 +729,6 @@ func (e *Engine) Confirm(
727729
logging.String("poolID", pool.ID),
728730
)
729731

730-
pool.status = types.AMMPoolStatusActive
731732
pool.maxCalculationLevels = e.maxCalculationLevels
732733

733734
e.add(pool)
@@ -772,6 +773,23 @@ func (e *Engine) Amend(
772773
return updated, pool, nil
773774
}
774775

776+
// GetDataSourcedAMMs returns any AMM's whose base price is determined by the given data source ID.
777+
func (e *Engine) GetDataSourcedAMMs(dataSourceID string) []*Pool {
778+
pools := []*Pool{}
779+
for _, p := range e.poolsCpy {
780+
if p.Parameters.DataSourceID == nil {
781+
continue
782+
}
783+
784+
if *p.Parameters.DataSourceID != dataSourceID {
785+
continue
786+
}
787+
788+
pools = append(pools, p)
789+
}
790+
return pools
791+
}
792+
775793
func (e *Engine) CancelAMM(
776794
ctx context.Context,
777795
cancel *types.CancelAMM,
@@ -849,6 +867,7 @@ func (e *Engine) sendUpdate(ctx context.Context, pool *Pool) {
849867
VirtualLiquidity: pool.upper.l,
850868
TheoreticalPosition: pool.upper.pv,
851869
},
870+
pool.MinimumPriceChangeTrigger,
852871
),
853872
)
854873
}

0 commit comments

Comments
 (0)