forked from cerc-io/ipld-eth-server
Allow users to watch and print summaries for contracts
This commit is contained in:
@@ -19,6 +19,10 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
||||
return present
|
||||
}
|
||||
|
||||
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract {
|
||||
return repository.watchedContracts[contractHash]
|
||||
}
|
||||
|
||||
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
|
||||
missingNumbers := []int64{}
|
||||
for blockNumber := int64(startingBlockNumber); blockNumber <= endingBlockNumber; blockNumber++ {
|
||||
|
||||
@@ -50,6 +50,23 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
||||
return exists
|
||||
}
|
||||
|
||||
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract {
|
||||
var savedContracts []core.WatchedContract
|
||||
contractRows, _ := repository.Db.Query(
|
||||
`select contract_hash from watched_contracts where contract_hash=$1`, contractHash)
|
||||
for contractRows.Next() {
|
||||
var savedContractHash string
|
||||
contractRows.Scan(&savedContractHash)
|
||||
savedContract := core.WatchedContract{Hash: savedContractHash}
|
||||
savedContracts = append(savedContracts, savedContract)
|
||||
}
|
||||
if len(savedContracts) > 0 {
|
||||
return &savedContracts[0]
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (repository Postgres) MaxBlockNumber() int64 {
|
||||
var highestBlockNumber int64
|
||||
repository.Db.Get(&highestBlockNumber, `SELECT MAX(block_number) FROM blocks`)
|
||||
|
||||
@@ -10,4 +10,5 @@ type Repository interface {
|
||||
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
||||
CreateWatchedContract(contract core.WatchedContract) error
|
||||
IsWatchedContract(contractHash string) bool
|
||||
FindWatchedContract(contractHash string) *core.WatchedContract
|
||||
}
|
||||
|
||||
@@ -194,14 +194,6 @@ var _ = Describe("Repositories", func() {
|
||||
|
||||
Expect(repository.MissingBlockNumbers(1, 5)).To(Equal([]int64{1, 2, 4, 5}))
|
||||
})
|
||||
|
||||
It("Adds a contract to the watched_contracts table", func() {
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
|
||||
Expect(repository.IsWatchedContract("x123")).To(BeTrue())
|
||||
Expect(repository.IsWatchedContract("x456")).To(BeFalse())
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Describe("The max block numbers", func() {
|
||||
@@ -218,6 +210,24 @@ var _ = Describe("Repositories", func() {
|
||||
Expect(repository.MaxBlockNumber()).To(Equal(int64(10)))
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Creating watched contracts", func() {
|
||||
It("returns the watched contract when it exists", func() {
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).NotTo(BeNil())
|
||||
Expect(watchedContract.Hash).To(Equal("x123"))
|
||||
|
||||
Expect(repository.IsWatchedContract("x123")).To(BeTrue())
|
||||
Expect(repository.IsWatchedContract("x456")).To(BeFalse())
|
||||
})
|
||||
|
||||
It("returns nil if contract does not exist", func() {
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).To(BeNil())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Describe("In memory repository", func() {
|
||||
|
||||
@@ -3,6 +3,7 @@ package testing
|
||||
import "github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
|
||||
func ClearData(postgres repositories.Postgres) {
|
||||
postgres.Db.MustExec("DELETE FROM watched_contracts")
|
||||
postgres.Db.MustExec("DELETE FROM transactions")
|
||||
postgres.Db.MustExec("DELETE FROM blocks")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user