我正在尝试订阅以太坊中的待处理交易。虽然我知道如何在 web3.js 中做
const subscription = web3.eth.subscribe("pendingTransactions", (err, res) => {
if (err) console.error(err);
});
我还没有找到任何可以在 Go 中执行的方法。例如,这个方法不存在:
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
//........
client, err := ethclient.Dial("<URL>")
gethClient := gethclient.New(rpcClient)
//.....
logs := make(chan types.Log)
sub, err := gethClient.SubscribePendingTransactions(context.Background(), logs) // doesn't exit
client.SubscribePendingTransactions undefined (type *ethclient.Client has no field or method SubscribePendingTransactions)
有什么方法吗?
它不会返回任何内容 - 这就是您收到该错误的原因。
相反,它会创建一个 go 通道,因此您应该执行以下操作来处理那些待处理的 Tx:
gethClient.SubscribePendingTransactions(context.Background(), ch)
// ch is our channel, every tx will appear here
for tx := range ch {
fmt.Println("new tx", tx)
}