Update contract summary output to include transaction info

This commit is contained in:
Matt Krump 2017-11-14 09:14:34 -06:00 committed by Eric Meyer
parent a23023f7d6
commit d0602833bb
6 changed files with 90 additions and 15 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}
}

View File

@ -1,10 +0,0 @@
package watched_contracts
import "fmt"
func PrintReport(summary *ContractSummary) {
fmt.Printf(`********************Contract Summary***********************
HASH: %v
`, summary.ContractHash)
}

View File

@ -10,6 +10,8 @@ import (
type ContractSummary struct {
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
}
}

View File

@ -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"))
})
})
})