2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
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.
|
|
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
2017-11-27 15:39:53 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2017-11-27 15:39:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2018-05-07 15:41:02 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fs"
|
2017-11-27 15:39:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-12-07 15:58:06 +00:00
|
|
|
ErrInvalidAbiFile = errors.New("invalid abi")
|
|
|
|
ErrMissingAbiFile = errors.New("missing abi")
|
|
|
|
ErrApiRequestFailed = errors.New("etherscan api request failed")
|
2017-11-27 15:39:53 +00:00
|
|
|
)
|
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
type Response struct {
|
|
|
|
Status string
|
|
|
|
Message string
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
type EtherScanAPI struct {
|
2017-12-07 15:58:06 +00:00
|
|
|
client *http.Client
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
func NewEtherScanClient(url string) *EtherScanAPI {
|
|
|
|
return &EtherScanAPI{
|
2017-12-07 15:58:06 +00:00
|
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
|
|
|
url: url,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
func GenURL(network string) string {
|
2018-01-08 21:59:47 +00:00
|
|
|
switch network {
|
|
|
|
case "ropsten":
|
|
|
|
return "https://ropsten.etherscan.io"
|
|
|
|
case "kovan":
|
|
|
|
return "https://kovan.etherscan.io"
|
|
|
|
case "rinkeby":
|
|
|
|
return "https://rinkeby.etherscan.io"
|
|
|
|
default:
|
|
|
|
return "https://api.etherscan.io"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
//https://api.etherscan.io/api?module=contract&action=getabi&address=%s
|
2018-02-13 16:31:57 +00:00
|
|
|
func (e *EtherScanAPI) GetAbi(contractHash string) (string, error) {
|
2017-12-07 15:58:06 +00:00
|
|
|
target := new(Response)
|
|
|
|
request := fmt.Sprintf("%s/api?module=contract&action=getabi&address=%s", e.url, contractHash)
|
|
|
|
r, err := e.client.Get(request)
|
|
|
|
if err != nil {
|
|
|
|
return "", ErrApiRequestFailed
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
2018-02-13 16:31:57 +00:00
|
|
|
err = json.NewDecoder(r.Body).Decode(&target)
|
|
|
|
return target.Result, err
|
2017-12-07 15:58:06 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 15:39:53 +00:00
|
|
|
func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
|
2017-12-04 21:12:27 +00:00
|
|
|
abiString, err := ReadAbiFile(abiFilePath)
|
2017-11-27 15:39:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return abi.ABI{}, ErrMissingAbiFile
|
|
|
|
}
|
2017-12-04 22:35:41 +00:00
|
|
|
return ParseAbi(abiString)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseAbi(abiString string) (abi.ABI, error) {
|
2017-11-27 15:39:53 +00:00
|
|
|
parsedAbi, err := abi.JSON(strings.NewReader(abiString))
|
|
|
|
if err != nil {
|
|
|
|
return abi.ABI{}, ErrInvalidAbiFile
|
|
|
|
}
|
|
|
|
return parsedAbi, nil
|
|
|
|
}
|
2017-12-04 21:12:27 +00:00
|
|
|
|
|
|
|
func ReadAbiFile(abiFilePath string) (string, error) {
|
2018-05-07 15:41:02 +00:00
|
|
|
reader := fs.FsReader{}
|
|
|
|
filesBytes, err := reader.Read(abiFilePath)
|
2017-12-04 21:12:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", ErrMissingAbiFile
|
|
|
|
}
|
|
|
|
return string(filesBytes), nil
|
|
|
|
}
|