First pass at adding non-string attributes

This commit is contained in:
Matt Krump 2017-11-28 17:04:09 -06:00
parent 72b656fc15
commit b26bcf74e9

View File

@ -22,7 +22,7 @@ func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (co
parsed, _ := ParseAbiFile(abiFilePath) parsed, _ := ParseAbiFile(abiFilePath)
var contractAttributes core.ContractAttributes var contractAttributes core.ContractAttributes
for _, abiElement := range parsed.Methods { 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() attributeType := abiElement.Outputs[0].Type.String()
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType}) contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
} }
@ -31,17 +31,32 @@ func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (co
return contractAttributes, nil 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) (*string, error) {
boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client) boundContract, err := bindContract(common.HexToAddress(contractHash), blockchain.client, blockchain.client)
if err != nil { if err != nil {
return nil, err return nil, err
} }
result := new(string) var result interface{}
err = boundContract.Call(&bind.CallOpts{}, result, attributeName) err = boundContract.Call(&bind.CallOpts{}, &result, attributeName)
if err != nil { if err != nil {
return nil, ErrInvalidStateAttribute 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) { func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {