Merge branch 'staging' into VDB-125-aggregate-log-fetching
This commit is contained in:
@@ -15,13 +15,12 @@
|
||||
package debt_ceiling
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
|
||||
"math/big"
|
||||
|
||||
"bytes"
|
||||
"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"
|
||||
)
|
||||
|
||||
type PitFileDebtCeilingConverter struct{}
|
||||
@@ -34,7 +33,7 @@ func (PitFileDebtCeilingConverter) ToModels(ethLogs []types.Log) ([]interface{},
|
||||
return nil, err
|
||||
}
|
||||
what := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00"))
|
||||
data := big.NewInt(0).SetBytes(ethLog.Topics[3].Bytes()).String()
|
||||
data := shared.ConvertToWad(ethLog.Topics[3].Big().String())
|
||||
|
||||
raw, err := json.Marshal(ethLog)
|
||||
if err != nil {
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
|
||||
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared/constants"
|
||||
)
|
||||
|
||||
@@ -36,8 +37,11 @@ func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error)
|
||||
}
|
||||
ilk := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00"))
|
||||
what := string(bytes.Trim(ethLog.Topics[3].Bytes(), "\x00"))
|
||||
riskBytes := ethLog.Data[len(ethLog.Data)-constants.DataItemLength:]
|
||||
risk := big.NewInt(0).SetBytes(riskBytes).String()
|
||||
dataBytes := ethLog.Data[len(ethLog.Data)-constants.DataItemLength:]
|
||||
data, err := getData(dataBytes, what)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
raw, err := json.Marshal(ethLog)
|
||||
if err != nil {
|
||||
@@ -46,7 +50,7 @@ func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error)
|
||||
model := PitFileIlkModel{
|
||||
Ilk: ilk,
|
||||
What: what,
|
||||
Data: risk,
|
||||
Data: data,
|
||||
LogIndex: ethLog.Index,
|
||||
TransactionIndex: ethLog.TxIndex,
|
||||
Raw: raw,
|
||||
@@ -56,6 +60,17 @@ func (PitFileIlkConverter) ToModels(ethLogs []types.Log) ([]interface{}, error)
|
||||
return models, nil
|
||||
}
|
||||
|
||||
func getData(dataBytes []byte, what string) (string, error) {
|
||||
n := big.NewInt(0).SetBytes(dataBytes).String()
|
||||
if what == "spot" {
|
||||
return shared.ConvertToRay(n), nil
|
||||
} else if what == "line" {
|
||||
return shared.ConvertToWad(n), nil
|
||||
} else {
|
||||
return "", errors.New("unexpected payload for 'what'")
|
||||
}
|
||||
}
|
||||
|
||||
func verifyLog(log types.Log) error {
|
||||
if len(log.Topics) < 4 {
|
||||
return errors.New("log missing topics")
|
||||
|
||||
@@ -47,13 +47,49 @@ var _ = Describe("Pit file ilk converter", func() {
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
It("converts a log to an model", func() {
|
||||
It("returns error if 'what' field is unknown", func() {
|
||||
log := types.Log{
|
||||
Address: test_data.EthPitFileIlkLineLog.Address,
|
||||
Topics: []common.Hash{
|
||||
test_data.EthPitFileIlkLineLog.Topics[0],
|
||||
test_data.EthPitFileIlkLineLog.Topics[1],
|
||||
test_data.EthPitFileIlkLineLog.Topics[2],
|
||||
common.HexToHash("0x1111111100000000000000000000000000000000000000000000000000000000"),
|
||||
},
|
||||
Data: test_data.EthPitFileIlkLineLog.Data,
|
||||
BlockNumber: test_data.EthPitFileIlkLineLog.BlockNumber,
|
||||
TxHash: test_data.EthPitFileIlkLineLog.TxHash,
|
||||
TxIndex: test_data.EthPitFileIlkLineLog.TxIndex,
|
||||
BlockHash: test_data.EthPitFileIlkLineLog.BlockHash,
|
||||
Index: test_data.EthPitFileIlkLineLog.Index,
|
||||
}
|
||||
converter := ilk.PitFileIlkConverter{}
|
||||
|
||||
models, err := converter.ToModels([]types.Log{test_data.EthPitFileIlkLog})
|
||||
_, err := converter.ToModels([]types.Log{log})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(models)).To(Equal(1))
|
||||
Expect(models[0].(ilk.PitFileIlkModel)).To(Equal(test_data.PitFileIlkModel))
|
||||
Expect(err).To(HaveOccurred())
|
||||
})
|
||||
|
||||
Describe("when log is valid", func() {
|
||||
It("converts to model with data converted to ray when what is 'spot'", func() {
|
||||
converter := ilk.PitFileIlkConverter{}
|
||||
|
||||
models, err := converter.ToModels([]types.Log{test_data.EthPitFileIlkSpotLog})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(models)).To(Equal(1))
|
||||
Expect(models[0].(ilk.PitFileIlkModel)).To(Equal(test_data.PitFileIlkSpotModel))
|
||||
})
|
||||
|
||||
It("converts to model with data converted to wad when what is 'line'", func() {
|
||||
converter := ilk.PitFileIlkConverter{}
|
||||
|
||||
models, err := converter.ToModels([]types.Log{test_data.EthPitFileIlkLineLog})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(len(models)).To(Equal(1))
|
||||
Expect(models[0].(ilk.PitFileIlkModel)).To(Equal(test_data.PitFileIlkLineModel))
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
@@ -44,12 +44,12 @@ var _ = Describe("Pit file ilk repository", func() {
|
||||
})
|
||||
|
||||
Describe("Create", func() {
|
||||
modelWithDifferentLogIdx := test_data.PitFileIlkModel
|
||||
modelWithDifferentLogIdx := test_data.PitFileIlkSpotModel
|
||||
modelWithDifferentLogIdx.LogIndex = modelWithDifferentLogIdx.LogIndex + 1
|
||||
inputs := shared_behaviors.CreateBehaviorInputs{
|
||||
CheckedHeaderColumnName: constants.PitFileIlkChecked,
|
||||
LogEventTableName: "maker.pit_file_ilk",
|
||||
TestModel: test_data.PitFileIlkModel,
|
||||
TestModel: test_data.PitFileIlkSpotModel,
|
||||
ModelWithDifferentLogIdx: modelWithDifferentLogIdx,
|
||||
Repository: &pitFileIlkRepository,
|
||||
}
|
||||
@@ -59,18 +59,18 @@ var _ = Describe("Pit file ilk repository", func() {
|
||||
It("adds a pit file ilk event", func() {
|
||||
headerID, err := headerRepository.CreateOrUpdateHeader(fakes.FakeHeader)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
err = pitFileIlkRepository.Create(headerID, []interface{}{test_data.PitFileIlkModel})
|
||||
err = pitFileIlkRepository.Create(headerID, []interface{}{test_data.PitFileIlkSpotModel})
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
var dbPitFile ilk.PitFileIlkModel
|
||||
err = db.Get(&dbPitFile, `SELECT ilk, what, data, log_idx, tx_idx, raw_log FROM maker.pit_file_ilk WHERE header_id = $1`, headerID)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(dbPitFile.Ilk).To(Equal(test_data.PitFileIlkModel.Ilk))
|
||||
Expect(dbPitFile.What).To(Equal(test_data.PitFileIlkModel.What))
|
||||
Expect(dbPitFile.Data).To(Equal(test_data.PitFileIlkModel.Data))
|
||||
Expect(dbPitFile.LogIndex).To(Equal(test_data.PitFileIlkModel.LogIndex))
|
||||
Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileIlkModel.TransactionIndex))
|
||||
Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileIlkModel.Raw))
|
||||
Expect(dbPitFile.Ilk).To(Equal(test_data.PitFileIlkSpotModel.Ilk))
|
||||
Expect(dbPitFile.What).To(Equal(test_data.PitFileIlkSpotModel.What))
|
||||
Expect(dbPitFile.Data).To(Equal(test_data.PitFileIlkSpotModel.Data))
|
||||
Expect(dbPitFile.LogIndex).To(Equal(test_data.PitFileIlkSpotModel.LogIndex))
|
||||
Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileIlkSpotModel.TransactionIndex))
|
||||
Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileIlkSpotModel.Raw))
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user