2017-12-04 21:36:11 +00:00
|
|
|
package contract_summary
|
2017-11-13 15:58:36 +00:00
|
|
|
|
|
|
|
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-12-04 22:54:35 +00:00
|
|
|
Attributes core.ContractAttributes
|
|
|
|
BlockNumber *big.Int
|
|
|
|
Contract core.Contract
|
2017-11-14 15:14:34 +00:00
|
|
|
ContractHash string
|
|
|
|
LastTransaction *core.Transaction
|
2017-12-04 22:54:35 +00:00
|
|
|
NumberOfTransactions int
|
2017-11-27 15:39:53 +00:00
|
|
|
blockChain core.Blockchain
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
var ErrContractDoesNotExist = func(contractHash string) error {
|
|
|
|
return errors.New(fmt.Sprintf("Contract %v does not exist", contractHash))
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
func NewSummary(blockchain core.Blockchain, repository repositories.Repository, contractHash string, blockNumber *big.Int) (ContractSummary, error) {
|
2017-12-04 22:54:35 +00:00
|
|
|
contract := repository.FindContract(contractHash)
|
|
|
|
if contract != nil {
|
|
|
|
return newContractSummary(blockchain, *contract, blockNumber), nil
|
2017-11-13 15:58:36 +00:00
|
|
|
} else {
|
2017-12-04 22:54:35 +00:00
|
|
|
return ContractSummary{}, ErrContractDoesNotExist(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{}
|
2017-12-04 22:54:35 +00:00
|
|
|
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 22:54:35 +00:00
|
|
|
func newContractSummary(blockchain core.Blockchain, contract core.Contract, blockNumber *big.Int) ContractSummary {
|
|
|
|
attributes, _ := blockchain.GetAttributes(contract)
|
2017-12-04 16:17:13 +00:00
|
|
|
return ContractSummary{
|
2017-12-04 22:35:41 +00:00
|
|
|
Attributes: attributes,
|
2017-12-04 18:54:33 +00:00
|
|
|
BlockNumber: blockNumber,
|
2017-12-04 22:54:35 +00:00
|
|
|
Contract: contract,
|
|
|
|
ContractHash: contract.Hash,
|
|
|
|
LastTransaction: lastTransaction(contract),
|
|
|
|
NumberOfTransactions: len(contract.Transactions),
|
|
|
|
blockChain: blockchain,
|
2017-11-14 15:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-04 22:54:35 +00:00
|
|
|
func lastTransaction(contract core.Contract) *core.Transaction {
|
|
|
|
if len(contract.Transactions) > 0 {
|
|
|
|
return &contract.Transactions[0]
|
2017-11-14 15:14:34 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2017-11-13 15:58:36 +00:00
|
|
|
}
|