2019-03-11 16:19:18 +00:00
// VulcanizeDB
2019-03-11 21:18:13 +00:00
// Copyright © 2019 Vulcanize
2019-03-11 16:19:18 +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/>.
package cmd
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
st "github.com/vulcanize/vulcanizedb/libraries/shared/transformer"
2019-09-30 17:37:16 +00:00
"github.com/vulcanize/vulcanizedb/pkg/config"
2020-01-29 19:00:07 +00:00
ft "github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/full/transformer"
ht "github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/header/transformer"
2019-03-11 16:19:18 +00:00
"github.com/vulcanize/vulcanizedb/utils"
)
// contractWatcherCmd represents the contractWatcher command
var contractWatcherCmd = & cobra . Command {
Use : "contractWatcher" ,
Short : "Watches events at the provided contract address using fully synced vDB" ,
Long : ` Uses input contract address and event filters to watch events
Expects an ethereum node to be running
Expects an archival node synced into vulcanizeDB
Requires a . toml config file :
[ database ]
name = "vulcanize_public"
hostname = "localhost"
port = 5432
[ client ]
2019-03-14 21:49:27 +00:00
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
2019-03-11 16:19:18 +00:00
[ contract ]
network = ""
addresses = [
"contractAddress1" ,
"contractAddress2"
]
[ contract . contractAddress1 ]
abi = ' ABI for contract 1 '
startingBlock = 982463
[ contract . contractAddress2 ]
abi = ' ABI for contract 2 '
events = [
"event1" ,
"event2"
]
eventArgs = [
"arg1" ,
"arg2"
]
methods = [
"method1" ,
"method2"
]
methodArgs = [
"arg1" ,
"arg2"
]
startingBlock = 4448566
piping = true
` ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2019-10-14 14:41:29 +00:00
subCommand = cmd . CalledAs ( )
logWithCommand = * log . WithField ( "SubCommand" , subCommand )
2019-03-11 16:19:18 +00:00
contractWatcher ( )
} ,
}
var (
mode string
)
func contractWatcher ( ) {
ticker := time . NewTicker ( 5 * time . Second )
defer ticker . Stop ( )
2019-07-19 01:58:01 +00:00
blockChain := getBlockChain ( )
2019-03-11 16:19:18 +00:00
db := utils . LoadPostgres ( databaseConfig , blockChain . Node ( ) )
2019-03-13 16:14:35 +00:00
var t st . ContractTransformer
2019-03-11 16:19:18 +00:00
con := config . ContractConfig { }
con . PrepConfig ( )
switch mode {
2019-05-01 06:02:30 +00:00
case "header" :
2019-05-07 22:27:21 +00:00
t = ht . NewTransformer ( con , blockChain , & db )
2019-03-11 16:19:18 +00:00
case "full" :
t = ft . NewTransformer ( con , blockChain , & db )
default :
2019-10-14 14:41:29 +00:00
logWithCommand . Fatal ( "Invalid mode" )
2019-03-11 16:19:18 +00:00
}
err := t . Init ( )
if err != nil {
2019-10-14 14:41:29 +00:00
logWithCommand . Fatal ( fmt . Sprintf ( "Failed to initialize transformer, err: %v " , err ) )
2019-03-11 16:19:18 +00:00
}
for range ticker . C {
2019-03-14 21:49:27 +00:00
err = t . Execute ( )
if err != nil {
2019-10-14 14:41:29 +00:00
logWithCommand . Error ( "Execution error for transformer: " , t . GetConfig ( ) . Name , err )
2019-03-14 21:49:27 +00:00
}
2019-03-11 16:19:18 +00:00
}
}
func init ( ) {
rootCmd . AddCommand ( contractWatcherCmd )
2019-05-01 06:02:30 +00:00
contractWatcherCmd . Flags ( ) . StringVarP ( & mode , "mode" , "o" , "header" , "'header' or 'full' mode to work with either header synced or fully synced vDB (default is header)" )
2019-03-11 16:19:18 +00:00
}