实时监控一个区块链某地址的实现方案

区块链数据都在链上大家都可以看到,前阵子发现一些卖NFT pass卡的,其实就是跟踪以太坊的钱包地址,发现几个大V都去免费mint NFT的时候就去跟着mint, 他实现第一步就是必须监控这些钱包的地址

监控区块链地址实现方案

个人分析,实现方式有两种,第一种是监听以太坊区块内容,监听最新区块内的数据,解析出地址和方法等方式和监控的地址对比,这个方式更快,但是更底层,实现起来难一些;第二种方式就是用爬虫的方式,比如以太坊链都是有以太坊浏览器的,这样就可以方便的用爬虫的方式监控某地址页面的变化,来判断账户中的变化,进行监控。

具体实现

我是以golang语言编写,定时对某个地址的以太坊浏览器的内容进行监控,如果有操作就进行分析

具体实现逻辑,就是监控最新“账单”时间,小于一定的时间,通过钉钉通知自己

在这里插入图片描述

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)
	}
}

总结,以上只是个简单的例子,用到了golang的一个爬虫框架colly,其实只是一个抛砖引玉的作用,我这里只做了钉钉提醒,其实可以在这个基础上做你想做的事情。

猜你喜欢

转载自blog.csdn.net/dtwangquan/article/details/126627733
今日推荐