forked from cerc-io/ipld-eth-server
Vdb 67 convert numbers to ray and wad (#105)
* Add method to convert values to ray or wad units * Convert data to ray or wad for cat_file_chop_lump * Use shared convert functions in price feed conversion * Pull common ray/wad values into vars * Fix after rebase with staging
This commit is contained in:
@@ -19,10 +19,16 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
var (
|
||||
chop = "chop"
|
||||
lump = "lump"
|
||||
)
|
||||
|
||||
type CatFileChopLumpConverter struct{}
|
||||
|
||||
func (CatFileChopLumpConverter) ToModels(ethLogs []types.Log) ([]interface{}, error) {
|
||||
@@ -44,7 +50,7 @@ func (CatFileChopLumpConverter) ToModels(ethLogs []types.Log) ([]interface{}, er
|
||||
result := CatFileChopLumpModel{
|
||||
Ilk: ilk,
|
||||
What: what,
|
||||
Data: data,
|
||||
Data: convertData(what, data),
|
||||
TransactionIndex: ethLog.TxIndex,
|
||||
LogIndex: ethLog.Index,
|
||||
Raw: raw,
|
||||
@@ -54,6 +60,17 @@ func (CatFileChopLumpConverter) ToModels(ethLogs []types.Log) ([]interface{}, er
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func convertData(what, data string) string {
|
||||
var convertedData string
|
||||
if what == chop {
|
||||
convertedData = shared.ConvertToRay(data)
|
||||
} else if what == lump {
|
||||
convertedData = shared.ConvertToWad(data)
|
||||
}
|
||||
|
||||
return convertedData
|
||||
}
|
||||
|
||||
func verifyLog(log types.Log) error {
|
||||
if len(log.Topics) < 4 {
|
||||
return errors.New("log missing topics")
|
||||
|
||||
@@ -31,6 +31,24 @@ var _ = Describe("Cat file chop lump converter", func() {
|
||||
converter = chop_lump.CatFileChopLumpConverter{}
|
||||
})
|
||||
|
||||
Context("chop events", func() {
|
||||
It("converts a chop log to a model", func() {
|
||||
models, err := converter.ToModels([]types.Log{test_data.EthCatFileChopLog})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(models).To(Equal([]interface{}{test_data.CatFileChopModel}))
|
||||
})
|
||||
})
|
||||
|
||||
Context("lump events", func() {
|
||||
It("converts a lump log to a model", func() {
|
||||
models, err := converter.ToModels([]types.Log{test_data.EthCatFileLumpLog})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(models).To(Equal([]interface{}{test_data.CatFileLumpModel}))
|
||||
})
|
||||
})
|
||||
|
||||
It("returns err if log is missing topics", func() {
|
||||
badLog := types.Log{
|
||||
Data: []byte{1, 1, 1, 1, 1},
|
||||
@@ -48,11 +66,4 @@ var _ = Describe("Cat file chop lump converter", func() {
|
||||
_, err := converter.ToModels([]types.Log{badLog})
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("converts a log to an model", func() {
|
||||
models, err := converter.ToModels([]types.Log{test_data.EthCatFileChopLumpLog})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(models).To(Equal([]interface{}{test_data.CatFileChopLumpModel}))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -41,34 +41,52 @@ var _ = Describe("Cat file chop lump repository", func() {
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
modelWithDifferentLogIdx := test_data.CatFileChopLumpModel
|
||||
modelWithDifferentLogIdx := test_data.CatFileChopModel
|
||||
modelWithDifferentLogIdx.LogIndex++
|
||||
inputs := shared_behaviors.CreateBehaviorInputs{
|
||||
CheckedHeaderColumnName: constants.CatFileChopLumpChecked,
|
||||
LogEventTableName: "maker.cat_file_chop_lump",
|
||||
TestModel: test_data.CatFileChopLumpModel,
|
||||
TestModel: test_data.CatFileChopModel,
|
||||
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
|
||||
Repository: &catFileRepository,
|
||||
}
|
||||
|
||||
shared_behaviors.SharedRepositoryCreateBehaviors(&inputs)
|
||||
|
||||
It("adds a cat file chop lump event", func() {
|
||||
It("adds a cat file chop event", func() {
|
||||
headerRepository := repositories.NewHeaderRepository(db)
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopLumpModel})
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileChopModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult chop_lump.CatFileChopLumpModel
|
||||
err = db.Get(&dbResult, `SELECT ilk, what, data, tx_idx, log_idx, raw_log FROM maker.cat_file_chop_lump WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(dbResult.Ilk).To(Equal(test_data.CatFileChopLumpModel.Ilk))
|
||||
Expect(dbResult.What).To(Equal(test_data.CatFileChopLumpModel.What))
|
||||
Expect(dbResult.Data).To(Equal(test_data.CatFileChopLumpModel.Data))
|
||||
Expect(dbResult.TransactionIndex).To(Equal(test_data.CatFileChopLumpModel.TransactionIndex))
|
||||
Expect(dbResult.LogIndex).To(Equal(test_data.CatFileChopLumpModel.LogIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.CatFileChopLumpModel.Raw))
|
||||
Expect(dbResult.Ilk).To(Equal(test_data.CatFileChopModel.Ilk))
|
||||
Expect(dbResult.What).To(Equal(test_data.CatFileChopModel.What))
|
||||
Expect(dbResult.Data).To(Equal(test_data.CatFileChopModel.Data))
|
||||
Expect(dbResult.TransactionIndex).To(Equal(test_data.CatFileChopModel.TransactionIndex))
|
||||
Expect(dbResult.LogIndex).To(Equal(test_data.CatFileChopModel.LogIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.CatFileChopModel.Raw))
|
||||
})
|
||||
|
||||
It("adds a cat file lump event", func() {
|
||||
headerRepository := repositories.NewHeaderRepository(db)
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = catFileRepository.Create(headerID, []interface{}{test_data.CatFileLumpModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbResult chop_lump.CatFileChopLumpModel
|
||||
err = db.Get(&dbResult, `SELECT ilk, what, data, tx_idx, log_idx, raw_log FROM maker.cat_file_chop_lump WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(dbResult.Ilk).To(Equal(test_data.CatFileLumpModel.Ilk))
|
||||
Expect(dbResult.What).To(Equal(test_data.CatFileLumpModel.What))
|
||||
Expect(dbResult.Data).To(Equal(test_data.CatFileLumpModel.Data))
|
||||
Expect(dbResult.TransactionIndex).To(Equal(test_data.CatFileLumpModel.TransactionIndex))
|
||||
Expect(dbResult.LogIndex).To(Equal(test_data.CatFileLumpModel.LogIndex))
|
||||
Expect(dbResult.Raw).To(MatchJSON(test_data.CatFileLumpModel.Raw))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user