[golang] go-ethereum으로 이벤트 구독

	go func(client *ethclient.Client) {
		contractAddresses := []common.Address{}
		for _, pair := range Pair {
			contractAddresses = append(contractAddresses, common.HexToAddress(pair.PairAddress))
		}

		SyncEventSig := []byte("Sync(uint112,uint112)")
		hash := sha3.NewLegacyKeccak256()
		hash.Write(SyncEventSig)
		SyncEventHashBytes := hash.Sum(nil)
		signature := common.BytesToHash(SyncEventHashBytes) // 0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1
		topic := []common.Hash{signature}
		topics := [][]common.Hash{topic}
		query := ethereum.FilterQuery{
			Addresses: contractAddresses,
			Topics:    topics,
		}

		logs := make(chan types.Log)
		sub := event.Resubscribe(2*time.Second, func(ctx context.Context) (event.Subscription, error) {
			return client.SubscribeFilterLogs(context.Background(), query, logs)
		})
		defer sub.Unsubscribe()
		for {
			select {
			case err := <-sub.Err():
				log.Fatal("Error on select:", err)
			case vLog := <-logs:
				fmt.Println("Log block number:", vLog.BlockNumber)
				// 여기서 하고 싶은 일 하기.
			}
		}
	}(client)

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다