Allow users to watch and print summaries for contracts

This commit is contained in:
Eric Meyer
2017-11-13 13:51:09 -06:00
parent f0f086e48d
commit f50a4d7726
11 changed files with 176 additions and 8 deletions
+4
View File
@@ -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++ {
+17
View File
@@ -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`)
+1
View File
@@ -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
}
+18 -8
View File
@@ -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() {
+1
View File
@@ -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")
}