2017-11-27 15:39:53 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2017-12-04 18:54:33 +00:00
|
|
|
"context"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum"
|
2017-11-27 15:39:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidStateAttribute = errors.New("invalid state attribute")
|
|
|
|
)
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) FetchContractData(abiJSON string, address string, method string, methodArg interface{}, result interface{}, blockNumber int64) error {
|
2018-02-13 16:31:57 +00:00
|
|
|
parsed, err := ParseAbi(abiJSON)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-12 19:09:54 +00:00
|
|
|
var input []byte
|
|
|
|
if methodArg != nil {
|
|
|
|
input, err = parsed.Pack(method, methodArg)
|
|
|
|
} else {
|
|
|
|
input, err = parsed.Pack(method)
|
|
|
|
}
|
2018-02-13 16:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-17 21:23:07 +00:00
|
|
|
output, err := blockChain.callContract(address, input, big.NewInt(blockNumber))
|
2018-02-13 16:31:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return parsed.Unpack(result, method, output)
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:23:07 +00:00
|
|
|
func (blockChain *BlockChain) callContract(contractHash string, input []byte, blockNumber *big.Int) ([]byte, error) {
|
2017-12-04 22:35:41 +00:00
|
|
|
to := common.HexToAddress(contractHash)
|
2017-12-04 18:54:33 +00:00
|
|
|
msg := ethereum.CallMsg{To: &to, Data: input}
|
2018-09-18 19:46:16 +00:00
|
|
|
return blockChain.ethClient.CallContract(context.Background(), msg, blockNumber)
|
2017-12-04 18:54:33 +00:00
|
|
|
}
|