Start reading available attributes from ABI
This commit is contained in:
parent
708ad114ac
commit
60bef69113
@ -5,5 +5,6 @@ type Blockchain interface {
|
||||
SubscribeToBlocks(blocks chan Block)
|
||||
StartListening()
|
||||
StopListening()
|
||||
GetContractAttributes(contractHash string) ([]ContractAttribute, error)
|
||||
GetContractStateAttribute(contractHash string, attributeName string) (*string, error)
|
||||
}
|
||||
|
@ -4,3 +4,8 @@ type WatchedContract struct {
|
||||
Hash string
|
||||
Transactions []Transaction
|
||||
}
|
||||
|
||||
type ContractAttribute struct {
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
@ -9,6 +9,10 @@ type Blockchain struct {
|
||||
WasToldToStop bool
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) {
|
||||
panic("implement me")
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) {
|
||||
result := new(string)
|
||||
*result = blockchain.contractAttributes[contractHash][attributeName]
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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
pkg/geth/testing/contract_attributes.go
Normal file
14
pkg/geth/testing/contract_attributes.go
Normal 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
pkg/geth/testing/valid_abi.json
Normal file
1
pkg/geth/testing/valid_abi.json
Normal 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"}]
|
@ -9,19 +9,19 @@ import (
|
||||
func GenerateConsoleOutput(summary *ContractSummary) string {
|
||||
return fmt.Sprintf(template(),
|
||||
summary.ContractHash,
|
||||
summary.GetStateAttribute("name"),
|
||||
summary.NumberOfTransactions,
|
||||
transactionToString(summary.LastTransaction),
|
||||
attributesString(summary),
|
||||
)
|
||||
}
|
||||
|
||||
func template() string {
|
||||
return `********************Contract Summary***********************
|
||||
HASH: %v
|
||||
NAME: %s
|
||||
NUMBER OF TRANSACTIONS: %d
|
||||
LAST TRANSACTION:
|
||||
%s
|
||||
ATTRIBUTES:
|
||||
%s
|
||||
`
|
||||
}
|
||||
|
||||
@ -34,3 +34,17 @@ func transactionToString(transaction *core.Transaction) string {
|
||||
From: %s`, transaction.Hash, transaction.To, transaction.From)
|
||||
}
|
||||
}
|
||||
|
||||
func attributesString(summary *ContractSummary) string {
|
||||
attributes := []string{"name", "symbol"}
|
||||
var formattedAttributes string
|
||||
for _, attribute := range attributes {
|
||||
formattedAttributes += formatAttribute(attribute, summary) + "\n" + " "
|
||||
}
|
||||
return formattedAttributes
|
||||
}
|
||||
|
||||
func formatAttribute(attributeName string, summary *ContractSummary) string {
|
||||
formattedAttribute := fmt.Sprintf("%s: %s", attributeName, summary.GetStateAttribute(attributeName))
|
||||
return formattedAttribute
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user