65 lines
1.7 KiB
Go
65 lines
1.7 KiB
Go
package vat_slip
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
|
)
|
|
|
|
type VatSlipTransformer struct {
|
|
Config shared.TransformerConfig
|
|
Converter Converter
|
|
Fetcher shared.LogFetcher
|
|
Repository Repository
|
|
}
|
|
|
|
type VatSlipTransformerInitializer struct {
|
|
Config shared.TransformerConfig
|
|
}
|
|
|
|
func (initializer VatSlipTransformerInitializer) NewVatSlipTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
|
converter := VatSlipConverter{}
|
|
fetcher := shared.NewFetcher(blockChain)
|
|
repository := NewVatSlipRepository(db)
|
|
return VatSlipTransformer{
|
|
Config: initializer.Config,
|
|
Converter: converter,
|
|
Fetcher: fetcher,
|
|
Repository: repository,
|
|
}
|
|
}
|
|
|
|
func (transformer VatSlipTransformer) Execute() error {
|
|
missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
log.Printf("Fetching vat slip event logs for %d headers \n", len(missingHeaders))
|
|
for _, header := range missingHeaders {
|
|
topics := [][]common.Hash{{common.HexToHash(shared.VatSlipSignature)}}
|
|
matchingLogs, err := transformer.Fetcher.FetchLogs(VatSlipConfig.ContractAddresses, topics, header.BlockNumber)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(matchingLogs) < 1 {
|
|
err = transformer.Repository.MarkHeaderChecked(header.Id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
models, err := transformer.Converter.ToModels(matchingLogs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = transformer.Repository.Create(header.Id, models)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|