2017-11-13 15:58:36 +00:00
|
|
|
package watched_contracts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"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-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-11-27 15:39:53 +00:00
|
|
|
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string) (*ContractSummary, error) {
|
|
|
|
watchedContract := repository.FindWatchedContract(contractHash)
|
|
|
|
if watchedContract != nil {
|
|
|
|
return newContractSummary(blockchain, *watchedContract), nil
|
2017-11-13 15:58:36 +00:00
|
|
|
} else {
|
|
|
|
return nil, NewContractNotWatchedErr(contractHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 15:31:23 +00:00
|
|
|
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
|
2017-11-27 15:39:53 +00:00
|
|
|
result, _ := contractSummary.blockChain.GetContractStateAttribute(contractSummary.ContractHash, attributeName)
|
2017-11-29 15:31:23 +00:00
|
|
|
return result
|
2017-11-27 15:21:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 15:39:53 +00:00
|
|
|
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
|
2017-11-30 22:15:32 +00:00
|
|
|
contract, _ := blockchain.GetContract(watchedContract.Hash)
|
2017-11-14 15:14:34 +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-11-30 22:15:32 +00:00
|
|
|
Attributes: contract.Attributes,
|
2017-11-14 15:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lastTransaction(contract core.WatchedContract) *core.Transaction {
|
|
|
|
if len(contract.Transactions) > 0 {
|
|
|
|
return &contract.Transactions[0]
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|