code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/lp_events.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/libs/num" 24 25 "github.com/cucumber/godog" 26 ) 27 28 func TheFollowingLPEventsShouldBeEmitted(broker *stubs.BrokerStub, table *godog.Table) error { 29 lpEvts := broker.GetLPEvents() 30 evtsByPartyID := map[string]map[string][]events.LiquidityProvision{} 31 for _, e := range lpEvts { 32 party := e.PartyID() 33 lpID := e.LiquidityProvision().Reference 34 m, ok := evtsByPartyID[party] 35 if !ok { 36 m = map[string][]events.LiquidityProvision{} 37 } 38 s, ok := m[lpID] 39 if !ok { 40 s = []events.LiquidityProvision{} 41 } 42 m[lpID] = append(s, e) 43 evtsByPartyID[party] = m 44 } 45 for _, row := range parseLPEventTable(table) { 46 lpe := LPEventWrapper{ 47 row: row, 48 } 49 party, id, version, final, amt := lpe.Party(), lpe.ID(), lpe.Version(), lpe.Final(), lpe.CommitmentAmount() 50 lpIDMap, ok := evtsByPartyID[party] 51 if !ok { 52 return fmt.Errorf("no LP events found for party %s", party) 53 } 54 evts, ok := lpIDMap[id] 55 if !ok { 56 return fmt.Errorf("no LP events found for LP ID %s (party %s)", id, party) 57 } 58 if final { 59 if err := checkLPEvent(evts[len(evts)-1], party, id, amt, version); err != nil { 60 return err 61 } 62 continue 63 } 64 // find matching event in slice 65 var err error 66 // find matching event 67 for _, e := range evts { 68 if err = checkLPEvent(e, party, id, amt, version); err == nil { 69 // match found, break 70 break 71 } 72 } 73 if err != nil { 74 return err 75 } 76 } 77 return nil 78 } 79 80 func checkLPEvent(last events.LiquidityProvision, party, id string, amt *num.Uint, version uint64) error { 81 if last.LiquidityProvision().Version != version { 82 return fmt.Errorf("version %d is not the last version for LP %s (party %s), last is %d", version, id, party, last.LiquidityProvision().Version) 83 } 84 if amt != nil && last.LiquidityProvision().CommitmentAmount != amt.String() { 85 return fmt.Errorf("commitment amount was %s, expected %s for last event for LP %s, party %s", 86 last.LiquidityProvision().CommitmentAmount, 87 amt, 88 id, 89 party, 90 ) 91 } 92 return nil 93 } 94 95 type LPEventWrapper struct { 96 row RowWrapper 97 } 98 99 func parseLPEventTable(table *godog.Table) []RowWrapper { 100 return StrictParseTable(table, []string{ 101 "party", 102 "id", 103 "version", 104 }, []string{ 105 "commitment amount", 106 "final", 107 }) 108 } 109 110 func (l LPEventWrapper) Party() string { 111 return l.row.MustStr("party") 112 } 113 114 func (l LPEventWrapper) ID() string { 115 return l.row.MustStr("id") 116 } 117 118 func (l LPEventWrapper) Version() uint64 { 119 return l.row.MustU64("version") 120 } 121 122 func (l LPEventWrapper) CommitmentAmount() *num.Uint { 123 if !l.row.HasColumn("commitment amount") { 124 return nil 125 } 126 return l.row.MustUint("commitment amount") 127 } 128 129 func (l LPEventWrapper) Final() bool { 130 if !l.row.HasColumn("final") { 131 return false 132 } 133 return l.row.MustBool("final") 134 }