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"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/repositories"
|
2017-11-09 19:34:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func LoadConfig(environment string) config.Config {
|
|
|
|
cfg, err := config.NewConfig(environment)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error loading config\n%v", err)
|
|
|
|
}
|
2017-12-04 16:17:13 +00:00
|
|
|
return cfg
|
2017-11-09 19:34:58 +00:00
|
|
|
}
|
2017-12-04 15:53:36 +00:00
|
|
|
|
2017-12-07 19:32:16 +00:00
|
|
|
func LoadPostgres(database config.Database, node core.Node) repositories.Postgres {
|
|
|
|
repository, err := repositories.NewPostgres(database, node)
|
2017-12-04 15:53:36 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error loading postgres\n%v", err)
|
|
|
|
}
|
|
|
|
return repository
|
|
|
|
}
|
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-01-08 21:59:47 +00:00
|
|
|
url := geth.GenUrl(network)
|
|
|
|
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
|
|
|
|
}
|