code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0083_mark_price.sql (about)

     1  -- +goose Up
     2  create type composite_price_type as enum('COMPOSITE_PRICE_TYPE_UNSPECIFIED','COMPOSITE_PRICE_TYPE_WEIGHTED','COMPOSITE_PRICE_TYPE_MEDIAN','COMPOSITE_PRICE_TYPE_LAST_TRADE');
     3  alter table market_data add column if not exists mark_price_type composite_price_type not null default('COMPOSITE_PRICE_TYPE_LAST_TRADE');
     4  alter table current_market_data add column if not exists mark_price_type composite_price_type;
     5  
     6  update current_market_data set mark_price_type = 'COMPOSITE_PRICE_TYPE_LAST_TRADE';
     7  
     8  alter table current_market_data alter mark_price_type set not null;
     9  
    10  -- +goose StatementBegin
    11  UPDATE proposals
    12  SET terms = jsonb_set(
    13    terms,
    14    '{terms, updateMarket, changes}',
    15    terms #> '{terms, updateMarket, changes}' || '{"markPriceConfiguration": {"decayWeight": "0", "decayPower": 0, "cashAmount":"0","compositePriceType":"COMPOSITE_PRICE_TYPE_LAST_TRADE"}}'
    16  )
    17  WHERE terms @> '{"terms": {"updateMarket": {}}}';
    18  
    19  UPDATE proposals
    20  SET terms = jsonb_set(
    21    terms,
    22    '{terms, newMarket, changes}',
    23    terms #> '{terms, newMarket, changes}' || '{"markPriceConfiguration": {"decayWeight": "0", "decayPower": 0, "cashAmount":"0","compositePriceType":"COMPOSITE_PRICE_TYPE_LAST_TRADE"}}'
    24  )
    25  WHERE terms @> '{"terms": {"newMarket": {}}}';
    26  
    27  
    28  
    29  CREATE OR REPLACE FUNCTION update_current_market_data()
    30      RETURNS TRIGGER
    31      LANGUAGE PLPGSQL AS
    32  $$
    33  BEGIN
    34  INSERT INTO current_market_data(synthetic_time,tx_hash,vega_time,seq_num,market,mark_price,mark_price_type,best_bid_price,best_bid_volume,
    35                                  best_offer_price,best_offer_volume,best_static_bid_price,best_static_bid_volume,
    36                                  best_static_offer_price,best_static_offer_volume,mid_price,static_mid_price,open_interest,
    37                                  auction_end,auction_start,indicative_price,indicative_volume,market_trading_mode,
    38                                  auction_trigger,extension_trigger,target_stake,supplied_stake,price_monitoring_bounds,
    39                                  market_value_proxy,liquidity_provider_fee_shares,market_state,next_mark_to_market, market_growth, last_traded_price, product_data, liquidity_provider_sla, next_network_closeout)
    40  VALUES(NEW.synthetic_time, NEW.tx_hash, NEW.vega_time, NEW.seq_num, NEW.market,
    41         NEW.mark_price, NEW.mark_price_type, NEW.best_bid_price, NEW.best_bid_volume, NEW.best_offer_price,
    42         NEW.best_offer_volume, NEW.best_static_bid_price, NEW.best_static_bid_volume,
    43         NEW.best_static_offer_price, NEW.best_static_offer_volume, NEW.mid_price,
    44         NEW.static_mid_price, NEW.open_interest, NEW.auction_end, NEW.auction_start,
    45         NEW.indicative_price, NEW.indicative_volume, NEW.market_trading_mode,
    46         NEW.auction_trigger, NEW.extension_trigger, NEW.target_stake, NEW.supplied_stake,
    47         NEW.price_monitoring_bounds, NEW.market_value_proxy,
    48         NEW.liquidity_provider_fee_shares, NEW.market_state, NEW.next_mark_to_market, NEW.market_growth, NEW.last_traded_price, NEW.product_data, NEW.liquidity_provider_sla, NEW.next_network_closeout)
    49      ON CONFLICT(market) DO UPDATE SET
    50      synthetic_time=EXCLUDED.synthetic_time,tx_hash=EXCLUDED.tx_hash,vega_time=EXCLUDED.vega_time,seq_num=EXCLUDED.seq_num,market=EXCLUDED.market,mark_price=EXCLUDED.mark_price,
    51                                 mark_price_type=EXCLUDED.mark_price_type,
    52                                 best_bid_price=EXCLUDED.best_bid_price,best_bid_volume=EXCLUDED.best_bid_volume,best_offer_price=EXCLUDED.best_offer_price,best_offer_volume=EXCLUDED.best_offer_volume,
    53                                 best_static_bid_price=EXCLUDED.best_static_bid_price,best_static_bid_volume=EXCLUDED.best_static_bid_volume,best_static_offer_price=EXCLUDED.best_static_offer_price,
    54                                 best_static_offer_volume=EXCLUDED.best_static_offer_volume,mid_price=EXCLUDED.mid_price,static_mid_price=EXCLUDED.static_mid_price,open_interest=EXCLUDED.open_interest,
    55                                 auction_end=EXCLUDED.auction_end,auction_start=EXCLUDED.auction_start,indicative_price=EXCLUDED.indicative_price,indicative_volume=EXCLUDED.indicative_volume,
    56                                 market_trading_mode=EXCLUDED.market_trading_mode,auction_trigger=EXCLUDED.auction_trigger,extension_trigger=EXCLUDED.extension_trigger,target_stake=EXCLUDED.target_stake,
    57                                 supplied_stake=EXCLUDED.supplied_stake,price_monitoring_bounds=EXCLUDED.price_monitoring_bounds,
    58                                 market_value_proxy=EXCLUDED.market_value_proxy,liquidity_provider_fee_shares=EXCLUDED.liquidity_provider_fee_shares,market_state=EXCLUDED.market_state,
    59                                 next_mark_to_market=EXCLUDED.next_mark_to_market,market_growth=EXCLUDED.market_growth,last_traded_price=EXCLUDED.last_traded_price,
    60                                 product_data=EXCLUDED.product_data,liquidity_provider_sla=EXCLUDED.liquidity_provider_sla,next_network_closeout=EXCLUDED.next_network_closeout;
    61  
    62  RETURN NULL;
    63  END;
    64  $$;
    65  -- +goose StatementEnd
    66  
    67  -- +goose Down
    68  
    69  alter table current_market_data drop column if exists mark_price_type;
    70  alter table market_data drop column if exists mark_price_type;
    71  drop type composite_price_type;
    72  
    73  -- +goose StatementBegin
    74  CREATE OR REPLACE FUNCTION update_current_market_data()
    75      RETURNS TRIGGER
    76      LANGUAGE PLPGSQL AS
    77  $$
    78  BEGIN
    79  INSERT INTO current_market_data(synthetic_time,tx_hash,vega_time,seq_num,market,mark_price,best_bid_price,best_bid_volume,
    80                                  best_offer_price,best_offer_volume,best_static_bid_price,best_static_bid_volume,
    81                                  best_static_offer_price,best_static_offer_volume,mid_price,static_mid_price,open_interest,
    82                                  auction_end,auction_start,indicative_price,indicative_volume,market_trading_mode,
    83                                  auction_trigger,extension_trigger,target_stake,supplied_stake,price_monitoring_bounds,
    84                                  market_value_proxy,liquidity_provider_fee_shares,market_state,next_mark_to_market, market_growth, last_traded_price, product_data, liquidity_provider_sla, next_network_closeout)
    85  VALUES(NEW.synthetic_time, NEW.tx_hash, NEW.vega_time, NEW.seq_num, NEW.market,
    86         NEW.mark_price, NEW.best_bid_price, NEW.best_bid_volume, NEW.best_offer_price,
    87         NEW.best_offer_volume, NEW.best_static_bid_price, NEW.best_static_bid_volume,
    88         NEW.best_static_offer_price, NEW.best_static_offer_volume, NEW.mid_price,
    89         NEW.static_mid_price, NEW.open_interest, NEW.auction_end, NEW.auction_start,
    90         NEW.indicative_price, NEW.indicative_volume, NEW.market_trading_mode,
    91         NEW.auction_trigger, NEW.extension_trigger, NEW.target_stake, NEW.supplied_stake,
    92         NEW.price_monitoring_bounds, NEW.market_value_proxy,
    93         NEW.liquidity_provider_fee_shares, NEW.market_state, NEW.next_mark_to_market, NEW.market_growth, NEW.last_traded_price, NEW.product_data, NEW.liquidity_provider_sla, NEW.next_network_closeout)
    94      ON CONFLICT(market) DO UPDATE SET
    95      synthetic_time=EXCLUDED.synthetic_time,tx_hash=EXCLUDED.tx_hash,vega_time=EXCLUDED.vega_time,seq_num=EXCLUDED.seq_num,market=EXCLUDED.market,mark_price=EXCLUDED.mark_price,
    96                                 best_bid_price=EXCLUDED.best_bid_price,best_bid_volume=EXCLUDED.best_bid_volume,best_offer_price=EXCLUDED.best_offer_price,best_offer_volume=EXCLUDED.best_offer_volume,
    97                                 best_static_bid_price=EXCLUDED.best_static_bid_price,best_static_bid_volume=EXCLUDED.best_static_bid_volume,best_static_offer_price=EXCLUDED.best_static_offer_price,
    98                                 best_static_offer_volume=EXCLUDED.best_static_offer_volume,mid_price=EXCLUDED.mid_price,static_mid_price=EXCLUDED.static_mid_price,open_interest=EXCLUDED.open_interest,
    99                                 auction_end=EXCLUDED.auction_end,auction_start=EXCLUDED.auction_start,indicative_price=EXCLUDED.indicative_price,indicative_volume=EXCLUDED.indicative_volume,
   100                                 market_trading_mode=EXCLUDED.market_trading_mode,auction_trigger=EXCLUDED.auction_trigger,extension_trigger=EXCLUDED.extension_trigger,target_stake=EXCLUDED.target_stake,
   101                                 supplied_stake=EXCLUDED.supplied_stake,price_monitoring_bounds=EXCLUDED.price_monitoring_bounds,
   102                                 market_value_proxy=EXCLUDED.market_value_proxy,liquidity_provider_fee_shares=EXCLUDED.liquidity_provider_fee_shares,market_state=EXCLUDED.market_state,
   103                                 next_mark_to_market=EXCLUDED.next_mark_to_market,market_growth=EXCLUDED.market_growth,last_traded_price=EXCLUDED.last_traded_price,
   104                                 product_data=EXCLUDED.product_data,liquidity_provider_sla=EXCLUDED.liquidity_provider_sla,next_network_closeout=EXCLUDED.next_network_closeout;
   105  
   106  RETURN NULL;
   107  END;
   108  $$;
   109  -- +goose StatementEnd