2018-01-25 19:21:55 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-01-26 00:08:26 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/filters"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2018-02-12 16:54:05 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/repositories/postgres"
|
2018-01-26 00:08:26 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/utils"
|
2018-01-25 19:21:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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",
|
2018-01-25 19:21:55 +00:00
|
|
|
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 19:21:55 +00:00
|
|
|
|
2018-01-25 21:46:55 +00:00
|
|
|
vulcanizedb addFilter --config config.toml --filter-filepath filter.json
|
2018-01-25 19:21:55 +00:00
|
|
|
|
|
|
|
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)
|
2018-02-12 16:54:05 +00:00
|
|
|
db := utils.LoadPostgres(databaseConfig, blockchain.Node())
|
|
|
|
filterRepository := postgres.FilterRepository{DB: &db}
|
2018-01-25 19:21:55 +00:00
|
|
|
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 {
|
2018-02-12 16:54:05 +00:00
|
|
|
err = filterRepository.CreateFilter(filter)
|
2018-01-25 19:21:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|