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