2019-03-11 16:20:22 +00:00
|
|
|
// VulcanizeDB
|
2019-03-11 21:18:13 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-03-11 16:20:22 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
// Contract watcher is built with a more generic interface
|
|
|
|
// that allows offloading more of the operational logic to
|
2019-03-11 16:20:22 +00:00
|
|
|
// the transformers, allowing them to act more dynamically
|
2019-03-13 16:14:35 +00:00
|
|
|
// Built to work primarily with the contract_watcher packaging
|
2019-03-11 16:20:22 +00:00
|
|
|
package watcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-17 23:16:01 +00:00
|
|
|
|
2019-09-11 03:37:12 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-03-11 16:20:22 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/core"
|
2020-02-10 15:00:55 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/postgres"
|
2019-03-11 16:20:22 +00:00
|
|
|
)
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
type ContractWatcher struct {
|
|
|
|
Transformers []transformer.ContractTransformer
|
2019-03-11 16:20:22 +00:00
|
|
|
DB *postgres.DB
|
|
|
|
BlockChain core.BlockChain
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
func NewContractWatcher(db *postgres.DB, bc core.BlockChain) ContractWatcher {
|
|
|
|
return ContractWatcher{
|
2019-03-11 16:20:22 +00:00
|
|
|
DB: db,
|
|
|
|
BlockChain: bc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-13 16:14:35 +00:00
|
|
|
func (watcher *ContractWatcher) AddTransformers(inits interface{}) error {
|
|
|
|
initializers, ok := inits.([]transformer.ContractTransformerInitializer)
|
2019-03-11 16:20:22 +00:00
|
|
|
if !ok {
|
2019-03-13 16:14:35 +00:00
|
|
|
return fmt.Errorf("initializers of type %T, not %T", inits, []transformer.ContractTransformerInitializer{})
|
2019-03-11 16:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, initializer := range initializers {
|
|
|
|
t := initializer(watcher.DB, watcher.BlockChain)
|
|
|
|
watcher.Transformers = append(watcher.Transformers, t)
|
|
|
|
}
|
|
|
|
|
2019-04-24 20:05:57 +00:00
|
|
|
for _, contractTransformer := range watcher.Transformers {
|
|
|
|
err := contractTransformer.Init()
|
2019-03-11 16:20:22 +00:00
|
|
|
if err != nil {
|
2019-09-11 03:37:12 +00:00
|
|
|
logrus.Print("Unable to initialize transformer:", contractTransformer.GetConfig().Name, err)
|
2019-03-11 16:20:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 21:49:27 +00:00
|
|
|
func (watcher *ContractWatcher) Execute() error {
|
2019-04-24 20:05:57 +00:00
|
|
|
for _, contractTransformer := range watcher.Transformers {
|
|
|
|
err := contractTransformer.Execute()
|
2019-03-11 16:20:22 +00:00
|
|
|
if err != nil {
|
2019-09-11 03:37:12 +00:00
|
|
|
logrus.Error("Unable to execute transformer:", contractTransformer.GetConfig().Name, err)
|
2019-03-11 16:20:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|