ipld-eth-server/pkg/contract_summary/console_presenter.go

61 lines
1.6 KiB
Go
Raw Normal View History

package contract_summary
import (
"fmt"
"github.com/8thlight/vulcanizedb/pkg/core"
2017-11-29 15:31:23 +00:00
"github.com/ethereum/go-ethereum/common"
)
func GenerateConsoleOutput(summary ContractSummary) string {
return fmt.Sprintf(template(),
summary.ContractHash,
summary.NumberOfTransactions,
transactionToString(summary.LastTransaction),
attributesString(summary),
)
}
func template() string {
return `********************Contract Summary***********************
2017-11-27 14:54:30 +00:00
HASH: %v
NUMBER OF TRANSACTIONS: %d
LAST TRANSACTION:
%s
ATTRIBUTES:
%s
`
}
func transactionToString(transaction *core.Transaction) string {
if transaction == nil {
return "NONE"
} else {
return fmt.Sprintf(`Hash: %s
2017-11-27 14:54:30 +00:00
To: %s
From: %s`, transaction.Hash, transaction.To, transaction.From)
}
}
func attributesString(summary ContractSummary) string {
var formattedAttributes string
for _, attribute := range summary.Attributes {
formattedAttributes += formatAttribute(attribute.Name, summary) + "\n" + " "
}
return formattedAttributes
}
func formatAttribute(attributeName string, summary ContractSummary) string {
2017-11-29 15:31:23 +00:00
var stringResult string
result := summary.GetStateAttribute(attributeName)
switch t := result.(type) {
case common.Address:
ca := result.(common.Address)
2017-11-30 16:23:01 +00:00
stringResult = fmt.Sprintf("%s: %v", attributeName, ca.Hex())
2017-11-29 15:31:23 +00:00
default:
_ = t
2017-11-30 16:23:01 +00:00
stringResult = fmt.Sprintf("%s: %v", attributeName, result)
2017-11-29 15:31:23 +00:00
}
return stringResult
}