diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 0036036d..162a1f7a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -6,5 +6,5 @@ type Blockchain interface { StartListening() StopListening() GetContractAttributes(contractHash string) (ContractAttributes, error) - GetContractStateAttribute(contractHash string, attributeName string) (*string, error) + GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) } diff --git a/pkg/fakes/blockchain.go b/pkg/fakes/blockchain.go index 966f5112..6cea88f6 100644 --- a/pkg/fakes/blockchain.go +++ b/pkg/fakes/blockchain.go @@ -1,8 +1,9 @@ package fakes import ( - "github.com/8thlight/vulcanizedb/pkg/core" "sort" + + "github.com/8thlight/vulcanizedb/pkg/core" ) type Blockchain struct { @@ -24,9 +25,8 @@ func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.C return contractAttributes, nil } -func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) { - result := new(string) - *result = blockchain.contractAttributes[contractHash][attributeName] +func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) { + result := blockchain.contractAttributes[contractHash][attributeName] return result, nil } diff --git a/pkg/geth/contract.go b/pkg/geth/contract.go index 25fd26ac..56eba35a 100644 --- a/pkg/geth/contract.go +++ b/pkg/geth/contract.go @@ -31,22 +31,7 @@ func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (co return contractAttributes, nil } -func convertresult(result interface{}, attributeName string) *string { - var stringResult = new(string) - fmt.Println(fmt.Sprintf("%s: %v (%T)", attributeName, result, result)) - switch t := result.(type) { - case common.Address: - ca := result.(common.Address) - *stringResult = fmt.Sprintf("%v", ca.Hex()) - default: - _ = t - *stringResult = fmt.Sprintf("%v", result) - } - return stringResult - -} - -func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) { +func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) { boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client) if err != nil { return nil, err @@ -56,7 +41,7 @@ func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, if err != nil { return nil, ErrInvalidStateAttribute } - return convertresult(result, attributeName), nil + return result, nil } func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) { diff --git a/pkg/geth/contract_test.go b/pkg/geth/contract_test.go index acd2792f..e55c8a1d 100644 --- a/pkg/geth/contract_test.go +++ b/pkg/geth/contract_test.go @@ -48,18 +48,6 @@ package geth_test // attribute := testing.FindAttribute(attributes, "unpause") // Expect(attribute).To(BeNil()) // }) -// -// It("temporarily filters out non-string attributes", func() { -// config, _ := cfg.NewConfig("public") -// blockchain := geth.NewGethBlockchain(config.Client.IPCPath) -// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07" -// -// attributes, err := blockchain.GetContractAttributes(contractHash) -// -// Expect(err).To(BeNil()) -// attribute := testing.FindAttribute(attributes, "decimals") -// Expect(attribute).To(BeNil()) -// }) // }) // // Describe("Getting a contract attribute", func() { @@ -70,7 +58,7 @@ package geth_test // // name, err := blockchain.GetContractStateAttribute(contractHash, "name") // -// Expect(*name).To(Equal("OMGToken")) +// Expect(name).To(Equal("OMGToken")) // Expect(err).To(BeNil()) // }) // diff --git a/pkg/watched_contracts/console_presenter.go b/pkg/watched_contracts/console_presenter.go index 6563d080..37f06f51 100644 --- a/pkg/watched_contracts/console_presenter.go +++ b/pkg/watched_contracts/console_presenter.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/8thlight/vulcanizedb/pkg/core" + "github.com/ethereum/go-ethereum/common" ) func GenerateConsoleOutput(summary *ContractSummary) string { @@ -45,6 +46,16 @@ func attributesString(summary *ContractSummary) string { } func formatAttribute(attributeName string, summary *ContractSummary) string { - formattedAttribute := fmt.Sprintf("%s: %s", attributeName, summary.GetStateAttribute(attributeName)) - return formattedAttribute + var stringResult string + result := summary.GetStateAttribute(attributeName) + fmt.Println(fmt.Sprintf("%s: %v (%T)", attributeName, result, result)) + switch t := result.(type) { + case common.Address: + ca := result.(common.Address) + stringResult = fmt.Sprintf("%v", ca.Hex()) + default: + _ = t + stringResult = fmt.Sprintf("%v", result) + } + return stringResult } diff --git a/pkg/watched_contracts/summary.go b/pkg/watched_contracts/summary.go index 3e9183a6..8007f514 100644 --- a/pkg/watched_contracts/summary.go +++ b/pkg/watched_contracts/summary.go @@ -29,9 +29,9 @@ func NewSummary(blockchain core.Blockchain, repository repositories.Repository, } } -func (contractSummary ContractSummary) GetStateAttribute(attributeName string) string { +func (contractSummary ContractSummary) GetStateAttribute(attributeName string) interface{} { result, _ := contractSummary.blockChain.GetContractStateAttribute(contractSummary.ContractHash, attributeName) - return *result + return result } func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary { diff --git a/pkg/watched_contracts/summary_test.go b/pkg/watched_contracts/summary_test.go index 6c4886e5..3a13e8f6 100644 --- a/pkg/watched_contracts/summary_test.go +++ b/pkg/watched_contracts/summary_test.go @@ -91,8 +91,9 @@ var _ bool = Describe("The watched contract summary", func() { blockchain.SetContractStateAttribute("0x123", "foo", "bar") contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123") + attribute := contractSummary.GetStateAttribute("foo") - Expect(contractSummary.GetStateAttribute("foo")).To(Equal("bar")) + Expect(attribute).To(Equal("bar")) }) It("gets attributes for the contract from the blockchain", func() {