2018-09-18 15:55:46 +00:00
|
|
|
package price_feeds_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-10-11 20:25:14 +00:00
|
|
|
"encoding/json"
|
2018-09-28 12:19:40 +00:00
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
2018-09-18 15:55:46 +00:00
|
|
|
"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"
|
2018-10-11 20:25:14 +00:00
|
|
|
|
2018-09-18 15:55:46 +00:00
|
|
|
"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()
|
|
|
|
|
2018-09-28 12:19:40 +00:00
|
|
|
time.AfterFunc(5*time.Second, func() {
|
|
|
|
defer GinkgoRecover()
|
2018-09-21 15:51:35 +00:00
|
|
|
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"))
|
|
|
|
})
|
2018-09-18 15:55:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2018-09-28 12:19:40 +00:00
|
|
|
time.AfterFunc(5*time.Second, func() {
|
|
|
|
defer GinkgoRecover()
|
2018-09-21 15:51:35 +00:00
|
|
|
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"))
|
|
|
|
})
|
2018-09-18 15:55:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2018-09-28 12:19:40 +00:00
|
|
|
time.AfterFunc(5*time.Second, func() {
|
|
|
|
defer GinkgoRecover()
|
2018-09-21 15:51:35 +00:00
|
|
|
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"))
|
|
|
|
})
|
2018-09-18 15:55:46 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
2018-09-18 19:46:16 +00:00
|
|
|
blockChain := geth.NewBlockChain(client, rpcClient, node, transactionConverter)
|
2018-09-18 15:55:46 +00:00
|
|
|
return blockChain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func persistHeader(rpcClient client.RpcClient, db *postgres.DB, blockNumber int64) error {
|
2018-09-18 19:46:16 +00:00
|
|
|
var poaHeader core.POAHeader
|
2018-09-18 15:55:46 +00:00
|
|
|
blockNumberArg := hexutil.EncodeBig(big.NewInt(int64(blockNumber)))
|
|
|
|
err := rpcClient.CallContext(context.Background(), &poaHeader, "eth_getBlockByNumber", blockNumberArg, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-11 18:21:49 +00:00
|
|
|
rawHeader, err := json.Marshal(poaHeader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-09-18 15:55:46 +00:00
|
|
|
headerRepository := repositories.NewHeaderRepository(db)
|
|
|
|
_, err = headerRepository.CreateOrUpdateHeader(core.Header{
|
|
|
|
BlockNumber: poaHeader.Number.ToInt().Int64(),
|
|
|
|
Hash: poaHeader.Hash.String(),
|
2018-10-11 18:21:49 +00:00
|
|
|
Raw: rawHeader,
|
2018-10-11 20:25:14 +00:00
|
|
|
Timestamp: poaHeader.Time.ToInt().String(),
|
2018-09-18 15:55:46 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|