2017-11-09 19:34:58 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2017-12-04 21:12:27 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2017-12-11 21:08:00 +00:00
|
|
|
"math/big"
|
|
|
|
|
2017-11-09 19:34:58 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/config"
|
2017-12-07 19:32:16 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/core"
|
2017-12-04 21:12:27 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/pkg/geth"
|
2017-12-04 15:53:36 +00:00
|
|
|
"github.com/8thlight/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 {
|
|
|
|
if !filepath.IsAbs(abiFilepath) {
|
|
|
|
abiFilepath = filepath.Join(config.ProjectRoot(), abiFilepath)
|
|
|
|
}
|
|
|
|
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-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
|
|
|
|
}
|