57 lines
2.0 KiB
Go
57 lines
2.0 KiB
Go
package geth
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"github.com/8thlight/vulcanizedb/pkg/config"
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
"github.com/ethereum/go-ethereum/common"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
|
)
|
|
|
|
func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) {
|
|
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
|
|
parsed, _ := ParseAbiFile(abiFilePath)
|
|
var contractAttributes []core.ContractAttribute
|
|
for _, abiElement := range parsed.Methods {
|
|
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const && abiElement.Outputs[0].Type.String() == "string" {
|
|
attributeType := abiElement.Outputs[0].Type.String()
|
|
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
|
|
}
|
|
}
|
|
sort.Slice(contractAttributes, func(i, j int) bool {
|
|
return contractAttributes[i].Name < contractAttributes[j].Name
|
|
})
|
|
return contractAttributes, nil
|
|
}
|
|
|
|
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)
|
|
if err != nil {
|
|
return nil, ErrInvalidStateAttribute
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func bindContract(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor) (*bind.BoundContract, error) {
|
|
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex()))
|
|
parsed, err := ParseAbiFile(abiFilePath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return bind.NewBoundContract(address, parsed, caller, transactor), nil
|
|
}
|