Extract Attributes to contract struct
This commit is contained in:
parent
687af1f3d4
commit
5c18639ef4
@ -1,10 +1,14 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
|
type Contract struct {
|
||||||
|
Attributes ContractAttributes
|
||||||
|
}
|
||||||
|
|
||||||
type Blockchain interface {
|
type Blockchain interface {
|
||||||
GetBlockByNumber(blockNumber int64) Block
|
GetBlockByNumber(blockNumber int64) Block
|
||||||
SubscribeToBlocks(blocks chan Block)
|
SubscribeToBlocks(blocks chan Block)
|
||||||
StartListening()
|
StartListening()
|
||||||
StopListening()
|
StopListening()
|
||||||
GetContractAttributes(contractHash string) (ContractAttributes, error)
|
GetContract(contractHash string) (Contract, error)
|
||||||
GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error)
|
GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error)
|
||||||
}
|
}
|
||||||
|
@ -13,16 +13,9 @@ type Blockchain struct {
|
|||||||
WasToldToStop bool
|
WasToldToStop bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
func (blockchain *Blockchain) GetContract(contractHash string) (core.Contract, error) {
|
||||||
var contractAttributes core.ContractAttributes
|
contractAttributes, err := blockchain.getContractAttributes(contractHash)
|
||||||
attributes, ok := blockchain.contractAttributes[contractHash]
|
return core.Contract{Attributes: contractAttributes}, err
|
||||||
if ok {
|
|
||||||
for key, _ := range attributes {
|
|
||||||
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Sort(contractAttributes)
|
|
||||||
return contractAttributes, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
|
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
|
||||||
@ -73,3 +66,15 @@ func (blockchain *Blockchain) SetContractStateAttribute(contractHash string, att
|
|||||||
}
|
}
|
||||||
blockchain.contractAttributes[contractHash][attributeName] = attributeValue
|
blockchain.contractAttributes[contractHash][attributeName] = attributeValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (blockchain *Blockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||||
|
var contractAttributes core.ContractAttributes
|
||||||
|
attributes, ok := blockchain.contractAttributes[contractHash]
|
||||||
|
if ok {
|
||||||
|
for key, _ := range attributes {
|
||||||
|
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(contractAttributes)
|
||||||
|
return contractAttributes, nil
|
||||||
|
}
|
||||||
|
@ -17,18 +17,13 @@ var (
|
|||||||
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
||||||
)
|
)
|
||||||
|
|
||||||
func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contract, error) {
|
||||||
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
|
attributes, err := blockchain.getContractAttributes(contractHash)
|
||||||
parsed, _ := ParseAbiFile(abiFilePath)
|
if err != nil {
|
||||||
var contractAttributes core.ContractAttributes
|
return core.Contract{}, err
|
||||||
for _, abiElement := range parsed.Methods {
|
} else {
|
||||||
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const {
|
return core.Contract{Attributes: attributes}, nil
|
||||||
attributeType := abiElement.Outputs[0].Type.String()
|
|
||||||
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
sort.Sort(contractAttributes)
|
|
||||||
return contractAttributes, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
|
func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string, attributeName string) (interface{}, error) {
|
||||||
@ -44,6 +39,20 @@ func (blockchain *GethBlockchain) GetContractStateAttribute(contractHash string,
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (blockchain *GethBlockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||||
|
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
|
||||||
|
parsed, _ := ParseAbiFile(abiFilePath)
|
||||||
|
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()
|
||||||
|
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(contractAttributes)
|
||||||
|
return contractAttributes, 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) {
|
||||||
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex()))
|
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", address.Hex()))
|
||||||
parsed, err := ParseAbiFile(abiFilePath)
|
parsed, err := ParseAbiFile(abiFilePath)
|
||||||
|
@ -1,88 +1,88 @@
|
|||||||
package geth_test
|
package geth_test
|
||||||
|
|
||||||
//import (
|
import (
|
||||||
// cfg "github.com/8thlight/vulcanizedb/pkg/config"
|
cfg "github.com/8thlight/vulcanizedb/pkg/config"
|
||||||
// "github.com/8thlight/vulcanizedb/pkg/geth"
|
"github.com/8thlight/vulcanizedb/pkg/geth"
|
||||||
// "github.com/8thlight/vulcanizedb/pkg/geth/testing"
|
"github.com/8thlight/vulcanizedb/pkg/geth/testing"
|
||||||
// . "github.com/onsi/ginkgo"
|
. "github.com/onsi/ginkgo"
|
||||||
// . "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
//)
|
)
|
||||||
//
|
|
||||||
//var _ = Describe("Reading contracts", func() {
|
var _ = Describe("Reading contracts", func() {
|
||||||
//
|
|
||||||
// Describe("Reading the list of attributes", func() {
|
Describe("Reading the list of attributes", func() {
|
||||||
// It("returns a string attribute for a real contract", func() {
|
It("returns a string attribute for a real contract", func() {
|
||||||
// config, _ := cfg.NewConfig("public")
|
config, _ := cfg.NewConfig("public")
|
||||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||||
//
|
|
||||||
// attributes, err := blockchain.GetContractAttributes(contractHash)
|
contract, err := blockchain.GetContract(contractHash)
|
||||||
//
|
|
||||||
// Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
// Expect(len(attributes)).NotTo(Equal(0))
|
Expect(len(contract.Attributes)).NotTo(Equal(0))
|
||||||
// symbolAttribute := *testing.FindAttribute(attributes, "symbol")
|
symbolAttribute := *testing.FindAttribute(contract.Attributes, "symbol")
|
||||||
// Expect(symbolAttribute.Name).To(Equal("symbol"))
|
Expect(symbolAttribute.Name).To(Equal("symbol"))
|
||||||
// Expect(symbolAttribute.Type).To(Equal("string"))
|
Expect(symbolAttribute.Type).To(Equal("string"))
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// It("does not return an attribute that takes an input", func() {
|
It("does not return an attribute that takes an input", func() {
|
||||||
// config, _ := cfg.NewConfig("public")
|
config, _ := cfg.NewConfig("public")
|
||||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||||
//
|
|
||||||
// attributes, err := blockchain.GetContractAttributes(contractHash)
|
contract, err := blockchain.GetContract(contractHash)
|
||||||
//
|
|
||||||
// Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
// attribute := testing.FindAttribute(attributes, "balanceOf")
|
attribute := testing.FindAttribute(contract.Attributes, "balanceOf")
|
||||||
// Expect(attribute).To(BeNil())
|
Expect(attribute).To(BeNil())
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// It("does not return an attribute that is not constant", func() {
|
It("does not return an attribute that is not constant", func() {
|
||||||
// config, _ := cfg.NewConfig("public")
|
config, _ := cfg.NewConfig("public")
|
||||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||||
//
|
|
||||||
// attributes, err := blockchain.GetContractAttributes(contractHash)
|
contract, err := blockchain.GetContract(contractHash)
|
||||||
//
|
|
||||||
// Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
// attribute := testing.FindAttribute(attributes, "unpause")
|
attribute := testing.FindAttribute(contract.Attributes, "unpause")
|
||||||
// Expect(attribute).To(BeNil())
|
Expect(attribute).To(BeNil())
|
||||||
// })
|
})
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// Describe("Getting a contract attribute", func() {
|
Describe("Getting a contract attribute", func() {
|
||||||
// It("returns the correct attribute for a real contract", func() {
|
It("returns the correct attribute for a real contract", func() {
|
||||||
// config, _ := cfg.NewConfig("public")
|
config, _ := cfg.NewConfig("public")
|
||||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||||
//
|
|
||||||
// name, err := blockchain.GetContractStateAttribute(contractHash, "name")
|
name, err := blockchain.GetContractStateAttribute(contractHash, "name")
|
||||||
//
|
|
||||||
// Expect(name).To(Equal("OMGToken"))
|
Expect(name).To(Equal("OMGToken"))
|
||||||
// Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// It("returns an error when there is no ABI for the given contract", func() {
|
It("returns an error when there is no ABI for the given contract", func() {
|
||||||
// config, _ := cfg.NewConfig("public")
|
config, _ := cfg.NewConfig("public")
|
||||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||||
// contractHash := "MISSINGHASH"
|
contractHash := "MISSINGHASH"
|
||||||
//
|
|
||||||
// name, err := blockchain.GetContractStateAttribute(contractHash, "name")
|
name, err := blockchain.GetContractStateAttribute(contractHash, "name")
|
||||||
//
|
|
||||||
// Expect(name).To(BeNil())
|
Expect(name).To(BeNil())
|
||||||
// Expect(err).To(Equal(geth.ErrMissingAbiFile))
|
Expect(err).To(Equal(geth.ErrMissingAbiFile))
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
// It("returns an error when asking for an attribute that does not exist", func() {
|
It("returns an error when asking for an attribute that does not exist", func() {
|
||||||
// config, _ := cfg.NewConfig("public")
|
config, _ := cfg.NewConfig("public")
|
||||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||||
//
|
|
||||||
// name, err := blockchain.GetContractStateAttribute(contractHash, "missing_attribute")
|
name, err := blockchain.GetContractStateAttribute(contractHash, "missing_attribute")
|
||||||
//
|
|
||||||
// Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
|
Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
|
||||||
// Expect(name).To(BeNil())
|
Expect(name).To(BeNil())
|
||||||
// })
|
})
|
||||||
// })
|
})
|
||||||
//
|
|
||||||
//})
|
})
|
||||||
|
@ -35,13 +35,13 @@ func (contractSummary ContractSummary) GetStateAttribute(attributeName string) i
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
|
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract) *ContractSummary {
|
||||||
attributes, _ := blockchain.GetContractAttributes(watchedContract.Hash)
|
contract, _ := blockchain.GetContract(watchedContract.Hash)
|
||||||
return &ContractSummary{
|
return &ContractSummary{
|
||||||
blockChain: blockchain,
|
blockChain: blockchain,
|
||||||
ContractHash: watchedContract.Hash,
|
ContractHash: watchedContract.Hash,
|
||||||
NumberOfTransactions: len(watchedContract.Transactions),
|
NumberOfTransactions: len(watchedContract.Transactions),
|
||||||
LastTransaction: lastTransaction(watchedContract),
|
LastTransaction: lastTransaction(watchedContract),
|
||||||
Attributes: attributes,
|
Attributes: contract.Attributes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user