code.vegaprotocol.io/vega@v0.79.0/core/examples/nullchain/market.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 nullchain
    17  
    18  import (
    19  	"errors"
    20  	"strings"
    21  
    22  	"code.vegaprotocol.io/vega/core/examples/nullchain/config"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  
    25  	"code.vegaprotocol.io/vega/protos/vega"
    26  )
    27  
    28  var ErrMarketCreationFailed = errors.New("market creation failed")
    29  
    30  func CreateMarketAny(w *Wallet, conn *Connection, proposer *Party, voters ...*Party) (*vega.Market, error) {
    31  	now, _ := conn.VegaTime()
    32  	txn, reference := MarketProposalTxn(now, proposer.pubkey)
    33  	err := w.SubmitTransaction(conn, proposer, txn)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	// Step foward until proposal is validated
    39  	if err := MoveByDuration(4 * config.BlockDuration); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	// Vote for the proposal
    44  	proposal, err := conn.GetProposalByReference(reference)
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	txn = VoteTxn(proposal.Id, types.VoteValueYes)
    50  	for _, voter := range voters {
    51  		w.SubmitTransaction(conn, voter, txn)
    52  	}
    53  
    54  	// Move forward until enacted
    55  	if err := MoveByDuration(20 * config.BlockDuration); err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	// Get the market
    60  	markets, err := conn.GetMarkets()
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	if len(markets) == 0 {
    66  		return nil, ErrMarketCreationFailed
    67  	}
    68  
    69  	// Return the last market as that *should* be the newest one
    70  	return markets[len(markets)-1], nil
    71  }
    72  
    73  func SettleMarket(w *Wallet, conn *Connection, oracle *Party) error {
    74  	terminationTxn := OracleTxn("trading.termination", "true")
    75  	err := w.SubmitTransaction(conn, oracle, terminationTxn)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	settlementTxn := OracleTxn(strings.Join([]string{"prices", config.NormalAsset, "value"}, "."), "1000")
    81  	err = w.SubmitTransaction(conn, oracle, settlementTxn)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	// Move time so that it is processed
    86  	return MoveByDuration(10 * config.BlockDuration)
    87  }