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/>.
|
2018-11-03 19:02:31 +00:00
|
|
|
|
|
|
|
package parser_test
|
|
|
|
|
|
|
|
import (
|
2018-11-07 21:50:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2018-11-03 19:02:31 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-11-04 21:26:39 +00:00
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth"
|
2020-01-29 19:00:07 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/helpers/test_helpers/mocks"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/parser"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/eth/contract_watcher/shared/types"
|
2018-11-03 19:02:31 +00:00
|
|
|
)
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
var _ = Describe("Parser", func() {
|
2018-11-03 19:02:31 +00:00
|
|
|
|
|
|
|
var p parser.Parser
|
|
|
|
var err error
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
|
p = parser.NewParser("")
|
|
|
|
})
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("Mock Parse", func() {
|
|
|
|
It("Uses parses given abi string", func() {
|
|
|
|
mp := mocks.NewParser(constants.DaiAbiString)
|
2019-10-18 17:34:22 +00:00
|
|
|
err = mp.Parse(constants.DaiContractAddress)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2018-11-03 19:02:31 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
parsedAbi := mp.ParsedAbi()
|
2019-10-28 11:30:24 +00:00
|
|
|
expectedAbi, err := eth.ParseAbi(constants.DaiAbiString)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(parsedAbi).To(Equal(expectedAbi))
|
2018-11-03 19:02:31 +00:00
|
|
|
|
2018-12-14 17:52:02 +00:00
|
|
|
methods := mp.GetSelectMethods([]string{"balanceOf"})
|
2018-12-19 18:42:59 +00:00
|
|
|
Expect(len(methods)).To(Equal(1))
|
|
|
|
balOf := methods[0]
|
|
|
|
Expect(balOf.Name).To(Equal("balanceOf"))
|
|
|
|
Expect(len(balOf.Args)).To(Equal(1))
|
|
|
|
Expect(len(balOf.Return)).To(Equal(1))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
events := mp.GetEvents([]string{"Transfer"})
|
2018-12-19 18:42:59 +00:00
|
|
|
_, ok := events["Mint"]
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(ok).To(Equal(false))
|
|
|
|
e, ok := events["Transfer"]
|
|
|
|
Expect(ok).To(Equal(true))
|
|
|
|
Expect(len(e.Fields)).To(Equal(3))
|
|
|
|
})
|
2018-11-03 19:02:31 +00:00
|
|
|
})
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("Parse", func() {
|
|
|
|
It("Fetches and parses abi from etherscan using contract address", func() {
|
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359" // dai contract address
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
2018-11-03 19:02:31 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
expectedAbi := constants.DaiAbiString
|
|
|
|
Expect(p.Abi()).To(Equal(expectedAbi))
|
2018-11-03 19:02:31 +00:00
|
|
|
|
2019-10-28 11:30:24 +00:00
|
|
|
expectedParsedAbi, err := eth.ParseAbi(expectedAbi)
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
Expect(p.ParsedAbi()).To(Equal(expectedParsedAbi))
|
|
|
|
})
|
2018-11-03 19:02:31 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
It("Fails with a normal, non-contract, account address", func() {
|
|
|
|
addr := "0xAb2A8F7cB56D9EC65573BA1bE0f92Fa2Ff7dd165"
|
|
|
|
err = p.Parse(addr)
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
})
|
2018-11-03 19:02:31 +00:00
|
|
|
})
|
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
Describe("GetEvents", func() {
|
|
|
|
It("Returns parsed events", func() {
|
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
events := p.GetEvents([]string{"Transfer"})
|
|
|
|
|
|
|
|
e, ok := events["Transfer"]
|
|
|
|
Expect(ok).To(Equal(true))
|
|
|
|
|
|
|
|
abiTy := e.Fields[0].Type.T
|
|
|
|
Expect(abiTy).To(Equal(abi.AddressTy))
|
|
|
|
|
|
|
|
pgTy := e.Fields[0].PgType
|
|
|
|
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
|
|
|
|
|
|
|
abiTy = e.Fields[1].Type.T
|
|
|
|
Expect(abiTy).To(Equal(abi.AddressTy))
|
|
|
|
|
|
|
|
pgTy = e.Fields[1].PgType
|
|
|
|
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
|
|
|
|
|
|
|
abiTy = e.Fields[2].Type.T
|
|
|
|
Expect(abiTy).To(Equal(abi.UintTy))
|
|
|
|
|
|
|
|
pgTy = e.Fields[2].PgType
|
2019-02-28 17:51:54 +00:00
|
|
|
Expect(pgTy).To(Equal("NUMERIC"))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
_, ok = events["Approval"]
|
|
|
|
Expect(ok).To(Equal(false))
|
|
|
|
})
|
2018-11-04 21:26:39 +00:00
|
|
|
})
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
Describe("GetSelectMethods", func() {
|
2018-11-07 21:50:43 +00:00
|
|
|
It("Parses and returns only methods specified in passed array", func() {
|
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
methods := p.GetSelectMethods([]string{"balanceOf"})
|
|
|
|
Expect(len(methods)).To(Equal(1))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
balOf := methods[0]
|
|
|
|
Expect(balOf.Name).To(Equal("balanceOf"))
|
|
|
|
Expect(len(balOf.Args)).To(Equal(1))
|
|
|
|
Expect(len(balOf.Return)).To(Equal(1))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
abiTy := balOf.Args[0].Type.T
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(abiTy).To(Equal(abi.AddressTy))
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
pgTy := balOf.Args[0].PgType
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
abiTy = balOf.Return[0].Type.T
|
2018-11-07 21:50:43 +00:00
|
|
|
Expect(abiTy).To(Equal(abi.UintTy))
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
pgTy = balOf.Return[0].PgType
|
2019-02-28 17:51:54 +00:00
|
|
|
Expect(pgTy).To(Equal("NUMERIC"))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
It("Parses and returns methods in the order they were specified", func() {
|
2018-11-07 21:50:43 +00:00
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
selectMethods := p.GetSelectMethods([]string{"balanceOf", "allowance"})
|
|
|
|
Expect(len(selectMethods)).To(Equal(2))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
balOf := selectMethods[0]
|
|
|
|
allow := selectMethods[1]
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
Expect(balOf.Name).To(Equal("balanceOf"))
|
|
|
|
Expect(allow.Name).To(Equal("allowance"))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
It("Returns nil if given a nil or empty array", func() {
|
2018-11-07 21:50:43 +00:00
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
var nilArr []types.Method
|
|
|
|
selectMethods := p.GetSelectMethods([]string{})
|
|
|
|
Expect(selectMethods).To(Equal(nilArr))
|
|
|
|
selectMethods = p.GetMethods(nil)
|
|
|
|
Expect(selectMethods).To(Equal(nilArr))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
2018-12-19 18:42:59 +00:00
|
|
|
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
Describe("GetMethods", func() {
|
|
|
|
It("Parses and returns only methods specified in passed array", func() {
|
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
2018-11-07 21:50:43 +00:00
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
methods := p.GetMethods([]string{"balanceOf"})
|
|
|
|
Expect(len(methods)).To(Equal(1))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
balOf := methods[0]
|
|
|
|
Expect(balOf.Name).To(Equal("balanceOf"))
|
|
|
|
Expect(len(balOf.Args)).To(Equal(1))
|
|
|
|
Expect(len(balOf.Return)).To(Equal(1))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
abiTy := balOf.Args[0].Type.T
|
|
|
|
Expect(abiTy).To(Equal(abi.AddressTy))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
pgTy := balOf.Args[0].PgType
|
|
|
|
Expect(pgTy).To(Equal("CHARACTER VARYING(66)"))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
abiTy = balOf.Return[0].Type.T
|
|
|
|
Expect(abiTy).To(Equal(abi.UintTy))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
pgTy = balOf.Return[0].PgType
|
2019-02-28 17:51:54 +00:00
|
|
|
Expect(pgTy).To(Equal("NUMERIC"))
|
2018-11-07 21:50:43 +00:00
|
|
|
|
2018-12-19 18:42:59 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns nil if given a nil array", func() {
|
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
var nilArr []types.Method
|
|
|
|
selectMethods := p.GetMethods(nil)
|
|
|
|
Expect(selectMethods).To(Equal(nilArr))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("Returns every method if given an empty array", func() {
|
|
|
|
contractAddr := "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"
|
|
|
|
err = p.Parse(contractAddr)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
selectMethods := p.GetMethods([]string{})
|
2019-07-15 22:08:40 +00:00
|
|
|
Expect(len(selectMethods)).To(Equal(25))
|
2018-11-07 21:50:43 +00:00
|
|
|
})
|
|
|
|
})
|
2018-11-03 19:02:31 +00:00
|
|
|
})
|