D
D
di2020-11-21 23:36:57
go
di, 2020-11-21 23:36:57

How to catch the event “ethereum came to the account” from the log?

Help please who understands.
I don’t understand how the subscription system works for ethereum at all.
Here is how I am trying now.

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 {
    // ???
  }
}

How can I understand from the logs whether a transaction has come or gone.
I just want to know that the ether came to the wallet. receive a message about this event. as? can't find examples

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Smolyakov, 2020-11-23
@yehors

You have a ch variable in your code ( var ch = make(chan types.Log) ) and then a loop where these log events are shown - the l variable . The variable has type types.Log , it will be possible to understand whether the transaction came to the specified address.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question