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: add pap state to market data #11723

Merged
merged 1 commit into from
Oct 1, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [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.
- [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.

### 🐛 Fixes

Expand All @@ -26,7 +27,7 @@
- [11684](https://github.com/vegaprotocol/vega/issues/11684) - Better error when `Arbitrum` bridge details are missing from validator configuration.
- [11696](https://github.com/vegaprotocol/vega/issues/11696) - Add binding for estimate fees API.
- [11699](https://github.com/vegaprotocol/vega/issues/11699) - Update factors of programs when they are updated.

- [11724](https://github.com/vegaprotocol/vega/issues/11724) - Allow nil initial time in time trigger.

## 0.78.2

Expand Down
2 changes: 1 addition & 1 deletion commands/transfer_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func checkTransfer(cmd *commandspb.Transfer) (e Errors) {
} else {
switch k := cmd.Kind.(type) {
case *commandspb.Transfer_OneOff:
if cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GENERAL && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_UNSPECIFIED && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY {
if cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GENERAL && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_UNSPECIFIED && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_BUY_BACK_FEES {
errs.AddForProperty("transfer.to_account_type", errors.New("account type is not valid for one off transfer"))
}
if k.OneOff.GetDeliverOn() < 0 {
Expand Down
17 changes: 17 additions & 0 deletions core/execution/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,18 @@ func (m *Market) GetMarketData() types.MarketData {
mode = m.mkt.TradingMode
}

var papState *vega.ProtocolAutomatedPurchaseData
if m.pap != nil {
var activeOrder *string
if len(m.pap.activeOrder) > 0 {
activeOrder = &m.pap.activeOrder
}
papState = &vega.ProtocolAutomatedPurchaseData{
Id: m.pap.ID,
OrderId: activeOrder,
}
}

return types.MarketData{
Market: m.GetID(),
BestBidPrice: m.priceToMarketPrecision(bestBidPrice),
Expand Down Expand Up @@ -471,6 +483,7 @@ func (m *Market) GetMarketData() types.MarketData {
MarketValueProxy: m.lastMarketValueProxy.BigInt().String(),
LiquidityProviderFeeShare: m.equityShares.LpsToLiquidityProviderFeeShare(m.liquidity.GetAverageLiquidityScores()),
LiquidityProviderSLA: m.liquidityEngine.LiquidityProviderSLAStats(m.timeService.GetTimeNow()),
PAPState: papState,
}
}

Expand Down Expand Up @@ -690,6 +703,8 @@ func (m *Market) OnTick(ctx context.Context, t time.Time) bool {
return true
}

m.checkPAP(ctx)

// first we expire orders
if !m.closed && m.canTrade() {
expired := m.removeExpiredOrders(ctx, t.UnixNano())
Expand Down Expand Up @@ -3544,3 +3559,5 @@ func (m *Market) getACcountTypesForPAP() (types.AccountType, types.AccountType,
}
return m.pap.getACcountTypesForPAP()
}

func (m *Market) BeginBlock(ctx context.Context) {}
56 changes: 18 additions & 38 deletions core/execution/spot/protocol_automated_purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

"code.vegaprotocol.io/vega/core/datasource"
dscommon "code.vegaprotocol.io/vega/core/datasource/common"
"code.vegaprotocol.io/vega/core/datasource/definition"
dsdefinition "code.vegaprotocol.io/vega/core/datasource/definition"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/execution/common"
"code.vegaprotocol.io/vega/core/products"
Expand Down Expand Up @@ -87,32 +87,20 @@ func (m *Market) NewProtocolAutomatedPurchase(ctx context.Context, ID string, co
readyToStop: false,
side: side,
}
specDef, err := definition.FromProto(config.PriceOracle, nil)

auctionVolumeSnapshotSchedule := datasource.SpecFromDefinition(config.AuctionVolumeSnapshotSchedule)
auctionSchedule := datasource.SpecFromDefinition(config.AuctionSchedule)
var err error
pap.scheuldingOracles, err = products.NewProtocolAutomatedPurchaseScheduleOracle(ctx, oracleEngine, auctionSchedule, auctionVolumeSnapshotSchedule, datasource.SpecBindingForAutomatedPurchaseFromProto(config.AutomatedPurchaseSpecBinding), m.papAuctionSchedule, m.papAuctionVolumeSnapshot)
if err != nil {
return err
}
dataSource := datasource.SpecFromDefinition(*definition.NewWith(specDef))
oracle, err := products.NewCompositePriceOracle(ctx, oracleEngine, dataSource, datasource.SpecBindingForCompositePriceFromProto(config.PriceOracleBinding), m.updatePAPPriceOracle)
oracle, err := products.NewCompositePriceOracle(ctx, oracleEngine, config.PriceOracle, datasource.SpecBindingForCompositePriceFromProto(config.PriceOracleBinding), m.updatePAPPriceOracle)
if err != nil {
return err
}
pap.priceOracle = oracle
auctionScheduleSpecDef, err := definition.FromProto(config.AuctionSchedule, nil)
if err != nil {
return err
}
auctionSchedule := datasource.SpecFromDefinition(*definition.NewWith(auctionScheduleSpecDef))
auctionSchedule.Data.GetInternalTimeTriggerSpecConfiguration().Triggers[0].SetNextTrigger(m.timeService.GetTimeNow())
auctionVolumeSnapshotScheduleSpecDef, err := definition.FromProto(config.AuctionVolumeSnapshotSchedule, nil)
if err != nil {
return err
}
auctionVolumeSnapshotSchedule := datasource.SpecFromDefinition(*definition.NewWith(auctionVolumeSnapshotScheduleSpecDef))
auctionVolumeSnapshotSchedule.Data.GetInternalTimeTriggerSpecConfiguration().Triggers[0].SetNextTrigger(m.timeService.GetTimeNow())
pap.scheuldingOracles, err = products.NewProtocolAutomatedPurchaseScheduleOracle(ctx, oracleEngine, auctionSchedule, auctionVolumeSnapshotSchedule, datasource.SpecBindingForAutomatedPurchaseFromProto(config.AutomatedPurchaseSpecBinding), m.papAuctionSchedule, m.papAuctionVolumeSnapshot)
if err != nil {
return err
}

m.pap = pap
return nil
}
Expand All @@ -136,26 +124,18 @@ func (m *Market) NewProtocolAutomatedPurchaseFromSnapshot(ctx context.Context, o
ap.nextAuctionAmount = num.MustUintFromString(apProto.NextAuctionAmount, 10)
}

specDef, err := definition.FromProto(ap.config.PriceOracle, nil)
if err != nil {
return nil, err
}
dataSource := datasource.SpecFromDefinition(*definition.NewWith(specDef))
oracle, err := products.NewCompositePriceOracle(ctx, oracleEngine, dataSource, datasource.SpecBindingForCompositePriceFromProto(ap.config.PriceOracleBinding), m.updatePAPPriceOracle)
specDef, _ := dsdefinition.FromProto(apProto.Config.PriceOracle, nil)
priceOracle := datasource.SpecFromDefinition(*dsdefinition.NewWith(specDef))

oracle, err := products.NewCompositePriceOracle(ctx, oracleEngine, priceOracle, datasource.SpecBindingForCompositePriceFromProto(apProto.Config.PriceOracleSpecBinding), m.updatePAPPriceOracle)
if err != nil {
return nil, err
}
ap.priceOracle = oracle
auctionScheduleSpecDef, err := definition.FromProto(ap.config.AuctionSchedule, nil)
if err != nil {
return nil, err
}
auctionSchedule := datasource.SpecFromDefinition(*definition.NewWith(auctionScheduleSpecDef))
auctionVolumeSnapshotScheduleSpecDef, err := definition.FromProto(ap.config.AuctionVolumeSnapshotSchedule, nil)
if err != nil {
return nil, err
}
auctionVolumeSnapshotSchedule := datasource.SpecFromDefinition(*definition.NewWith(auctionVolumeSnapshotScheduleSpecDef))
auctionSchedule := datasource.SpecFromDefinition(ap.config.AuctionSchedule)
auctionSchedule.Data.GetInternalTimeTriggerSpecConfiguration().Triggers[0].SetNextTrigger(m.timeService.GetTimeNow().Truncate(time.Second))
auctionVolumeSnapshotSchedule := datasource.SpecFromDefinition(ap.config.AuctionVolumeSnapshotSchedule)
auctionVolumeSnapshotSchedule.Data.GetInternalTimeTriggerSpecConfiguration().Triggers[0].SetNextTrigger(m.timeService.GetTimeNow().Truncate(time.Second))
ap.scheuldingOracles, err = products.NewProtocolAutomatedPurchaseScheduleOracle(ctx, oracleEngine, auctionSchedule, auctionVolumeSnapshotSchedule, datasource.SpecBindingForAutomatedPurchaseFromProto(ap.config.AutomatedPurchaseSpecBinding), m.papAuctionSchedule, m.papAuctionVolumeSnapshot)
if err != nil {
return nil, err
Expand Down Expand Up @@ -346,8 +326,8 @@ func (m *Market) stopPAP(ctx context.Context) {
m.pap.nextAuctionAmount = nil
}

// BeginBlock checks if a pap has expired, if so and it.
func (m *Market) BeginBlock(ctx context.Context) {
// checkPAP checks if a pap has expired, if so and it.
func (m *Market) checkPAP(ctx context.Context) {
// no pap - nothing to do
if m.pap == nil {
return
Expand Down
2 changes: 1 addition & 1 deletion core/governance/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ func (e *Engine) validateChange(terms *types.ProposalTerms) (types.ProposalError
return validateUpdateVolumeRebateProgram(e.netp, terms.GetUpdateVolumeRebateProgram())
case types.ProposalTermsTypeNewProtocolAutomatedPurchase:
automatedPurchase := terms.GetAutomatedPurchase()
return e.validateNewProtocolAutomatedPurchaseConfiguration(automatedPurchase)
return e.validateNewProtocolAutomatedPurchaseConfiguration(automatedPurchase, enct, e.timeService.GetTimeNow())
default:
return types.ProposalErrorUnspecified, nil
}
Expand Down
18 changes: 17 additions & 1 deletion core/governance/engine_new_automated_purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ package governance

import (
"fmt"
"time"

"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/types"
)

func (e *Engine) validateNewProtocolAutomatedPurchaseConfiguration(automatedPurchase *types.NewProtocolAutomatedPurchase) (types.ProposalError, error) {
func (e *Engine) validateNewProtocolAutomatedPurchaseConfiguration(automatedPurchase *types.NewProtocolAutomatedPurchase, et *enactmentTime, currentTime time.Time) (types.ProposalError, error) {
if _, ok := e.markets.GetMarket(automatedPurchase.Changes.MarketID, false); !ok {
return types.ProposalErrorInvalidMarket, ErrMarketDoesNotExist
}
Expand All @@ -43,5 +44,20 @@ func (e *Engine) validateNewProtocolAutomatedPurchaseConfiguration(automatedPurc
if papConfigured, _ := e.markets.MarketHasActivePAP(automatedPurchase.Changes.MarketID); papConfigured {
return types.ProposalErrorInvalidMarket, fmt.Errorf("market already has an active protocol automated purchase program")
}

tt := automatedPurchase.Changes.AuctionSchedule.GetInternalTimeTriggerSpecConfiguration()
currentTime = currentTime.Truncate(time.Second)
if tt.Triggers[0].Initial == nil {
tt.SetInitial(time.Unix(et.current, 0), currentTime)
}
tt.SetNextTrigger(currentTime)

tt = automatedPurchase.Changes.AuctionVolumeSnapshotSchedule.GetInternalTimeTriggerSpecConfiguration()
currentTime = currentTime.Truncate(time.Second)
if tt.Triggers[0].Initial == nil {
tt.SetInitial(time.Unix(et.current, 0), currentTime)
}
tt.SetNextTrigger(currentTime)

return types.ProposalErrorUnspecified, nil
}
Loading
Loading