// Subscribe pending transactions from the fullnode.
Subscribe pending transactions from the fullnode.
// Subscribe pending transactions from the fullnode .
func SubscribePendingTx(fullNodeUrl string, fromWhiteList map[common.Address]bool, toWhiteList map[common.Address]bool, stopCh <-chan struct{}, txCh chan<- pojo.TxData) error {
	ctx := context.Background()
	rpcClient, err := rpc.DialContext(ctx, fullNodeUrl)
	if err != nil {
		return err
	}
	ethClient, err := ethclient.DialContext(ctx, fullNodeUrl)
	if err != nil {
		return err
	}
	gethClient := gethclient.New(rpcClient)
	txHashCh := make(chan common.Hash, 1024)
	sub, err := gethClient.SubscribePendingTransactions(ctx, txHashCh)
	if err != nil {
		log.Fatalln(err)
	}
	go func() {
		for {
			select {
			case <-stopCh:
				sub.Unsubscribe()
				rpcClient.Close()
				return
			case txnHash := <-txHashCh:
				go func() {
					tx, isPending, err := utils.TransactionByHashWithRetry(ethClient, txnHash, 11)
					if err != nil {
						return
					}
					if tx == nil || !isPending {
						return
					}
					interactWithContract := tx.To() != nil && len(tx.Data()) > 0
					if !interactWithContract {
						return
					}
					txData := pojo.TxData(pojo.NewRawTransaction(tx, fullNodeUrl))
					if len(toWhiteList) == 0 && len(fromWhiteList) == 0 {
						txCh <- txData
					} else if len(toWhiteList) > 0 && toWhiteList[*tx.To()] {
						txCh <- txData
					} else if len(fromWhiteList) > 0 {
						msg, err := tx.AsMessage(types.LatestSignerForChainID(tx.ChainId()), nil)
						if err == nil {
							if fromWhiteList[msg.From()] {
								txCh <- txData
							}
						}
					}
				}()
			}
		}
	}()
	return nil
}
func SubscribePendingTx ( fullNodeUrl string , fromWhiteList map [ common . Address ] bool , toWhiteList map [ common . Address ] bool , stopCh <- chan struct { } , txCh chan <- pojo . TxData ) error { ctx := context . Background ( ) rpcClient , err := rpc . DialContext ( ctx , fullNodeUrl ) if err != nil { return err } ethClient , err := ethclient . DialContext ( ctx , fullNodeUrl ) if err != nil { return err } gethClient := gethclient . New ( rpcClient ) txHashCh := make ( chan common . Hash , 1024 ) sub , err := gethClient . SubscribePendingTransactions ( ctx , txHashCh ) if err != nil { log . Fatalln ( err ) } go func ( ) { for { select { case <- stopCh : sub . Unsubscribe ( ) rpcClient . Close ( ) return case txnHash := <- txHashCh : go func ( ) { tx , isPending , err := utils . TransactionByHashWithRetry ( ethClient , txnHash , 11 ) if err != nil { return } if tx == nil || ! isPending { return } interactWithContract := tx . To ( ) != nil && len ( tx . Data ( ) ) > 0 if ! interactWithContract { return } txData := pojo . TxData ( pojo . NewRawTransaction ( tx , fullNodeUrl ) ) if len ( toWhiteList ) == 0 && len ( fromWhiteList ) == 0 { txCh <- txData } else if len ( toWhiteList ) > 0 && toWhiteList [ * tx . To ( ) ] { txCh <- txData } else if len ( fromWhiteList ) > 0 { msg , err := tx . AsMessage ( types . LatestSignerForChainID ( tx . ChainId ( ) ) , nil ) if err == nil { if fromWhiteList [ msg . From ( ) ] { txCh <- txData } } } } ( ) } } } ( ) return nil }
Subscribe pending transactions from the fullnode.
Subscribe pending transactions from the fullnode .
// Usually happens when eth.syncing is not false // log.Printf("TransactionByHashWithRetry(%s) failed, error: %v", txnHash.Hex(), err) // only care about pending transactions // Only care about transactions that interact with smart contracts // if both are empty, there is no filtering at all // transactions sent to addresses in `toWhiteList` // or transactions sent from addresses in `toWhiteList`