2018-05-05 20:25:54 +00:00
|
|
|
// Copyright 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package every_block_test
|
|
|
|
|
|
|
|
import (
|
2018-07-20 16:37:46 +00:00
|
|
|
"math/big"
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2018-05-05 20:25:54 +00:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2018-05-05 20:25:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/examples/constants"
|
|
|
|
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
|
2018-07-20 16:37:46 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/fakes"
|
2018-05-05 20:25:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
|
|
|
|
rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
2018-08-09 17:23:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-05-05 20:25:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("ERC20 Fetcher", func() {
|
|
|
|
blockNumber := int64(5502914)
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
Describe("totalSupply", func() {
|
|
|
|
It("fetches total supply data from the blockchain with the correct arguments", func() {
|
|
|
|
fakeBlockChain := fakes.NewMockBlockChain()
|
|
|
|
testFetcher := every_block.NewFetcher(fakeBlockChain)
|
2018-07-20 16:37:46 +00:00
|
|
|
testAbi := "testAbi"
|
|
|
|
testContractAddress := "testContractAddress"
|
2018-08-09 16:58:06 +00:00
|
|
|
|
|
|
|
_, err := testFetcher.FetchBigInt("totalSupply", testAbi, testContractAddress, blockNumber, nil)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
expectedResult := big.Int{}
|
|
|
|
expected := &expectedResult
|
2018-08-09 16:58:06 +00:00
|
|
|
fakeBlockChain.AssertFetchContractDataCalledWith(testAbi, testContractAddress, "totalSupply", nil, &expected, blockNumber)
|
2018-05-05 20:25:54 +00:00
|
|
|
})
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
It("fetches dai token's total supply at the given block height", func() {
|
|
|
|
infuraIPC := "https://mainnet.infura.io/v3/b09888c1113640cc9ab42750ce750c05"
|
2018-07-20 16:37:46 +00:00
|
|
|
rawRpcClient, err := rpc.Dial(infuraIPC)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
|
|
|
|
ethClient := ethclient.NewClient(rawRpcClient)
|
|
|
|
blockChainClient := client.NewEthClient(ethClient)
|
|
|
|
node := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
|
|
|
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
|
|
|
|
realFetcher := every_block.NewFetcher(blockChain)
|
2018-08-09 16:58:06 +00:00
|
|
|
result, err := realFetcher.FetchBigInt("totalSupply", constants.DaiAbiString, constants.DaiContractAddress, blockNumber, nil)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
expectedResult := big.Int{}
|
|
|
|
expectedResult.SetString("27647235749155415536952630", 10)
|
|
|
|
Expect(result).To(Equal(expectedResult))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if the call to the blockchain fails", func() {
|
2018-07-20 16:37:46 +00:00
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetFetchContractDataErr(fakes.FakeError)
|
|
|
|
errorFetcher := every_block.NewFetcher(blockChain)
|
2018-08-09 16:58:06 +00:00
|
|
|
result, err := errorFetcher.FetchBigInt("totalSupply", "", "", 0, nil)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
Expect(result.String()).To(Equal("0"))
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("totalSupply"))
|
2018-07-20 16:37:46 +00:00
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
2018-05-05 20:25:54 +00:00
|
|
|
})
|
|
|
|
})
|
2018-08-09 17:23:42 +00:00
|
|
|
|
|
|
|
Describe("stopped", func() {
|
|
|
|
It("checks whether or not the contract has been stopped", func() {
|
|
|
|
fakeBlockChain := fakes.NewMockBlockChain()
|
|
|
|
testFetcher := every_block.NewFetcher(fakeBlockChain)
|
|
|
|
testAbi := "testAbi"
|
|
|
|
testContractAddress := "testContractAddress"
|
|
|
|
|
|
|
|
_, err := testFetcher.FetchBool("stopped", testAbi, testContractAddress, blockNumber, nil)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
var expectedResult bool
|
|
|
|
expected := &expectedResult
|
|
|
|
fakeBlockChain.AssertFetchContractDataCalledWith(testAbi, testContractAddress, "stopped", nil, &expected, blockNumber)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("fetches dai token's stopped status at the given block height", func() {
|
|
|
|
infuraIPC := "https://mainnet.infura.io/v3/b09888c1113640cc9ab42750ce750c05"
|
|
|
|
rawRpcClient, err := rpc.Dial(infuraIPC)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
|
|
|
|
ethClient := ethclient.NewClient(rawRpcClient)
|
|
|
|
blockChainClient := client.NewEthClient(ethClient)
|
|
|
|
node := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
|
|
|
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
|
|
|
|
realFetcher := every_block.NewFetcher(blockChain)
|
|
|
|
result, err := realFetcher.FetchBool("stopped", constants.DaiAbiString, constants.DaiContractAddress, blockNumber, nil)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(result).To(Equal(false))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if the call to the blockchain fails", func() {
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetFetchContractDataErr(fakes.FakeError)
|
|
|
|
errorFetcher := every_block.NewFetcher(blockChain)
|
|
|
|
result, err := errorFetcher.FetchBool("stopped", "", "", 0, nil)
|
|
|
|
|
|
|
|
Expect(result).To(Equal(false))
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("stopped"))
|
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("owner", func() {
|
|
|
|
It("checks what the contract's owner address is", func() {
|
|
|
|
fakeBlockChain := fakes.NewMockBlockChain()
|
|
|
|
testFetcher := every_block.NewFetcher(fakeBlockChain)
|
|
|
|
testAbi := "testAbi"
|
|
|
|
testContractAddress := "testContractAddress"
|
|
|
|
|
|
|
|
_, err := testFetcher.FetchAddress("owner", testAbi, testContractAddress, blockNumber, nil)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
var expectedResult common.Address
|
|
|
|
expected := &expectedResult
|
|
|
|
fakeBlockChain.AssertFetchContractDataCalledWith(testAbi, testContractAddress, "owner", nil, &expected, blockNumber)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("fetches dai token's owner address at the given block height", func() {
|
|
|
|
infuraIPC := "https://mainnet.infura.io/v3/b09888c1113640cc9ab42750ce750c05"
|
|
|
|
rawRpcClient, err := rpc.Dial(infuraIPC)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
rpcClient := client.NewRpcClient(rawRpcClient, infuraIPC)
|
|
|
|
ethClient := ethclient.NewClient(rawRpcClient)
|
|
|
|
blockChainClient := client.NewEthClient(ethClient)
|
|
|
|
node := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := rpc2.NewRpcTransactionConverter(ethClient)
|
|
|
|
blockChain := geth.NewBlockChain(blockChainClient, node, transactionConverter)
|
|
|
|
realFetcher := every_block.NewFetcher(blockChain)
|
|
|
|
result, err := realFetcher.FetchAddress("owner", constants.DaiAbiString, constants.DaiContractAddress, blockNumber, nil)
|
|
|
|
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
expectedResult := common.HexToAddress("0x0000000000000000000000000000000000000000")
|
|
|
|
Expect(result).To(Equal(expectedResult))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("returns an error if the call to the blockchain fails", func() {
|
|
|
|
blockChain := fakes.NewMockBlockChain()
|
|
|
|
blockChain.SetFetchContractDataErr(fakes.FakeError)
|
|
|
|
errorFetcher := every_block.NewFetcher(blockChain)
|
|
|
|
result, err := errorFetcher.FetchAddress("owner", "", "", 0, nil)
|
|
|
|
|
|
|
|
expectedResult := new(common.Address)
|
|
|
|
Expect(result).To(Equal(*expectedResult))
|
|
|
|
Expect(err).To(HaveOccurred())
|
|
|
|
Expect(err.Error()).To(ContainSubstring("owner"))
|
|
|
|
Expect(err.Error()).To(ContainSubstring(fakes.FakeError.Error()))
|
|
|
|
})
|
|
|
|
})
|
2018-08-09 16:58:06 +00:00
|
|
|
})
|