code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/the_loss_socialisation_amount_is.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 "fmt" 20 21 "code.vegaprotocol.io/vega/core/events" 22 "code.vegaprotocol.io/vega/core/integration/stubs" 23 "code.vegaprotocol.io/vega/core/types" 24 "code.vegaprotocol.io/vega/libs/num" 25 "code.vegaprotocol.io/vega/logging" 26 27 "github.com/cucumber/godog" 28 ) 29 30 func TheLossSocialisationAmountsAre(broker *stubs.BrokerStub, table *godog.Table) error { 31 lsMkt := getLossSocPerMarket(broker) 32 for _, r := range parseLossSocTable(table) { 33 lsr := lossSocRow{r: r} 34 mevts, ok := lsMkt[lsr.Market()] 35 if !ok { 36 return fmt.Errorf("no loss socialisation events found for market %s", lsr.Market()) 37 } 38 parties := map[string]types.LossType{} 39 for _, e := range mevts { 40 if lsr.Amount().EQ(e.Amount()) { 41 parties[e.PartyID()] = e.LossType() 42 } 43 } 44 if c := lsr.Count(); c != -1 { 45 if len(parties) != c { 46 return fmt.Errorf("expected %d loss socialisation events for market %s and amount %s, instead found %d", c, lsr.Market(), lsr.Amount().String(), len(parties)) 47 } 48 } 49 for _, p := range lsr.Party() { 50 lt, ok := parties[p] 51 if !ok { 52 return fmt.Errorf("no loss socialisation found for party %s on market %s for amount %s (type: %s)", p, lsr.Market(), lsr.Amount().String(), lsr.Type().String()) 53 } 54 if !lsr.matchesType(lt) { 55 return fmt.Errorf("loss socialisation for party %s on market %s for amount %s is of type %s, not %s", p, lsr.Market(), lsr.Amount().String(), lt.String(), lsr.Type().String()) 56 } 57 } 58 } 59 return nil 60 } 61 62 func DebugLossSocialisationEvents(broker *stubs.BrokerStub, log *logging.Logger) error { 63 lsEvts := getLossSocPerMarket(broker) 64 for mkt, evts := range lsEvts { 65 log.Infof("\nLoss socialisation events for market %s:", mkt) 66 for _, e := range evts { 67 log.Infof( 68 "Party: %s - Amount: %s", 69 e.PartyID(), 70 e.Amount().String(), 71 ) 72 } 73 log.Info("----------------------------------------------------------------------------") 74 } 75 return nil 76 } 77 78 func getLossSocPerMarket(broker *stubs.BrokerStub) map[string][]*events.LossSoc { 79 evts := broker.GetLossSoc() 80 ret := map[string][]*events.LossSoc{} 81 for _, e := range evts { 82 mkt := e.MarketID() 83 mevts, ok := ret[mkt] 84 if !ok { 85 mevts = []*events.LossSoc{} 86 } 87 ret[mkt] = append(mevts, e) 88 } 89 return ret 90 } 91 92 func parseLossSocTable(table *godog.Table) []RowWrapper { 93 return StrictParseTable(table, []string{ 94 "market", 95 "amount", 96 }, []string{ 97 "party", 98 "count", 99 "type", 100 }) 101 } 102 103 type lossSocRow struct { 104 r RowWrapper 105 } 106 107 func (l lossSocRow) Market() string { 108 return l.r.MustStr("market") 109 } 110 111 func (l lossSocRow) Amount() *num.Int { 112 return l.r.MustInt("amount") 113 } 114 115 func (l lossSocRow) Party() []string { 116 if l.r.HasColumn("party") { 117 return l.r.MustStrSlice("party", ",") 118 } 119 return nil 120 } 121 122 func (l lossSocRow) Count() int { 123 if l.r.HasColumn("count") { 124 return int(l.r.MustI64("count")) 125 } 126 return -1 127 } 128 129 func (l lossSocRow) matchesType(t types.LossType) bool { 130 if l.r.HasColumn("type") { 131 return l.Type() == t 132 } 133 return true 134 } 135 136 func (l lossSocRow) Type() types.LossType { 137 if !l.r.HasColumn("type") { 138 return types.LossTypeUnspecified 139 } 140 return l.r.MustLossType("type") 141 }