Go语言 如何订阅“入局交易成功”事件?

4sup72z8  于 2022-12-07  发布在  Go
关注(0)|答案(3)|浏览(155)

如何订阅“入局交易成功”事件,即想知道以太币到我钱包了,如何使用订阅,我什么都不懂。

package main

import (
  "context"
  "github.com/ethereum/go-ethereum"
  "github.com/ethereum/go-ethereum/common"
  "github.com/ethereum/go-ethereum/core/types"
  "github.com/ethereum/go-ethereum/ethclient"
  "log"
)

func main() {
  client, err := ethclient.Dial("wss://mainnet.infura.io/ws/v3/XXXXXXXXXXXXXXXX")
  if err != nil {
    log.Fatal(err)
  }

  accs := map[string]string{
"0x92321477416e93Ea452f16015e2F2a13B3BDe8B7":"12e2cc06fb999fa29306f10db6b366e61a4946b9527286a0c56640c94cebd950",
  }

  keys := make([]common.Address, 0, len(accs))
  for k := range accs {
    keys = append(keys, common.HexToAddress(k))
  }

  var ch = make(chan types.Log)
  sub, err := client.SubscribeFilterLogs(context.Background(), ethereum.FilterQuery{
    BlockHash: nil,
    FromBlock: nil,
    ToBlock:   nil,
    Addresses: keys,
    Topics:    nil,
  }, ch)
  if err != nil {
    log.Fatal(err)
  }
  defer sub.Unsubscribe()

  for l := range ch {
    // ???
  }
}

请帮帮我。我在哪里可以找到例子?
enter image description here

xggvc2p6

xggvc2p61#

我正在浏览infura.io's API documentation,我没有发现他们有支付的端点。您在上面链接的image是他们的电子邮件通知UI选项,与API无关。要订阅操作,您必须为它们提供一个回调函数,以便在该操作发生时执行。你的回拨会发出警报,但他们会在触发器(支付)发生时调用它。他们有地方输入webhook吗?如果有,这将是你的订阅。
虽然此选项可能不是订阅,但您可以轮询getBalance端点,并将结果与先前的结果进行比较,如果结果增加,则向您发出警报。

c3frrgcw

c3frrgcw2#

我的解决方案

sub, err := client.SubscribeNewHead(context.Background(), ch)

吸气块

b, err := client.BlockByNumber(context.Background(), l.Number)

检查所有交易

for _, tx := range b.Transactions() {
       msg, err := tx.AsMessage(types.NewEIP155Signer(tx.ChainId()))
   }

在邮件收件人()地址中)))

xbp102n0

xbp102n03#

实际上go-ethereum已经在他们的测试脚本中提供了一个演示:

import (
    "context"
    "log"
    "os"
    "os/signal"
    "syscall"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/ethereum/go-ethereum/ethclient/gethclient"
    "github.com/ethereum/go-ethereum/rpc"
)

const (
    url = "https://mainnet.infura.io/v3/xxxxxxxx"
    wss = "wss://eth-mainnet.alchemyapi.io/v2/xxxxxxxxxxx"
    // wss = "wss://mainnet.infura.io/ws/v3/xxxxxxxx"
)

func watch() {
    backend, err := ethclient.Dial(url)
    if err != nil {
        log.Printf("failed to dial: %v", err)
        return
    }

    rpcCli, err := rpc.Dial(wss)
    if err != nil {
        log.Printf("failed to dial: %v", err)
        return
    }
    gcli := gethclient.New(rpcCli)

    txch := make(chan common.Hash, 100)
    _, err = gcli.SubscribePendingTransactions(context.Background(), txch)
    if err != nil {
        log.Printf("failed to SubscribePendingTransactions: %v", err)
        return
    }
    for {
        select {
        case txhash := <-txch:
            tx, _, err := backend.TransactionByHash(context.Background(), txhash)
            if err != nil {
                continue
            }
            data, _ := tx.MarshalJSON()
            log.Printf("tx: %v", string(data))
        }
    }
}

func DoTest() {
    go watch()
    signalChan := make(chan os.Signal, 1)
    signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
    <-signalChan
}

相关问题