decred.org/dcrdex@v1.0.5/server/db/driver/pg/testhelpers_test.go (about)

     1  package pg
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"os"
     7  	"time"
     8  
     9  	"decred.org/dcrdex/dex"
    10  	"decred.org/dcrdex/dex/order"
    11  	"decred.org/dcrdex/server/account"
    12  	"github.com/decred/slog"
    13  )
    14  
    15  func startLogger() {
    16  	logger := slog.NewBackend(os.Stdout).Logger("PG_DB_TEST")
    17  	logger.SetLevel(slog.LevelDebug)
    18  	UseLogger(logger)
    19  }
    20  
    21  const (
    22  	LotSize         = uint64(100_0000_0000) // 100
    23  	RateStep        = uint64(10_0000)       // 0.001
    24  	EpochDuration   = uint64(10_000)
    25  	MarketBuyBuffer = 1.1
    26  )
    27  
    28  // The asset integer IDs should set in TestMain or other bring up function (e.g.
    29  // openDB()) prior to using them.
    30  var (
    31  	AssetDCR uint32
    32  	AssetBTC uint32
    33  	AssetLTC uint32
    34  )
    35  
    36  func randomBytes(len int) []byte {
    37  	bytes := make([]byte, len)
    38  	rand.Read(bytes)
    39  	return bytes
    40  }
    41  
    42  func randomAccountID() account.AccountID {
    43  	pk := randomBytes(account.PubKeySize) // size is not important since it is going to be hashed
    44  	return account.NewID(pk)
    45  }
    46  
    47  func randomPreimage() (pi order.Preimage) {
    48  	rand.Read(pi[:])
    49  	return
    50  }
    51  
    52  func randomCommitment() (com order.Commitment) {
    53  	rand.Read(com[:])
    54  	return
    55  }
    56  
    57  func mktConfig() (markets []*dex.MarketInfo) {
    58  	mktConfig, err := dex.NewMarketInfoFromSymbols("DCR", "BTC", LotSize, RateStep, EpochDuration, 0, MarketBuyBuffer)
    59  	if err != nil {
    60  		panic(fmt.Sprintf("you broke it: %v", err))
    61  	}
    62  	markets = append(markets, mktConfig)
    63  
    64  	mktConfig, err = dex.NewMarketInfoFromSymbols("BTC", "LTC", LotSize, RateStep, EpochDuration, 0, MarketBuyBuffer)
    65  	if err != nil {
    66  		panic(fmt.Sprintf("you broke it: %v", err))
    67  	}
    68  	markets = append(markets, mktConfig)
    69  
    70  	// specify more here...
    71  	return
    72  }
    73  
    74  func newMatch(maker *order.LimitOrder, taker order.Order, quantity uint64, epochID order.EpochID) *order.Match {
    75  	return &order.Match{
    76  		Maker:        maker,
    77  		Taker:        taker,
    78  		Quantity:     quantity,
    79  		Rate:         maker.Rate,
    80  		FeeRateBase:  12,
    81  		FeeRateQuote: 14,
    82  		Status:       order.NewlyMatched,
    83  		Sigs:         order.Signatures{},
    84  		Epoch:        epochID,
    85  	}
    86  }
    87  
    88  func newLimitOrderRevealed(sell bool, rate, quantityLots uint64, force order.TimeInForce, timeOffset int64) (*order.LimitOrder, order.Preimage) {
    89  	lo := newLimitOrder(sell, rate, quantityLots, force, timeOffset)
    90  	pi := randomPreimage()
    91  	lo.Commit = pi.Commit()
    92  	return lo, pi
    93  }
    94  
    95  func newLimitOrder(sell bool, rate, quantityLots uint64, force order.TimeInForce, timeOffset int64) *order.LimitOrder {
    96  	return newLimitOrderWithAssets(sell, rate, quantityLots, force, timeOffset, AssetDCR, AssetBTC)
    97  }
    98  
    99  func newLimitOrderWithAssets(sell bool, rate, quantityLots uint64, force order.TimeInForce, timeOffset int64, base, quote uint32) *order.LimitOrder {
   100  	addr := "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui"
   101  	if sell {
   102  		addr = "149RQGLaHf2gGiL4NXZdH7aA8nYEuLLrgm"
   103  	}
   104  	return &order.LimitOrder{
   105  		P: order.Prefix{
   106  			AccountID:  randomAccountID(),
   107  			BaseAsset:  base,
   108  			QuoteAsset: quote,
   109  			OrderType:  order.LimitOrderType,
   110  			ClientTime: time.Unix(1566497653+timeOffset, 0).UTC(),
   111  			ServerTime: time.Unix(1566497656+timeOffset, 0).UTC(),
   112  			Commit:     randomCommitment(),
   113  		},
   114  		T: order.Trade{
   115  			Coins: []order.CoinID{
   116  				randomBytes(36),
   117  				randomBytes(36),
   118  			},
   119  			Sell:     sell,
   120  			Quantity: quantityLots * LotSize,
   121  			Address:  addr,
   122  		},
   123  		Rate:  rate,
   124  		Force: force,
   125  	}
   126  }
   127  
   128  func newMarketSellOrder(quantityLots uint64, timeOffset int64) *order.MarketOrder {
   129  	return &order.MarketOrder{
   130  		P: order.Prefix{
   131  			AccountID:  randomAccountID(),
   132  			BaseAsset:  AssetDCR,
   133  			QuoteAsset: AssetBTC,
   134  			OrderType:  order.MarketOrderType,
   135  			ClientTime: time.Unix(1566497653+timeOffset, 0).UTC(),
   136  			ServerTime: time.Unix(1566497656+timeOffset, 0).UTC(),
   137  			Commit:     randomCommitment(),
   138  		},
   139  		T: order.Trade{
   140  			Coins:    []order.CoinID{randomBytes(36)},
   141  			Sell:     true,
   142  			Quantity: quantityLots * LotSize,
   143  			Address:  "149RQGLaHf2gGiL4NXZdH7aA8nYEuLLrgm",
   144  		},
   145  	}
   146  }
   147  
   148  func newMarketBuyOrder(quantityQuoteAsset uint64, timeOffset int64) *order.MarketOrder {
   149  	return &order.MarketOrder{
   150  		P: order.Prefix{
   151  			AccountID:  randomAccountID(),
   152  			BaseAsset:  AssetDCR,
   153  			QuoteAsset: AssetBTC,
   154  			OrderType:  order.MarketOrderType,
   155  			ClientTime: time.Unix(1566497653+timeOffset, 0).UTC(),
   156  			ServerTime: time.Unix(1566497656+timeOffset, 0).UTC(),
   157  			Commit:     randomCommitment(),
   158  		},
   159  		T: order.Trade{
   160  			Coins:    []order.CoinID{randomBytes(36)},
   161  			Sell:     false,
   162  			Quantity: quantityQuoteAsset,
   163  			Address:  "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui",
   164  		},
   165  	}
   166  }
   167  
   168  func newCancelOrder(targetOrderID order.OrderID, base, quote uint32, timeOffset int64) *order.CancelOrder {
   169  	return &order.CancelOrder{
   170  		P: order.Prefix{
   171  			AccountID:  randomAccountID(),
   172  			BaseAsset:  base,
   173  			QuoteAsset: quote,
   174  			OrderType:  order.CancelOrderType,
   175  			ClientTime: time.Unix(1566497653+timeOffset, 0).UTC(),
   176  			ServerTime: time.Unix(1566497656+timeOffset, 0).UTC(),
   177  			Commit:     randomCommitment(),
   178  		},
   179  		TargetOrderID: targetOrderID,
   180  	}
   181  }