Get ABI via etherscan API (#96)

- Added ABI request
- Add unique constraint on contract hash for watched contracts
This commit is contained in:
Matt K
2017-12-07 09:58:06 -06:00
committed by GitHub
parent f496303f15
commit 18163f970e
13 changed files with 213 additions and 108 deletions
+40 -2
View File
@@ -5,14 +5,52 @@ import (
"io/ioutil"
"strings"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
)
var (
ErrInvalidAbiFile = errors.New("invalid abi")
ErrMissingAbiFile = errors.New("missing abi")
ErrInvalidAbiFile = errors.New("invalid abi")
ErrMissingAbiFile = errors.New("missing abi")
ErrApiRequestFailed = errors.New("etherscan api request failed")
)
type Response struct {
Status string
Message string
Result string
}
type EtherScanApi struct {
client *http.Client
url string
}
func NewEtherScanClient(url string) *EtherScanApi {
return &EtherScanApi{
client: &http.Client{Timeout: 10 * time.Second},
url: url,
}
}
//https://api.etherscan.io/api?module=contract&action=getabi&address=%s
func (e *EtherScanApi) GetAbi(contractHash string) (string, error) {
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()
json.NewDecoder(r.Body).Decode(&target)
return target.Result, nil
}
func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
abiString, err := ReadAbiFile(abiFilePath)
if err != nil {
+88 -34
View File
@@ -3,49 +3,103 @@ package geth_test
import (
"path/filepath"
"net/http"
"fmt"
"log"
cfg "github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/geth"
"github.com/ethereum/go-ethereum/accounts/abi"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"
)
var _ = Describe("Reading ABI files", func() {
var _ = Describe("ABI files", func() {
It("loads a valid ABI file", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
Describe("Reading ABI files", func() {
contractAbi, err := geth.ParseAbiFile(path)
It("loads a valid ABI file", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
Expect(contractAbi).NotTo(BeNil())
Expect(err).To(BeNil())
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).NotTo(BeNil())
Expect(err).To(BeNil())
})
It("reads the contents of a valid ABI file", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
contractAbi, err := geth.ReadAbiFile(path)
Expect(contractAbi).To(Equal("[{\"foo\": \"bar\"}]"))
Expect(err).To(BeNil())
})
It("returns an error when the file does not exist", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).To(Equal(abi.ABI{}))
Expect(err).To(Equal(geth.ErrMissingAbiFile))
})
It("returns an error when the file has invalid contents", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "invalid_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).To(Equal(abi.ABI{}))
Expect(err).To(Equal(geth.ErrInvalidAbiFile))
})
Describe("Request ABI from endpoint", func() {
var (
server *ghttp.Server
client *geth.EtherScanApi
abiString string
)
BeforeEach(func() {
server = ghttp.NewServer()
client = geth.NewEtherScanClient(server.URL())
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "sample_abi.json")
abiString, err := geth.ReadAbiFile(path)
_, err = geth.ParseAbi(abiString)
if err != nil {
log.Fatalln("Could not parse ABI")
}
})
AfterEach(func() {
server.Close()
})
Describe("Fetching ABI from api (etherscan)", func() {
BeforeEach(func() {
response := fmt.Sprintf(`{"status":"1","message":"OK","result":%q}`, abiString)
server.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api", "module=contract&action=getabi&address=0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"),
ghttp.RespondWith(http.StatusOK, response),
),
)
})
It("should make a GET request with supplied contract hash", func() {
abi, err := client.GetAbi("0xd26114cd6EE289AccF82350c8d8487fedB8A0C07")
Expect(server.ReceivedRequests()).Should(HaveLen(1))
Expect(err).ShouldNot(HaveOccurred())
Expect(abi).Should(Equal(abiString))
})
})
})
})
It("reads the contents of a valid ABI file", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
contractAbi, err := geth.ReadAbiFile(path)
Expect(contractAbi).To(Equal("[{\"foo\": \"bar\"}]"))
Expect(err).To(BeNil())
})
It("returns an error when the file does not exist", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).To(Equal(abi.ABI{}))
Expect(err).To(Equal(geth.ErrMissingAbiFile))
})
It("returns an error when the file has invalid contents", func() {
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "invalid_abi.json")
contractAbi, err := geth.ParseAbiFile(path)
Expect(contractAbi).To(Equal(abi.ABI{}))
Expect(err).To(Equal(geth.ErrInvalidAbiFile))
})
})
-17
View File
@@ -2,15 +2,12 @@ package geth
import (
"errors"
"fmt"
"path/filepath"
"sort"
"context"
"math/big"
"github.com/8thlight/vulcanizedb/pkg/config"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
@@ -59,17 +56,3 @@ func (blockchain *GethBlockchain) GetAttributes(contract core.Contract) (core.Co
sort.Sort(contractAttributes)
return contractAttributes, nil
}
func (blockchain *GethBlockchain) GetContractAttributesOld(contractHash string) (core.ContractAttributes, error) {
abiFilePath := filepath.Join(config.ProjectRoot(), "contracts", "public", fmt.Sprintf("%s.json", contractHash))
parsed, _ := ParseAbiFile(abiFilePath)
var contractAttributes core.ContractAttributes
for _, abiElement := range parsed.Methods {
if (len(abiElement.Outputs) > 0) && (len(abiElement.Inputs) == 0) && abiElement.Const {
attributeType := abiElement.Outputs[0].Type.String()
contractAttributes = append(contractAttributes, core.ContractAttribute{abiElement.Name, attributeType})
}
}
sort.Sort(contractAttributes)
return contractAttributes, nil
}
+18 -19
View File
@@ -38,7 +38,12 @@ func (repository Postgres) CreateContract(contract core.Contract) error {
abiToInsert = &abi
}
_, err := repository.Db.Exec(
`INSERT INTO watched_contracts (contract_hash, contract_abi) VALUES ($1, $2)`, contract.Hash, abiToInsert)
`INSERT INTO watched_contracts (contract_hash, contract_abi)
VALUES ($1, $2)
ON CONFLICT (contract_hash)
DO UPDATE
SET contract_hash = $1, contract_abi = $2
`, contract.Hash, abiToInsert)
if err != nil {
return ErrDBInsertFailed
}
@@ -53,15 +58,16 @@ func (repository Postgres) ContractExists(contractHash string) bool {
}
func (repository Postgres) FindContract(contractHash string) *core.Contract {
var savedContracts []core.Contract
contractRows, _ := repository.Db.Query(
var hash string
var abi string
row := repository.Db.QueryRow(
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
savedContracts = repository.loadContract(contractRows)
if len(savedContracts) > 0 {
return &savedContracts[0]
} else {
err := row.Scan(&hash, &abi)
if err == sql.ErrNoRows {
return nil
}
savedContract := repository.addTransactions(core.Contract{Hash: hash, Abi: abi})
return &savedContract
}
func (repository Postgres) MaxBlockNumber() int64 {
@@ -197,16 +203,9 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
return transactions
}
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.Contract {
var savedContracts []core.Contract
for contractRows.Next() {
var savedContractHash string
var savedContractAbi string
contractRows.Scan(&savedContractHash, &savedContractAbi)
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash)
transactions := repository.loadTransactions(transactionRows)
savedContract := core.Contract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
savedContracts = append(savedContracts, savedContract)
}
return savedContracts
func (repository Postgres) addTransactions(contract core.Contract) core.Contract {
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, contract.Hash)
transactions := repository.loadTransactions(transactionRows)
savedContract := core.Contract{Hash: contract.Hash, Transactions: transactions, Abi: contract.Abi}
return savedContract
}
+14
View File
@@ -262,6 +262,20 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
Expect(contract).ToNot(BeNil())
Expect(contract.Abi).To(Equal("{\"some\": \"json\"}"))
})
It("updates the ABI of the contract if hash already present", func() {
repository.CreateContract(core.Contract{
Abi: "{\"some\": \"json\"}",
Hash: "x123",
})
repository.CreateContract(core.Contract{
Abi: "{\"some\": \"different json\"}",
Hash: "x123",
})
contract := repository.FindContract("x123")
Expect(contract).ToNot(BeNil())
Expect(contract.Abi).To(Equal("{\"some\": \"different json\"}"))
})
})
}