client.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package extension
  2. import (
  3. "context"
  4. "github.com/ethereum/go-ethereum"
  5. "github.com/ethereum/go-ethereum/common"
  6. "github.com/ethereum/go-ethereum/core/types"
  7. "github.com/ethereum/go-ethereum/ethclient"
  8. )
  9. type Client interface {
  10. SubscribeToLogs(query ethereum.FilterQuery) (<-chan types.Log, ethereum.Subscription, error)
  11. NextNonce(from common.Address) (uint64, error)
  12. TransactionByHash(hash common.Hash) (*types.Transaction, error)
  13. TransactionInBlock(blockHash common.Hash, txIndex uint) (*types.Transaction, error)
  14. Close()
  15. }
  16. type InProcessClient struct {
  17. client *ethclient.Client
  18. }
  19. func NewInProcessClient(client *ethclient.Client) *InProcessClient {
  20. return &InProcessClient{
  21. client: client,
  22. }
  23. }
  24. func (client *InProcessClient) SubscribeToLogs(query ethereum.FilterQuery) (<-chan types.Log, ethereum.Subscription, error) {
  25. retrievedLogsChan := make(chan types.Log)
  26. sub, err := client.client.SubscribeFilterLogs(context.Background(), query, retrievedLogsChan)
  27. return retrievedLogsChan, sub, err
  28. }
  29. func (client *InProcessClient) NextNonce(from common.Address) (uint64, error) {
  30. return client.client.PendingNonceAt(context.Background(), from)
  31. }
  32. func (client *InProcessClient) TransactionByHash(hash common.Hash) (*types.Transaction, error) {
  33. tx, _, err := client.client.TransactionByHash(context.Background(), hash)
  34. return tx, err
  35. }
  36. func (client *InProcessClient) TransactionInBlock(blockHash common.Hash, txIndex uint) (*types.Transaction, error) {
  37. tx, err := client.client.TransactionInBlock(context.Background(), blockHash, txIndex)
  38. if err != nil {
  39. return nil, err
  40. }
  41. // Fetch the underlying private tx if we got a Privacy Marker Transaction
  42. if tx.IsPrivacyMarker() {
  43. return client.client.GetPrivateTransaction(context.Background(), tx.Hash())
  44. }
  45. return tx, nil
  46. }
  47. func (client *InProcessClient) Close() {
  48. client.client.Close()
  49. }