ipld-eth-server/pkg/contract_summary/summary.go
Matt K 0e837e2d03 Refactoring (#101)
* Make naming consistent for watched_contracts

* Update FindContract and FindBlockByNumber to return errors rather than nil
2017-12-13 10:51:11 -06:00

55 lines
1.6 KiB
Go

package contract_summary
import (
"math/big"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/repositories"
)
type ContractSummary struct {
Attributes core.ContractAttributes
BlockNumber *big.Int
Contract core.Contract
ContractHash string
LastTransaction *core.Transaction
NumberOfTransactions int
blockChain core.Blockchain
}
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
contract, err := repository.FindContract(contractHash)
if err != nil {
return ContractSummary{}, err
} else {
return newContractSummary(blockchain, contract, blockNumber), nil
}
}
func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} {
var result interface{}
result, _ = contractSummary.blockChain.GetAttribute(contractSummary.Contract, attributeName, contractSummary.BlockNumber)
return result
}
func newContractSummary(blockchain core.Blockchain, contract core.Contract, blockNumber *big.Int) ContractSummary {
attributes, _ := blockchain.GetAttributes(contract)
return ContractSummary{
Attributes: attributes,
BlockNumber: blockNumber,
Contract: contract,
ContractHash: contract.Hash,
LastTransaction: lastTransaction(contract),
NumberOfTransactions: len(contract.Transactions),
blockChain: blockchain,
}
}
func lastTransaction(contract core.Contract) *core.Transaction {
if len(contract.Transactions) > 0 {
return &contract.Transactions[0]
} else {
return nil
}
}