2017-11-13 15:58:36 +00:00
|
|
|
package watched_contracts
|
|
|
|
|
|
|
|
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-30 22:36:18 +00:00
|
|
|
Contract core.Contract
|
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-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{}
|
|
|
|
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName, contractSummary.BlockNumber)
|
2017-11-29 15:31:23 +00:00
|
|
|
return result
|
2017-11-27 15:21:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:33:07 +00:00
|
|
|
func newContractSummary(blockchain core.Blockchain, watchedContract repositories.WatchedContract, blockNumber *big.Int) ContractSummary {
|
2017-11-30 22:15:32 +00:00
|
|
|
contract, _ := blockchain.GetContract(watchedContract.Hash)
|
2017-12-04 16:17:13 +00:00
|
|
|
return ContractSummary{
|
2017-11-27 15:39:53 +00:00
|
|
|
blockChain: blockchain,
|
2017-11-30 22:36:18 +00:00
|
|
|
Contract: contract,
|
2017-11-27 15:39:53 +00:00
|
|
|
ContractHash: watchedContract.Hash,
|
|
|
|
NumberOfTransactions: len(watchedContract.Transactions),
|
|
|
|
LastTransaction: lastTransaction(watchedContract),
|
2017-11-30 22:15:32 +00:00
|
|
|
Attributes: contract.Attributes,
|
2017-12-04 18:54:33 +00:00
|
|
|
BlockNumber: blockNumber,
|
2017-11-14 15:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:33:07 +00:00
|
|
|
func lastTransaction(watchedContract repositories.WatchedContract) *core.Transaction {
|
|
|
|
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
|
|
|
}
|