-
Notifications
You must be signed in to change notification settings - Fork 0
/
egateway.go
71 lines (60 loc) · 1.98 KB
/
egateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package egateway
import (
"context"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/gsstoykov/go-ethereum-fetcher/model"
)
type IEthereumGateway interface {
GetByTransactionHash(txHashString string) (*model.Transaction, error)
}
type EthereumGateway struct {
client *ethclient.Client
}
func NewEthereumGateway(client *ethclient.Client) IEthereumGateway {
return &EthereumGateway{client: client}
}
func (eg *EthereumGateway) GetByTransactionHash(txHashString string) (*model.Transaction, error) {
// Retrieve transaction by hash
txHash := common.HexToHash(txHashString)
tx, _, err := eg.client.TransactionByHash(context.Background(), txHash)
if err != nil {
return nil, fmt.Errorf("failed to retrieve transaction: %v", err)
}
// Retrieve transaction receipt
receipt, err := eg.client.TransactionReceipt(context.Background(), txHash)
if err != nil {
return nil, fmt.Errorf("failed to retrieve transaction receipt: %v", err)
}
// Derive the sender (from) address
chainID, err := eg.client.NetworkID(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get network ID: %v", err)
}
signer := types.NewEIP155Signer(chainID)
from, err := types.Sender(signer, tx)
if err != nil {
return nil, fmt.Errorf("failed to derive sender from transaction: %v", err)
}
// Get the recipient (to) address
to := ""
if tx.To() != nil {
to = tx.To().Hex()
}
// Create the transaction model
transaction := &model.Transaction{
TransactionHash: tx.Hash().Hex(),
TransactionStatus: int(receipt.Status),
BlockHash: receipt.BlockHash.Hex(),
BlockNumber: int(receipt.BlockNumber.Uint64()),
From: from.Hex(),
To: to,
ContractAddress: receipt.ContractAddress.Hex(),
LogsCount: len(receipt.Logs),
Input: common.Bytes2Hex(tx.Data()),
Value: tx.Value().String(),
}
return transaction, nil
}