Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update soroban fees #231

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/transform/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type TransactionOutput struct {
SorobanResourcesReadBytes uint32 `json:"soroban_resources_read_bytes"`
SorobanResourcesWriteBytes uint32 `json:"soroban_resources_write_bytes"`
TransactionResultCode string `json:"transaction_result_code"`
InclusionFeeBid int64 `json:"inclusion_fee_bid"`
InclusionFeeCharged int64 `json:"inclusion_fee_charged"`
ResourceFeeRefund int64 `json:"resource_fee_refund"`
}

type LedgerTransactionOutput struct {
Expand Down
50 changes: 50 additions & 0 deletions internal/transform/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func TransformTransaction(transaction ingest.LedgerTransaction, lhe xdr.LedgerHe
var outputSorobanResourcesInstructions uint32
var outputSorobanResourcesReadBytes uint32
var outputSorobanResourcesWriteBytes uint32
var outputInclusionFeeBid int64
var outputInclusionFeeCharged int64
var outputResourceFeeRefund int64

transactionEnvelopeV1, ok := transaction.Envelope.GetV1()
if ok {
Expand All @@ -147,6 +150,20 @@ func TransformTransaction(transaction ingest.LedgerTransaction, lhe xdr.LedgerHe
outputSorobanResourcesInstructions = uint32(sorobanData.Resources.Instructions)
outputSorobanResourcesReadBytes = uint32(sorobanData.Resources.ReadBytes)
outputSorobanResourcesWriteBytes = uint32(sorobanData.Resources.WriteBytes)
outputInclusionFeeBid = int64(transactionEnvelopeV1.Tx.Fee) - outputResourceFee

accountBalanceStart, accountBalanceEnd := getAccountBalanceFromLedgerEntryChanges(transaction.FeeChanges, sourceAccount.Address())
initialFeeCharged := accountBalanceStart - accountBalanceEnd
outputInclusionFeeCharged = initialFeeCharged - outputResourceFee

meta, ok := transaction.UnsafeMeta.GetV3()
if ok {
accountBalanceStart, accountBalanceEnd := getAccountBalanceFromLedgerEntryChanges(meta.TxChangesAfter, sourceAccount.Address())
outputResourceFeeRefund = accountBalanceEnd - accountBalanceStart
}

// TODO: FeeCharged is calculated incorrectly in protocol 20. Remove when protocol is updated and the bug is fixed
outputFeeCharged = outputFeeCharged - outputResourceFeeRefund
sydneynotthecity marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -187,6 +204,9 @@ func TransformTransaction(transaction ingest.LedgerTransaction, lhe xdr.LedgerHe
SorobanResourcesReadBytes: outputSorobanResourcesReadBytes,
SorobanResourcesWriteBytes: outputSorobanResourcesWriteBytes,
TransactionResultCode: outputTxResultCode,
InclusionFeeBid: outputInclusionFeeBid,
InclusionFeeCharged: outputInclusionFeeCharged,
ResourceFeeRefund: outputResourceFeeRefund,
}

// Add Muxed Account Details, if exists
Expand Down Expand Up @@ -216,6 +236,36 @@ func TransformTransaction(transaction ingest.LedgerTransaction, lhe xdr.LedgerHe
return transformedTransaction, nil
}

func getAccountBalanceFromLedgerEntryChanges(changes xdr.LedgerEntryChanges, sourceAccountAddress string) (int64, int64) {
var accountBalanceStart int64
var accountBalanceEnd int64

for _, change := range changes {
switch change.Type {
case xdr.LedgerEntryChangeTypeLedgerEntryUpdated:
accountEntry, ok := change.Updated.Data.GetAccount()
if !ok {
continue
}

if accountEntry.AccountId.Address() == sourceAccountAddress {
accountBalanceEnd = int64(accountEntry.Balance)
}
case xdr.LedgerEntryChangeTypeLedgerEntryState:
accountEntry, ok := change.State.Data.GetAccount()
if !ok {
continue
}

if accountEntry.AccountId.Address() == sourceAccountAddress {
accountBalanceStart = int64(accountEntry.Balance)
Comment on lines +257 to +267
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this logic work with fee bump transactions as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the above logic runs for a fee bump transaction. Right now it only looks at V1 transactions which can have sorobanData

Right now the fee for fee bump transactions is saved in max_fee. Do these fees need some breakdown as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to handle fee bump transactions as well. Although there aren't any in mainnet nor testnet so far

}
}
}

return accountBalanceStart, accountBalanceEnd
}

func formatSigners(s []xdr.SignerKey) pq.StringArray {
if s == nil {
return nil
Expand Down
Loading