Merge pull request #70 from 8thlight/show-contract-summary
Allow users to watch and print summaries for contracts
This commit is contained in:
commit
874051cf89
@ -66,6 +66,16 @@ func tasks(p *do.Project) {
|
||||
context.Bash(dumpSchema)
|
||||
})
|
||||
|
||||
p.Task("showContractSummary", nil, func(context *do.Context) {
|
||||
environment := parseEnvironment(context)
|
||||
contractHash := context.Args.MayString("", "contract-hash", "c")
|
||||
if contractHash == "" {
|
||||
log.Fatalln("--contract-hash required")
|
||||
}
|
||||
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}}`,
|
||||
do.M{"environment": environment, "contractHash": contractHash, "$in": "cmd/show_contract_summary"})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
25
cmd/show_contract_summary/main.go
Normal file
25
cmd/show_contract_summary/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"log"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/cmd"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
"github.com/8thlight/vulcanizedb/pkg/watched_contracts"
|
||||
)
|
||||
|
||||
func main() {
|
||||
environment := flag.String("environment", "", "Environment name")
|
||||
contractHash := flag.String("contract-hash", "", "Contract hash to show summary")
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
|
||||
repository := repositories.NewPostgres(config.Database)
|
||||
contractSummary, err := watched_contracts.NewSummary(repository, *contractHash)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
watched_contracts.PrintReport(contractSummary)
|
||||
}
|
@ -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")
|
||||
}
|
||||
|
10
pkg/watched_contracts/reporter.go
Normal file
10
pkg/watched_contracts/reporter.go
Normal file
@ -0,0 +1,10 @@
|
||||
package watched_contracts
|
||||
|
||||
import "fmt"
|
||||
|
||||
func PrintReport(summary *ContractSummary) {
|
||||
fmt.Printf(`********************Contract Summary***********************
|
||||
|
||||
HASH: %v
|
||||
`, summary.ContractHash)
|
||||
}
|
30
pkg/watched_contracts/summary.go
Normal file
30
pkg/watched_contracts/summary.go
Normal file
@ -0,0 +1,30 @@
|
||||
package watched_contracts
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
type ContractSummary struct {
|
||||
ContractHash string
|
||||
}
|
||||
|
||||
var NewContractNotWatchedErr = func(contractHash string) error {
|
||||
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
|
||||
}
|
||||
|
||||
func NewSummary(repository repositories.Repository, contractHash string) (*ContractSummary, error) {
|
||||
contract := repository.FindWatchedContract(contractHash)
|
||||
if contract != nil {
|
||||
return newContractSummary(*contract), nil
|
||||
} else {
|
||||
return nil, NewContractNotWatchedErr(contractHash)
|
||||
}
|
||||
}
|
||||
|
||||
func newContractSummary(contract core.WatchedContract) *ContractSummary {
|
||||
return &ContractSummary{ContractHash: contract.Hash}
|
||||
}
|
47
pkg/watched_contracts/summary_test.go
Normal file
47
pkg/watched_contracts/summary_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package watched_contracts_test
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
"github.com/8thlight/vulcanizedb/pkg/watched_contracts"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("The watched contract summary", func() {
|
||||
|
||||
Context("when the given contract is not being watched", func() {
|
||||
It("returns an error", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
|
||||
contractSummary, err := watched_contracts.NewSummary(repository, "123")
|
||||
|
||||
Expect(contractSummary).To(BeNil())
|
||||
Expect(err).NotTo(BeNil())
|
||||
})
|
||||
})
|
||||
|
||||
Context("when the given contract is being watched", func() {
|
||||
It("returns the summary", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
|
||||
contractSummary, err := watched_contracts.NewSummary(repository, "0x123")
|
||||
|
||||
Expect(contractSummary).NotTo(BeNil())
|
||||
Expect(err).To(BeNil())
|
||||
})
|
||||
|
||||
It("includes the contract hash in the summary", func() {
|
||||
repository := repositories.NewInMemory()
|
||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
||||
repository.CreateWatchedContract(watchedContract)
|
||||
|
||||
contractSummary, _ := watched_contracts.NewSummary(repository, "0x123")
|
||||
|
||||
Expect(contractSummary.ContractHash).To(Equal("0x123"))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
13
pkg/watched_contracts/watched_contracts_suite_test.go
Normal file
13
pkg/watched_contracts/watched_contracts_suite_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package watched_contracts_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWatchedContracts(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "WatchedContracts Suite")
|
||||
}
|
Loading…
Reference in New Issue
Block a user