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

62 lines
1.9 KiB
Go
Raw Normal View History

package contract_summary
import (
"errors"
"fmt"
"math/big"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
type ContractSummary struct {
ContractHash string
NumberOfTransactions int
LastTransaction *core.Transaction
blockChain core.Blockchain
Attributes core.ContractAttributes
BlockNumber *big.Int
WatchedContract core.WatchedContract
}
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.WatchedContract, attributeName, contractSummary.BlockNumber)
2017-11-29 15:31:23 +00:00
return result
}
2017-12-04 21:31:39 +00:00
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary {
attributes, _ := blockchain.GetAttributes(watchedContract)
return ContractSummary{
blockChain: blockchain,
ContractHash: watchedContract.Hash,
NumberOfTransactions: len(watchedContract.Transactions),
LastTransaction: lastTransaction(watchedContract),
Attributes: attributes,
BlockNumber: blockNumber,
WatchedContract: watchedContract,
}
}
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]
} else {
return nil
}
}