Real-time monitoring of an address in a blockchain implementation

Everyone can see the blockchain data on the chain. A while ago, I found some NFT pass cards that were selling NFT pass cards. They were actually tracking the wallet address of Ethereum. When I found that several big Vs went to mint NFT for free, I followed mint. His first step is to monitor the addresses of these wallets

Monitoring blockchain address implementation scheme

For personal analysis, there are two implementation methods. The first one is to monitor the content of the Ethereum block, monitor the data in the latest block, and analyze the address and method. Compared with the monitored address, this method is faster, but at a lower level. It is more difficult to implement; the second way is to use crawlers. For example, the Ethereum chain has an Ethereum browser, so that it is convenient to use crawlers to monitor the changes of a certain address page to judge the changes in the account. , to monitor.

Implementation

I am written in golang language, regularly monitor the content of the Ethereum browser at a certain address, and analyze it if there is an operation

The specific implementation logic is to monitor the latest "bill" time, if it is less than a certain time, notify yourself through DingTalk

insert image description here

package main

import (
	"bytes"
	"fmt"
	"github.com/gocolly/colly"
	"io/ioutil"
	"net/http"
	"strings"
	"time"
)



func sendDDingMsg(msg string){

	//钉钉机器人自己获取自己的api
	postUrl := "https://oapi.dingtalk.com/robot/send?access_token=xxxxx
	msgjson := `{"text":{"content":"free mint报警, 赶快去查看\r\n` + msg +`"},"msgtype":"text"}`

	req, err := http.NewRequest("POST", postUrl, bytes.NewBuffer([]byte(msgjson)))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	fmt.Println("response Status:", resp.Status)
	fmt.Println("response Headers:", resp.Header)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("response Body:", string(body))
	fmt.Println("发送钉钉消息成功")
}

func startTimer(f func(), duration time.Duration) {
	go func() {
		for {
			fmt.Println("start search")
			f()
			now := time.Now()
			// 计算下一个零点
			next := now.Add(duration)
			next = time.Date(next.Year(), next.Month(), next.Day(), next.Hour(), next.Minute(), next.Second(), 0, next.Location())
			t := time.NewTimer(next.Sub(now))
			<-t.C
		}
	}()
}

func startM(){
	c := colly.NewCollector()
	
	c.OnHTML("tr", func(e *colly.HTMLElement) {
			/*mint_time := e.ChildAttr("td","showAge")
			fmt.Println("mint time is ", mint_time)*/

		mint_time := e.ChildText("td.showAge")
		if mint_time == "" {
			return
		}
		fmt.Println("mint time is ", mint_time)


		if strings.Contains(mint_time, "hrs") || strings.Contains(mint_time, "day") || strings.Contains(mint_time, "hr")  {
			return
		}

		var time_s int = 6
		if strings.Contains(mint_time, "mins") {
			fmt.Sscanf(mint_time, "%d mins ago", &time_s)
		}
		if strings.Contains(mint_time, "min") {
			fmt.Sscanf(mint_time, "%d min ago", &time_s)
		}

		if time_s <= 2 {

			mint_contract := e.ChildText("td:nth-child(9)")
			if mint_contract != "" {
				mint_contract = "https://etherscan.io/address/" + mint_contract
				fmt.Println("mint contract is ", mint_contract)
				mint_hash := e.ChildText("td:nth-child(2)")

				mint_hash = "https://etherscan.io/tx/" + mint_hash
				fmt.Println("mint hash is ", mint_hash)
				mint_method := e.ChildText("td:nth-child(3)")
				fmt.Println("mint Method is ", mint_method)
				sendDDingMsg("监控地址为:https://etherscan.io/address/0x111111111111111\r\n 交易页为:" + mint_hash + "\r\n 调用函数为:" + mint_method + "\r\n 交易合同页为:"+ mint_contract)
			}

		}

	})

	c.OnRequest(func(r *colly.Request) {
		fmt.Println("Visiting", r.URL)
	})

	//这里写需要监控页的地址
	c.Visit("https://etherscan.io/address/0x1111111111111111")
}

func main() {

	startTimer(startM, time.Duration(1)*time.Minute)
	for {
		fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
		fmt.Println("wait .....")
		time.Sleep(30 * time.Second)
	}
}

To sum up, the above is just a simple example. Colly, a crawler framework of golang, is used. In fact, it is just a function of attracting new ideas. I only made Dingding reminder here. In fact, you can do what you want on this basis.

Guess you like

Origin blog.csdn.net/dtwangquan/article/details/126627733