2018-08-07 15:51:34 +00:00
|
|
|
// Copyright 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-07 20:17:29 +00:00
|
|
|
package flip_kick
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-08-09 21:55:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
2018-08-07 15:51:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FlipKickTransformer struct {
|
2018-08-09 21:55:02 +00:00
|
|
|
Fetcher shared.LogFetcher
|
2018-08-07 15:51:34 +00:00
|
|
|
Converter Converter
|
|
|
|
Repository Repository
|
2018-08-14 21:59:41 +00:00
|
|
|
Config shared.TransformerConfig
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FlipKickTransformerInitializer struct {
|
2018-08-14 21:59:41 +00:00
|
|
|
Config shared.TransformerConfig
|
2018-08-07 15:51:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i FlipKickTransformerInitializer) NewFlipKickTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
2018-08-09 21:55:02 +00:00
|
|
|
fetcher := shared.NewFetcher(blockChain)
|
2018-08-07 15:51:34 +00:00
|
|
|
repository := NewFlipKickRepository(db)
|
|
|
|
transformer := FlipKickTransformer{
|
|
|
|
Fetcher: fetcher,
|
|
|
|
Repository: repository,
|
|
|
|
Converter: FlipKickConverter{},
|
|
|
|
Config: i.Config,
|
|
|
|
}
|
|
|
|
|
|
|
|
return transformer
|
|
|
|
}
|
|
|
|
|
2018-08-14 21:59:41 +00:00
|
|
|
func (fkt *FlipKickTransformer) SetConfig(config shared.TransformerConfig) {
|
2018-08-07 15:51:34 +00:00
|
|
|
fkt.Config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2018-08-07 20:17:29 +00:00
|
|
|
FetcherError = "Error fetching FlipKick log events for block number %d: %s"
|
2018-08-07 15:51:34 +00:00
|
|
|
LogToEntityError = "Error converting eth log to FlipKick entity for block number %d: %s"
|
2018-08-07 20:17:29 +00:00
|
|
|
EntityToModelError = "Error converting eth log to FlipKick entity for block number %d: %s"
|
|
|
|
RepositoryError = "Error creating flip_kick record for block number %d: %s"
|
|
|
|
TransformerError = "There has been %d error(s) transforming FlipKick event logs, see the logs for more details."
|
2018-08-07 15:51:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type transformerError struct {
|
|
|
|
err string
|
|
|
|
blockNumber int64
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *transformerError) Error() string {
|
|
|
|
return fmt.Sprintf(te.msg, te.blockNumber, te.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTransformerError(err error, blockNumber int64, msg string) error {
|
|
|
|
e := transformerError{err.Error(), blockNumber, msg}
|
|
|
|
log.Println(e.Error())
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fkt FlipKickTransformer) Execute() error {
|
|
|
|
config := fkt.Config
|
2018-08-16 21:36:35 +00:00
|
|
|
topics := [][]common.Hash{{common.HexToHash(shared.FlipKickSignature)}}
|
2018-08-07 15:51:34 +00:00
|
|
|
|
|
|
|
headers, err := fkt.Repository.MissingHeaders(config.StartingBlockNumber, config.EndingBlockNumber)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Error:", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Fetching event logs for %d headers \n", len(headers))
|
|
|
|
var resultingErrors []error
|
|
|
|
for _, header := range headers {
|
2018-08-29 14:12:29 +00:00
|
|
|
ethLogs, err := fkt.Fetcher.FetchLogs(config.ContractAddress, topics, header.BlockNumber)
|
2018-08-07 15:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, FetcherError))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ethLog := range ethLogs {
|
2018-08-29 14:12:29 +00:00
|
|
|
entity, err := fkt.Converter.ToEntity(config.ContractAddress, config.ContractAbi, ethLog)
|
2018-08-07 15:51:34 +00:00
|
|
|
if err != nil {
|
|
|
|
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, LogToEntityError))
|
|
|
|
}
|
|
|
|
model, err := fkt.Converter.ToModel(*entity)
|
|
|
|
if err != nil {
|
|
|
|
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, EntityToModelError))
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fkt.Repository.Create(header.Id, model)
|
|
|
|
if err != nil {
|
|
|
|
resultingErrors = append(resultingErrors, newTransformerError(err, header.BlockNumber, RepositoryError))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resultingErrors) > 0 {
|
|
|
|
for _, err := range resultingErrors {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := fmt.Sprintf(TransformerError, len(resultingErrors))
|
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|