bug: Added missing out chain data (#4303)

This commit is contained in:
Cuteivist 2023-11-30 12:37:32 +01:00 committed by GitHub
parent d2379aa129
commit 79f0d8a5ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 57 deletions

View File

@ -1345,8 +1345,11 @@ func TestGetTxDetails(t *testing.T) {
require.Equal(t, td.tr1.Hash.String(), details.ID) require.Equal(t, td.tr1.Hash.String(), details.ID)
require.Equal(t, 0, details.MultiTxID) require.Equal(t, 0, details.MultiTxID)
require.Equal(t, td.tr1.Nonce, details.Nonce) require.Equal(t, td.tr1.Nonce, details.Nonce)
require.Equal(t, td.tr1.BlkNumber, details.BlockNumber) require.Equal(t, len(details.ChainDetails), 1)
require.Equal(t, td.tr1.Contract, *details.Contract) require.Equal(t, td.tr1.ChainID, common.ChainID(details.ChainDetails[0].ChainID))
require.Equal(t, td.tr1.BlkNumber, details.ChainDetails[0].BlockNumber)
require.Equal(t, td.tr1.Hash, details.ChainDetails[0].Hash)
require.Equal(t, td.tr1.Contract, *details.ChainDetails[0].Contract)
} }
func TestGetMultiTxDetails(t *testing.T) { func TestGetMultiTxDetails(t *testing.T) {
@ -1365,6 +1368,13 @@ func TestGetMultiTxDetails(t *testing.T) {
require.Equal(t, "", details.ID) require.Equal(t, "", details.ID)
require.Equal(t, int(td.multiTx1.MultiTransactionID), details.MultiTxID) require.Equal(t, int(td.multiTx1.MultiTransactionID), details.MultiTxID)
require.Equal(t, td.multiTx1Tr2.Nonce, details.Nonce) require.Equal(t, td.multiTx1Tr2.Nonce, details.Nonce)
require.Equal(t, td.multiTx1Tr2.BlkNumber, details.BlockNumber) require.Equal(t, 2, len(details.ChainDetails))
require.Equal(t, td.multiTx1Tr1.Contract, *details.Contract) require.Equal(t, td.multiTx1Tr1.ChainID, common.ChainID(details.ChainDetails[0].ChainID))
require.Equal(t, td.multiTx1Tr1.BlkNumber, details.ChainDetails[0].BlockNumber)
require.Equal(t, td.multiTx1Tr1.Hash, details.ChainDetails[0].Hash)
require.Equal(t, td.multiTx1Tr1.Contract, *details.ChainDetails[0].Contract)
require.Equal(t, td.multiTx1Tr2.ChainID, common.ChainID(details.ChainDetails[1].ChainID))
require.Equal(t, td.multiTx1Tr2.BlkNumber, details.ChainDetails[1].BlockNumber)
require.Equal(t, td.multiTx1Tr2.Hash, details.ChainDetails[1].Hash)
require.Equal(t, td.multiTx1Tr2.Contract, *details.ChainDetails[1].Contract)
} }

View File

@ -21,18 +21,23 @@ const (
ProtocolUniswap ProtocolUniswap
) )
type EntryChainDetails struct {
ChainID int64 `json:"chainId"`
BlockNumber int64 `json:"blockNumber"`
Hash eth.Hash `json:"hash"`
Contract *eth.Address `json:"contractAddress,omitempty"`
}
type EntryDetails struct { type EntryDetails struct {
ID string `json:"id"` ID string `json:"id"`
MultiTxID int `json:"multiTxId"` MultiTxID int `json:"multiTxId"`
Nonce uint64 `json:"nonce"` Nonce uint64 `json:"nonce"`
BlockNumber int64 `json:"blockNumber"` ChainDetails []EntryChainDetails `json:"chainDetails"`
Input string `json:"input"` Input string `json:"input"`
ProtocolType *ProtocolType `json:"protocolType,omitempty"` ProtocolType *ProtocolType `json:"protocolType,omitempty"`
Hash *eth.Hash `json:"hash,omitempty"` MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
Contract *eth.Address `json:"contractAddress,omitempty"` GasLimit uint64 `json:"gasLimit"`
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"` TotalFees *hexutil.Big `json:"totalFees,omitempty"`
GasLimit hexutil.Uint64 `json:"gasLimit"`
TotalFees *hexutil.Big `json:"totalFees,omitempty"`
} }
func protocolTypeFromDBType(dbType string) (protocolType *ProtocolType) { func protocolTypeFromDBType(dbType string) (protocolType *ProtocolType) {
@ -56,14 +61,21 @@ func getMultiTxDetails(ctx context.Context, db *sql.DB, multiTxID int) (*EntryDe
if multiTxID <= 0 { if multiTxID <= 0 {
return nil, errors.New("invalid tx id") 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, ` rows, err := db.QueryContext(ctx, `
SELECT SELECT
tx_hash, tx_hash,
blk_number, blk_number,
network_id,
type, type,
account_nonce, account_nonce,
tx, contract_address,
contract_address CASE
WHEN json_extract(tx, '$.gas') = '0x0' THEN NULL
ELSE transfers.tx
END as tx,
base_gas_fee
FROM FROM
transfers transfers
WHERE WHERE
@ -74,66 +86,77 @@ func getMultiTxDetails(ctx context.Context, db *sql.DB, multiTxID int) (*EntryDe
defer rows.Close() defer rows.Close()
var maxFeePerGas *hexutil.Big var maxFeePerGas *hexutil.Big
var gasLimit hexutil.Uint64
var input string var input string
var protocolType *ProtocolType var protocolType *ProtocolType
var transferHash *eth.Hash var nonce, gasLimit uint64
var contractAddress *eth.Address var totalFees *hexutil.Big
var blockNumber int64 var chainDetailsList []EntryChainDetails
var nonce uint64
for rows.Next() { for rows.Next() {
var contractTypeDB sql.NullString var contractTypeDB sql.NullString
var chainIDDB, nonceDB, blockNumber sql.NullInt64
var transferHashDB, contractAddressDB sql.RawBytes var transferHashDB, contractAddressDB sql.RawBytes
var blockNumberDB int64 var baseGasFees string
var nonceDB uint64
tx := &types.Transaction{} tx := &types.Transaction{}
nullableTx := sqlite.JSONBlob{Data: tx} nullableTx := sqlite.JSONBlob{Data: tx}
err := rows.Scan(&transferHashDB, &blockNumberDB, &contractTypeDB, &nonceDB, &nullableTx, &contractAddressDB) err := rows.Scan(&transferHashDB, &blockNumber, &chainIDDB, &contractTypeDB, &nonceDB, &contractAddressDB, &nullableTx, &baseGasFees)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var chainID int64
if chainIDDB.Valid {
chainID = chainIDDB.Int64
}
chainDetails := getChainDetails(chainID, &chainDetailsList)
if len(transferHashDB) > 0 { if len(transferHashDB) > 0 {
transferHash = new(eth.Hash) chainDetails.Hash = eth.BytesToHash(transferHashDB)
*transferHash = eth.BytesToHash(transferHashDB)
} }
if contractTypeDB.Valid && protocolType == nil { if contractTypeDB.Valid && protocolType == nil {
protocolType = protocolTypeFromDBType(contractTypeDB.String) protocolType = protocolTypeFromDBType(contractTypeDB.String)
} }
if blockNumberDB > 0 { if blockNumber.Valid {
blockNumber = blockNumberDB chainDetails.BlockNumber = blockNumber.Int64
} }
if nonceDB > 0 { if nonceDB.Valid {
nonce = nonceDB nonce = uint64(nonceDB.Int64)
}
if len(input) == 0 && nullableTx.Valid {
if len(input) == 0 {
input = "0x" + hex.EncodeToString(tx.Data())
}
if maxFeePerGas == nil {
maxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
gasLimit = hexutil.Uint64(tx.Gas())
}
} }
if contractAddress == nil && len(contractAddressDB) > 0 { if len(contractAddressDB) > 0 && chainDetails.Contract == nil {
contractAddress = new(eth.Address) chainDetails.Contract = new(eth.Address)
*contractAddress = eth.BytesToAddress(contractAddressDB) *chainDetails.Contract = eth.BytesToAddress(contractAddressDB)
}
if nullableTx.Valid {
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 err = rows.Err(); err != nil { if err = rows.Err(); err != nil {
return nil, err return nil, err
} }
if maxFeePerGas == nil {
maxFeePerGas = (*hexutil.Big)(big.NewInt(0))
}
if len(input) == 0 {
input = "0x"
}
return &EntryDetails{ return &EntryDetails{
MultiTxID: multiTxID, MultiTxID: multiTxID,
Nonce: nonce, Nonce: nonce,
BlockNumber: blockNumber,
Hash: transferHash,
ProtocolType: protocolType, ProtocolType: protocolType,
Input: input, Input: input,
Contract: contractAddress,
MaxFeePerGas: maxFeePerGas, MaxFeePerGas: maxFeePerGas,
GasLimit: gasLimit, GasLimit: gasLimit,
ChainDetails: chainDetailsList,
TotalFees: totalFees,
}, nil }, nil
} }
@ -145,6 +168,7 @@ func getTxDetails(ctx context.Context, db *sql.DB, id string) (*EntryDetails, er
SELECT SELECT
tx_hash, tx_hash,
blk_number, blk_number,
network_id,
account_nonce, account_nonce,
tx, tx,
contract_address, contract_address,
@ -165,34 +189,44 @@ func getTxDetails(ctx context.Context, db *sql.DB, id string) (*EntryDetails, er
tx := &types.Transaction{} tx := &types.Transaction{}
nullableTx := sqlite.JSONBlob{Data: tx} nullableTx := sqlite.JSONBlob{Data: tx}
var transferHashDB, contractAddressDB sql.RawBytes var transferHashDB, contractAddressDB sql.RawBytes
var blockNumber int64 var chainIDDB, nonceDB, blockNumberDB sql.NullInt64
var nonce uint64
var baseGasFees string var baseGasFees string
err = rows.Scan(&transferHashDB, &blockNumber, &nonce, &nullableTx, &contractAddressDB, &baseGasFees) err = rows.Scan(&transferHashDB, &blockNumberDB, &chainIDDB, &nonceDB, &nullableTx, &contractAddressDB, &baseGasFees)
if err != nil { if err != nil {
return nil, err return nil, err
} }
details := &EntryDetails{ details := &EntryDetails{
ID: id, ID: id,
Nonce: nonce, }
BlockNumber: blockNumber,
var chainID int64
if chainIDDB.Valid {
chainID = chainIDDB.Int64
}
chainDetails := getChainDetails(chainID, &details.ChainDetails)
if blockNumberDB.Valid {
chainDetails.BlockNumber = blockNumberDB.Int64
}
if nonceDB.Valid {
details.Nonce = uint64(nonceDB.Int64)
} }
if len(transferHashDB) > 0 { if len(transferHashDB) > 0 {
details.Hash = new(eth.Hash) chainDetails.Hash = eth.BytesToHash(transferHashDB)
*details.Hash = eth.BytesToHash(transferHashDB)
} }
if len(contractAddressDB) > 0 { if len(contractAddressDB) > 0 {
details.Contract = new(eth.Address) chainDetails.Contract = new(eth.Address)
*details.Contract = eth.BytesToAddress(contractAddressDB) *chainDetails.Contract = eth.BytesToAddress(contractAddressDB)
} }
if nullableTx.Valid { if nullableTx.Valid {
details.Input = "0x" + hex.EncodeToString(tx.Data()) details.Input = "0x" + hex.EncodeToString(tx.Data())
details.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap()) details.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
details.GasLimit = hexutil.Uint64(tx.Gas()) details.GasLimit = tx.Gas()
baseGasFees, _ := new(big.Int).SetString(baseGasFees, 0) baseGasFees, _ := new(big.Int).SetString(baseGasFees, 0)
details.TotalFees = (*hexutil.Big)(getTotalFees(tx, baseGasFees)) details.TotalFees = (*hexutil.Big)(getTotalFees(tx, baseGasFees))
} }
@ -224,3 +258,15 @@ func getTotalFees(tx *types.Transaction, baseFee *big.Int) *big.Int {
return new(big.Int).Mul(gasPrice, gasUsed) return new(big.Int).Mul(gasPrice, gasUsed)
} }
func getChainDetails(chainID int64, data *[]EntryChainDetails) *EntryChainDetails {
for i, entry := range *data {
if entry.ChainID == chainID {
return &(*data)[i]
}
}
*data = append(*data, EntryChainDetails{
ChainID: chainID,
})
return &(*data)[len(*data)-1]
}

View File

@ -320,6 +320,7 @@ func InsertTestTransferWithOptions(tb testing.TB, db *sql.DB, address eth_common
transfer := transferDBFields{ transfer := transferDBFields{
chainID: uint64(tr.ChainID), chainID: uint64(tr.ChainID),
id: tr.Hash, id: tr.Hash,
txHash: &tr.Hash,
address: address, address: address,
blockHash: blkHash, blockHash: blkHash,
blockNumber: big.NewInt(tr.BlkNumber), blockNumber: big.NewInt(tr.BlkNumber),