Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: wip #313

Draft
wants to merge 1 commit into
base: feature/indexer-explorer
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ jobs:
with:
version: v1.59
args: --timeout 15m --verbose ${{ env.GO_LINT_MODULES }}
env:
GOWORK: off

- name: Run Build
run: echo ${GO_MODULES} | tr ' ' '\n' | xargs -I {} sh -c 'go build -C {} -v ./...'
Expand Down
1 change: 0 additions & 1 deletion indexer/README.md

This file was deleted.

119 changes: 119 additions & 0 deletions indexer/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package main

import (
"io"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3"
"github.com/lmittmann/w3/module/eth"
"github.com/lmittmann/w3/w3types"
)

// Chain defines an interface for interacting with a blockchain.
type Chain interface {
// BlockNumber returns the current block number.
BlockNumber() (*big.Int, error)

// Blocks returns [start, end] blocks.
Blocks(start, end *big.Int) ([]*types.Block, error)

// TxReceipts returns the receipts for the given transaction hashes if they exist.
TxReceipts(txHashes []common.Hash) (map[string]*types.Receipt, error)
}

var (
_ Chain = (*EthereumChain)(nil)
_ io.Closer = (*EthereumChain)(nil)
)

// EthereumChain implements the Chain interface and
// represents a connection to the Ethereum blockchain.
type EthereumChain struct {
client *w3.Client
}

// NewEthereumChain creates a new EthereumChain instance connected to the specified URL.
func NewEthereumChain(url string) (*EthereumChain, error) {
client, err := w3.Dial(url)
if err != nil {
return nil, err
}
return &EthereumChain{client: client}, nil
}

// BlockNumber implements the Chain.BlockNumber method.
func (e *EthereumChain) BlockNumber() (*big.Int, error) {
var number big.Int
if err := e.client.Call(eth.BlockNumber().Returns(&number)); err != nil {
return nil, err
}
return &number, nil
}

// Blocks implements the Chain.Blocks method.
func (e *EthereumChain) Blocks(start, end *big.Int) ([]*types.Block, error) {
var (
window = new(big.Int).Sub(end, start)
length = new(big.Int).Abs(window).Uint64() + 1
numbers = make([]*big.Int, length)
)

switch window.Sign() {
case -1:
for i := 0; i < len(numbers); i++ {
numbers[i] = new(big.Int).Sub(end, new(big.Int).SetUint64(uint64(i)))
}
case 1:
for i := 0; i < len(numbers); i++ {
numbers[i] = new(big.Int).Add(start, new(big.Int).SetUint64(uint64(i)))
}
default:
return nil, nil
}

var (
batch = make([]w3types.RPCCaller, len(numbers))
blocks = make([]*types.Block, len(numbers))
)

for i := range numbers {
block := new(types.Block)
batch[i] = eth.BlockByNumber(numbers[i]).Returns(block)
blocks[i] = block
}

if err := e.client.Call(batch...); err != nil {
return nil, err
}
return blocks, nil
}

// TxReceipts implements the Chain.TxReceipts method.
func (e *EthereumChain) TxReceipts(txHashes []common.Hash) (map[string]*types.Receipt, error) {
var (
batch = make([]w3types.RPCCaller, len(txHashes))
txReceipts = make([]types.Receipt, len(txHashes))
)

for i, txHash := range txHashes {
batch[i] = eth.TxReceipt(txHash).Returns(&txReceipts[i])
}

if err := e.client.Call(batch...); err != nil {
return map[string]*types.Receipt{}, nil
}

res := make(map[string]*types.Receipt, len(txReceipts))
for _, txReceipt := range txReceipts {
res[txReceipt.TxHash.Hex()] = &txReceipt
}
return res, nil
}

// Close closes the connection to the Ethereum blockchain.
// It implements the io.Closer interface.
func (e *EthereumChain) Close() error {
return e.client.Close()
}
Loading