From d0602833bbf2797e0571f4640b048dcbad29e9b9 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Tue, 14 Nov 2017 09:14:34 -0600 Subject: [PATCH] Update contract summary output to include transaction info --- cmd/show_contract_summary/main.go | 5 ++- pkg/repositories/postgres.go | 2 +- pkg/watched_contracts/console_presenter.go | 34 ++++++++++++++++++++ pkg/watched_contracts/reporter.go | 10 ------ pkg/watched_contracts/summary.go | 18 +++++++++-- pkg/watched_contracts/summary_test.go | 36 +++++++++++++++++++++- 6 files changed, 90 insertions(+), 15 deletions(-) create mode 100644 pkg/watched_contracts/console_presenter.go delete mode 100644 pkg/watched_contracts/reporter.go diff --git a/cmd/show_contract_summary/main.go b/cmd/show_contract_summary/main.go index a0454db5..712897ee 100644 --- a/cmd/show_contract_summary/main.go +++ b/cmd/show_contract_summary/main.go @@ -5,6 +5,8 @@ import ( "log" + "fmt" + "github.com/8thlight/vulcanizedb/cmd" "github.com/8thlight/vulcanizedb/pkg/repositories" "github.com/8thlight/vulcanizedb/pkg/watched_contracts" @@ -21,5 +23,6 @@ func main() { if err != nil { log.Fatalln(err) } - watched_contracts.PrintReport(contractSummary) + output := watched_contracts.GenerateConsoleOutput(contractSummary) + fmt.Println(output) } diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index cb9e0646..417168e2 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -200,7 +200,7 @@ func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedCo for contractRows.Next() { var savedContractHash string contractRows.Scan(&savedContractHash) - transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1`, savedContractHash) + transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash) transactions := repository.loadTransactions(transactionRows) savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions} savedContracts = append(savedContracts, savedContract) diff --git a/pkg/watched_contracts/console_presenter.go b/pkg/watched_contracts/console_presenter.go new file mode 100644 index 00000000..d63aedc8 --- /dev/null +++ b/pkg/watched_contracts/console_presenter.go @@ -0,0 +1,34 @@ +package watched_contracts + +import ( + "fmt" + + "github.com/8thlight/vulcanizedb/pkg/core" +) + +func GenerateConsoleOutput(summary *ContractSummary) string { + return fmt.Sprintf(template(), + summary.ContractHash, + summary.NumberOfTransactions, + transactionToString(summary.LastTransaction), + ) +} + +func template() string { + return `********************Contract Summary*********************** + HASH: %v + NUMBER OF TRANSACTIONS: %d + LAST TRANSACTION: + %s + ` +} + +func transactionToString(transaction *core.Transaction) string { + if transaction == nil { + return "NONE" + } else { + return fmt.Sprintf(`Hash: %s + To: %s + From: %s`, transaction.Hash, transaction.To, transaction.From) + } +} diff --git a/pkg/watched_contracts/reporter.go b/pkg/watched_contracts/reporter.go deleted file mode 100644 index f5ec8e6d..00000000 --- a/pkg/watched_contracts/reporter.go +++ /dev/null @@ -1,10 +0,0 @@ -package watched_contracts - -import "fmt" - -func PrintReport(summary *ContractSummary) { - fmt.Printf(`********************Contract Summary*********************** - -HASH: %v -`, summary.ContractHash) -} diff --git a/pkg/watched_contracts/summary.go b/pkg/watched_contracts/summary.go index 3b935859..a97e6344 100644 --- a/pkg/watched_contracts/summary.go +++ b/pkg/watched_contracts/summary.go @@ -9,7 +9,9 @@ import ( ) type ContractSummary struct { - ContractHash string + ContractHash string + NumberOfTransactions int + LastTransaction *core.Transaction } var NewContractNotWatchedErr = func(contractHash string) error { @@ -26,5 +28,17 @@ func NewSummary(repository repositories.Repository, contractHash string) (*Contr } func newContractSummary(contract core.WatchedContract) *ContractSummary { - return &ContractSummary{ContractHash: contract.Hash} + return &ContractSummary{ + ContractHash: contract.Hash, + NumberOfTransactions: len(contract.Transactions), + LastTransaction: lastTransaction(contract), + } +} + +func lastTransaction(contract core.WatchedContract) *core.Transaction { + if len(contract.Transactions) > 0 { + return &contract.Transactions[0] + } else { + return nil + } } diff --git a/pkg/watched_contracts/summary_test.go b/pkg/watched_contracts/summary_test.go index 9081b3fe..2f2344ab 100644 --- a/pkg/watched_contracts/summary_test.go +++ b/pkg/watched_contracts/summary_test.go @@ -8,7 +8,7 @@ import ( . "github.com/onsi/gomega" ) -var _ = Describe("The watched contract summary", func() { +var _ bool = Describe("The watched contract summary", func() { Context("when the given contract is not being watched", func() { It("returns an error", func() { @@ -42,6 +42,40 @@ var _ = Describe("The watched contract summary", func() { Expect(contractSummary.ContractHash).To(Equal("0x123")) }) + + It("sets the number of transactions", func() { + repository := repositories.NewInMemory() + watchedContract := core.WatchedContract{Hash: "0x123"} + repository.CreateWatchedContract(watchedContract) + block := core.Block{ + Transactions: []core.Transaction{ + {To: "0x123"}, + {To: "0x123"}, + }, + } + repository.CreateBlock(block) + + contractSummary, _ := watched_contracts.NewSummary(repository, "0x123") + + Expect(contractSummary.NumberOfTransactions).To(Equal(2)) + }) + + It("sets the last transaction", func() { + repository := repositories.NewInMemory() + watchedContract := core.WatchedContract{Hash: "0x123"} + repository.CreateWatchedContract(watchedContract) + block := core.Block{ + Transactions: []core.Transaction{ + {Hash: "TRANSACTION2", To: "0x123"}, + {Hash: "TRANSACTION1", To: "0x123"}, + }, + } + repository.CreateBlock(block) + + contractSummary, _ := watched_contracts.NewSummary(repository, "0x123") + + Expect(contractSummary.LastTransaction.Hash).To(Equal("TRANSACTION2")) + }) }) })