modulepackage
0.0.0-20230818095857-68c0e598bf11
Repository: https://github.com/func25/gother.git
Documentation: pkg.go.dev
# README
gother
gother is the library supporting interaction with smart contract, fetch contract's logs from blocks,...
Content:
Installation
- You first need Go installed (version 1.18+ is required):
$ go get -u github.com/func25/gother
- Import it in your code:
import "github.com/func25/gother"
Get started
Dial to node
You need to dial the RPC node first:
gother.DialCtx(context.Background(), "https://data-seed-prebsc-1-s3.binance.org:8545/")
// or gother.Dial("https://data-seed-prebsc-1-s3.binance.org:8545/")
Worker workflow
In some cases, we need to crawl contract logs from the block; you can collect with the support of a scanner and lazier.
Scanner: scan blocks to get the logs
// scan 100 blocks from block 2160030
scan := gother.NewScanner(100, 21600030)
logs, scannedBlock, err := scan.Scan(ctx)
if err != nil {
return err
}
// print the transaction hash of logs
for _, l := range logs {
fmt.Println(l.TxHash)
}
fmt.Printf("I have scanned to block %d\n", scannedBlock)
But how can you scan logs emitted from a specific smart contract?
// scan 100 blocks from block 2160030 and get the logs
// which emitted from contract: 0xbA01E92eA9B940745f89785fC9cED4DDc17Da450
scan := gother.NewScanner(100, 21600030, common.HexToAddress("0xbA01E92eA9B940745f89785fC9cED4DDc17Da450"))
Agent & Lazier: scan but in the loop
This lib supports you define an agent to do the loop: scan -> wait -> scan -> wait -> scan..., and the agent must meet the interface IAgent
// the target interface
type IAgent interface {
FromBlock(ctx context.Context) (uint64, error) // get the next block which want to scan from
ProcessLog(ctx context.Context, log types.Log) error // process the logs that agent collects
UpdateBlock(ctx context.Context, block uint64) error // update the scanned block after scanning
}
// create an agent
type Agent struct {
Block uint64
}
// Scanner will scan from this block
func (s *Agent) FromBlock(ctx context.Context) (uint64, error) {
return s.Block + 1, nil
}
// Process the crawled log
func (s *Agent) ProcessLog(ctx context.Context, log types.Log) error {
if log.Removed {
return nil
}
fmt.Println(log.TxHash.Hex())
return nil
}
// Save the scanned block after scanning
func (s *Agent) UpdateBlock(ctx context.Context, block uint64) error {
s.Block = block
return nil
}
After creating the agent, wrap it with the lazier and do scanning
// create an agent
agent := Agent{Block: 21600130}
// wrap the agent with lazier and wait duration of lazier is 3 seconds
lazier := gother.Lazier[*Agent]{Agent: &agent, Duration: time.Second * 3}
// scan 100 blocks each time, the `from` of scanner will be replaced with FromBlock(ctx) of agent
lazier.Scan(*gother.NewScanner(100, 0))
for {} // block the thread
# Functions
DialCtx will connect to `url` and set the default client to client(url) if default client is nil.
DialCtx will connect to `url` and set the default client to client(url) if default client is nil.
No description provided by the author
No description provided by the author
JsonABI parses json abi to abi.ABI, panic if error.
No description provided by the author
Keccak256Sign will hash data to 32 bytes (= keccak256) then signing it, return hexa-signature which is 132 bytes (0x...).
Keccak256SignBytes will hash data to 32 bytes (= keccak256) then signing it return bytes-signature which is 65 bytes (32 bytes r, 32 bytes s, 1 byte v).
No description provided by the author
NewScanner create a scanner which will scan `scanNum` blocks each time, it scans from `from` and only scans log from smart contracts which has address in `addresses` array.
No description provided by the author
RecoverHexSig will recover public key from [65]byte signature.
RecoverHexSig will recover public key from hex signature.
SetClient is used in case you already have ethclient.Client and want to use features of gother.
Sign signs the data with prefix `\x19Ethereum Signed Message:\n${len(data)}`.
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
Lazier is too lazy, he/she ignores errors when processing logs and updating block.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Interfaces
No description provided by the author