package price_feeds_test import ( "context" "encoding/json" "math/big" "time" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/rpc" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/pkg/geth" "github.com/vulcanize/vulcanizedb/pkg/geth/client" rpc2 "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc" "github.com/vulcanize/vulcanizedb/pkg/geth/node" "github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds" "github.com/vulcanize/vulcanizedb/test_config" ) var _ = Describe("Price feeds transformer", func() { var ( db *postgres.DB blockChain core.BlockChain ) BeforeEach(func() { ipc := "https://kovan.infura.io/J5Vd2fRtGsw0zZ0Ov3BL" rpcClient, ethClient, err := getClients(ipc) Expect(err).NotTo(HaveOccurred()) blockChain, err = getBlockChain(rpcClient, ethClient) Expect(err).NotTo(HaveOccurred()) db = test_config.NewTestDB(blockChain.Node()) test_config.CleanTestDB(db) for i := 8763054; i < 8763063; i++ { err = persistHeader(rpcClient, db, int64(i)) Expect(err).NotTo(HaveOccurred()) } }) It("persists a ETH/USD price feed event", func() { config := price_feeds.IPriceFeedConfig{ ContractAddresses: []string{"0x9FfFE440258B79c5d6604001674A4722FfC0f7Bc"}, StartingBlockNumber: 8763054, EndingBlockNumber: 8763054, } transformerInitializer := price_feeds.PriceFeedTransformerInitializer{Config: config} transformer := transformerInitializer.NewPriceFeedTransformer(db, blockChain) err := transformer.Execute() time.AfterFunc(5*time.Second, func() { defer GinkgoRecover() Expect(err).NotTo(HaveOccurred()) var model price_feeds.PriceFeedModel err = db.Get(&model, `SELECT block_number, medianizer_address, usd_value, tx_idx, raw_log FROM maker.price_feeds WHERE block_number = $1`, config.StartingBlockNumber) Expect(err).NotTo(HaveOccurred()) Expect(model.UsdValue).To(Equal("207.314891143")) }) }) It("persists a MKR/USD price feed event", func() { config := price_feeds.IPriceFeedConfig{ ContractAddresses: []string{"0xB1997239Cfc3d15578A3a09730f7f84A90BB4975"}, StartingBlockNumber: 8763059, EndingBlockNumber: 8763059, } transformerInitializer := price_feeds.PriceFeedTransformerInitializer{Config: config} transformer := transformerInitializer.NewPriceFeedTransformer(db, blockChain) err := transformer.Execute() time.AfterFunc(5*time.Second, func() { defer GinkgoRecover() Expect(err).NotTo(HaveOccurred()) var model price_feeds.PriceFeedModel err = db.Get(&model, `SELECT block_number, medianizer_address, usd_value, tx_idx, raw_log FROM maker.price_feeds WHERE block_number = $1`, config.StartingBlockNumber) Expect(err).NotTo(HaveOccurred()) Expect(model.UsdValue).To(Equal("391.803979212")) }) }) It("persists a REP/USD price feed event", func() { config := price_feeds.IPriceFeedConfig{ ContractAddresses: []string{"0xf88bBDc1E2718F8857F30A180076ec38d53cf296"}, StartingBlockNumber: 8763062, EndingBlockNumber: 8763062, } transformerInitializer := price_feeds.PriceFeedTransformerInitializer{Config: config} transformer := transformerInitializer.NewPriceFeedTransformer(db, blockChain) err := transformer.Execute() time.AfterFunc(5*time.Second, func() { defer GinkgoRecover() Expect(err).NotTo(HaveOccurred()) var model price_feeds.PriceFeedModel err = db.Get(&model, `SELECT block_number, medianizer_address, usd_value, tx_idx, raw_log FROM maker.price_feeds WHERE block_number = $1`, config.StartingBlockNumber) Expect(err).NotTo(HaveOccurred()) Expect(model.UsdValue).To(Equal("12.8169284827")) }) }) }) func getClients(ipc string) (client.RpcClient, *ethclient.Client, error) { raw, err := rpc.Dial(ipc) if err != nil { return client.RpcClient{}, ðclient.Client{}, err } return client.NewRpcClient(raw, ipc), ethclient.NewClient(raw), nil } func getBlockChain(rpcClient client.RpcClient, ethClient *ethclient.Client) (core.BlockChain, error) { client := client.NewEthClient(ethClient) node := node.MakeNode(rpcClient) transactionConverter := rpc2.NewRpcTransactionConverter(client) blockChain := geth.NewBlockChain(client, rpcClient, node, transactionConverter) return blockChain, nil } func persistHeader(rpcClient client.RpcClient, db *postgres.DB, blockNumber int64) error { var poaHeader core.POAHeader blockNumberArg := hexutil.EncodeBig(big.NewInt(int64(blockNumber))) err := rpcClient.CallContext(context.Background(), &poaHeader, "eth_getBlockByNumber", blockNumberArg, false) if err != nil { return err } rawHeader, err := json.Marshal(poaHeader) if err != nil { return err } headerRepository := repositories.NewHeaderRepository(db) _, err = headerRepository.CreateOrUpdateHeader(core.Header{ BlockNumber: poaHeader.Number.ToInt().Int64(), Hash: poaHeader.Hash.String(), Raw: rawHeader, Timestamp: poaHeader.Time.ToInt().String(), }) return err }