From b26bcf74e99d73948ff91b4bdfefaa00aebe16c6 Mon Sep 17 00:00:00 2001 From: Matt Krump Date: Tue, 28 Nov 2017 17:04:09 -0600 Subject: [PATCH] First pass at adding non-string attributes --- pkg/geth/contract.go | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/geth/contract.go b/pkg/geth/contract.go index de65de3e..25fd26ac 100644 --- a/pkg/geth/contract.go +++ b/pkg/geth/contract.go @@ -22,7 +22,7 @@ func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (co parsed, _ := ParseAbiFile(abiFilePath) var contractAttributes core.ContractAttributes for _, abiElement := range parsed.Methods { - if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const && abiElement.Outputs[0].Type.String() == "string" { + if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const { attributeType := abiElement.Outputs[0].Type.String() contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType}) } @@ -31,17 +31,32 @@ 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) { boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client) if err != nil { return nil, err } - result := new(string) - err = boundContract.Call(&bind.CallOpts{}, result, attributeName) + var result interface{} + err = boundContract.Call(&bind.CallOpts{}, &result, attributeName) if err != nil { return nil, ErrInvalidStateAttribute } - return result, nil + return convertresult(result, attributeName), nil } func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {