Created
March 5, 2022 02:44
-
-
Save AdvaithD/027f3aad216906309eac55b2cb680ad4 to your computer and use it in GitHub Desktop.
stream txns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func StreamNewTxs(rpcClient *rpc.Client, fullMode bool) { | |
// Go channel to pipe data from client subscription | |
newTxsChannel := make(chan common.Hash) | |
// Subscribe to receive one time events for new txs | |
rpcClient.EthSubscribe( | |
context.Background(), newTxsChannel, "newPendingTransactions", // no additional args | |
) | |
client := GetCurrentClient() | |
fmt.Println("Subscribed to mempool txs") | |
// Configure chain ID and signer to ensure you're configured to mainnet | |
chainID, _ := client.NetworkID(context.Background()) | |
signer := types.NewEIP155Signer(chainID) | |
for { | |
select { | |
// Code block is executed when a new tx hash is piped to the channel | |
case transactionHash := <-newTxsChannel: | |
// Get transaction object from hash by querying the client | |
tx, is_pending, _ := client.TransactionByHash(context.Background(), transactionHash) | |
// If tx is valid and still unconfirmed | |
if is_pending { | |
_, _ = signer.Sender(tx) | |
//fmt.Println(fullMode) | |
handleTransaction(tx, client, false, fullMode) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment