Giter Site home page Giter Site logo

ethmonitor's Introduction

eth monitor [一闪一闪亮晶晶,star一下好心情]

Go Report Card Go GitHub version release

  • ETH 多智能合约监控框架,针对智能合约监听做了统一封装,提供handle支持业务实现个性化操作。
  • 注意私链暴露到公网后容易被攻击,可以做一些IP限制等措施保证开发环境稳定

特性

  • 支持自定义业务handle
  • 支持多合约监听

概念图

  • 其中①②③④为用户实现接口需要处理的点
  1. 对tx中的to地址是否需要监听做判断
  2. 针对符合条件的tx做业务处理,已将tx成交与否,gas消耗,合约参数等常规属性解析完毕,也暴露了msg和receipt供业务自行解析
  3. monitor启动时加载,保证服务启动续接块高
  4. 单个block分析完毕后触发,用户可自行实现数据持久化

使用介绍

// 实现TxHandler
var _ ethmonitor.TxHandler = &Mock{}

type Mock struct {
}

// 持久化块高
func (m *Mock) SaveHeight(ctx context.Context, height *ethmonitor.BlockHeight) error {
	// 保存扫过的块高到自己喜欢的中间件
	return nil
}

// 启动monitor时始化监听块高
func (m *Mock) LoadLastHeight(ctx context.Context) (*ethmonitor.BlockHeight, error) {
	// 从自己喜欢的中间件获取块高
	return big.NewInt(1), nil
}

// 具体业务处理
func (m *Mock) Do(ctx context.Context, info *ethmonitor.TxInfo) {
	
}

// 是否包含需要监控的合约地址
// NOTE: 如果是多智能合约监听,可以使用map维护多个
// 配套的,需要把这些合约的abi合并后再初始化monitor时赋值给AbiStr,注意去重
func (m *Mock) ContainContact(ctx context.Context, address ethmonitor.ContractAddress) bool {
	return true
}

func main() {
	opt := &ethmonitor.Options{
		RpcUrl: "http://localhost:8545",
		AbiStr: `
[
    { "type" : "function", "name" : "send", "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
]`,
		Handler: &Mock{},
	}
	monitor, err := ethmonitor.New(opt)
	if err != nil {
		panic(err)
	}
	monitor.Run()
}

合约相关操作

// Action 智能合约的方法
type Action struct {
	Method string                 // 合约方法
	Inputs map[string]interface{} // 合约入参及对应的value
}

// 如何解析智能合约对应参数的value
// 对应abi方法为: { "type" : "function", "name" : "send", "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
// 我们监控所得实体如下,可以通过反射获得
var act = &Action{
	Method : "send"
	Inputs : map[string]interface{}{"amount": big.NewInt(250)}
}

amount,ok := act.Inputs["amount"].(*big.Int)

if ok {
    fmt.Println(amount.String())
}

维护及联系

  • 微信号:WB343688972 或者 扫码,请标注ETH 按引导加群

  • 欢迎 star 及加群交流

ethmonitor's People

Contributors

rennbon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.