code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/the_automated_puchase.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 steps
    17  
    18  import (
    19  	"context"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/datasource"
    23  	"code.vegaprotocol.io/vega/core/integration/steps/market"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  	"code.vegaprotocol.io/vega/libs/num"
    26  	"code.vegaprotocol.io/vega/protos/vega"
    27  
    28  	"github.com/cucumber/godog"
    29  )
    30  
    31  func TheAutomatedPurchasePrograms(
    32  	config *market.Config,
    33  	executionEngine Execution,
    34  	table *godog.Table,
    35  ) error {
    36  	rows := parseAutomatedPurchaseTable(table)
    37  	aps := make(map[string]*types.NewProtocolAutomatedPurchaseChanges, len(rows))
    38  	for _, row := range rows {
    39  		r := apRow{row: row}
    40  		ap, err := NewProtocolAutomatedPurchase(r, config)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		aps[r.row.mustColumn("id")] = ap
    45  	}
    46  	for id, ap := range aps {
    47  		if err := executionEngine.NewProtocolAutomatedPurchase(context.Background(), id, ap); err != nil {
    48  			return err
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  type apRow struct {
    55  	row RowWrapper
    56  }
    57  
    58  func NewProtocolAutomatedPurchase(r apRow, config *market.Config) (*types.NewProtocolAutomatedPurchaseChanges, error) {
    59  	duration, err := time.ParseDuration(r.row.MustStr("auction duration"))
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	stalnessTol, err := time.ParseDuration(r.row.MustStr("price oracle staleness tolerance"))
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	minSize, _ := num.UintFromString(r.row.MustStr("minimum auction size"), 10)
    69  	maxSize, _ := num.UintFromString(r.row.MustStr("maximum auction size"), 10)
    70  	oracleOffset, _ := num.DecimalFromString(r.row.MustStr("oracle offset factor"))
    71  
    72  	auctionSchedule, _ := config.OracleConfigs.GetTimeTrigger(r.row.MustStr("auction schedule oracle"))
    73  	auctionVolumeSnapshotSchedule, _ := config.OracleConfigs.GetTimeTrigger(r.row.MustStr("auction volume snapshot schedule oracle"))
    74  
    75  	auctionPriceOracle, priceOracleBinding, _ := config.OracleConfigs.GetOracleDefinitionForCompositePrice(r.row.MustStr("price oracle"))
    76  	expiry := r.row.MustI64("expiry timestamp")
    77  	expiryTime := time.Unix(expiry, 0)
    78  
    79  	priceOracle := datasource.FromOracleSpecProto(auctionPriceOracle)
    80  
    81  	return &types.NewProtocolAutomatedPurchaseChanges{
    82  		From:                          r.row.MustStr("from"),
    83  		FromAccountType:               types.AccountType(vega.AccountType_value[r.row.MustStr("from account type")]),
    84  		ToAccountType:                 types.AccountType(vega.AccountType_value[r.row.MustStr("to account type")]),
    85  		MarketID:                      r.row.MustStr("market id"),
    86  		AuctionDuration:               duration,
    87  		MinimumAuctionSize:            minSize,
    88  		MaximumAuctionSize:            maxSize,
    89  		ExpiryTimestamp:               expiryTime,
    90  		OraclePriceStalenessTolerance: stalnessTol,
    91  		OracleOffsetFactor:            oracleOffset,
    92  		AuctionSchedule:               auctionSchedule.GetDefinition(),
    93  		AuctionVolumeSnapshotSchedule: auctionVolumeSnapshotSchedule.GetDefinition(),
    94  		PriceOracle:                   priceOracle,
    95  		PriceOracleBinding:            priceOracleBinding,
    96  		AutomatedPurchaseSpecBinding: &vega.DataSourceSpecToAutomatedPurchaseBinding{
    97  			AuctionScheduleProperty:               "vegaprotocol.builtin.timetrigger",
    98  			AuctionVolumeSnapshotScheduleProperty: "vegaprotocol.builtin.timetrigger",
    99  		},
   100  	}, nil
   101  }
   102  
   103  func parseAutomatedPurchaseTable(table *godog.Table) []RowWrapper {
   104  	return StrictParseTable(table, []string{
   105  		"id",
   106  		"from",
   107  		"from account type",
   108  		"to account type",
   109  		"market id",
   110  		"price oracle",
   111  		"price oracle staleness tolerance",
   112  		"oracle offset factor",
   113  		"auction schedule oracle",
   114  		"auction volume snapshot schedule oracle",
   115  		"auction duration",
   116  		"minimum auction size",
   117  		"maximum auction size",
   118  		"expiry timestamp",
   119  	}, []string{})
   120  }