2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2017-11-09 19:34:58 +00:00
|
|
|
|
2018-11-07 21:50:43 +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.
|
2017-11-09 19:34:58 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// 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.
|
2017-12-04 21:12:27 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
// 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-01-25 19:21:55 +00:00
|
|
|
package utils
|
2017-12-11 21:08:00 +00:00
|
|
|
|
2017-11-09 19:34:58 +00:00
|
|
|
import (
|
2019-10-28 10:48:31 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-12-11 21:08:00 +00:00
|
|
|
"math/big"
|
2018-01-25 19:21:55 +00:00
|
|
|
"os"
|
2018-08-31 19:48:43 +00:00
|
|
|
"path/filepath"
|
2018-01-25 19:21:55 +00:00
|
|
|
|
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"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
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 {
|
2019-10-28 10:48:31 +00:00
|
|
|
logrus.Fatal("Error loading postgres: ", err)
|
2017-12-04 15:53:36 +00:00
|
|
|
}
|
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)
|
2019-10-28 11:30:24 +00:00
|
|
|
abi, err := eth.ReadAbiFile(abiFilepath)
|
2017-12-04 21:12:27 +00:00
|
|
|
if err != nil {
|
2019-10-28 10:48:31 +00:00
|
|
|
logrus.Fatalf("Error reading ABI file at \"%s\"\n %v", abiFilepath, err)
|
2017-12-04 21:12:27 +00:00
|
|
|
}
|
|
|
|
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 {
|
2019-10-28 11:30:24 +00:00
|
|
|
url := eth.GenURL(network)
|
|
|
|
etherscan := eth.NewEtherScanClient(url)
|
2019-10-28 10:48:31 +00:00
|
|
|
logrus.Printf("No ABI supplied. Retrieving ABI from Etherscan: %s", url)
|
2017-12-07 15:58:06 +00:00
|
|
|
contractAbiString, _ = etherscan.GetAbi(contractHash)
|
|
|
|
}
|
2019-10-28 11:30:24 +00:00
|
|
|
_, err := eth.ParseAbi(contractAbiString)
|
2017-12-07 15:58:06 +00:00
|
|
|
if err != nil {
|
2019-10-28 10:48:31 +00:00
|
|
|
logrus.Fatalln("Invalid ABI: ", err)
|
2017-12-07 15:58:06 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2019-10-28 10:48:31 +00:00
|
|
|
|
|
|
|
func RollbackAndLogFailure(tx *sqlx.Tx, txErr error, fieldName string) {
|
|
|
|
rollbackErr := tx.Rollback()
|
|
|
|
if rollbackErr != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{"rollbackErr": rollbackErr, "txErr": txErr}).
|
|
|
|
Warnf("failed to rollback transaction after failing to insert %s", fieldName)
|
|
|
|
}
|
|
|
|
}
|