2018-01-25 19:21:55 +00:00
|
|
|
package utils
|
2017-11-09 19:34:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2017-12-04 21:12:27 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2017-12-11 21:08:00 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-01-25 19:21:55 +00:00
|
|
|
"os"
|
|
|
|
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/config"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-01-06 20:31:53 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2017-11-09 19:34:58 +00:00
|
|
|
)
|
|
|
|
|
2018-02-02 21:53:16 +00:00
|
|
|
func LoadPostgres(database config.Database, node core.Node) postgres.DB {
|
2018-02-12 16:54:05 +00:00
|
|
|
db, err := postgres.NewDB(database, node)
|
2017-12-04 15:53:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error loading postgres\n%v", err)
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
return *db
|
2017-12-04 15:53:36 +00:00
|
|
|
}
|
2017-12-04 21:12:27 +00:00
|
|
|
|
|
|
|
func ReadAbiFile(abiFilepath string) string {
|
2018-01-23 18:43:35 +00:00
|
|
|
abiFilepath = AbsFilePath(abiFilepath)
|
2017-12-04 21:12:27 +00:00
|
|
|
abi, err := geth.ReadAbiFile(abiFilepath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error reading ABI file at \"%s\"\n %v", abiFilepath, err)
|
|
|
|
}
|
|
|
|
return abi
|
|
|
|
}
|
2017-12-07 15:58:06 +00:00
|
|
|
|
2018-01-23 18:43:35 +00:00
|
|
|
func AbsFilePath(filePath string) string {
|
|
|
|
if !filepath.IsAbs(filePath) {
|
2018-01-25 19:21:55 +00:00
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
filePath = filepath.Join(cwd, filePath)
|
2018-01-23 18:43:35 +00:00
|
|
|
}
|
|
|
|
return filePath
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:59:47 +00:00
|
|
|
func GetAbi(abiFilepath string, contractHash string, network string) string {
|
2017-12-07 15:58:06 +00:00
|
|
|
var contractAbiString string
|
|
|
|
if abiFilepath != "" {
|
|
|
|
contractAbiString = ReadAbiFile(abiFilepath)
|
|
|
|
} else {
|
2018-02-13 16:31:57 +00:00
|
|
|
url := geth.GenURL(network)
|
2018-01-08 21:59:47 +00:00
|
|
|
etherscan := geth.NewEtherScanClient(url)
|
|
|
|
log.Printf("No ABI supplied. Retrieving ABI from Etherscan: %s", url)
|
2017-12-07 15:58:06 +00:00
|
|
|
contractAbiString, _ = etherscan.GetAbi(contractHash)
|
|
|
|
}
|
|
|
|
_, err := geth.ParseAbi(contractAbiString)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Invalid ABI")
|
|
|
|
}
|
|
|
|
return contractAbiString
|
|
|
|
}
|
2017-12-11 21:08:00 +00:00
|
|
|
|
|
|
|
func RequestedBlockNumber(blockNumber *int64) *big.Int {
|
|
|
|
var _blockNumber *big.Int
|
|
|
|
if *blockNumber == -1 {
|
|
|
|
_blockNumber = nil
|
|
|
|
} else {
|
|
|
|
_blockNumber = big.NewInt(*blockNumber)
|
|
|
|
}
|
|
|
|
return _blockNumber
|
|
|
|
}
|