2017-11-27 15:39:53 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2017-11-27 22:18:42 +00:00
|
|
|
"sort"
|
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
2017-11-27 15:39:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-02-08 16:12:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2017-11-27 15:39:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
|
|
|
)
|
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
func (blockchain *Blockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
2017-12-04 22:54:35 +00:00
|
|
|
parsed, err := ParseAbi(contract.Abi)
|
2017-11-28 23:04:09 +00:00
|
|
|
var result interface{}
|
2017-12-04 18:54:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
input, err := parsed.Pack(attributeName)
|
|
|
|
if err != nil {
|
2017-12-04 19:30:57 +00:00
|
|
|
return nil, ErrInvalidStateAttribute
|
2017-12-04 18:54:33 +00:00
|
|
|
}
|
2017-12-04 22:54:35 +00:00
|
|
|
output, err := callContract(contract.Hash, input, blockchain, blockNumber)
|
2017-12-04 18:54:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = parsed.Unpack(&result, attributeName, output)
|
2017-11-27 15:39:53 +00:00
|
|
|
if err != nil {
|
2017-12-04 19:30:57 +00:00
|
|
|
return nil, err
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
2017-11-29 15:31:23 +00:00
|
|
|
return result, nil
|
2017-11-27 15:39:53 +00:00
|
|
|
}
|
2017-12-04 22:35:41 +00:00
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
func callContract(contractHash string, input []byte, blockchain *Blockchain, blockNumber *big.Int) ([]byte, error) {
|
2017-12-04 22:35:41 +00:00
|
|
|
to := common.HexToAddress(contractHash)
|
2017-12-04 18:54:33 +00:00
|
|
|
msg := ethereum.CallMsg{To: &to, Data: input}
|
|
|
|
return blockchain.client.CallContract(context.Background(), msg, blockNumber)
|
|
|
|
}
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2018-01-05 17:55:00 +00:00
|
|
|
func (blockchain *Blockchain) GetAttributes(contract core.Contract) (core.ContractAttributes, error) {
|
2017-12-04 22:54:35 +00:00
|
|
|
parsed, _ := ParseAbi(contract.Abi)
|
2017-12-04 22:35:41 +00:00
|
|
|
var contractAttributes core.ContractAttributes
|
|
|
|
for _, abiElement := range parsed.Methods {
|
|
|
|
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const {
|
|
|
|
attributeType := abiElement.Outputs[0].Type.String()
|
2018-02-08 16:12:08 +00:00
|
|
|
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: abiElement.Name, Type: attributeType})
|
2017-12-04 22:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Sort(contractAttributes)
|
|
|
|
return contractAttributes, nil
|
|
|
|
}
|