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/>.
|
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
package eth_test
|
2017-11-27 15:39:53 +00:00
|
|
|
|
|
|
|
import (
|
2017-12-07 15:58:06 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
2017-11-27 15:39:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2017-12-07 15:58:06 +00:00
|
|
|
"github.com/onsi/gomega/ghttp"
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/test_config"
|
2017-11-27 15:39:53 +00:00
|
|
|
)
|
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
var _ = Describe("ABI files", func() {
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
Describe("Reading ABI files", func() {
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
It("loads a valid ABI file", func() {
|
2018-02-13 16:31:57 +00:00
|
|
|
path := test_config.ABIFilePath + "valid_abi.json"
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
contractAbi, err := eth.ParseAbiFile(path)
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
Expect(contractAbi).NotTo(BeNil())
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
2017-12-04 21:12:27 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
It("reads the contents of a valid ABI file", func() {
|
2018-02-13 16:31:57 +00:00
|
|
|
path := test_config.ABIFilePath + "valid_abi.json"
|
2017-12-04 21:12:27 +00:00
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
contractAbi, err := eth.ReadAbiFile(path)
|
2017-12-04 21:12:27 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
Expect(contractAbi).To(Equal("[{\"foo\": \"bar\"}]"))
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
})
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
It("returns an error when the file does not exist", func() {
|
2018-02-13 16:31:57 +00:00
|
|
|
path := test_config.ABIFilePath + "missing_abi.json"
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
contractAbi, err := eth.ParseAbiFile(path)
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
Expect(contractAbi).To(Equal(abi.ABI{}))
|
2019-10-28 11:30:24 +00:00
|
|
|
Expect(err).To(Equal(eth.ErrMissingAbiFile))
|
2017-12-07 15:58:06 +00:00
|
|
|
})
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
It("returns an error when the file has invalid contents", func() {
|
2018-02-13 16:31:57 +00:00
|
|
|
path := test_config.ABIFilePath + "invalid_abi.json"
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
contractAbi, err := eth.ParseAbiFile(path)
|
2017-12-07 15:58:06 +00:00
|
|
|
|
|
|
|
Expect(contractAbi).To(Equal(abi.ABI{}))
|
2019-10-28 11:30:24 +00:00
|
|
|
Expect(err).To(Equal(eth.ErrInvalidAbiFile))
|
2017-12-07 15:58:06 +00:00
|
|
|
})
|
2017-11-27 15:39:53 +00:00
|
|
|
|
2017-12-07 15:58:06 +00:00
|
|
|
Describe("Request ABI from endpoint", func() {
|
|
|
|
|
|
|
|
var (
|
|
|
|
server *ghttp.Server
|
2019-10-28 11:30:24 +00:00
|
|
|
client *eth.EtherScanAPI
|
2017-12-07 15:58:06 +00:00
|
|
|
abiString string
|
2018-02-13 16:31:57 +00:00
|
|
|
err error
|
2017-12-07 15:58:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
server = ghttp.NewServer()
|
2019-10-28 11:30:24 +00:00
|
|
|
client = eth.NewEtherScanClient(server.URL())
|
2018-02-13 16:31:57 +00:00
|
|
|
path := test_config.ABIFilePath + "sample_abi.json"
|
2019-10-28 11:30:24 +00:00
|
|
|
abiString, err = eth.ReadAbiFile(path)
|
2018-02-13 16:31:57 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2019-10-28 11:30:24 +00:00
|
|
|
_, err = eth.ParseAbi(abiString)
|
2018-02-13 16:31:57 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2017-12-07 15:58:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-01-08 21:59:47 +00:00
|
|
|
|
|
|
|
Describe("Generating etherscan endpoints based on network", func() {
|
|
|
|
It("should return the main endpoint as the default", func() {
|
2019-10-28 11:30:24 +00:00
|
|
|
url := eth.GenURL("")
|
2018-01-08 21:59:47 +00:00
|
|
|
Expect(url).To(Equal("https://api.etherscan.io"))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("generates various test network endpoint if test network is supplied", func() {
|
2019-10-28 11:30:24 +00:00
|
|
|
ropstenUrl := eth.GenURL("ropsten")
|
|
|
|
rinkebyUrl := eth.GenURL("rinkeby")
|
|
|
|
kovanUrl := eth.GenURL("kovan")
|
2018-01-08 21:59:47 +00:00
|
|
|
|
|
|
|
Expect(ropstenUrl).To(Equal("https://ropsten.etherscan.io"))
|
|
|
|
Expect(kovanUrl).To(Equal("https://kovan.etherscan.io"))
|
|
|
|
Expect(rinkebyUrl).To(Equal("https://rinkeby.etherscan.io"))
|
|
|
|
})
|
|
|
|
})
|
2017-12-07 15:58:06 +00:00
|
|
|
})
|
2017-11-27 15:39:53 +00:00
|
|
|
})
|