ipld-eth-server/pkg/contract_summary/summary.go
Matt K 605b0a96ae Add graphql server (#27)
* Add graphql server

* Update Makefile

* Update log_filters constraint

* Add GetLogFilter to repo

* Update travis (use Makefile, go fmt, go vet)

* Add logFilter schema and resolvers

* Add GetWatchedEvent to watched_events_repo

* Add watchedEventLog schema and resolvers
2018-02-08 10:12:08 -06:00

55 lines
1.6 KiB
Go

package contract_summary
import (
"math/big"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/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.GetContract(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
}
}