diff --git a/pkg/repositories/watched_events.go b/pkg/repositories/watched_events.go new file mode 100644 index 00000000..056449bb --- /dev/null +++ b/pkg/repositories/watched_events.go @@ -0,0 +1,40 @@ +package repositories + +type WatchedEventLog struct { + Name string `json:"name"` // name + BlockNumber int64 `json:"block_number" db:"block_number"` // block_number + Address string `json:"address"` // address + TxHash string `json:"tx_hash" db:"tx_hash"` // tx_hash + Index int64 `json:"index"` // index + Topic0 string `json:"topic0"` // topic0 + Topic1 string `json:"topic1"` // topic1 + Topic2 string `json:"topic2"` // topic2 + Topic3 string `json:"topic3"` // topic3 + Data string `json:"data"` // data +} + +type WatchedEventLogs interface { + AllWatchedEventLogs() ([]*WatchedEventLog, error) +} + +func (pg *Postgres) AllWatchedEventLogs() ([]*WatchedEventLog, error) { + rows, err := pg.Db.Queryx("SELECT name, block_number, address, tx_hash, index, topic0, topic1, topic2, topic3, data FROM watched_event_logs") + if err != nil { + return nil, err + } + defer rows.Close() + + lgs := make([]*WatchedEventLog, 0) + for rows.Next() { + lg := new(WatchedEventLog) + err := rows.StructScan(lg) + if err != nil { + return nil, err + } + lgs = append(lgs, lg) + } + if err = rows.Err(); err != nil { + return nil, err + } + return lgs, nil +} diff --git a/pkg/repositories/watched_events_test.go b/pkg/repositories/watched_events_test.go new file mode 100644 index 00000000..a3ffb406 --- /dev/null +++ b/pkg/repositories/watched_events_test.go @@ -0,0 +1,70 @@ +package repositories_test + +import ( + "github.com/vulcanize/vulcanizedb/pkg/repositories" + "github.com/vulcanize/vulcanizedb/pkg/repositories/testing" + + "log" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/config" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/filters" +) + +var _ = Describe("Watched Events Repository", func() { + var repository repositories.Postgres + + BeforeEach(func() { + cfg, err := config.NewConfig("private") + if err != nil { + log.Fatal(err) + } + repository, err = repositories.NewPostgres(cfg.Database, core.Node{}) + if err != nil { + log.Fatal(err) + } + testing.ClearData(repository) + }) + + It("retrieves watched logs that match the event filter", func() { + filter := filters.LogFilter{ + Name: "Filter1", + FromBlock: 0, + ToBlock: 10, + Address: "0x123", + Topics: core.Topics{0: "event1=10", 2: "event3=hello"}, + } + logs := []core.Log{ + { + BlockNumber: 0, + TxHash: "0x1", + Address: "0x123", + Topics: core.Topics{0: "event1=10", 2: "event3=hello"}, + Index: 0, + Data: "", + }, + } + expectedWatchedEventLog := []*repositories.WatchedEventLog{ + { + Name: "Filter1", + BlockNumber: 0, + TxHash: "0x1", + Address: "0x123", + Topic0: "event1=10", + Topic2: "event3=hello", + Index: 0, + Data: "", + }, + } + err := repository.AddFilter(filter) + Expect(err).ToNot(HaveOccurred()) + err = repository.CreateLogs(logs) + Expect(err).ToNot(HaveOccurred()) + matchingLogs, err := repository.AllWatchedEventLogs() + Expect(err).ToNot(HaveOccurred()) + Expect(matchingLogs).To(Equal(expectedWatchedEventLog)) + + }) +})