2023-03-27 12:14:12 -07:00
|
|
|
package chains
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-07-04 11:25:08 -07:00
|
|
|
"time"
|
2023-03-27 12:14:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type ethGetTransactionByHashResponse struct {
|
|
|
|
BlockHash string `json:"blockHash"`
|
|
|
|
BlockNumber string `json:"blockNumber"`
|
|
|
|
From string `json:"from"`
|
|
|
|
To string `json:"to"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ethGetBlockByHashResponse struct {
|
|
|
|
Timestamp string `json:"timestamp"`
|
|
|
|
Number string `json:"number"`
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:16 -07:00
|
|
|
func fetchEthTx(
|
2023-03-27 12:14:12 -07:00
|
|
|
ctx context.Context,
|
2023-07-04 11:25:08 -07:00
|
|
|
rateLimiter *time.Ticker,
|
2023-03-30 08:25:16 -07:00
|
|
|
baseUrl string,
|
2023-07-04 11:25:08 -07:00
|
|
|
txHash string,
|
2023-03-27 12:14:12 -07:00
|
|
|
) (*TxDetail, error) {
|
|
|
|
|
|
|
|
// initialize RPC client
|
2023-07-04 11:25:08 -07:00
|
|
|
client, err := rpcDialContext(ctx, baseUrl)
|
2023-03-27 12:14:12 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to initialize RPC client: %w", err)
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
2023-11-27 07:31:35 -08:00
|
|
|
nativeTxHash := txHashLowerCaseWith0x(txHash)
|
2023-03-27 12:14:12 -07:00
|
|
|
// query transaction data
|
|
|
|
var txReply ethGetTransactionByHashResponse
|
2023-07-04 11:25:08 -07:00
|
|
|
{
|
2023-11-27 07:31:35 -08:00
|
|
|
err = client.CallContext(ctx, rateLimiter, &txReply, "eth_getTransactionByHash", nativeTxHash)
|
2023-07-04 11:25:08 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get tx by hash: %w", err)
|
|
|
|
}
|
|
|
|
if txReply.BlockHash == "" || txReply.From == "" {
|
|
|
|
return nil, ErrTransactionNotFound
|
|
|
|
}
|
2023-04-05 06:33:28 -07:00
|
|
|
}
|
2023-03-27 12:14:12 -07:00
|
|
|
|
|
|
|
// build results and return
|
|
|
|
txDetail := &TxDetail{
|
2023-06-14 13:46:53 -07:00
|
|
|
From: strings.ToLower(txReply.From),
|
2023-11-27 07:31:35 -08:00
|
|
|
NativeTxHash: nativeTxHash,
|
2023-03-27 12:14:12 -07:00
|
|
|
}
|
|
|
|
return txDetail, nil
|
|
|
|
}
|