Start reading available attributes from ABI

This commit is contained in:
Eric Meyer
2017-11-28 09:41:23 -06:00
parent 708ad114ac
commit 60bef69113
9 changed files with 117 additions and 7 deletions
+2 -3
View File
@@ -13,8 +13,7 @@ import (
var _ = Describe("Reading ABI files", func() {
It("loads a valid ABI file", func() {
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
path := filepath.Join(cfg.ProjectRoot(), "contracts", "public", contractHash+".json")
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
@@ -23,7 +22,7 @@ var _ = Describe("Reading ABI files", func() {
})
It("returns an error when the file does not exist", func() {
path := filepath.Join(cfg.ProjectRoot(), "contracts", "public", "missing_file.json")
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
+19
View File
@@ -5,7 +5,10 @@ import (
"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"
)
@@ -14,6 +17,22 @@ 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 {
+54 -1
View File
@@ -3,11 +3,64 @@ package geth_test
//import (
// cfg "github.com/8thlight/vulcanizedb/pkg/config"
// "github.com/8thlight/vulcanizedb/pkg/geth"
// "github.com/8thlight/vulcanizedb/pkg/geth/testing"
// . "github.com/onsi/ginkgo"
// . "github.com/onsi/gomega"
//)
//
//var _ = Describe("The Geth blockchain", func() {
//var _ = Describe("Reading contracts", func() {
//
// Describe("Reading the list of attributes", func() {
// It("returns a string attribute for a real contract", func() {
// config, _ := cfg.NewConfig("public")
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
//
// attributes, err := blockchain.GetContractAttributes(contractHash)
//
// Expect(err).To(BeNil())
// Expect(len(attributes)).NotTo(Equal(0))
// symbolAttribute := *testing.FindAttribute(attributes, "symbol")
// Expect(symbolAttribute.Name).To(Equal("symbol"))
// Expect(symbolAttribute.Type).To(Equal("string"))
// })
//
// It("does not return an attribute that takes an input", 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, "balanceOf")
// Expect(attribute).To(BeNil())
// })
//
// It("does not return an attribute that is not constant", 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, "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() {
// It("returns the correct attribute for a real contract", func() {
+14
View File
@@ -0,0 +1,14 @@
package testing
import (
"github.com/8thlight/vulcanizedb/pkg/core"
)
func FindAttribute(contractAttributes []core.ContractAttribute, attributeName string) *core.ContractAttribute {
for _, contractAttribute := range contractAttributes {
if contractAttribute.Name == attributeName {
return &contractAttribute
}
}
return nil
}
+1
View File
@@ -0,0 +1 @@
[{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]