2017-12-04 21:36:11 +00:00
|
|
|
package contract_summary
|
2017-11-13 15:58:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
"math/big"
|
|
|
|
|
2017-11-13 15:58:36 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ContractSummary struct {
|
2017-11-14 15:14:34 +00:00
|
|
|
ContractHash string
|
|
|
|
NumberOfTransactions int
|
|
|
|
LastTransaction *core.Transaction
|
2017-11-27 15:39:53 +00:00
|
|
|
blockChain core.Blockchain
|
2017-11-28 20:03:21 +00:00
|
|
|
Attributes core.ContractAttributes
|
2017-12-04 18:54:33 +00:00
|
|
|
BlockNumber *big.Int
|
2017-12-04 22:35:41 +00:00
|
|
|
WatchedContract core.WatchedContract
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var NewContractNotWatchedErr = func(contractHash string) error {
|
|
|
|
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
|
|
|
|
}
|
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
|
2017-11-27 15:39:53 +00:00
|
|
|
watchedContract := repository.FindWatchedContract(contractHash)
|
|
|
|
if watchedContract != nil {
|
2017-12-04 18:54:33 +00:00
|
|
|
return newContractSummary(blockchain, *watchedContract, blockNumber), nil
|
2017-11-13 15:58:36 +00:00
|
|
|
} else {
|
2017-12-04 16:17:13 +00:00
|
|
|
return ContractSummary{}, NewContractNotWatchedErr(contractHash)
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 15:31:23 +00:00
|
|
|
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
|
2017-12-04 18:54:33 +00:00
|
|
|
var result interface{}
|
2017-12-04 22:35:41 +00:00
|
|
|
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.WatchedContract, attributeName, contractSummary.BlockNumber)
|
2017-11-29 15:31:23 +00:00
|
|
|
return result
|
2017-11-27 15:21:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 21:31:39 +00:00
|
|
|
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary {
|
2017-12-04 22:35:41 +00:00
|
|
|
attributes, _ := blockchain.GetAttributes(watchedContract)
|
2017-12-04 16:17:13 +00:00
|
|
|
return ContractSummary{
|
2017-11-27 15:39:53 +00:00
|
|
|
blockChain: blockchain,
|
|
|
|
ContractHash: watchedContract.Hash,
|
|
|
|
NumberOfTransactions: len(watchedContract.Transactions),
|
|
|
|
LastTransaction: lastTransaction(watchedContract),
|
2017-12-04 22:35:41 +00:00
|
|
|
Attributes: attributes,
|
2017-12-04 18:54:33 +00:00
|
|
|
BlockNumber: blockNumber,
|
2017-12-04 22:35:41 +00:00
|
|
|
WatchedContract: watchedContract,
|
2017-11-14 15:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:31:39 +00:00
|
|
|
func lastTransaction(watchedContract core.WatchedContract) *core.Transaction {
|
2017-12-04 19:33:07 +00:00
|
|
|
if len(watchedContract.Transactions) > 0 {
|
|
|
|
return &watchedContract.Transactions[0]
|
2017-11-14 15:14:34 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|