2018-05-05 20:25:54 +00:00
|
|
|
// Copyright 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package every_block
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/big"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-05-05 20:25:54 +00:00
|
|
|
)
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Interface definition for a Fetcher
|
2018-05-05 20:25:54 +00:00
|
|
|
type ERC20FetcherInterface interface {
|
2018-08-09 16:58:06 +00:00
|
|
|
FetchBigInt(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error)
|
|
|
|
FetchBool(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error)
|
|
|
|
FetchAddress(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Address, error)
|
2018-07-20 16:37:46 +00:00
|
|
|
GetBlockChain() core.BlockChain
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Initializes and returns a Fetcher with the given blockchain
|
|
|
|
func NewFetcher(blockChain core.BlockChain) Fetcher {
|
2018-05-05 20:25:54 +00:00
|
|
|
return Fetcher{
|
2018-08-09 16:58:06 +00:00
|
|
|
BlockChain: blockChain,
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Fetcher struct
|
2018-05-05 20:25:54 +00:00
|
|
|
type Fetcher struct {
|
2018-08-09 16:58:06 +00:00
|
|
|
BlockChain core.BlockChain
|
2018-05-05 20:25:54 +00:00
|
|
|
ContractAbi string
|
|
|
|
ContractAddress string
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Fetcher error
|
2018-05-05 20:25:54 +00:00
|
|
|
type fetcherError struct {
|
|
|
|
err string
|
|
|
|
fetchMethod string
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Fetcher error method
|
2018-05-05 20:25:54 +00:00
|
|
|
func (fe *fetcherError) Error() string {
|
|
|
|
return fmt.Sprintf("Error fetching %s: %s", fe.fetchMethod, fe.err)
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Used to create a new Fetcher error for a given error and fetch method
|
2018-05-05 20:25:54 +00:00
|
|
|
func newFetcherError(err error, fetchMethod string) *fetcherError {
|
|
|
|
e := fetcherError{err.Error(), fetchMethod}
|
|
|
|
log.Println(e.Error())
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Method used to fetch big.Int result from contract
|
|
|
|
func (f Fetcher) FetchBigInt(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
|
2018-05-05 20:25:54 +00:00
|
|
|
var result = new(big.Int)
|
2018-08-09 16:58:06 +00:00
|
|
|
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *result, newFetcherError(err, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Method used to fetch bool result from contract
|
|
|
|
func (f Fetcher) FetchBool(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error) {
|
|
|
|
var result = new(bool)
|
|
|
|
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *result, newFetcherError(err, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method used to fetch address result from contract
|
|
|
|
func (f Fetcher) FetchAddress(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Address, error) {
|
|
|
|
var result = new(common.Address)
|
|
|
|
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *result, newFetcherError(err, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getter method for Fetcher's blockchain
|
2018-07-20 16:37:46 +00:00
|
|
|
func (f Fetcher) GetBlockChain() core.BlockChain {
|
2018-08-09 16:58:06 +00:00
|
|
|
return f.BlockChain
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|