fix_: Fetch pending tx hash (#5319)

This commit is contained in:
Cuteivist 2024-06-11 10:36:27 +02:00 committed by GitHub
parent 501b0a06c2
commit 6a72afce8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 71 additions and 35 deletions

View File

@ -7,6 +7,9 @@ import (
"errors"
"math/big"
// used for embedding the sql query in the binary
_ "embed"
eth "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
@ -40,6 +43,12 @@ type EntryDetails struct {
TotalFees *hexutil.Big `json:"totalFees,omitempty"`
}
//go:embed multiTxDetails.sql
var queryMultiTxDetailsString string
//go:embed txDetails.sql
var queryTxDetailsString string
func protocolTypeFromDBType(dbType string) (protocolType *ProtocolType) {
protocolType = new(ProtocolType)
switch common.Type(dbType) {
@ -62,24 +71,7 @@ func getMultiTxDetails(ctx context.Context, db *sql.DB, multiTxID int) (*EntryDe
return nil, errors.New("invalid tx id")
}
// Extracting tx only when values are not null to prevent errors during the scan.
rows, err := db.QueryContext(ctx, `
SELECT
tx_hash,
blk_number,
network_id,
type,
account_nonce,
contract_address,
CASE
WHEN json_extract(tx, '$.gas') = '0x0' THEN NULL
ELSE transfers.tx
END as tx,
base_gas_fee
FROM
transfers
WHERE
multi_transaction_id = ?;`, multiTxID)
rows, err := db.QueryContext(ctx, queryMultiTxDetailsString, multiTxID, multiTxID)
if err != nil {
return nil, err
}
@ -95,10 +87,11 @@ func getMultiTxDetails(ctx context.Context, db *sql.DB, multiTxID int) (*EntryDe
var contractTypeDB sql.NullString
var chainIDDB, nonceDB, blockNumber sql.NullInt64
var transferHashDB, contractAddressDB sql.RawBytes
var baseGasFees string
var baseGasFees *string
var baseGasFeesDB sql.NullString
tx := &types.Transaction{}
nullableTx := sqlite.JSONBlob{Data: tx}
err := rows.Scan(&transferHashDB, &blockNumber, &chainIDDB, &contractTypeDB, &nonceDB, &contractAddressDB, &nullableTx, &baseGasFees)
err := rows.Scan(&transferHashDB, &blockNumber, &chainIDDB, &contractTypeDB, &nonceDB, &contractAddressDB, &nullableTx, &baseGasFeesDB)
if err != nil {
return nil, err
}
@ -109,6 +102,10 @@ func getMultiTxDetails(ctx context.Context, db *sql.DB, multiTxID int) (*EntryDe
}
chainDetails := getChainDetails(chainID, &chainDetailsList)
if baseGasFeesDB.Valid {
baseGasFees = common.NewAndSet(baseGasFeesDB.String)
}
if len(transferHashDB) > 0 {
chainDetails.Hash = eth.BytesToHash(transferHashDB)
}
@ -132,8 +129,10 @@ func getMultiTxDetails(ctx context.Context, db *sql.DB, multiTxID int) (*EntryDe
input = "0x" + hex.EncodeToString(tx.Data())
maxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
gasLimit = tx.Gas()
baseGasFees, _ := new(big.Int).SetString(baseGasFees, 0)
totalFees = (*hexutil.Big)(getTotalFees(tx, baseGasFees))
if baseGasFees != nil {
baseGasFees, _ := new(big.Int).SetString(*baseGasFees, 0)
totalFees = (*hexutil.Big)(getTotalFees(tx, baseGasFees))
}
}
}
if err = rows.Err(); err != nil {
@ -164,19 +163,7 @@ func getTxDetails(ctx context.Context, db *sql.DB, id string) (*EntryDetails, er
if len(id) == 0 {
return nil, errors.New("invalid tx id")
}
rows, err := db.QueryContext(ctx, `
SELECT
tx_hash,
blk_number,
network_id,
account_nonce,
tx,
contract_address,
base_gas_fee
FROM
transfers
WHERE
hash = ?;`, eth.HexToHash(id))
rows, err := db.QueryContext(ctx, queryTxDetailsString, eth.HexToHash(id))
if err != nil {
return nil, err
}

View File

@ -0,0 +1,35 @@
-- Query searches for additional details of a multi transaction
-- Pending transactions are saved as multi transactions. Some of data isn't available in multi transaction table, so we need to query pending transactions table to get it.
-- Tx property is only exctracted when values are not null to prevent errors during the scan.
SELECT
transfers.tx_hash AS tx_hash,
transfers.blk_number AS blk_number,
transfers.network_id AS network_id,
transfers.type AS type,
transfers.account_nonce as nonce,
transfers.contract_address as contract_address,
CASE
WHEN json_extract(transfers.tx, '$.gas') = '0x0' THEN NULL
ELSE transfers.tx
END as tx,
transfers.base_gas_fee AS base_gas_fee
FROM
transfers
WHERE
multi_transaction_id = ?
UNION
ALL
SELECT
pt.hash as tx_hash,
NULL AS blk_number,
pt.network_id as network_id,
NULL as type,
pt.nonce as nonce,
NULL as contract_address,
NULL as tx,
NULL as base_gas_fee
FROM
pending_transactions as pt
WHERE
pt.multi_transaction_id = ?

View File

@ -0,0 +1,14 @@
-- Query searches for additional details of a transaction
SELECT
tx_hash,
blk_number,
network_id,
account_nonce,
tx,
contract_address,
base_gas_fee
FROM
transfers
WHERE
hash = ?