From 0101c5861d6d2c5b58c346d1fd61b18b25d22e17 Mon Sep 17 00:00:00 2001 From: Simon Chow Date: Fri, 27 Oct 2023 12:13:31 -0400 Subject: [PATCH] Fix balance changes bug for soroban --- cmd/export_operations.go | 2 +- internal/transform/diagnostic_events.go | 4 ++++ internal/transform/operation.go | 12 ++++++------ internal/transform/operation_test.go | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/export_operations.go b/cmd/export_operations.go index 39067878..becf1095 100644 --- a/cmd/export_operations.go +++ b/cmd/export_operations.go @@ -31,7 +31,7 @@ var operationsCmd = &cobra.Command{ numFailures := 0 totalNumBytes := 0 for _, transformInput := range operations { - transformed, err := transform.TransformOperation(transformInput.Operation, transformInput.OperationIndex, transformInput.Transaction, transformInput.LedgerSeqNum, transformInput.LedgerCloseMeta) + transformed, err := transform.TransformOperation(transformInput.Operation, transformInput.OperationIndex, transformInput.Transaction, transformInput.LedgerSeqNum, transformInput.LedgerCloseMeta, env.NetworkPassphrase) if err != nil { txIndex := transformInput.Transaction.Index cmdLogger.LogError(fmt.Errorf("could not transform operation %d in transaction %d in ledger %d: %v", transformInput.OperationIndex, txIndex, transformInput.LedgerSeqNum, err)) diff --git a/internal/transform/diagnostic_events.go b/internal/transform/diagnostic_events.go index f7cb343f..46161c07 100644 --- a/internal/transform/diagnostic_events.go +++ b/internal/transform/diagnostic_events.go @@ -31,6 +31,10 @@ func TransformDiagnosticEvent(transaction ingest.LedgerTransaction, lhe xdr.Ledg return []DiagnosticEventOutput{}, nil, false } + if transactionMeta.SorobanMeta == nil { + return []DiagnosticEventOutput{}, nil, false + } + var transformedDiagnosticEvents []DiagnosticEventOutput for _, diagnoticEvent := range transactionMeta.SorobanMeta.DiagnosticEvents { diff --git a/internal/transform/operation.go b/internal/transform/operation.go index a1da1c31..54fe3ec7 100644 --- a/internal/transform/operation.go +++ b/internal/transform/operation.go @@ -27,7 +27,7 @@ type liquidityPoolDelta struct { } // TransformOperation converts an operation from the history archive ingestion system into a form suitable for BigQuery -func TransformOperation(operation xdr.Operation, operationIndex int32, transaction ingest.LedgerTransaction, ledgerSeq int32, ledgerCloseMeta xdr.LedgerCloseMeta) (OperationOutput, error) { +func TransformOperation(operation xdr.Operation, operationIndex int32, transaction ingest.LedgerTransaction, ledgerSeq int32, ledgerCloseMeta xdr.LedgerCloseMeta, network string) (OperationOutput, error) { outputTransactionID := toid.New(ledgerSeq, int32(transaction.Index), 0).ToInt64() outputOperationID := toid.New(ledgerSeq, int32(transaction.Index), operationIndex+1).ToInt64() //operationIndex needs +1 increment to stay in sync with ingest package @@ -51,7 +51,7 @@ func TransformOperation(operation xdr.Operation, operationIndex int32, transacti return OperationOutput{}, fmt.Errorf("The operation type (%d) is negative for operation %d (operation id=%d)", outputOperationType, operationIndex, outputOperationID) } - outputDetails, err := extractOperationDetails(operation, transaction, operationIndex) + outputDetails, err := extractOperationDetails(operation, transaction, operationIndex, network) if err != nil { return OperationOutput{}, err } @@ -474,7 +474,7 @@ func addOperationFlagToOperationDetails(result map[string]interface{}, flag uint result[prefix+"flags_s"] = stringFlags } -func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerTransaction, operationIndex int32) (map[string]interface{}, error) { +func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerTransaction, operationIndex int32, network string) (map[string]interface{}, error) { details := map[string]interface{}{} sourceAccount := getOperationSourceAccount(operation, transaction) operationType := operation.Body.Type @@ -967,7 +967,7 @@ func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerT } details["parameters"] = params - if balanceChanges, err := parseAssetBalanceChangesFromContractEvents(transaction); err != nil { + if balanceChanges, err := parseAssetBalanceChangesFromContractEvents(transaction, network); err != nil { return nil, err } else { details["asset_balance_changes"] = balanceChanges @@ -1739,7 +1739,7 @@ func (operation *transactionOperationWrapper) parseAssetBalanceChangesFromContra return balanceChanges, nil } -func parseAssetBalanceChangesFromContractEvents(transaction ingest.LedgerTransaction) ([]map[string]interface{}, error) { +func parseAssetBalanceChangesFromContractEvents(transaction ingest.LedgerTransaction, network string) ([]map[string]interface{}, error) { balanceChanges := []map[string]interface{}{} diagnosticEvents, err := transaction.GetDiagnosticEvents() @@ -1753,7 +1753,7 @@ func parseAssetBalanceChangesFromContractEvents(transaction ingest.LedgerTransac // Parse the xdr contract event to contractevents.StellarAssetContractEvent model // has some convenience like to/from attributes are expressed in strkey format for accounts(G...) and contracts(C...) - if sacEvent, err := contractevents.NewStellarAssetContractEvent(&contractEvent, "Test SDF Future Network ; October 2022"); err == nil { + if sacEvent, err := contractevents.NewStellarAssetContractEvent(&contractEvent, network); err == nil { switch sacEvent.GetType() { case contractevents.EventTypeTransfer: transferEvt := sacEvent.(*contractevents.TransferEvent) diff --git a/internal/transform/operation_test.go b/internal/transform/operation_test.go index 984dcb57..7ae5b341 100644 --- a/internal/transform/operation_test.go +++ b/internal/transform/operation_test.go @@ -67,7 +67,7 @@ func TestTransformOperation(t *testing.T) { } for _, test := range tests { - actualOutput, actualError := TransformOperation(test.input.operation, test.input.index, test.input.transaction, 0, test.input.ledgerClosedMeta) + actualOutput, actualError := TransformOperation(test.input.operation, test.input.index, test.input.transaction, 0, test.input.ledgerClosedMeta, "") assert.Equal(t, test.wantErr, actualError) assert.Equal(t, test.wantOutput, actualOutput) }