ipld-eth-server/pkg/watched_contracts/summary.go

62 lines
2.0 KiB
Go
Raw Normal View History

package watched_contracts
import (
"errors"
"fmt"
"math/big"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
type ContractSummary struct {
Contract core.Contract
ContractHash string
NumberOfTransactions int
LastTransaction *core.Transaction
blockChain core.Blockchain
Attributes core.ContractAttributes
BlockNumber *big.Int
}
var NewContractNotWatchedErr = func(contractHash string) error {
return errors.New(fmt.Sprintf("Contract %v not being watched", contractHash))
}
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
watchedContract := repository.FindWatchedContract(contractHash)
if watchedContract != nil {
return newContractSummary(blockchain, *watchedContract, blockNumber), nil
} else {
return ContractSummary{}, NewContractNotWatchedErr(contractHash)
}
}
2017-11-29 15:31:23 +00:00
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
var result interface{}
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName, contractSummary.BlockNumber)
2017-11-29 15:31:23 +00:00
return result
}
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)
return ContractSummary{
blockChain: blockchain,
Contract: contract,
ContractHash: watchedContract.Hash,
NumberOfTransactions: len(watchedContract.Transactions),
LastTransaction: lastTransaction(watchedContract),
2017-11-30 22:15:32 +00:00
Attributes: contract.Attributes,
BlockNumber: blockNumber,
}
}
2017-12-04 19:33:07 +00:00
func lastTransaction(watchedContract repositories.WatchedContract) *core.Transaction {
if len(watchedContract.Transactions) > 0 {
return &watchedContract.Transactions[0]
} else {
return nil
}
}