Skip to content

Commit

Permalink
Update the way we generate the fee event
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikiyer56 committed Feb 21, 2025
1 parent a858f64 commit 30472ff
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions ingest/ledger_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (t *LedgerTransaction) txInternalError() bool {
return t.Result.Result.Result.Code == xdr.TransactionResultCodeTxInternalError
}

func (t *LedgerTransaction) FeeAccount() xdr.MuxedAccount {
return t.Envelope.FeeAccount()
}

// GetFeeChanges returns a developer friendly representation of LedgerEntryChanges
// connected to fees.
func (t *LedgerTransaction) GetFeeChanges() []Change {
Expand Down
27 changes: 11 additions & 16 deletions ingest/processors/token_transfer/token_transfer_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,17 @@ func ProcessTokenTransferEventsFromOperation(tx ingest.LedgerTransaction, opInde
}

func generateFeeEvent(tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
var events []*TokenTransferEvent
feeChanges := tx.GetFeeChanges()
for _, change := range feeChanges {
if change.Type != xdr.LedgerEntryTypeAccount {
return nil, errors.Errorf("invalid ledgerEntryType for fee change: %s", change.Type.String())
}

// Do I need to do all this? Can I not simply use tx.Result.Result.FeeCharged
preBalance := change.Pre.Data.MustAccount().Balance
postBalance := change.Post.Data.MustAccount().Balance
accId := change.Pre.Data.MustAccount().AccountId
amt := amount.String(postBalance - preBalance)
event := NewFeeEvent(tx.Ledger.LedgerSequence(), tx.Ledger.ClosedAt(), tx.Hash.HexString(), protoAddressFromAccountId(accId), amt, assetProto.NewNativeAsset())
events = append(events, event)
}
return events, nil
/*
For a feeBump transaction, this will be the outer transaction.
FeeAccount() gives the proper "muxed" account that paid the fees.
And we want the "muxed" Account, so that it can be passed directly to protoAddressFromAccount
*/
feeAccount := tx.FeeAccount()
// FeeCharged() takes care of a bug in an intermediate protocol release. So using that
feeAmt, _ := tx.FeeCharged()

event := NewFeeEvent(tx.Ledger.LedgerSequence(), tx.Ledger.ClosedAt(), tx.Hash.HexString(), protoAddressFromAccount(feeAccount), amount.String(xdr.Int64(feeAmt)), assetProto.NewNativeAsset())
return []*TokenTransferEvent{event}, nil
}

// Function stubs
Expand Down
9 changes: 9 additions & 0 deletions xdr/transaction_envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ func (e TransactionEnvelope) SourceAccount() MuxedAccount {
}
}

func (e TransactionEnvelope) FeeAccount() MuxedAccount {
switch e.Type {
case EnvelopeTypeEnvelopeTypeTxFeeBump:
return e.FeeBumpAccount()
default:
return e.SourceAccount()
}
}

// Fee returns the fee defined for the transaction envelope
// If the transaction envelope is for a fee bump transaction, Fee()
// returns the fee defined in the inner transaction
Expand Down

0 comments on commit 30472ff

Please sign in to comment.