code.vegaprotocol.io/vega@v0.79.0/core/events/tx_error.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 events 17 18 import ( 19 "context" 20 "fmt" 21 22 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 23 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 24 ) 25 26 type TxErr struct { 27 *Base 28 evt *eventspb.TxErrorEvent 29 } 30 31 func NewTxErrEvent(ctx context.Context, err error, partyID string, tx interface{}, cmd string) *TxErr { 32 evt := &TxErr{ 33 Base: newBase(ctx, TxErrEvent), 34 evt: &eventspb.TxErrorEvent{ 35 PartyId: partyID, 36 ErrMsg: fmt.Sprintf("%v - %v", cmd, err.Error()), 37 }, 38 } 39 switch tv := tx.(type) { 40 case *commandspb.OrderSubmission: 41 evt.evt.Transaction = &eventspb.TxErrorEvent_OrderSubmission{ 42 OrderSubmission: tv, 43 } 44 case *commandspb.OrderCancellation: 45 evt.evt.Transaction = &eventspb.TxErrorEvent_OrderCancellation{ 46 OrderCancellation: tv, 47 } 48 case *commandspb.OrderAmendment: 49 evt.evt.Transaction = &eventspb.TxErrorEvent_OrderAmendment{ 50 OrderAmendment: tv, 51 } 52 case *commandspb.VoteSubmission: 53 evt.evt.Transaction = &eventspb.TxErrorEvent_VoteSubmission{ 54 VoteSubmission: tv, 55 } 56 case *commandspb.WithdrawSubmission: 57 evt.evt.Transaction = &eventspb.TxErrorEvent_WithdrawSubmission{ 58 WithdrawSubmission: tv, 59 } 60 case *commandspb.LiquidityProvisionSubmission: 61 evt.evt.Transaction = &eventspb.TxErrorEvent_LiquidityProvisionSubmission{ 62 LiquidityProvisionSubmission: tv, 63 } 64 case *commandspb.LiquidityProvisionCancellation: 65 evt.evt.Transaction = &eventspb.TxErrorEvent_LiquidityProvisionCancellation{ 66 LiquidityProvisionCancellation: tv, 67 } 68 case *commandspb.LiquidityProvisionAmendment: 69 evt.evt.Transaction = &eventspb.TxErrorEvent_LiquidityProvisionAmendment{ 70 LiquidityProvisionAmendment: tv, 71 } 72 case *commandspb.ProposalSubmission: 73 evt.evt.Transaction = &eventspb.TxErrorEvent_Proposal{ 74 Proposal: tv, 75 } 76 case *commandspb.DelegateSubmission: 77 evt.evt.Transaction = &eventspb.TxErrorEvent_DelegateSubmission{ 78 DelegateSubmission: tv, 79 } 80 case *commandspb.UndelegateSubmission: 81 evt.evt.Transaction = &eventspb.TxErrorEvent_UndelegateSubmission{ 82 UndelegateSubmission: tv, 83 } 84 case *commandspb.Transfer: 85 evt.evt.Transaction = &eventspb.TxErrorEvent_Transfer{ 86 Transfer: tv, 87 } 88 case *commandspb.CancelTransfer: 89 evt.evt.Transaction = &eventspb.TxErrorEvent_CancelTransfer{ 90 CancelTransfer: tv, 91 } 92 case *commandspb.AnnounceNode: 93 evt.evt.Transaction = &eventspb.TxErrorEvent_AnnounceNode{ 94 AnnounceNode: tv, 95 } 96 case *commandspb.OracleDataSubmission: 97 evt.evt.Transaction = &eventspb.TxErrorEvent_OracleDataSubmission{ 98 OracleDataSubmission: tv, 99 } 100 case *commandspb.ProtocolUpgradeProposal: 101 evt.evt.Transaction = &eventspb.TxErrorEvent_ProtocolUpgradeProposal{ 102 ProtocolUpgradeProposal: tv, 103 } 104 case *commandspb.IssueSignatures: 105 evt.evt.Transaction = &eventspb.TxErrorEvent_IssueSignatures{ 106 IssueSignatures: tv, 107 } 108 case *commandspb.BatchMarketInstructions: 109 evt.evt.Transaction = &eventspb.TxErrorEvent_BatchMarketInstructions{ 110 BatchMarketInstructions: tv, 111 } 112 case error: // unsupported command error 113 evt.evt.ErrMsg = fmt.Sprintf("%v - %v", err, tv) 114 } 115 return evt 116 } 117 118 func (t TxErr) IsParty(id string) bool { 119 return t.evt.PartyId == id 120 } 121 122 func (t TxErr) Proto() eventspb.TxErrorEvent { 123 return *t.evt 124 } 125 126 func (t TxErr) StreamMessage() *eventspb.BusEvent { 127 busEvent := newBusEventFromBase(t.Base) 128 busEvent.Event = &eventspb.BusEvent_TxErrEvent{ 129 TxErrEvent: t.evt, 130 } 131 132 return busEvent 133 } 134 135 func TxErrEventFromStream(ctx context.Context, be *eventspb.BusEvent) *TxErr { 136 return &TxErr{ 137 Base: newBaseFromBusEvent(ctx, TxErrEvent, be), 138 evt: be.GetTxErrEvent(), 139 } 140 }