2018-11-03 19:00:25 +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.
package cmd
import (
"bufio"
"fmt"
"log"
"os"
"strings"
2018-11-03 19:02:31 +00:00
"time"
2018-11-03 19:00:25 +00:00
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/spf13/cobra"
2018-11-03 19:02:31 +00:00
"github.com/vulcanize/vulcanizedb/libraries/shared"
2018-11-03 19:00:25 +00:00
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
vRpc "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
"github.com/vulcanize/vulcanizedb/pkg/omni/transformer"
"github.com/vulcanize/vulcanizedb/pkg/omni/types"
)
// omniWatcherCmd represents the omniWatcher command
var omniWatcherCmd = & cobra . Command {
Use : "omniWatcher" ,
Short : "Watches events at the provided contract address" ,
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 ]
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
` ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
omniWatcher ( )
} ,
}
func omniWatcher ( ) {
2018-11-03 19:02:31 +00:00
ticker := time . NewTicker ( 5 * time . Second )
defer ticker . Stop ( )
2018-11-03 19:00:25 +00:00
2018-11-03 19:02:31 +00:00
if contractAddress == "" && len ( contractAddresses ) == 0 {
2018-11-03 19:00:25 +00:00
log . Fatal ( "Contract address required" )
}
2018-11-03 19:02:31 +00:00
if len ( contractEvents ) == 0 || len ( contractMethods ) == 0 {
2018-11-03 19:00:25 +00:00
var str string
for str != "y" {
reader := bufio . NewReader ( os . Stdin )
2018-11-03 19:02:31 +00:00
if len ( contractEvents ) == 0 && len ( contractMethods ) == 0 {
fmt . Print ( "Warning: no events or methods specified, proceed to watch every event and method? (Y/n)\n> " )
} else if len ( contractEvents ) == 0 {
fmt . Print ( "Warning: no events specified, proceed to watch every event? (Y/n)\n> " )
} else {
fmt . Print ( "Warning: no methods specified, proceed to watch every method? (Y/n)\n> " )
}
2018-11-03 19:00:25 +00:00
resp , err := reader . ReadBytes ( '\n' )
if err != nil {
log . Fatal ( err )
}
str = strings . ToLower ( string ( resp ) )
if str == "n" {
return
}
}
}
rawRpcClient , err := rpc . Dial ( ipc )
if err != nil {
2018-11-03 19:02:31 +00:00
log . Fatal ( fmt . Sprintf ( "Failed to initialize rpc client\r\nerr: %v\r\n" , err ) )
2018-11-03 19:00:25 +00:00
}
2018-11-03 19:02:31 +00:00
2018-11-03 19:00:25 +00:00
rpcClient := client . NewRpcClient ( rawRpcClient , ipc )
ethClient := ethclient . NewClient ( rawRpcClient )
client := client . NewEthClient ( ethClient )
node := node . MakeNode ( rpcClient )
transactionConverter := vRpc . NewRpcTransactionConverter ( ethClient )
blockChain := geth . NewBlockChain ( client , node , transactionConverter )
db , err := postgres . NewDB ( databaseConfig , blockChain . Node ( ) )
if err != nil {
log . Fatal ( fmt . Sprintf ( "Failed to initialize database\r\nerr: %v\r\n" , err ) )
}
con := types . Config {
DB : db ,
BC : blockChain ,
Network : network ,
}
t := transformer . NewTransformer ( & con )
2018-11-03 19:02:31 +00:00
contractAddresses = append ( contractAddresses , contractAddress )
for _ , addr := range contractAddresses {
t . Set ( addr , contractEvents )
}
2018-11-03 19:00:25 +00:00
err = t . Init ( )
if err != nil {
2018-11-03 19:02:31 +00:00
log . Fatal ( fmt . Sprintf ( "Failed to initialized transformer\r\nerr: %v\r\n" , err ) )
2018-11-03 19:00:25 +00:00
}
2018-11-03 19:02:31 +00:00
w := shared . Watcher { }
w . AddTransformer ( t )
for range ticker . C {
w . Execute ( )
}
2018-11-03 19:00:25 +00:00
}
func init ( ) {
rootCmd . AddCommand ( omniWatcherCmd )
omniWatcherCmd . Flags ( ) . StringVarP ( & contractAddress , "contract-address" , "a" , "" , "Single address to generate watchers for" )
2018-11-03 19:02:31 +00:00
omniWatcherCmd . Flags ( ) . StringArrayVarP ( & contractAddresses , "contract-addresses" , "l" , [ ] string { } , "List of addresses to generate watchers for" )
omniWatcherCmd . Flags ( ) . StringArrayVarP ( & contractEvents , "contract-events" , "e" , [ ] string { } , "Subset of events to watch; by default all events are watched" )
omniWatcherCmd . Flags ( ) . StringArrayVarP ( & contractEvents , "contract-methods" , "m" , [ ] string { } , "Subset of methods to watch; by default all methods are watched" )
2018-11-03 19:00:25 +00:00
omniWatcherCmd . Flags ( ) . StringVarP ( & network , "network" , "n" , "" , ` Network the contract is deployed on; options: "ropsten", "kovan", and "rinkeby"; default is mainnet" ` )
}