Skip to content

Commit

Permalink
feat: introduce AMM for future markets - phase 1
Browse files Browse the repository at this point in the history
  • Loading branch information
wwestgarth committed Apr 26, 2024
1 parent b9d006d commit 0f29607
Show file tree
Hide file tree
Showing 158 changed files with 24,557 additions and 6,703 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@

- [10702](https://github.com/vegaprotocol/vega/pull/10700) - Use second precision in `GetMarginIncrease()` so that perpetual product uses second precision everywhere.
- [10698](https://github.com/vegaprotocol/vega/issues/10698) - Prevent `L2` from making too many `RPC` calls.
- [971](https://github.com/vegaprotocol/core-test-coverage/issues/971) - Add `AMM` support to the integration test framework.

## 0.74.2

Expand Down Expand Up @@ -199,6 +200,9 @@
- [10611](https://github.com/vegaprotocol/vega/issues/10611) - Added internal config price to update `perps`.
- [10615](https://github.com/vegaprotocol/vega/issues/10615) - Fix oracle scaling function in internal composite price.
- [10621](https://github.com/vegaprotocol/vega/issues/10621) - Fix market activity tracker storing incorrect data for previous `epochMakerFeesPaid`.
- [10643](https://github.com/vegaprotocol/vega/issues/10643) - Games `API` not showing quantum values and added filter for team and party.

## 0.74.0

### 🚨 Breaking changes

Expand Down
1 change: 1 addition & 0 deletions cmd/data-node/commands/start/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ func (l *NodeCommand) createGRPCServer(config api.Config) *api.GRPCServer {
l.gamesService,
l.marginModesService,
l.timeWeightedNotionalPositionService,
l.ammPoolsService,
)
return grpcServer
}
7 changes: 7 additions & 0 deletions cmd/data-node/commands/start/sqlsubscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ type SQLSubscribers struct {
gamesStore *sqlstore.Games
marginModesStore *sqlstore.MarginModes
timeWeightedNotionalPositionStore *sqlstore.TimeWeightedNotionalPosition
ammPoolsStore *sqlstore.AMMPools

// Services
candleService *candlesv2.Svc
Expand Down Expand Up @@ -138,6 +139,7 @@ type SQLSubscribers struct {
gamesService *service.Games
marginModesService *service.MarginModes
timeWeightedNotionalPositionService *service.TimeWeightedNotionalPosition
ammPoolsService *service.AMMPools

// Subscribers
accountSub *sqlsubscribers.Account
Expand Down Expand Up @@ -190,6 +192,7 @@ type SQLSubscribers struct {
transactionResultsSub *sqlsubscribers.TransactionResults
marginModesSub *sqlsubscribers.MarginModes
timeWeightedNotionalPositionSub *sqlsubscribers.TimeWeightedNotionalPosition
ammPoolsSub *sqlsubscribers.AMMPools
}

func (s *SQLSubscribers) GetSQLSubscribers() []broker.SQLBrokerSubscriber {
Expand Down Expand Up @@ -246,6 +249,7 @@ func (s *SQLSubscribers) GetSQLSubscribers() []broker.SQLBrokerSubscriber {
s.transactionResultsSub,
s.marginModesSub,
s.timeWeightedNotionalPositionSub,
s.ammPoolsSub,
}
}

Expand Down Expand Up @@ -307,6 +311,7 @@ func (s *SQLSubscribers) CreateAllStores(ctx context.Context, Log *logging.Logge
s.gamesStore = sqlstore.NewGames(transactionalConnectionSource)
s.marginModesStore = sqlstore.NewMarginModes(transactionalConnectionSource)
s.timeWeightedNotionalPositionStore = sqlstore.NewTimeWeightedNotionalPosition(transactionalConnectionSource)
s.ammPoolsStore = sqlstore.NewAMMPools(transactionalConnectionSource)
}

func (s *SQLSubscribers) SetupServices(ctx context.Context, log *logging.Logger, candlesConfig candlesv2.Config) error {
Expand Down Expand Up @@ -363,6 +368,7 @@ func (s *SQLSubscribers) SetupServices(ctx context.Context, log *logging.Logger,
s.gamesService = service.NewGames(s.gamesStore)
s.marginModesService = service.NewMarginModes(s.marginModesStore)
s.timeWeightedNotionalPositionService = service.NewTimeWeightedNotionalPosition(s.timeWeightedNotionalPositionStore)
s.ammPoolsService = service.NewAMMPools(s.ammPoolsStore)

s.transactionResultsSub = sqlsubscribers.NewTransactionResults(log)
s.transactionResultsService = service.NewTransactionResults(s.transactionResultsSub)
Expand Down Expand Up @@ -432,4 +438,5 @@ func (s *SQLSubscribers) SetupSQLSubscribers() {
s.vestingSummarySub = sqlsubscribers.NewVestingBalancesSummary(s.partyVestingBalancesStore, s.partyLockedBalancesStore)
s.marginModesSub = sqlsubscribers.NewMarginModes(s.marginModesService)
s.timeWeightedNotionalPositionSub = sqlsubscribers.NewTimeWeightedNotionalPosition(s.timeWeightedNotionalPositionService)
s.ammPoolsSub = sqlsubscribers.NewAMMPools(s.ammPoolsService)
}
129 changes: 129 additions & 0 deletions commands/amend_amm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package commands

import (
"math/big"

"code.vegaprotocol.io/vega/libs/num"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
)

func CheckAmendAMM(cmd *commandspb.AmendAMM) error {
return checkAmendAMM(cmd).ErrorOrNil()
}

func checkAmendAMM(cmd *commandspb.AmendAMM) Errors {
errs := NewErrors()

if cmd == nil {
return errs.FinalAddForProperty("amend_amm", ErrIsRequired)
}

if len(cmd.MarketId) <= 0 {
errs.AddForProperty("amend_amm.market_id", ErrIsRequired)
} else if !IsVegaID(cmd.MarketId) {
errs.AddForProperty("amend_amm.market_id", ErrShouldBeAValidVegaID)
}

if len(cmd.SlippageTolerance) <= 0 {
errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsRequired)
} else if slippageTolerance, err := num.DecimalFromString(cmd.SlippageTolerance); err != nil {
errs.AddForProperty("amend_amm.slippage_tolerance", ErrIsNotValidNumber)
} else if slippageTolerance.LessThanOrEqual(num.DecimalZero()) || slippageTolerance.GreaterThan(num.DecimalOne()) {
errs.AddForProperty("amend_amm.slippage_tolerance", ErrMustBeBetween01)
}

var hasUpdate bool

if cmd.CommitmentAmount != nil {
hasUpdate = true
if amount, _ := big.NewInt(0).SetString(*cmd.CommitmentAmount, 10); amount == nil {
errs.FinalAddForProperty("amend_amm.commitment_amount", ErrIsNotValidNumber)
} else if amount.Cmp(big.NewInt(0)) <= 0 {
errs.AddForProperty("amend_amm.commitment_amount", ErrMustBePositive)
}
}

if cmd.ProposedFee != nil {
hasUpdate = true
if proposedFee, err := num.DecimalFromString(*cmd.ProposedFee); err != nil {
errs.AddForProperty("amend_amm.proposed_fee", ErrIsNotValid)
} else if proposedFee.LessThanOrEqual(num.DecimalZero()) {
errs.AddForProperty("amend_amm.proposed_fee", ErrMustBePositive)
}
}

if cmd.ConcentratedLiquidityParameters != nil {
if cmd.ConcentratedLiquidityParameters.Base != nil {
hasUpdate = true
if amount, _ := big.NewInt(0).SetString(*cmd.ConcentratedLiquidityParameters.Base, 10); amount == nil {
errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrIsNotValidNumber)
} else if amount.Cmp(big.NewInt(0)) <= 0 {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.base", ErrMustBePositive)
}
}
if cmd.ConcentratedLiquidityParameters.LowerBound != nil {
hasUpdate = true
if amount, _ := big.NewInt(0).SetString(*cmd.ConcentratedLiquidityParameters.LowerBound, 10); amount == nil {
errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.lower_bound", ErrIsNotValidNumber)
} else if amount.Cmp(big.NewInt(0)) <= 0 {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.lower_bound", ErrMustBePositive)
}
}
if cmd.ConcentratedLiquidityParameters.UpperBound != nil {
hasUpdate = true
if amount, _ := big.NewInt(0).SetString(*cmd.ConcentratedLiquidityParameters.UpperBound, 10); amount == nil {
errs.FinalAddForProperty("amend_amm.concentrated_liquidity_parameters.upper_bound", ErrIsNotValidNumber)
} else if amount.Cmp(big.NewInt(0)) <= 0 {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.upper_bound", ErrMustBePositive)
}
}

if cmd.ConcentratedLiquidityParameters.MarginRatioAtUpperBound != nil {
hasUpdate = true
if marginRatio, err := num.DecimalFromString(*cmd.ConcentratedLiquidityParameters.MarginRatioAtUpperBound); err != nil {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.margin_ratio_at_upper_bound", ErrIsNotValidNumber)
} else if marginRatio.LessThan(num.DecimalZero()) {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.margin_ratio_at_upper_bound", ErrMustBePositive)
}
}

if cmd.ConcentratedLiquidityParameters.MarginRatioAtLowerBound != nil {
hasUpdate = true
if marginRatio, err := num.DecimalFromString(*cmd.ConcentratedLiquidityParameters.MarginRatioAtLowerBound); err != nil {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.margin_ratio_at_lower_bound", ErrIsNotValidNumber)
} else if marginRatio.LessThan(num.DecimalZero()) {
errs.AddForProperty("amend_amm.concentrated_liquidity_parameters.margin_ratio_at_lower_bound", ErrMustBePositive)
}
}

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

// no update, but also no error, invalid
if !hasUpdate && errs.Empty() {
errs.FinalAdd(ErrNoUpdatesProvided)
}

return errs
}
Loading

0 comments on commit 0f29607

Please sign in to comment.