Coder Social home page Coder Social logo

ethidxer's Introduction

Ethidxer

ethidxer is a library for synchronizing event data on the blockchain (EVM)

Example

If a contract written in solidty is deployed

contract ExampleContract {
    event ExampleEvent(
        address indexed param1,
        int32 indexed param2,
        int256 indexed param3,
        address param4,
        uint256 param5
    );

    function triggerEvent(
        address _param1,
        int32 _param2,
        int256 _param3,
        address _param4,
        uint256 _param5
    ) public {
        emit ExampleEvent(_param1, _param2, _param3, _param4, _param5);
    }
}

You can easily pull events from the blockchain

import (
	"context"
	"fmt"
	"math/big"
	"strings"

	"github.com/baekhyunee7/ethidxer"
	"github.com/baekhyunee7/ethidxer/example/indexer/contract"
	"github.com/ethereum/go-ethereum/accounts/abi"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/ethclient"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Indexed struct {
	Param1 common.Address
	Param2 int32
	Param3 *big.Int
}

type Data struct {
	Param4 common.Address
	Param5 *big.Int
}

func main() {
	db, _ := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
	_ = ethidxer.AutoMigrate(db)
	rpcCli, _ := ethclient.Dial("http://localhost:8545")
	abi, _ := abi.JSON(strings.NewReader(string(contract.ContractABI)))
	indexer := ethidxer.Builder("example").
		Db(db). // gorm instance
		RpcCli(rpcCli). // ethclient.Client instance
		Addrs([]string{"0x5fbdb2315678afecb367f032d93f642f64180aa3"}). // deployed contract address
		Abi(&abi). // abi generate from abigen
		Build(ethidxer.WithSleepDuration(time.Minute))
	ethidxer.Register(indexer, "ExampleEvent", func(t1 *Indexed, t2 *Data) error {
		fmt.Printf("%s %d %s\n", t1.Param1, t1.Param2, t1.Param3.String())
		fmt.Printf("%s %s\n", t2.Param4, t2.Param5.String())
		return nil
	})
	err := indexer.Run(context.Background())
	if err != nil {
		panic(err)
	}
}
  • ethidxer.AutoMigrate will migrate a table check_points. It is used to record the height of the currently synchronized blockchain.
  • ethidxer.Register is used to register events and event callbacks.
  • ethidxer.TrasactionHook hook a function that will ensure transactionality during database transaction execution.
  • Run loop to pull events on the blockchain and process them.
  • Build a indexer instance will be created and configuration options can also be passed through.

Option

  • WithReserveBlockNum specifies how many latest blocks will not be processed immediately. To prevent events from being canceled due to blockchain forks.
  • WithLogger specifies logger.
  • WithBatchSize used to determine how many blocks to pull at one time.
  • WithSleepDuration used to configure how long it will sleep if no new blocks are generated.

Please see specific examples

ethidxer's People

Contributors

baekhyunee7 avatar

Watchers

 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.