forked from cerc-io/ipld-eth-server
1 parent
7501fe70a7
commit
71de8e970d
8 files changed
+143
-44
No files matched your search
+31
-14
@@ -7,9 +7,13 @@ import (
|
||||
|
||||
"sort"
|
||||
|
||||
"context"
|
||||
"math/big"
|
||||
|
||||
"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"
|
||||
"github.com/ethereum/go-ethereum/accounts/abi"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
@@ -30,18 +34,40 @@ func (blockchain *GethBlockchain) GetContract(contractHash string) (core.Contrac
|
||||
}
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string) (interface{}, error) {
|
||||
boundContract, err := bindContract(common.HexToAddress(contract.Hash), blockchain.client, blockchain.client)
|
||||
func (blockchain *GethBlockchain) getParseAbi(contract core.Contract) (abi.ABI, error) {
|
||||
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contract.Hash))
|
||||
parsed, err := ParseAbiFile(abiFilePath)
|
||||
if err != nil {
|
||||
return abi.ABI{}, err
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attributeName string, blockNumber *big.Int) (interface{}, error) {
|
||||
parsed, err := blockchain.getParseAbi(contract)
|
||||
var result interface{}
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
input, err := parsed.Pack(attributeName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result interface{}
|
||||
err = boundContract.Call(&bind.CallOpts{}, &result, attributeName)
|
||||
output, err := callContract(contract, input, err, blockchain, blockNumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = parsed.Unpack(&result, attributeName, output)
|
||||
if err != nil {
|
||||
return nil, ErrInvalidStateAttribute
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
func callContract(contract core.Contract, input []byte, err error, blockchain *GethBlockchain, blockNumber *big.Int) ([]byte, error) {
|
||||
to := common.HexToAddress(contract.Hash)
|
||||
msg := ethereum.CallMsg{To: &to, Data: input}
|
||||
return blockchain.client.CallContract(context.Background(), msg, blockNumber)
|
||||
}
|
||||
|
||||
func (blockchain *GethBlockchain) getContractAttributes(contractHash string) (core.ContractAttributes, error) {
|
||||
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
|
||||
@@ -56,12 +82,3 @@ func (blockchain *GethBlockchain) getContractAttributes(contractHash string) (co
|
||||
sort.Sort(contractAttributes)
|
||||
return contractAttributes, 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 bind.BoundContract{}, err
|
||||
}
|
||||
return *bind.NewBoundContract(address, parsed, caller, transactor), nil
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package geth_test
|
||||
|
||||
//import (
|
||||
// "math/big"
|
||||
//
|
||||
// cfg "github.com/8thlight/vulcanizedb/pkg/config"
|
||||
// "github.com/8thlight/vulcanizedb/pkg/geth"
|
||||
// "github.com/8thlight/vulcanizedb/pkg/geth/testing"
|
||||
@@ -57,19 +59,43 @@ package geth_test
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "name")
|
||||
// name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// })
|
||||
//
|
||||
// It("returns the correct attribute for a real contract", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
//
|
||||
// Expect(err).To(BeNil())
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// })
|
||||
//
|
||||
// It("returns the correct attribute for a real contract at a specific block height", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "name", big.NewInt(4652791))
|
||||
//
|
||||
// Expect(name).To(Equal("OMGToken"))
|
||||
// Expect(err).To(BeNil())
|
||||
// })
|
||||
//
|
||||
// It("returns an error when there is no ABI for the given contract", func() {
|
||||
// config, _ := cfg.NewConfig("public")
|
||||
// blockchain := geth.NewGethBlockchain(config.Client.IPCPath)
|
||||
// contractHash := "MISSINGHASH"
|
||||
//
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "name")
|
||||
// name, err := blockchain.GetAttribute(contract, "name", nil)
|
||||
//
|
||||
// Expect(err).To(Equal(geth.ErrMissingAbiFile))
|
||||
// Expect(name).To(BeNil())
|
||||
@@ -81,7 +107,7 @@ package geth_test
|
||||
// contractHash := "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"
|
||||
//
|
||||
// contract, _ := blockchain.GetContract(contractHash)
|
||||
// name, err := blockchain.GetAttribute(contract, "missing_attribute")
|
||||
// name, err := blockchain.GetAttribute(contract, "missing_attribute", nil)
|
||||
//
|
||||
// Expect(err).To(Equal(geth.ErrInvalidStateAttribute))
|
||||
// Expect(name).To(BeNil())
|
||||
|
||||
Reference in new issue
Block a user