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

feat: allow AMM base price to be automatically amended by an oracle p… #11740

Merged
merged 1 commit into from
Oct 21, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [11714](https://github.com/vegaprotocol/vega/issues/11714) - Improve `AMM` performance by caching best prices and volumes.
- [11642](https://github.com/vegaprotocol/vega/issues/11642) - `AMMs` with empty price levels are now allowed.
- [11685](https://github.com/vegaprotocol/vega/issues/11685) - Automated purchase support added.
- [11732](https://github.com/vegaprotocol/vega/issues/11732) - `AMMs` can now have their base price automatically updated by a market's data source.
- [11726](https://github.com/vegaprotocol/vega/issues/11726) - Combined `AMM` uncrossing orders for better performance when uncrossing the book.
- [11711](https://github.com/vegaprotocol/vega/issues/11711) - Manage closed team membership by updating the allow list.
- [11722](https://github.com/vegaprotocol/vega/issues/11722) - Expose active protocol automated purchase identifier in market data API.
Expand Down
28 changes: 23 additions & 5 deletions commands/amend_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,26 @@ func checkAmendAMM(cmd *commandspb.AmendAMM) Errors {
}
}

if cmd.MinimumPriceChangeTrigger != nil {
if minPriceChange, err := num.DecimalFromString(*cmd.MinimumPriceChangeTrigger); err != nil {
errs.AddForProperty("submit_amm.mimimum_price_change_trigger", ErrIsNotValid)
} else if minPriceChange.LessThan(num.DecimalZero()) {
errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositiveOrZero)
}
}

if cmd.ConcentratedLiquidityParameters != nil {
var haveLower, haveUpper, emptyBase bool
hasUpdate = true
var base, lowerBound, upperBound *big.Int
if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
if len(cmd.ConcentratedLiquidityParameters.Base) == 0 {
emptyBase = true
} else if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsNotValidNumber)
} else if base.Cmp(big.NewInt(0)) <= 0 {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrMustBePositive)
}

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

if len(cmd.SlippageTolerance) <= 0 {
errs.AddForProperty("submit_amm.slippage_tolerance", ErrIsRequired)
errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsRequired)
} else if slippageTolerance, err := num.DecimalFromString(cmd.SlippageTolerance); err != nil {
errs.AddForProperty("submit_amm.slippage_tolerance", ErrIsNotValidNumber)
errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsNotValidNumber)
} else if slippageTolerance.LessThan(num.DecimalZero()) {
errs.AddForProperty("submit_amm.slippage_tolerance", ErrMustBePositive)
errs.AddForProperty("amend_amm.slippage_tolerance", ErrMustBePositive)
}

if cmd.ConcentratedLiquidityParameters.DataSourceId == nil && emptyBase {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
}

if cmd.ConcentratedLiquidityParameters.DataSourceId != nil && !IsVegaID(*cmd.ConcentratedLiquidityParameters.DataSourceId) {
errs.AddForProperty("amend_amm.data_source_id", ErrShouldBeAValidVegaID)
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/amend_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestCheckAmendAMM(t *testing.T) {
Base: "",
},
},
errStr: "amend_amm.concentrated_liquidity_parameters.base (is not a valid number)",
errStr: "amend_amm.concentrated_liquidity_parameters.base (is required)",
},
{
submission: commandspb.AmendAMM{
Expand Down
20 changes: 18 additions & 2 deletions commands/submit_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,28 @@ func checkSubmitAMM(cmd *commandspb.SubmitAMM) Errors {
errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositive)
}

if cmd.MinimumPriceChangeTrigger != nil {
if minPriceChange, err := num.DecimalFromString(*cmd.MinimumPriceChangeTrigger); err != nil {
errs.AddForProperty("submit_amm.mimimum_price_change_trigger", ErrIsNotValid)
} else if minPriceChange.LessThan(num.DecimalZero()) {
errs.AddForProperty("submit_amm.proposed_fee", ErrMustBePositiveOrZero)
}
}

if cmd.ConcentratedLiquidityParameters == nil {
errs.FinalAddForProperty("submit_amm.concentrated_liquidity_parameters", ErrIsRequired)
} else {
var emptyLower, emptyUpper, emptyBase bool
var base, lowerBound, upperBound *big.Int

if len(cmd.ConcentratedLiquidityParameters.Base) <= 0 {
errs.FinalAddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
emptyBase = true
} else if base, _ = big.NewInt(0).SetString(cmd.ConcentratedLiquidityParameters.Base, 10); base == nil {
errs.FinalAddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrIsNotValidNumber)
} else if base.Cmp(big.NewInt(0)) <= 0 {
errs.AddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrMustBePositive)
}

var emptyLower, emptyUpper bool
if cmd.ConcentratedLiquidityParameters.LowerBound == nil {
emptyLower = true
} else if len(*cmd.ConcentratedLiquidityParameters.LowerBound) <= 0 {
Expand Down Expand Up @@ -139,6 +147,14 @@ func checkSubmitAMM(cmd *commandspb.SubmitAMM) Errors {
if base != nil && upperBound != nil && base.Cmp(upperBound) >= 0 {
errs.AddForProperty("submit_amm.concentrated_liquidity_parameters.base", errors.New("should be a smaller value than upper_bound"))
}

if cmd.ConcentratedLiquidityParameters.DataSourceId == nil && emptyBase {
errs.AddForProperty("submit_amm.concentrated_liquidity_parameters.base", ErrIsRequired)
}

if cmd.ConcentratedLiquidityParameters.DataSourceId != nil && !IsVegaID(*cmd.ConcentratedLiquidityParameters.DataSourceId) {
errs.AddForProperty("submit_amm.data_source_id", ErrShouldBeAValidVegaID)
}
}

return errs
Expand Down
24 changes: 13 additions & 11 deletions core/events/amm_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,23 @@ func NewAMMPoolEvent(
fees num.Decimal,
lowerCurve *AMMCurve,
upperCurve *AMMCurve,
minimumPriceChangeTrigger num.Decimal,
) *AMMPool {
return &AMMPool{
Base: newBase(ctx, AMMPoolEvent),
pool: &eventspb.AMM{
Id: poolID,
PartyId: party,
MarketId: market,
AmmPartyId: ammParty,
Commitment: commitment.String(),
Parameters: p.ToProtoEvent(),
Status: status,
StatusReason: statusReason,
ProposedFee: fees.String(),
LowerCurve: lowerCurve.ToProtoEvent(),
UpperCurve: upperCurve.ToProtoEvent(),
Id: poolID,
PartyId: party,
MarketId: market,
AmmPartyId: ammParty,
Commitment: commitment.String(),
Parameters: p.ToProtoEvent(),
Status: status,
StatusReason: statusReason,
ProposedFee: fees.String(),
LowerCurve: lowerCurve.ToProtoEvent(),
UpperCurve: upperCurve.ToProtoEvent(),
MinimumPriceChangeTrigger: minimumPriceChangeTrigger.String(),
},
}
}
Expand Down
21 changes: 20 additions & 1 deletion core/execution/amm/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,8 @@ func (e *Engine) Create(
e.positionFactor,
e.maxCalculationLevels,
e.allowedEmptyAMMLevels,
submit.SlippageTolerance,
submit.MinimumPriceChangeTrigger,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -727,7 +729,6 @@ func (e *Engine) Confirm(
logging.String("poolID", pool.ID),
)

pool.status = types.AMMPoolStatusActive
pool.maxCalculationLevels = e.maxCalculationLevels

e.add(pool)
Expand Down Expand Up @@ -772,6 +773,23 @@ func (e *Engine) Amend(
return updated, pool, nil
}

// GetDataSourcedAMMs returns any AMM's whose base price is determined by the given data source ID.
func (e *Engine) GetDataSourcedAMMs(dataSourceID string) []*Pool {
pools := []*Pool{}
for _, p := range e.poolsCpy {
if p.Parameters.DataSourceID == nil {
continue
}

if *p.Parameters.DataSourceID != dataSourceID {
continue
}

pools = append(pools, p)
}
return pools
}

func (e *Engine) CancelAMM(
ctx context.Context,
cancel *types.CancelAMM,
Expand Down Expand Up @@ -849,6 +867,7 @@ func (e *Engine) sendUpdate(ctx context.Context, pool *Pool) {
VirtualLiquidity: pool.upper.l,
TheoreticalPosition: pool.upper.pv,
},
pool.MinimumPriceChangeTrigger,
),
)
}
Expand Down
Loading
Loading