ipld-eth-server/cmd/addFilter.go

78 lines
2.1 KiB
Go
Raw Normal View History

package cmd
import (
"encoding/json"
"io/ioutil"
"log"
"github.com/spf13/cobra"
2018-01-26 00:08:26 +00:00
"github.com/vulcanize/vulcanizedb/pkg/filters"
"github.com/vulcanize/vulcanizedb/pkg/geth"
"github.com/vulcanize/vulcanizedb/pkg/repositories/postgres"
2018-01-26 00:08:26 +00:00
"github.com/vulcanize/vulcanizedb/utils"
)
// addFilterCmd represents the addFilter command
var addFilterCmd = &cobra.Command{
Use: "addFilter",
2018-01-25 21:46:55 +00:00
Short: "Adds event filter to vulcanizedb",
Long: `An event filter is added to the vulcanize_db.
All events matching the filter conitions will be tracked
2018-01-25 21:46:55 +00:00
in vulcanizedb.
2018-01-25 21:46:55 +00:00
vulcanizedb addFilter --config config.toml --filter-filepath filter.json
The event filters are expected to match
the format described in the ethereum RPC wiki:
https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_newfilter
[{
"fromBlock": "0x1",
"toBlock": "0x2",
"address": "0x8888f1f195afa192cfee860698584c030f4c9db1",
"topics": ["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
null,
"0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",
"0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"]
}]
`,
Run: func(cmd *cobra.Command, args []string) {
addFilter()
},
}
var filterFilepath string
func init() {
rootCmd.AddCommand(addFilterCmd)
addFilterCmd.PersistentFlags().StringVar(&filterFilepath, "filter-filepath", "", "path/to/filter.json")
addFilterCmd.MarkFlagRequired("filter-filepath")
}
func addFilter() {
if filterFilepath == "" {
log.Fatal("filter-filepath required")
}
var logFilters filters.LogFilters
blockchain := geth.NewBlockchain(ipc)
db := utils.LoadPostgres(databaseConfig, blockchain.Node())
filterRepository := postgres.FilterRepository{DB: &db}
absFilePath := utils.AbsFilePath(filterFilepath)
logFilterBytes, err := ioutil.ReadFile(absFilePath)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(logFilterBytes, &logFilters)
if err != nil {
log.Fatal(err)
}
for _, filter := range logFilters {
err = filterRepository.CreateFilter(filter)
if err != nil {
log.Fatal(err)
}
}
}