2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2018 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-11-27 15:39:53 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
2017-12-04 18:54:33 +00:00
|
|
|
"context"
|
2018-11-15 18:53:08 +00:00
|
|
|
"errors"
|
2017-12-04 18:54:33 +00:00
|
|
|
"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-08-09 16:58:06 +00:00
|
|
|
func (blockChain *BlockChain) FetchContractData(abiJSON string, address string, method string, methodArgs []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
|
2018-08-09 16:58:06 +00:00
|
|
|
if methodArgs != nil {
|
|
|
|
input, err = parsed.Pack(method, methodArgs...)
|
2018-03-12 19:09:54 +00:00
|
|
|
} 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-11-15 18:53:08 +00:00
|
|
|
return blockChain.ethClient.CallContract(context.Background(), msg, blockNumber)
|
2017-12-04 18:54:33 +00:00
|
|
|
}
|