decred.org/dcrdex@v1.0.5/dex/order/match_test.go (about) 1 package order 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "math/rand" 7 "testing" 8 "time" 9 ) 10 11 func TestMatchID(t *testing.T) { 12 rnd := rand.New(rand.NewSource(1)) 13 randomCommitment := func() (com Commitment) { 14 rnd.Read(com[:]) 15 return 16 } 17 18 // taker 19 marketID0, _ := hex.DecodeString("f1f4fb29235f5eeef55b27d909aac860828f6cf45f0b0fab92c6265844c50e54") 20 var marketID OrderID 21 copy(marketID[:], marketID0) 22 23 rate := uint64(13241324) 24 qty := uint64(132413241324) 25 26 mo := &MarketOrder{ 27 P: Prefix{ 28 AccountID: acct0, 29 BaseAsset: AssetDCR, 30 QuoteAsset: AssetBTC, 31 OrderType: MarketOrderType, 32 ClientTime: time.Unix(1566497653, 0), 33 ServerTime: time.Unix(1566497656, 0), 34 Commit: randomCommitment(), 35 }, 36 T: Trade{ 37 Coins: []CoinID{ 38 utxoCoinID("a985d8df97571b130ce30a049a76ffedaa79b6e69b173ff81b1bf9fc07f063c7", 1), 39 }, 40 Sell: true, 41 Quantity: qty, 42 Address: "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui", 43 }, 44 } 45 46 // maker 47 limitID0, _ := hex.DecodeString("f215571e8eec872b0bc6e14c989f217b4fe6fd68a0db07f0ffe75f18d28c28e3") 48 var limitID OrderID 49 copy(limitID[:], limitID0) 50 51 lo := &LimitOrder{ 52 P: Prefix{ 53 AccountID: acct0, 54 BaseAsset: AssetDCR, 55 QuoteAsset: AssetBTC, 56 OrderType: LimitOrderType, 57 ClientTime: time.Unix(1566497653, 0), 58 ServerTime: time.Unix(1566497656, 0), 59 Commit: randomCommitment(), 60 }, 61 T: Trade{ 62 Coins: []CoinID{ 63 utxoCoinID("01516d9c7ffbe260b811dc04462cedd3f8969ce3a3ffe6231ae870775a92e9b0", 1), 64 }, 65 Sell: false, 66 Quantity: qty, 67 Address: "DcqXswjTPnUcd4FRCkX4vRJxmVtfgGVa5ui", 68 }, 69 Rate: rate, 70 Force: StandingTiF, 71 } 72 73 set := &MatchSet{ 74 Taker: mo, 75 Makers: []*LimitOrder{lo, lo}, 76 Amounts: []uint64{qty, qty}, 77 Rates: []uint64{rate, rate}, 78 Total: qty * 2, 79 } 80 81 matches := set.Matches() 82 if len(matches) != 2 { 83 t.Fatalf("wrong number of matches. expected 2, got %d", len(matches)) 84 } 85 match, match2 := matches[0], matches[1] 86 if match.ID() != match2.ID() { 87 t.Fatalf("identical matches has different IDs. %s != %s", match.ID(), match2.ID()) 88 } 89 90 expIDStr := "e80341f82c7b1187c8480f7ebbb179925e62358773429d396e624d30e15a7727" 91 expID, _ := hex.DecodeString(expIDStr) 92 matchID := match.ID() 93 if !bytes.Equal(expID, matchID[:]) { 94 t.Fatalf("expected match ID %x, got %x", expID, matchID[:]) 95 } 96 if matchID.String() != expIDStr { 97 t.Fatalf("wrong hex ID. wanted %s, got %s", expIDStr, matchID.String()) 98 } 99 if match.Maker.ID() != limitID { 100 t.Fatalf("wrong maker ID. expected %s, got %s", limitID, match.Maker.ID()) 101 } 102 if match.Taker.ID() != marketID { 103 t.Fatalf("wrong taker ID. expected %s, got %s", marketID, match.Taker.ID()) 104 } 105 } 106 107 func TestMatchSet(t *testing.T) { 108 matchSet := &MatchSet{ 109 Rates: []uint64{1e8, 2e8}, 110 Amounts: []uint64{5, 10}, 111 } 112 h, l := matchSet.HighLowRates() 113 if h != 2e8 { 114 t.Fatalf("wrong high rate. wanted 2e8, got %d", h) 115 } 116 if l != 1e8 { 117 t.Fatalf("wrong low rate. wanted 1e8, got %d", l) 118 } 119 qv := matchSet.QuoteVolume() 120 if qv != 25 { 121 t.Fatalf("wrong quote volume. wanted 25, got %d", qv) 122 } 123 }