Added sort method to ContractAttributes array
This commit is contained in:
parent
aa3318451b
commit
1bae6db483
@ -5,6 +5,6 @@ type Blockchain interface {
|
||||
SubscribeToBlocks(blocks chan Block)
|
||||
StartListening()
|
||||
StopListening()
|
||||
GetContractAttributes(contractHash string) ([]ContractAttribute, error)
|
||||
GetContractAttributes(contractHash string) (ContractAttributes, error)
|
||||
GetContractStateAttribute(contractHash string, attributeName string) (*string, error)
|
||||
}
|
||||
|
@ -9,3 +9,16 @@ type ContractAttribute struct {
|
||||
Name string
|
||||
Type string
|
||||
}
|
||||
|
||||
type ContractAttributes []ContractAttribute
|
||||
|
||||
func (s ContractAttributes) Len() int {
|
||||
return len(s)
|
||||
}
|
||||
func (s ContractAttributes) Swap(i, j int) {
|
||||
s[i], s[j] = s[j], s[i]
|
||||
}
|
||||
|
||||
func (s ContractAttributes) Less(i, j int) bool {
|
||||
return s[i].Name < s[j].Name
|
||||
}
|
@ -2,6 +2,7 @@ package fakes
|
||||
|
||||
import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Blockchain struct {
|
||||
@ -11,15 +12,16 @@ type Blockchain struct {
|
||||
WasToldToStop bool
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) {
|
||||
var contractAttribute []core.ContractAttribute
|
||||
func (blockchain *Blockchain) GetContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||
var contractAttributes core.ContractAttributes
|
||||
attributes, ok := blockchain.contractAttributes[contractHash]
|
||||
if ok {
|
||||
for key, _ := range attributes {
|
||||
contractAttribute = append(contractAttribute, core.ContractAttribute{Name: key, Type: "string"})
|
||||
contractAttributes = append(contractAttributes, core.ContractAttribute{Name: key, Type: "string"})
|
||||
}
|
||||
}
|
||||
return contractAttribute, nil
|
||||
sort.Sort(contractAttributes)
|
||||
return contractAttributes, nil
|
||||
}
|
||||
|
||||
func (blockchain *Blockchain) GetContractStateAttribute(contractHash string, attributeName string) (*string, error) {
|
||||
|
@ -17,19 +17,17 @@ var (
|
||||
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
||||
)
|
||||
|
||||
func (blockchain *GethBlockchain) GetContractAttributes(contractHash string) ([]core.ContractAttribute, error) {
|
||||
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.ContractAttribute
|
||||
var contractAttributes core.ContractAttributes
|
||||
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
|
||||
})
|
||||
sort.Sort(contractAttributes)
|
||||
return contractAttributes, nil
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ import (
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
)
|
||||
|
||||
func FindAttribute(contractAttributes []core.ContractAttribute, attributeName string) *core.ContractAttribute {
|
||||
func FindAttribute(contractAttributes core.ContractAttributes, attributeName string) *core.ContractAttribute {
|
||||
for _, contractAttribute := range contractAttributes {
|
||||
if contractAttribute.Name == attributeName {
|
||||
return &contractAttribute
|
||||
|
@ -13,7 +13,7 @@ type ContractSummary struct {
|
||||
NumberOfTransactions int
|
||||
LastTransaction *core.Transaction
|
||||
blockChain core.Blockchain
|
||||
Attributes []core.ContractAttribute
|
||||
Attributes core.ContractAttributes
|
||||
}
|
||||
|
||||
var NewContractNotWatchedErr = func(contractHash string) error {
|
||||
|
@ -106,9 +106,9 @@ var _ bool = Describe("The watched contract summary", func() {
|
||||
contractSummary, _ := watched_contracts.NewSummary(blockchain, repository, "0x123")
|
||||
|
||||
Expect(contractSummary.Attributes).To(Equal(
|
||||
[]core.ContractAttribute{
|
||||
{Name: "foo", Type: "string"},
|
||||
core.ContractAttributes{
|
||||
{Name: "baz", Type: "string"},
|
||||
{Name: "foo", Type: "string"},
|
||||
},
|
||||
))
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user