code.vegaprotocol.io/vega@v0.79.0/core/governance/engine_new_automated_purchase.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package governance 17 18 import ( 19 "fmt" 20 "time" 21 22 "code.vegaprotocol.io/vega/core/assets" 23 "code.vegaprotocol.io/vega/core/types" 24 ) 25 26 func (e *Engine) validateNewProtocolAutomatedPurchaseConfiguration(automatedPurchase *types.NewProtocolAutomatedPurchase, et *enactmentTime, currentTime time.Time) (types.ProposalError, error) { 27 if _, ok := e.markets.GetMarket(automatedPurchase.Changes.MarketID, false); !ok { 28 return types.ProposalErrorInvalidMarket, ErrMarketDoesNotExist 29 } 30 if !e.assets.IsEnabled(automatedPurchase.Changes.From) { 31 return types.ProposalErrorInvalidAsset, assets.ErrAssetDoesNotExist 32 } 33 mkt, _ := e.markets.GetMarket(automatedPurchase.Changes.MarketID, false) 34 if mkt.GetSpot() == nil { 35 return types.ProposalErrorInvalidMarket, fmt.Errorf("market for automated purchase must be a spot market") 36 } 37 spot := mkt.GetSpot().Spot 38 if automatedPurchase.Changes.From != spot.BaseAsset && automatedPurchase.Changes.From != spot.QuoteAsset { 39 return types.ProposalErrorInvalidMarket, fmt.Errorf("mismatch between asset for automated purchase and the spot market configuration - asset is not one of base/quote assets of the market") 40 } 41 if mkt.State == types.MarketStateClosed || mkt.State == types.MarketStateCancelled || mkt.State == types.MarketStateRejected || mkt.State == types.MarketStateTradingTerminated || mkt.State == types.MarketStateSettled { 42 return types.ProposalErrorInvalidMarket, fmt.Errorf("market for automated purchase must be active") 43 } 44 if papConfigured, _ := e.markets.MarketHasActivePAP(automatedPurchase.Changes.MarketID); papConfigured { 45 return types.ProposalErrorInvalidMarket, fmt.Errorf("market already has an active protocol automated purchase program") 46 } 47 48 tt := automatedPurchase.Changes.AuctionSchedule.GetInternalTimeTriggerSpecConfiguration() 49 currentTime = currentTime.Truncate(time.Second) 50 if tt.Triggers[0].Initial == nil { 51 tt.SetInitial(time.Unix(et.current, 0), currentTime) 52 } 53 tt.SetNextTrigger(currentTime) 54 55 tt = automatedPurchase.Changes.AuctionVolumeSnapshotSchedule.GetInternalTimeTriggerSpecConfiguration() 56 currentTime = currentTime.Truncate(time.Second) 57 if tt.Triggers[0].Initial == nil { 58 tt.SetInitial(time.Unix(et.current, 0), currentTime) 59 } 60 tt.SetNextTrigger(currentTime) 61 62 return types.ProposalErrorUnspecified, nil 63 }