diff --git a/ingest/ledger_transaction.go b/ingest/ledger_transaction.go index 56f5af6070..77ca777206 100644 --- a/ingest/ledger_transaction.go +++ b/ingest/ledger_transaction.go @@ -1,8 +1,6 @@ package ingest import ( - "fmt" - "github.com/stellar/go/support/errors" "github.com/stellar/go/xdr" ) @@ -155,40 +153,5 @@ func operationChanges(ops []xdr.OperationMeta, index uint32) []Change { // GetDiagnosticEvents returns all contract events emitted by a given operation. func (t *LedgerTransaction) GetDiagnosticEvents() ([]xdr.DiagnosticEvent, error) { - switch t.UnsafeMeta.V { - case 1: - return nil, nil - case 2: - return nil, nil - case 3: - var diagnosticEvents []xdr.DiagnosticEvent - var contractEvents []xdr.ContractEvent - if sorobanMeta := t.UnsafeMeta.MustV3().SorobanMeta; sorobanMeta != nil { - diagnosticEvents = sorobanMeta.DiagnosticEvents - if len(diagnosticEvents) > 0 { - // all contract events and diag events for a single operation(by it's index in the tx) were available - // in tx meta's DiagnosticEvents, no need to look anywhere else for events - return diagnosticEvents, nil - } - - contractEvents = sorobanMeta.Events - if len(contractEvents) == 0 { - // no events were present in this tx meta - return nil, nil - } - } - - // tx meta only provided contract events, no diagnostic events, we convert the contract - // event to a diagnostic event, to fit the response interface. - convertedDiagnosticEvents := make([]xdr.DiagnosticEvent, len(contractEvents)) - for i, event := range contractEvents { - convertedDiagnosticEvents[i] = xdr.DiagnosticEvent{ - InSuccessfulContractCall: true, - Event: event, - } - } - return convertedDiagnosticEvents, nil - default: - return nil, fmt.Errorf("unsupported TransactionMeta version: %v", t.UnsafeMeta.V) - } + return t.UnsafeMeta.GetDiagnosticEvents() } diff --git a/xdr/transaction_meta.go b/xdr/transaction_meta.go index 3fee38ae93..cd8443551a 100644 --- a/xdr/transaction_meta.go +++ b/xdr/transaction_meta.go @@ -1,5 +1,9 @@ package xdr +import ( + "fmt" +) + // Operations is a helper on TransactionMeta that returns operations // meta from `TransactionMeta.Operations` or `TransactionMeta.V1.Operations`. func (transactionMeta *TransactionMeta) OperationsMeta() []OperationMeta { @@ -16,3 +20,43 @@ func (transactionMeta *TransactionMeta) OperationsMeta() []OperationMeta { panic("Unsupported TransactionMeta version") } } + +// GetDiagnosticEvents returns all contract events emitted by a given operation. +func (t *TransactionMeta) GetDiagnosticEvents() ([]DiagnosticEvent, error) { + switch t.V { + case 1: + return nil, nil + case 2: + return nil, nil + case 3: + var diagnosticEvents []DiagnosticEvent + var contractEvents []ContractEvent + if sorobanMeta := t.MustV3().SorobanMeta; sorobanMeta != nil { + diagnosticEvents = sorobanMeta.DiagnosticEvents + if len(diagnosticEvents) > 0 { + // all contract events and diag events for a single operation(by its index in the tx) were available + // in tx meta's DiagnosticEvents, no need to look anywhere else for events + return diagnosticEvents, nil + } + + contractEvents = sorobanMeta.Events + if len(contractEvents) == 0 { + // no events were present in this tx meta + return nil, nil + } + } + + // tx meta only provided contract events, no diagnostic events, we convert the contract + // event to a diagnostic event, to fit the response interface. + convertedDiagnosticEvents := make([]DiagnosticEvent, len(contractEvents)) + for i, event := range contractEvents { + convertedDiagnosticEvents[i] = DiagnosticEvent{ + InSuccessfulContractCall: true, + Event: event, + } + } + return convertedDiagnosticEvents, nil + default: + return nil, fmt.Errorf("unsupported TransactionMeta version: %v", t.V) + } +}