2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +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/>.
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
package transformer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-12-07 15:38:46 +00:00
|
|
|
|
2019-03-11 16:19:18 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-11-04 21:26:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-11-04 21:26:39 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
|
2018-11-23 18:12:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/full/converter"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/full/retriever"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/contract"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/parser"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/poller"
|
2018-11-24 04:26:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/repository"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/omni/shared/types"
|
2018-11-04 21:26:39 +00:00
|
|
|
)
|
|
|
|
|
2018-11-23 18:12:24 +00:00
|
|
|
// Requires a fully synced vDB and a running eth node (or infura)
|
2019-03-11 16:19:18 +00:00
|
|
|
type transformer struct {
|
2018-11-07 21:50:43 +00:00
|
|
|
// Database interfaces
|
2018-11-20 16:38:23 +00:00
|
|
|
datastore.FilterRepository // Log filters repo; accepts filters generated by Contract.GenerateFilters()
|
|
|
|
datastore.WatchedEventRepository // Watched event log views, created by the log filters
|
2018-11-23 18:12:24 +00:00
|
|
|
repository.EventRepository // Holds transformed watched event log data
|
2018-11-04 21:26:39 +00:00
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
// Pre-processing interfaces
|
|
|
|
parser.Parser // Parses events and methods out of contract abi fetched using contract address
|
|
|
|
retriever.BlockRetriever // Retrieves first block for contract and current block height
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// Processing interfaces
|
2018-11-20 16:38:23 +00:00
|
|
|
converter.Converter // Converts watched event logs into custom log
|
|
|
|
poller.Poller // Polls methods using contract's token holder addresses and persists them using method datastore
|
|
|
|
|
2019-03-11 16:19:18 +00:00
|
|
|
// Store contract configuration information
|
|
|
|
Config config.ContractConfig
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
// Store contract info as mapping to contract address
|
|
|
|
Contracts map[string]*contract.Contract
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transformer takes in config for blockchain, database, and network id
|
2019-03-11 16:19:18 +00:00
|
|
|
func NewTransformer(con config.ContractConfig, BC core.BlockChain, DB *postgres.DB) *transformer {
|
|
|
|
return &transformer{
|
2018-11-24 04:26:07 +00:00
|
|
|
Poller: poller.NewPoller(BC, DB, types.FullSync),
|
2019-03-11 16:19:18 +00:00
|
|
|
Parser: parser.NewParser(con.Network),
|
2018-11-07 21:50:43 +00:00
|
|
|
BlockRetriever: retriever.NewBlockRetriever(DB),
|
|
|
|
Converter: converter.NewConverter(&contract.Contract{}),
|
2018-11-04 21:26:39 +00:00
|
|
|
Contracts: map[string]*contract.Contract{},
|
2018-11-07 21:50:43 +00:00
|
|
|
WatchedEventRepository: repositories.WatchedEventRepository{DB: DB},
|
|
|
|
FilterRepository: repositories.FilterRepository{DB: DB},
|
2018-11-24 04:26:07 +00:00
|
|
|
EventRepository: repository.NewEventRepository(DB, types.FullSync),
|
2019-03-11 16:19:18 +00:00
|
|
|
Config: con,
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use after creating and setting transformer
|
|
|
|
// Loops over all of the addr => filter sets
|
|
|
|
// Uses parser to pull event info from abi
|
|
|
|
// Use this info to generate event filters
|
2019-03-11 16:19:18 +00:00
|
|
|
func (tr *transformer) Init() error {
|
|
|
|
for contractAddr := range tr.Config.Addresses {
|
|
|
|
// Configure Abi
|
|
|
|
if tr.Config.Abis[contractAddr] == "" {
|
|
|
|
// If no abi is given in the config, this method will try fetching from internal look-up table and etherscan
|
|
|
|
err := tr.Parser.Parse(contractAddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If we have an abi from the config, load that into the parser
|
|
|
|
err := tr.Parser.ParseAbiStr(tr.Config.Abis[contractAddr])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 18:15:22 +00:00
|
|
|
// Get first block and most recent block number in the header repo
|
2019-03-11 16:19:18 +00:00
|
|
|
firstBlock, err := tr.BlockRetriever.RetrieveFirstBlock(contractAddr)
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-11 16:19:18 +00:00
|
|
|
lastBlock, err := tr.BlockRetriever.RetrieveMostRecentBlock()
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-04 18:15:22 +00:00
|
|
|
// Set to specified range if it falls within the bounds
|
2019-03-11 16:19:18 +00:00
|
|
|
if firstBlock < tr.Config.StartingBlocks[contractAddr] {
|
|
|
|
firstBlock = tr.Config.StartingBlocks[contractAddr]
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Get contract name if it has one
|
2018-11-07 21:50:43 +00:00
|
|
|
var name = new(string)
|
2019-03-11 16:19:18 +00:00
|
|
|
tr.FetchContractData(tr.Abi(), contractAddr, "name", nil, &name, lastBlock)
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-07 15:38:46 +00:00
|
|
|
// Remove any potential accidental duplicate inputs in arg filter values
|
|
|
|
eventArgs := map[string]bool{}
|
2019-03-11 16:19:18 +00:00
|
|
|
for _, arg := range tr.Config.EventArgs[contractAddr] {
|
2018-12-07 15:38:46 +00:00
|
|
|
eventArgs[arg] = true
|
2018-11-07 21:50:43 +00:00
|
|
|
}
|
2018-12-07 15:38:46 +00:00
|
|
|
methodArgs := map[string]bool{}
|
2019-03-11 16:19:18 +00:00
|
|
|
for _, arg := range tr.Config.MethodArgs[contractAddr] {
|
2018-12-07 15:38:46 +00:00
|
|
|
methodArgs[arg] = true
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Aggregate info into contract object
|
2018-12-07 15:38:46 +00:00
|
|
|
info := contract.Contract{
|
2019-03-11 16:19:18 +00:00
|
|
|
Name: *name,
|
|
|
|
Network: tr.Config.Network,
|
|
|
|
Address: contractAddr,
|
|
|
|
Abi: tr.Parser.Abi(),
|
|
|
|
ParsedAbi: tr.Parser.ParsedAbi(),
|
|
|
|
StartingBlock: firstBlock,
|
|
|
|
LastBlock: lastBlock,
|
|
|
|
Events: tr.Parser.GetEvents(tr.Config.Events[contractAddr]),
|
|
|
|
Methods: tr.Parser.GetSelectMethods(tr.Config.Methods[contractAddr]),
|
|
|
|
FilterArgs: eventArgs,
|
|
|
|
MethodArgs: methodArgs,
|
|
|
|
Piping: tr.Config.Piping[contractAddr],
|
2018-12-07 15:38:46 +00:00
|
|
|
}.Init()
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
// Use info to create filters
|
2018-11-07 21:50:43 +00:00
|
|
|
err = info.GenerateFilters()
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
// Iterate over filters and push them to the repo using filter repository interface
|
2018-11-04 21:26:39 +00:00
|
|
|
for _, filter := range info.Filters {
|
2019-03-11 16:19:18 +00:00
|
|
|
err = tr.CreateFilter(filter)
|
2018-12-14 17:52:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
// Store contract info for further processing
|
2019-03-11 16:19:18 +00:00
|
|
|
tr.Contracts[contractAddr] = info
|
2018-11-04 21:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
// Iterates through stored, initialized contract objects
|
|
|
|
// Iterates through contract's event filters, grabbing watched event logs
|
|
|
|
// Uses converter to convert logs into custom log type
|
|
|
|
// Persists converted logs into custuom postgres tables
|
|
|
|
// Calls selected methods, using token holder address generated during event log conversion
|
2019-03-11 16:19:18 +00:00
|
|
|
func (tr *transformer) Execute() error {
|
|
|
|
if len(tr.Contracts) == 0 {
|
2018-11-20 16:38:23 +00:00
|
|
|
return errors.New("error: transformer has no initialized contracts to work with")
|
|
|
|
}
|
2018-11-04 21:26:39 +00:00
|
|
|
// Iterate through all internal contracts
|
2019-03-11 16:19:18 +00:00
|
|
|
for _, con := range tr.Contracts {
|
2018-11-04 21:26:39 +00:00
|
|
|
// Update converter with current contract
|
2019-03-11 16:19:18 +00:00
|
|
|
tr.Update(con)
|
2018-11-04 21:26:39 +00:00
|
|
|
|
|
|
|
// Iterate through contract filters and get watched event logs
|
2018-12-18 17:31:21 +00:00
|
|
|
for eventSig, filter := range con.Filters {
|
2019-03-11 16:19:18 +00:00
|
|
|
watchedEvents, err := tr.GetWatchedEvents(filter.Name)
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
// Iterate over watched event logs
|
2018-11-04 21:26:39 +00:00
|
|
|
for _, we := range watchedEvents {
|
2018-11-20 16:38:23 +00:00
|
|
|
// Convert them to our custom log type
|
2019-03-11 16:19:18 +00:00
|
|
|
cstm, err := tr.Converter.Convert(*we, con.Events[eventSig])
|
2018-11-20 16:38:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-24 04:26:07 +00:00
|
|
|
if cstm == nil {
|
|
|
|
continue
|
2018-11-23 18:12:24 +00:00
|
|
|
}
|
2018-11-20 16:38:23 +00:00
|
|
|
|
2018-11-23 18:12:24 +00:00
|
|
|
// If log is not empty, immediately persist in repo
|
2018-11-20 16:38:23 +00:00
|
|
|
// Run this in seperate goroutine?
|
2019-03-11 16:19:18 +00:00
|
|
|
err = tr.PersistLogs([]types.Log{*cstm}, con.Events[eventSig], con.Address, con.Name)
|
2018-11-04 21:26:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:38:23 +00:00
|
|
|
// After persisting all watched event logs
|
|
|
|
// poller polls select contract methods
|
|
|
|
// and persists the results into custom pg tables
|
|
|
|
// Run this in seperate goroutine?
|
2019-03-11 16:19:18 +00:00
|
|
|
if err := tr.PollContract(*con); err != nil {
|
2018-11-04 21:26:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2019-03-11 16:19:18 +00:00
|
|
|
func (tr *transformer) GetConfig() config.ContractConfig {
|
|
|
|
return tr.Config
|
2018-12-14 17:52:02 +00:00
|
|
|
}
|