2019-01-24 20:41:30 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2019-01-24 20:41:30 +00:00
|
|
|
|
|
|
|
// 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/>.
|
2018-05-05 20:25:54 +00:00
|
|
|
|
2019-01-15 21:49:18 +00:00
|
|
|
package fetcher
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
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-08-31 19:48:43 +00:00
|
|
|
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-05-05 20:25:54 +00:00
|
|
|
)
|
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
// Fetcher serves as the lower level data fetcher that calls the underlying
|
|
|
|
// blockchain's FetchConctractData method for a given return type
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// FetcherInterface is the interface definition for a fetcher
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
type FetcherInterface 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)
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
FetchString(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (string, error)
|
|
|
|
FetchHash(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Hash, error)
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
// Used to create a new Fetcher error for a given error and fetch method
|
|
|
|
func newFetcherError(err error, fetchMethod string) *fetcherError {
|
|
|
|
e := fetcherError{err.Error(), fetchMethod}
|
|
|
|
log.Println(e.Error())
|
|
|
|
return &e
|
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 {
|
2019-09-24 03:39:04 +00:00
|
|
|
BlockChain core.BlockChain // Underlying Blockchain
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
// Generic Fetcher methods used by Getters to call contract methods
|
2018-05-05 20:25:54 +00:00
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// FetchBigInt is the method used to fetch big.Int value from contract
|
2018-08-09 16:58:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// FetchBool is the method used to fetch bool value from contract
|
2018-08-09 16:58:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// FetchAddress is the method used to fetch address value from contract
|
2018-08-09 16:58:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// FetchString is the method used to fetch string value from contract
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
func (f Fetcher) FetchString(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (string, error) {
|
|
|
|
var result = new(string)
|
|
|
|
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *result, newFetcherError(err, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:12:17 +00:00
|
|
|
// FetchHash is the method used to fetch hash value from contract
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
func (f Fetcher) FetchHash(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Hash, error) {
|
|
|
|
var result = new(common.Hash)
|
|
|
|
err := f.BlockChain.FetchContractData(contractAbi, contractAddress, method, methodArgs, &result, blockNumber)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return *result, newFetcherError(err, method)
|
|
|
|
}
|
|
|
|
|
|
|
|
return *result, nil
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|