github.com/0xsequence/ethkit@v1.25.0/cmd/chain-mempool/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 8 "github.com/0xsequence/ethkit/ethmempool" 9 "github.com/0xsequence/ethkit/util" 10 ) 11 12 var ( 13 ETH_NODE_URL = "" 14 ETH_NODE_WSS_URL = "" 15 ) 16 17 func init() { 18 testConfig, err := util.ReadTestConfig("../../ethkit-test.json") 19 if err != nil { 20 panic(err) 21 } 22 23 if testConfig["POLYGON_MAINNET_WSS_URL"] != "" { 24 ETH_NODE_URL = testConfig["POLYGON_MAINNET_URL"] 25 ETH_NODE_WSS_URL = testConfig["POLYGON_MAINNET_WSS_URL"] 26 } 27 // if testConfig["MAINNET_URL"] != "" { 28 // ETH_NODE_URL = testConfig["MAINNET_URL"] 29 // ETH_NODE_WSS_URL = testConfig["MAINNET_WSS_URL"] 30 // } 31 } 32 33 func main() { 34 mempool, err := ethmempool.NewMempool(ETH_NODE_WSS_URL) 35 if err != nil { 36 log.Fatal(err) 37 } 38 39 // go func() { 40 // err := mempool.Run(context.Background()) 41 // if err != nil { 42 // log.Fatal(err) 43 // } 44 // }() 45 46 sub := mempool.Subscribe() 47 48 // sub := mempool.SubscribeWithFilter(func(pendingTxnHash string) bool { 49 // // random example, where we can filter the list before we notify 50 // // our subscriber 51 // if strings.HasPrefix(pendingTxnHash, "0x1") || strings.HasPrefix(pendingTxnHash, "0x0") { 52 // return true 53 // } 54 // return false 55 // }) 56 57 defer sub.Unsubscribe() 58 59 go func() { 60 for { 61 select { 62 case pendingTxnHash := <-sub.PendingTransactionHash(): 63 fmt.Println("newPendingTxn:", pendingTxnHash) 64 65 case <-sub.Done(): 66 return 67 } 68 } 69 }() 70 71 err = mempool.Run(context.Background()) 72 if err != nil { 73 log.Fatal(err) 74 } 75 } 76 77 // func main() { 78 // client, err := rpc.Dial(ETH_NODE_WSS_URL) 79 // if err != nil { 80 // log.Fatal(err) 81 // } 82 83 // ch := make(chan string) 84 // sub, err := client.EthSubscribe(context.Background(), ch, "newPendingTransactions") 85 // if err != nil { 86 // log.Fatal(err) 87 // } 88 89 // go func() { 90 // for { 91 // select { 92 93 // case err := <-sub.Err(): 94 // fmt.Println("sub err!", err) 95 // os.Exit(1) 96 97 // case txnHash := <-ch: 98 // fmt.Println("===> new pending txn:", txnHash) 99 100 // } 101 // } 102 // }() 103 104 // time.Sleep(2 * time.Minute) 105 // sub.Unsubscribe() 106 // }