// VulcanizeDB // Copyright © 2018 Vulcanize // 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 . package cmd import ( "fmt" "log" "time" "github.com/spf13/cobra" "github.com/vulcanize/vulcanizedb/libraries/shared" "github.com/vulcanize/vulcanizedb/pkg/omni/full/transformer" "github.com/vulcanize/vulcanizedb/utils" ) // omniWatcherCmd represents the omniWatcher command var omniWatcherCmd = &cobra.Command{ Use: "omniWatcher", 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] ipcPath = "/Users/user/Library/Ethereum/geth.ipc" `, Run: func(cmd *cobra.Command, args []string) { omniWatcher() }, } func omniWatcher() { if contractAddress == "" && len(contractAddresses) == 0 { log.Fatal("Contract address required") } ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() blockChain := getBlockChain() db := utils.LoadPostgres(databaseConfig, blockChain.Node()) t := transformer.NewTransformer(network, blockChain, &db) contractAddresses = append(contractAddresses, contractAddress) for _, addr := range contractAddresses { t.SetEvents(addr, contractEvents) t.SetMethods(addr, contractMethods) t.SetEventArgs(addr, eventArgs) t.SetMethodArgs(addr, methodArgs) t.SetRange(addr, [2]int64{startingBlockNumber, endingBlockNumber}) } err := t.Init() if err != nil { log.Fatal(fmt.Sprintf("Failed to initialized transformer\r\nerr: %v\r\n", err)) } w := shared.Watcher{} w.AddTransformer(t) for range ticker.C { w.Execute() } } func init() { rootCmd.AddCommand(omniWatcherCmd) omniWatcherCmd.Flags().StringVarP(&contractAddress, "contract-address", "a", "", "Single address to generate watchers for") omniWatcherCmd.Flags().StringArrayVarP(&contractAddresses, "contract-addresses", "l", []string{}, "list of addresses to use; warning: watcher targets the same events and methods for each address") omniWatcherCmd.Flags().StringArrayVarP(&contractEvents, "events", "e", []string{}, "Subset of events to watch; by default all events are watched") omniWatcherCmd.Flags().StringArrayVarP(&contractMethods, "methods", "m", nil, "Subset of methods to poll; by default no methods are polled") omniWatcherCmd.Flags().StringArrayVarP(&eventArgs, "event-args", "f", []string{}, "Argument values to filter event logs for; will only persist event logs that emit at least one of the value specified") omniWatcherCmd.Flags().StringArrayVarP(&methodArgs, "method-args", "g", []string{}, "Argument values to limit methods to; will only call methods with emitted values that were specified here") omniWatcherCmd.Flags().StringVarP(&network, "network", "n", "", `Network the contract is deployed on; options: "ropsten", "kovan", and "rinkeby"; default is mainnet"`) omniWatcherCmd.Flags().Int64VarP(&startingBlockNumber, "starting-block-number", "s", 0, "Block to begin watching- default is first block the contract exists") omniWatcherCmd.Flags().Int64VarP(&endingBlockNumber, "ending-block-number", "d", -1, "Block to end watching- default is most recent block") omniWatcherCmd.Flags().BoolVarP(&createAddrList, "create-address-list", "c", false, "Set to true to persist address seen in emitted events into the database") }