From e1f609528d0ecc2b6bb798e4511b52089ef9384d Mon Sep 17 00:00:00 2001 From: Rob Mulholand Date: Wed, 5 Sep 2018 09:24:28 -0500 Subject: [PATCH] Add Pit file stability fee event - file event on the pit contract is overloaded and each implementation logs a note - existing Pit file transformer now specified as dealing with ilks --- .../1535667935_create_pit_file_table.down.sql | 3 +- .../1535667935_create_pit_file_table.up.sql | 16 +- db/schema.sql | 101 +++++++++-- pkg/transformers/pit_file/config.go | 2 +- pkg/transformers/pit_file/converter_test.go | 20 --- .../debt_ceiling/debt_ceiling_suite_test.go | 13 ++ .../pit_file/{ => ilk}/converter.go | 13 +- .../pit_file/ilk/converter_test.go | 20 +++ .../ilk_suite_test.go} | 6 +- pkg/transformers/pit_file/{ => ilk}/model.go | 6 +- .../pit_file/{ => ilk}/repository.go | 20 +-- .../pit_file/ilk/repository_test.go | 129 ++++++++++++++ .../pit_file/{ => ilk}/transformer.go | 25 +-- .../pit_file/{ => ilk}/transformer_test.go | 70 ++++---- .../pit_file/stability_fee/converter.go | 27 +++ .../pit_file/stability_fee/converter_test.go | 20 +++ .../pit_file/stability_fee/model.go | 8 + .../pit_file/stability_fee/repository.go | 48 ++++++ .../{ => stability_fee}/repository_test.go | 51 +++--- .../stability_fee/stability_fee_suite_test.go | 13 ++ .../pit_file/stability_fee/transformer.go | 57 ++++++ .../stability_fee/transformer_test.go | 163 ++++++++++++++++++ pkg/transformers/shared/constants.go | 30 ++-- .../mocks/pit_file/{ => ilk}/converter.go | 12 +- .../mocks/pit_file/ilk/repository.go | 40 +++++ .../test_data/mocks/pit_file/repository.go | 42 ----- .../mocks/pit_file/stability_fee/converter.go | 25 +++ .../pit_file/stability_fee/repository.go | 38 ++++ pkg/transformers/test_data/pit_file.go | 40 ++++- pkg/transformers/transformers.go | 8 +- test_config/test_config.go | 3 +- 31 files changed, 858 insertions(+), 211 deletions(-) delete mode 100644 pkg/transformers/pit_file/converter_test.go create mode 100644 pkg/transformers/pit_file/debt_ceiling/debt_ceiling_suite_test.go rename pkg/transformers/pit_file/{ => ilk}/converter.go (69%) create mode 100644 pkg/transformers/pit_file/ilk/converter_test.go rename pkg/transformers/pit_file/{pit_file_suite_test.go => ilk/ilk_suite_test.go} (56%) rename pkg/transformers/pit_file/{ => ilk}/model.go (65%) rename pkg/transformers/pit_file/{ => ilk}/repository.go (54%) create mode 100644 pkg/transformers/pit_file/ilk/repository_test.go rename pkg/transformers/pit_file/{ => ilk}/transformer.go (52%) rename pkg/transformers/pit_file/{ => ilk}/transformer_test.go (65%) create mode 100644 pkg/transformers/pit_file/stability_fee/converter.go create mode 100644 pkg/transformers/pit_file/stability_fee/converter_test.go create mode 100644 pkg/transformers/pit_file/stability_fee/model.go create mode 100644 pkg/transformers/pit_file/stability_fee/repository.go rename pkg/transformers/pit_file/{ => stability_fee}/repository_test.go (69%) create mode 100644 pkg/transformers/pit_file/stability_fee/stability_fee_suite_test.go create mode 100644 pkg/transformers/pit_file/stability_fee/transformer.go create mode 100644 pkg/transformers/pit_file/stability_fee/transformer_test.go rename pkg/transformers/test_data/mocks/pit_file/{ => ilk}/converter.go (51%) create mode 100644 pkg/transformers/test_data/mocks/pit_file/ilk/repository.go delete mode 100644 pkg/transformers/test_data/mocks/pit_file/repository.go create mode 100644 pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go create mode 100644 pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go diff --git a/db/migrations/1535667935_create_pit_file_table.down.sql b/db/migrations/1535667935_create_pit_file_table.down.sql index 4f92d89f..0b8b947d 100644 --- a/db/migrations/1535667935_create_pit_file_table.down.sql +++ b/db/migrations/1535667935_create_pit_file_table.down.sql @@ -1 +1,2 @@ -DROP TABLE maker.pit_file; \ No newline at end of file +DROP TABLE maker.pit_file_ilk; +DROP TABLE maker.pit_file_stability_fee; \ No newline at end of file diff --git a/db/migrations/1535667935_create_pit_file_table.up.sql b/db/migrations/1535667935_create_pit_file_table.up.sql index ff0312b4..8c0dd5ae 100644 --- a/db/migrations/1535667935_create_pit_file_table.up.sql +++ b/db/migrations/1535667935_create_pit_file_table.up.sql @@ -1,10 +1,20 @@ -CREATE TABLE maker.pit_file ( +CREATE TABLE maker.pit_file_ilk ( id SERIAL PRIMARY KEY, header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, ilk TEXT, what TEXT, - risk NUMERIC, + data NUMERIC, tx_idx INTEGER NOT NUll, raw_log JSONB, UNIQUE (header_id, tx_idx) -); \ No newline at end of file +); + +CREATE TABLE maker.pit_file_stability_fee ( + id SERIAL PRIMARY KEY, + header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE, + what TEXT, + data TEXT, + tx_idx INTEGER NOT NULL, + raw_log JSONB, + UNIQUE (header_id, tx_idx) +) \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index cfa0ed9a..a84f76af 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -157,25 +157,25 @@ ALTER SEQUENCE maker.frob_id_seq OWNED BY maker.frob.id; -- --- Name: pit_file; Type: TABLE; Schema: maker; Owner: - +-- Name: pit_file_ilk; Type: TABLE; Schema: maker; Owner: - -- -CREATE TABLE maker.pit_file ( +CREATE TABLE maker.pit_file_ilk ( id integer NOT NULL, header_id integer NOT NULL, ilk text, what text, - risk numeric, + data numeric, tx_idx integer NOT NULL, raw_log jsonb ); -- --- Name: pit_file_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- Name: pit_file_ilk_id_seq; Type: SEQUENCE; Schema: maker; Owner: - -- -CREATE SEQUENCE maker.pit_file_id_seq +CREATE SEQUENCE maker.pit_file_ilk_id_seq AS integer START WITH 1 INCREMENT BY 1 @@ -185,10 +185,44 @@ CREATE SEQUENCE maker.pit_file_id_seq -- --- Name: pit_file_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- Name: pit_file_ilk_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - -- -ALTER SEQUENCE maker.pit_file_id_seq OWNED BY maker.pit_file.id; +ALTER SEQUENCE maker.pit_file_ilk_id_seq OWNED BY maker.pit_file_ilk.id; + + +-- +-- Name: pit_file_stability_fee; Type: TABLE; Schema: maker; Owner: - +-- + +CREATE TABLE maker.pit_file_stability_fee ( + id integer NOT NULL, + header_id integer NOT NULL, + what text, + data text, + tx_idx integer NOT NULL, + raw_log jsonb +); + + +-- +-- Name: pit_file_stability_fee_id_seq; Type: SEQUENCE; Schema: maker; Owner: - +-- + +CREATE SEQUENCE maker.pit_file_stability_fee_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: pit_file_stability_fee_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: - +-- + +ALTER SEQUENCE maker.pit_file_stability_fee_id_seq OWNED BY maker.pit_file_stability_fee.id; -- @@ -656,10 +690,17 @@ ALTER TABLE ONLY maker.frob ALTER COLUMN id SET DEFAULT nextval('maker.frob_id_s -- --- Name: pit_file id; Type: DEFAULT; Schema: maker; Owner: - +-- Name: pit_file_ilk id; Type: DEFAULT; Schema: maker; Owner: - -- -ALTER TABLE ONLY maker.pit_file ALTER COLUMN id SET DEFAULT nextval('maker.pit_file_id_seq'::regclass); +ALTER TABLE ONLY maker.pit_file_ilk ALTER COLUMN id SET DEFAULT nextval('maker.pit_file_ilk_id_seq'::regclass); + + +-- +-- Name: pit_file_stability_fee id; Type: DEFAULT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.pit_file_stability_fee ALTER COLUMN id SET DEFAULT nextval('maker.pit_file_stability_fee_id_seq'::regclass); -- @@ -788,19 +829,35 @@ ALTER TABLE ONLY maker.frob -- --- Name: pit_file pit_file_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: pit_file_ilk pit_file_ilk_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - -- -ALTER TABLE ONLY maker.pit_file - ADD CONSTRAINT pit_file_header_id_tx_idx_key UNIQUE (header_id, tx_idx); +ALTER TABLE ONLY maker.pit_file_ilk + ADD CONSTRAINT pit_file_ilk_header_id_tx_idx_key UNIQUE (header_id, tx_idx); -- --- Name: pit_file pit_file_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- Name: pit_file_ilk pit_file_ilk_pkey; Type: CONSTRAINT; Schema: maker; Owner: - -- -ALTER TABLE ONLY maker.pit_file - ADD CONSTRAINT pit_file_pkey PRIMARY KEY (id); +ALTER TABLE ONLY maker.pit_file_ilk + ADD CONSTRAINT pit_file_ilk_pkey PRIMARY KEY (id); + + +-- +-- Name: pit_file_stability_fee pit_file_stability_fee_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.pit_file_stability_fee + ADD CONSTRAINT pit_file_stability_fee_header_id_tx_idx_key UNIQUE (header_id, tx_idx); + + +-- +-- Name: pit_file_stability_fee pit_file_stability_fee_pkey; Type: CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.pit_file_stability_fee + ADD CONSTRAINT pit_file_stability_fee_pkey PRIMARY KEY (id); -- @@ -999,11 +1056,19 @@ ALTER TABLE ONLY maker.price_feeds -- --- Name: pit_file pit_file_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - +-- Name: pit_file_ilk pit_file_ilk_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - -- -ALTER TABLE ONLY maker.pit_file - ADD CONSTRAINT pit_file_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; +ALTER TABLE ONLY maker.pit_file_ilk + ADD CONSTRAINT pit_file_ilk_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; + + +-- +-- Name: pit_file_stability_fee pit_file_stability_fee_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: - +-- + +ALTER TABLE ONLY maker.pit_file_stability_fee + ADD CONSTRAINT pit_file_stability_fee_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE; -- diff --git a/pkg/transformers/pit_file/config.go b/pkg/transformers/pit_file/config.go index 4298910c..bbec9911 100644 --- a/pkg/transformers/pit_file/config.go +++ b/pkg/transformers/pit_file/config.go @@ -7,7 +7,7 @@ import ( var PitFileConfig = shared.TransformerConfig{ ContractAddress: "0xff3f2400f1600f3f493a9a92704a29b96795af1a", // temporary address from Ganache deploy ContractAbi: shared.PitABI, - Topics: []string{shared.PitFileSignatureOne}, + Topics: []string{shared.PitFileIlkSignature, shared.PitFileDebtCeilingSignature, shared.PitFileStabilityFeeSignature}, StartingBlockNumber: 0, EndingBlockNumber: 100, } diff --git a/pkg/transformers/pit_file/converter_test.go b/pkg/transformers/pit_file/converter_test.go deleted file mode 100644 index 30e97fd6..00000000 --- a/pkg/transformers/pit_file/converter_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package pit_file_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" - "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" - "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" -) - -var _ = Describe("Pit file converter", func() { - It("converts a log to an entity", func() { - converter := pit_file.PitFileConverter{} - - model, err := converter.ToModel(test_data.PitAddress, shared.PitABI, test_data.EthPitFileLog) - - Expect(err).NotTo(HaveOccurred()) - Expect(model).To(Equal(test_data.PitFileModel)) - }) -}) diff --git a/pkg/transformers/pit_file/debt_ceiling/debt_ceiling_suite_test.go b/pkg/transformers/pit_file/debt_ceiling/debt_ceiling_suite_test.go new file mode 100644 index 00000000..97af51bc --- /dev/null +++ b/pkg/transformers/pit_file/debt_ceiling/debt_ceiling_suite_test.go @@ -0,0 +1,13 @@ +package debt_ceiling_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestDebtCeiling(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "DebtCeiling Suite") +} diff --git a/pkg/transformers/pit_file/converter.go b/pkg/transformers/pit_file/ilk/converter.go similarity index 69% rename from pkg/transformers/pit_file/converter.go rename to pkg/transformers/pit_file/ilk/converter.go index 0bcc4116..37409cd5 100644 --- a/pkg/transformers/pit_file/converter.go +++ b/pkg/transformers/pit_file/ilk/converter.go @@ -1,4 +1,4 @@ -package pit_file +package ilk import ( "bytes" @@ -8,13 +8,12 @@ import ( ) type Converter interface { - ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileModel, error) + ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileIlkModel, error) } -type PitFileConverter struct { -} +type PitFileIlkConverter struct{} -func (PitFileConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (entity PitFileModel, err error) { +func (PitFileIlkConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (entity PitFileIlkModel, err error) { ilk := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) what := string(bytes.Trim(ethLog.Topics[3].Bytes(), "\x00")) itemByteLength := 32 @@ -22,10 +21,10 @@ func (PitFileConverter) ToModel(contractAddress string, contractAbi string, ethL risk := big.NewInt(0).SetBytes(riskBytes).String() raw, err := json.Marshal(ethLog) - return PitFileModel{ + return PitFileIlkModel{ Ilk: ilk, What: what, - Risk: risk, + Data: risk, TransactionIndex: ethLog.TxIndex, Raw: raw, }, err diff --git a/pkg/transformers/pit_file/ilk/converter_test.go b/pkg/transformers/pit_file/ilk/converter_test.go new file mode 100644 index 00000000..0e2ac687 --- /dev/null +++ b/pkg/transformers/pit_file/ilk/converter_test.go @@ -0,0 +1,20 @@ +package ilk_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +var _ = Describe("Pit file ilk converter", func() { + It("converts a log to an model", func() { + converter := ilk.PitFileIlkConverter{} + + model, err := converter.ToModel(test_data.PitAddress, shared.PitABI, test_data.EthPitFileIlkLog) + + Expect(err).NotTo(HaveOccurred()) + Expect(model).To(Equal(test_data.PitFileIlkModel)) + }) +}) diff --git a/pkg/transformers/pit_file/pit_file_suite_test.go b/pkg/transformers/pit_file/ilk/ilk_suite_test.go similarity index 56% rename from pkg/transformers/pit_file/pit_file_suite_test.go rename to pkg/transformers/pit_file/ilk/ilk_suite_test.go index c992d4c9..18f7f9dc 100644 --- a/pkg/transformers/pit_file/pit_file_suite_test.go +++ b/pkg/transformers/pit_file/ilk/ilk_suite_test.go @@ -1,4 +1,4 @@ -package pit_file_test +package ilk_test import ( "testing" @@ -7,7 +7,7 @@ import ( . "github.com/onsi/gomega" ) -func TestPitFile(t *testing.T) { +func TestIlk(t *testing.T) { RegisterFailHandler(Fail) - RunSpecs(t, "PitFile Suite") + RunSpecs(t, "Ilk Suite") } diff --git a/pkg/transformers/pit_file/model.go b/pkg/transformers/pit_file/ilk/model.go similarity index 65% rename from pkg/transformers/pit_file/model.go rename to pkg/transformers/pit_file/ilk/model.go index d42ba67d..cbf1a470 100644 --- a/pkg/transformers/pit_file/model.go +++ b/pkg/transformers/pit_file/ilk/model.go @@ -1,9 +1,9 @@ -package pit_file +package ilk -type PitFileModel struct { +type PitFileIlkModel struct { Ilk string What string - Risk string + Data string TransactionIndex uint `db:"tx_idx"` Raw []byte `db:"raw_log"` } diff --git a/pkg/transformers/pit_file/repository.go b/pkg/transformers/pit_file/ilk/repository.go similarity index 54% rename from pkg/transformers/pit_file/repository.go rename to pkg/transformers/pit_file/ilk/repository.go index 4184269a..9f65bbe8 100644 --- a/pkg/transformers/pit_file/repository.go +++ b/pkg/transformers/pit_file/ilk/repository.go @@ -1,4 +1,4 @@ -package pit_file +package ilk import ( "github.com/vulcanize/vulcanizedb/pkg/core" @@ -6,35 +6,35 @@ import ( ) type Repository interface { - Create(headerID int64, transactionIndex uint, model PitFileModel) error + Create(headerID int64, model PitFileIlkModel) error MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) } -type PitFileRepository struct { +type PitFileIlkRepository struct { db *postgres.DB } -func NewPitFileRepository(db *postgres.DB) PitFileRepository { - return PitFileRepository{ +func NewPitFileIlkRepository(db *postgres.DB) PitFileIlkRepository { + return PitFileIlkRepository{ db: db, } } -func (repository PitFileRepository) Create(headerID int64, transactionIndex uint, model PitFileModel) error { +func (repository PitFileIlkRepository) Create(headerID int64, model PitFileIlkModel) error { _, err := repository.db.Exec( - `INSERT into maker.pit_file (header_id, ilk, what, risk, tx_idx, raw_log) + `INSERT into maker.pit_file_ilk (header_id, ilk, what, data, tx_idx, raw_log) VALUES($1, $2, $3, $4::NUMERIC, $5, $6)`, - headerID, model.Ilk, model.What, model.Risk, model.TransactionIndex, model.Raw, + headerID, model.Ilk, model.What, model.Data, model.TransactionIndex, model.Raw, ) return err } -func (repository PitFileRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { +func (repository PitFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { var result []core.Header err := repository.db.Select( &result, `SELECT headers.id, headers.block_number FROM headers - LEFT JOIN maker.pit_file on headers.id = header_id + LEFT JOIN maker.pit_file_ilk on headers.id = header_id WHERE header_id ISNULL AND headers.block_number >= $1 AND headers.block_number <= $2 diff --git a/pkg/transformers/pit_file/ilk/repository_test.go b/pkg/transformers/pit_file/ilk/repository_test.go new file mode 100644 index 00000000..63307c5c --- /dev/null +++ b/pkg/transformers/pit_file/ilk/repository_test.go @@ -0,0 +1,129 @@ +package ilk_test + +import ( + "database/sql" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/test_config" +) + +var _ = Describe("Pit file ilk repository", func() { + Describe("Create", func() { + It("adds a pit file ilk event", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) + Expect(err).NotTo(HaveOccurred()) + pitFileRepository := ilk.NewPitFileIlkRepository(db) + + err = pitFileRepository.Create(headerID, test_data.PitFileIlkModel) + + Expect(err).NotTo(HaveOccurred()) + var dbPitFile ilk.PitFileIlkModel + err = db.Get(&dbPitFile, `SELECT ilk, what, data, 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.TransactionIndex).To(Equal(test_data.PitFileIlkModel.TransactionIndex)) + Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileIlkModel.Raw)) + }) + + It("does not duplicate pit file ilk events", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) + Expect(err).NotTo(HaveOccurred()) + pitFileRepository := ilk.NewPitFileIlkRepository(db) + err = pitFileRepository.Create(headerID, test_data.PitFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + err = pitFileRepository.Create(headerID, test_data.PitFileIlkModel) + + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) + }) + + It("removes pit file ilk if corresponding header is deleted", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) + Expect(err).NotTo(HaveOccurred()) + pitFileRepository := ilk.NewPitFileIlkRepository(db) + err = pitFileRepository.Create(headerID, test_data.PitFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) + + Expect(err).NotTo(HaveOccurred()) + var dbPitFile ilk.PitFileIlkModel + err = db.Get(&dbPitFile, `SELECT ilk, what, data, tx_idx, raw_log FROM maker.pit_file_ilk WHERE header_id = $1`, headerID) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(sql.ErrNoRows)) + }) + }) + + Describe("MissingHeaders", func() { + It("returns headers with no associated pit file ilk event", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + headerRepository := repositories.NewHeaderRepository(db) + startingBlockNumber := int64(1) + pitFileBlockNumber := int64(2) + endingBlockNumber := int64(3) + blockNumbers := []int64{startingBlockNumber, pitFileBlockNumber, endingBlockNumber, endingBlockNumber + 1} + var headerIDs []int64 + for _, n := range blockNumbers { + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + headerIDs = append(headerIDs, headerID) + Expect(err).NotTo(HaveOccurred()) + } + pitFileRepository := ilk.NewPitFileIlkRepository(db) + err := pitFileRepository.Create(headerIDs[1], test_data.PitFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + headers, err := pitFileRepository.MissingHeaders(startingBlockNumber, endingBlockNumber) + + Expect(err).NotTo(HaveOccurred()) + Expect(len(headers)).To(Equal(2)) + Expect(headers[0].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber))) + Expect(headers[1].BlockNumber).To(Or(Equal(startingBlockNumber), Equal(endingBlockNumber))) + }) + + It("only returns headers associated with the current node", func() { + db := test_config.NewTestDB(core.Node{}) + test_config.CleanTestDB(db) + blockNumbers := []int64{1, 2, 3} + headerRepository := repositories.NewHeaderRepository(db) + dbTwo := test_config.NewTestDB(core.Node{ID: "second"}) + headerRepositoryTwo := repositories.NewHeaderRepository(dbTwo) + var headerIDs []int64 + for _, n := range blockNumbers { + headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + Expect(err).NotTo(HaveOccurred()) + headerIDs = append(headerIDs, headerID) + _, err = headerRepositoryTwo.CreateOrUpdateHeader(core.Header{BlockNumber: n}) + Expect(err).NotTo(HaveOccurred()) + } + pitFileRepository := ilk.NewPitFileIlkRepository(db) + pitFileRepositoryTwo := ilk.NewPitFileIlkRepository(dbTwo) + err := pitFileRepository.Create(headerIDs[0], test_data.PitFileIlkModel) + Expect(err).NotTo(HaveOccurred()) + + nodeOneMissingHeaders, err := pitFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeOneMissingHeaders)).To(Equal(len(blockNumbers) - 1)) + + nodeTwoMissingHeaders, err := pitFileRepositoryTwo.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) + Expect(err).NotTo(HaveOccurred()) + Expect(len(nodeTwoMissingHeaders)).To(Equal(len(blockNumbers))) + }) + }) +}) diff --git a/pkg/transformers/pit_file/transformer.go b/pkg/transformers/pit_file/ilk/transformer.go similarity index 52% rename from pkg/transformers/pit_file/transformer.go rename to pkg/transformers/pit_file/ilk/transformer.go index 3c91c04d..f1a3a1a8 100644 --- a/pkg/transformers/pit_file/transformer.go +++ b/pkg/transformers/pit_file/ilk/transformer.go @@ -1,28 +1,29 @@ -package pit_file +package ilk import ( "github.com/ethereum/go-ethereum/common" "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" ) -type PitFileTransformer struct { +type PitFileIlkTransformer struct { Config shared.TransformerConfig Converter Converter Fetcher shared.LogFetcher Repository Repository } -type PitFileTransformerInitializer struct { +type PitFileIlkTransformerInitializer struct { Config shared.TransformerConfig } -func (initializer PitFileTransformerInitializer) NewPitFileTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { - converter := PitFileConverter{} +func (initializer PitFileIlkTransformerInitializer) NewPitFileIlkTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { + converter := PitFileIlkConverter{} fetcher := shared.NewFetcher(blockChain) - repository := NewPitFileRepository(db) - return PitFileTransformer{ + repository := NewPitFileIlkRepository(db) + return PitFileIlkTransformer{ Config: initializer.Config, Converter: converter, Fetcher: fetcher, @@ -30,23 +31,23 @@ func (initializer PitFileTransformerInitializer) NewPitFileTransformer(db *postg } } -func (transformer PitFileTransformer) Execute() error { +func (transformer PitFileIlkTransformer) Execute() error { missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) if err != nil { return err } for _, header := range missingHeaders { - topics := [][]common.Hash{{common.HexToHash(shared.PitFileSignatureOne)}} - matchingLogs, err := transformer.Fetcher.FetchLogs(PitFileConfig.ContractAddress, topics, header.BlockNumber) + topics := [][]common.Hash{{common.HexToHash(shared.PitFileIlkSignature)}} + matchingLogs, err := transformer.Fetcher.FetchLogs(pit_file.PitFileConfig.ContractAddress, topics, header.BlockNumber) if err != nil { return err } for _, log := range matchingLogs { - model, err := transformer.Converter.ToModel(PitFileConfig.ContractAddress, shared.PitABI, log) + model, err := transformer.Converter.ToModel(pit_file.PitFileConfig.ContractAddress, shared.PitABI, log) if err != nil { return err } - err = transformer.Repository.Create(header.Id, log.TxIndex, model) + err = transformer.Repository.Create(header.Id, model) if err != nil { return err } diff --git a/pkg/transformers/pit_file/transformer_test.go b/pkg/transformers/pit_file/ilk/transformer_test.go similarity index 65% rename from pkg/transformers/pit_file/transformer_test.go rename to pkg/transformers/pit_file/ilk/transformer_test.go index ea39787d..e4c3d135 100644 --- a/pkg/transformers/pit_file/transformer_test.go +++ b/pkg/transformers/pit_file/ilk/transformer_test.go @@ -1,4 +1,4 @@ -package pit_file_test +package ilk_test import ( "github.com/ethereum/go-ethereum/common" @@ -8,19 +8,20 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" - pit_file_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/pit_file" + pit_file_ilk_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/pit_file/ilk" ) -var _ = Describe("Pit file transformer", func() { +var _ = Describe("Pit file ilk transformer", func() { It("gets missing headers for block numbers specified in config", func() { - repository := &pit_file_mocks.MockPitFileRepository{} - transformer := pit_file.PitFileTransformer{ + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} + transformer := ilk.PitFileIlkTransformer{ Config: pit_file.PitFileConfig, Fetcher: &mocks.MockLogFetcher{}, - Converter: &pit_file_mocks.MockPitFileConverter{}, + Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, Repository: repository, } @@ -32,11 +33,11 @@ var _ = Describe("Pit file transformer", func() { }) It("returns error if repository returns error for missing headers", func() { - repository := &pit_file_mocks.MockPitFileRepository{} + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeadersErr(fakes.FakeError) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: &mocks.MockLogFetcher{}, - Converter: &pit_file_mocks.MockPitFileConverter{}, + Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, Repository: repository, } @@ -48,11 +49,11 @@ var _ = Describe("Pit file transformer", func() { It("fetches logs for missing headers", func() { fetcher := &mocks.MockLogFetcher{} - repository := &pit_file_mocks.MockPitFileRepository{} + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: fetcher, - Converter: &pit_file_mocks.MockPitFileConverter{}, + Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, Repository: repository, } @@ -61,17 +62,17 @@ var _ = Describe("Pit file transformer", func() { Expect(err).NotTo(HaveOccurred()) Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) Expect(fetcher.FetchedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) - Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileSignatureOne)}})) + Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileIlkSignature)}})) }) It("returns error if fetcher returns error", func() { fetcher := &mocks.MockLogFetcher{} fetcher.SetFetcherError(fakes.FakeError) - repository := &pit_file_mocks.MockPitFileRepository{} + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: fetcher, - Converter: &pit_file_mocks.MockPitFileConverter{}, + Converter: &pit_file_ilk_mocks.MockPitFileIlkConverter{}, Repository: repository, } @@ -82,12 +83,12 @@ var _ = Describe("Pit file transformer", func() { }) It("converts matching logs", func() { - converter := &pit_file_mocks.MockPitFileConverter{} + converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} fetcher := &mocks.MockLogFetcher{} - fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileLog}) - repository := &pit_file_mocks.MockPitFileRepository{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: fetcher, Converter: converter, Repository: repository, @@ -98,17 +99,17 @@ var _ = Describe("Pit file transformer", func() { Expect(err).NotTo(HaveOccurred()) Expect(converter.PassedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) Expect(converter.PassedContractABI).To(Equal(pit_file.PitFileConfig.ContractAbi)) - Expect(converter.PassedLog).To(Equal(test_data.EthPitFileLog)) + Expect(converter.PassedLog).To(Equal(test_data.EthPitFileIlkLog)) }) It("returns error if converter returns error", func() { - converter := &pit_file_mocks.MockPitFileConverter{} + converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} converter.SetConverterError(fakes.FakeError) fetcher := &mocks.MockLogFetcher{} - fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileLog}) - repository := &pit_file_mocks.MockPitFileRepository{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: fetcher, Converter: converter, Repository: repository, @@ -121,13 +122,13 @@ var _ = Describe("Pit file transformer", func() { }) It("persists pit file model", func() { - converter := &pit_file_mocks.MockPitFileConverter{} + converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} fetcher := &mocks.MockLogFetcher{} - fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileLog}) - repository := &pit_file_mocks.MockPitFileRepository{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} fakeHeader := core.Header{BlockNumber: 1, Id: 2} repository.SetMissingHeaders([]core.Header{fakeHeader}) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: fetcher, Converter: converter, Repository: repository, @@ -137,18 +138,17 @@ var _ = Describe("Pit file transformer", func() { Expect(err).NotTo(HaveOccurred()) Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) - Expect(repository.PassedTransactionIndex).To(Equal(test_data.EthPitFileLog.TxIndex)) - Expect(repository.PassedModel).To(Equal(test_data.PitFileModel)) + Expect(repository.PassedModel).To(Equal(test_data.PitFileIlkModel)) }) It("returns error if repository returns error for create", func() { - converter := &pit_file_mocks.MockPitFileConverter{} + converter := &pit_file_ilk_mocks.MockPitFileIlkConverter{} fetcher := &mocks.MockLogFetcher{} - fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileLog}) - repository := &pit_file_mocks.MockPitFileRepository{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileIlkLog}) + repository := &pit_file_ilk_mocks.MockPitFileIlkRepository{} repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) repository.SetCreateError(fakes.FakeError) - transformer := pit_file.PitFileTransformer{ + transformer := ilk.PitFileIlkTransformer{ Fetcher: fetcher, Converter: converter, Repository: repository, diff --git a/pkg/transformers/pit_file/stability_fee/converter.go b/pkg/transformers/pit_file/stability_fee/converter.go new file mode 100644 index 00000000..2864ab5c --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/converter.go @@ -0,0 +1,27 @@ +package stability_fee + +import ( + "bytes" + "encoding/json" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" +) + +type Converter interface { + ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileStabilityFeeModel, error) +} + +type PitFileStabilityFeeConverter struct{} + +func (PitFileStabilityFeeConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (PitFileStabilityFeeModel, error) { + what := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) + data := common.HexToAddress(ethLog.Topics[1].String()).Hex() + + raw, err := json.Marshal(ethLog) + return PitFileStabilityFeeModel{ + What: what, + Data: data, + TransactionIndex: ethLog.TxIndex, + Raw: raw, + }, err +} diff --git a/pkg/transformers/pit_file/stability_fee/converter_test.go b/pkg/transformers/pit_file/stability_fee/converter_test.go new file mode 100644 index 00000000..af0e17d4 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/converter_test.go @@ -0,0 +1,20 @@ +package stability_fee_test + +import ( + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +var _ = Describe("Pit file stability fee converter", func() { + It("converts a log to an model", func() { + converter := stability_fee.PitFileStabilityFeeConverter{} + + model, err := converter.ToModel(test_data.PitAddress, shared.PitABI, test_data.EthPitFileStabilityFeeLog) + + Expect(err).NotTo(HaveOccurred()) + Expect(model).To(Equal(test_data.PitFileStabilityFeeModel)) + }) +}) diff --git a/pkg/transformers/pit_file/stability_fee/model.go b/pkg/transformers/pit_file/stability_fee/model.go new file mode 100644 index 00000000..afa061b5 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/model.go @@ -0,0 +1,8 @@ +package stability_fee + +type PitFileStabilityFeeModel struct { + What string + Data string + TransactionIndex uint `db:"tx_idx"` + Raw []byte `db:"raw_log"` +} diff --git a/pkg/transformers/pit_file/stability_fee/repository.go b/pkg/transformers/pit_file/stability_fee/repository.go new file mode 100644 index 00000000..75f41d01 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/repository.go @@ -0,0 +1,48 @@ +package stability_fee + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" +) + +type Repository interface { + Create(headerID int64, model PitFileStabilityFeeModel) error + MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) +} + +type PitFileStabilityFeeRepository struct { + db *postgres.DB +} + +func NewPitFileStabilityFeeRepository(db *postgres.DB) PitFileStabilityFeeRepository { + return PitFileStabilityFeeRepository{ + db: db, + } +} + +func (repository PitFileStabilityFeeRepository) Create(headerID int64, model PitFileStabilityFeeModel) error { + _, err := repository.db.Exec( + `INSERT into maker.pit_file_stability_fee (header_id, what, data, tx_idx, raw_log) + VALUES($1, $2, $3, $4, $5)`, + headerID, model.What, model.Data, model.TransactionIndex, model.Raw, + ) + return err +} + +func (repository PitFileStabilityFeeRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { + var result []core.Header + err := repository.db.Select( + &result, + `SELECT headers.id, headers.block_number FROM headers + LEFT JOIN maker.pit_file_stability_fee on headers.id = header_id + WHERE header_id ISNULL + AND headers.block_number >= $1 + AND headers.block_number <= $2 + AND headers.eth_node_fingerprint = $3`, + startingBlockNumber, + endingBlockNumber, + repository.db.Node.ID, + ) + + return result, err +} diff --git a/pkg/transformers/pit_file/repository_test.go b/pkg/transformers/pit_file/stability_fee/repository_test.go similarity index 69% rename from pkg/transformers/pit_file/repository_test.go rename to pkg/transformers/pit_file/stability_fee/repository_test.go index bb7624ee..14a0a243 100644 --- a/pkg/transformers/pit_file/repository_test.go +++ b/pkg/transformers/pit_file/stability_fee/repository_test.go @@ -1,37 +1,38 @@ -package pit_file_test +package stability_fee_test import ( "database/sql" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" "github.com/vulcanize/vulcanizedb/test_config" ) -var _ = Describe("Pit file repository", func() { +var _ = Describe("", func() { Describe("Create", func() { - It("adds a pit file event", func() { + It("adds a pit file stability fee event", func() { db := test_config.NewTestDB(core.Node{}) test_config.CleanTestDB(db) headerRepository := repositories.NewHeaderRepository(db) headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) Expect(err).NotTo(HaveOccurred()) - pitFileRepository := pit_file.NewPitFileRepository(db) + pitFileRepository := stability_fee.NewPitFileStabilityFeeRepository(db) - err = pitFileRepository.Create(headerID, 123, test_data.PitFileModel) + err = pitFileRepository.Create(headerID, test_data.PitFileStabilityFeeModel) Expect(err).NotTo(HaveOccurred()) - var dbPitFile pit_file.PitFileModel - err = db.Get(&dbPitFile, `SELECT ilk, what, risk, tx_idx, raw_log FROM maker.pit_file WHERE header_id = $1`, headerID) + var dbPitFile stability_fee.PitFileStabilityFeeModel + err = db.Get(&dbPitFile, `SELECT what, data, tx_idx, raw_log FROM maker.pit_file_stability_fee WHERE header_id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) - Expect(dbPitFile.Ilk).To(Equal(test_data.PitFileModel.Ilk)) - Expect(dbPitFile.What).To(Equal(test_data.PitFileModel.What)) - Expect(dbPitFile.Risk).To(Equal(test_data.PitFileModel.Risk)) - Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileModel.TransactionIndex)) - Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileModel.Raw)) + Expect(dbPitFile.What).To(Equal(test_data.PitFileStabilityFeeModel.What)) + Expect(dbPitFile.Data).To(Equal(test_data.PitFileStabilityFeeModel.Data)) + Expect(dbPitFile.TransactionIndex).To(Equal(test_data.PitFileStabilityFeeModel.TransactionIndex)) + Expect(dbPitFile.Raw).To(MatchJSON(test_data.PitFileStabilityFeeModel.Raw)) }) It("does not duplicate pit file events", func() { @@ -40,11 +41,11 @@ var _ = Describe("Pit file repository", func() { headerRepository := repositories.NewHeaderRepository(db) headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) Expect(err).NotTo(HaveOccurred()) - pitFileRepository := pit_file.NewPitFileRepository(db) - err = pitFileRepository.Create(headerID, 123, test_data.PitFileModel) + pitFileRepository := stability_fee.NewPitFileStabilityFeeRepository(db) + err = pitFileRepository.Create(headerID, test_data.PitFileStabilityFeeModel) Expect(err).NotTo(HaveOccurred()) - err = pitFileRepository.Create(headerID, 123, test_data.PitFileModel) + err = pitFileRepository.Create(headerID, test_data.PitFileStabilityFeeModel) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("pq: duplicate key value violates unique constraint")) @@ -56,15 +57,15 @@ var _ = Describe("Pit file repository", func() { headerRepository := repositories.NewHeaderRepository(db) headerID, err := headerRepository.CreateOrUpdateHeader(core.Header{}) Expect(err).NotTo(HaveOccurred()) - pitFileRepository := pit_file.NewPitFileRepository(db) - err = pitFileRepository.Create(headerID, 123, test_data.PitFileModel) + pitFileRepository := stability_fee.NewPitFileStabilityFeeRepository(db) + err = pitFileRepository.Create(headerID, test_data.PitFileStabilityFeeModel) Expect(err).NotTo(HaveOccurred()) _, err = db.Exec(`DELETE FROM headers WHERE id = $1`, headerID) Expect(err).NotTo(HaveOccurred()) - var dbPitFile pit_file.PitFileModel - err = db.Get(&dbPitFile, `SELECT ilk, what, risk, tx_idx, raw_log FROM maker.pit_file WHERE header_id = $1`, headerID) + var dbPitFile stability_fee.PitFileStabilityFeeModel + err = db.Get(&dbPitFile, `SELECT what, data, tx_idx, raw_log FROM maker.pit_file_stability_fee WHERE header_id = $1`, headerID) Expect(err).To(HaveOccurred()) Expect(err).To(MatchError(sql.ErrNoRows)) }) @@ -85,8 +86,8 @@ var _ = Describe("Pit file repository", func() { headerIDs = append(headerIDs, headerID) Expect(err).NotTo(HaveOccurred()) } - pitFileRepository := pit_file.NewPitFileRepository(db) - err := pitFileRepository.Create(headerIDs[1], 123, test_data.PitFileModel) + pitFileRepository := stability_fee.NewPitFileStabilityFeeRepository(db) + err := pitFileRepository.Create(headerIDs[1], test_data.PitFileStabilityFeeModel) Expect(err).NotTo(HaveOccurred()) headers, err := pitFileRepository.MissingHeaders(startingBlockNumber, endingBlockNumber) @@ -112,9 +113,9 @@ var _ = Describe("Pit file repository", func() { _, err = headerRepositoryTwo.CreateOrUpdateHeader(core.Header{BlockNumber: n}) Expect(err).NotTo(HaveOccurred()) } - pitFileRepository := pit_file.NewPitFileRepository(db) - pitFileRepositoryTwo := pit_file.NewPitFileRepository(dbTwo) - err := pitFileRepository.Create(headerIDs[0], 0, test_data.PitFileModel) + pitFileRepository := stability_fee.NewPitFileStabilityFeeRepository(db) + pitFileRepositoryTwo := stability_fee.NewPitFileStabilityFeeRepository(dbTwo) + err := pitFileRepository.Create(headerIDs[0], test_data.PitFileStabilityFeeModel) Expect(err).NotTo(HaveOccurred()) nodeOneMissingHeaders, err := pitFileRepository.MissingHeaders(blockNumbers[0], blockNumbers[len(blockNumbers)-1]) diff --git a/pkg/transformers/pit_file/stability_fee/stability_fee_suite_test.go b/pkg/transformers/pit_file/stability_fee/stability_fee_suite_test.go new file mode 100644 index 00000000..c7d645a9 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/stability_fee_suite_test.go @@ -0,0 +1,13 @@ +package stability_fee_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestStabilityFee(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "StabilityFee Suite") +} diff --git a/pkg/transformers/pit_file/stability_fee/transformer.go b/pkg/transformers/pit_file/stability_fee/transformer.go new file mode 100644 index 00000000..f19ef343 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/transformer.go @@ -0,0 +1,57 @@ +package stability_fee + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" +) + +type PitFileStabilityFeeTransformer struct { + Config shared.TransformerConfig + Converter Converter + Fetcher shared.LogFetcher + Repository Repository +} + +type PitFileStabilityFeeTransformerInitializer struct { + Config shared.TransformerConfig +} + +func (initializer PitFileStabilityFeeTransformerInitializer) NewPitFileStabilityFeeTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer { + converter := PitFileStabilityFeeConverter{} + fetcher := shared.NewFetcher(blockChain) + repository := NewPitFileStabilityFeeRepository(db) + return PitFileStabilityFeeTransformer{ + Config: initializer.Config, + Converter: converter, + Fetcher: fetcher, + Repository: repository, + } +} + +func (transformer PitFileStabilityFeeTransformer) Execute() error { + missingHeaders, err := transformer.Repository.MissingHeaders(transformer.Config.StartingBlockNumber, transformer.Config.EndingBlockNumber) + if err != nil { + return err + } + for _, header := range missingHeaders { + topics := [][]common.Hash{{common.HexToHash(shared.PitFileStabilityFeeSignature)}} + matchingLogs, err := transformer.Fetcher.FetchLogs(pit_file.PitFileConfig.ContractAddress, topics, header.BlockNumber) + if err != nil { + return err + } + for _, log := range matchingLogs { + model, err := transformer.Converter.ToModel(pit_file.PitFileConfig.ContractAddress, shared.PitABI, log) + if err != nil { + return err + } + err = transformer.Repository.Create(header.Id, model) + if err != nil { + return err + } + } + } + return nil +} diff --git a/pkg/transformers/pit_file/stability_fee/transformer_test.go b/pkg/transformers/pit_file/stability_fee/transformer_test.go new file mode 100644 index 00000000..be70b670 --- /dev/null +++ b/pkg/transformers/pit_file/stability_fee/transformer_test.go @@ -0,0 +1,163 @@ +package stability_fee_test + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/fakes" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" + "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks" + stability_fee_mocks "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data/mocks/pit_file/stability_fee" +) + +var _ = Describe("", func() { + It("gets missing headers for block numbers specified in config", func() { + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Config: pit_file.PitFileConfig, + Fetcher: &mocks.MockLogFetcher{}, + Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedStartingBlockNumber).To(Equal(pit_file.PitFileConfig.StartingBlockNumber)) + Expect(repository.PassedEndingBlockNumber).To(Equal(pit_file.PitFileConfig.EndingBlockNumber)) + }) + + It("returns error if repository returns error for missing headers", func() { + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + repository.SetMissingHeadersErr(fakes.FakeError) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: &mocks.MockLogFetcher{}, + Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("fetches logs for missing headers", func() { + fetcher := &mocks.MockLogFetcher{} + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}, {BlockNumber: 2}}) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: fetcher, + Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(fetcher.FetchedBlocks).To(Equal([]int64{1, 2})) + Expect(fetcher.FetchedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) + Expect(fetcher.FetchedTopics).To(Equal([][]common.Hash{{common.HexToHash(shared.PitFileStabilityFeeSignature)}})) + }) + + It("returns error if fetcher returns error", func() { + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetcherError(fakes.FakeError) + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: fetcher, + Converter: &stability_fee_mocks.MockPitFileStabilityFeeConverter{}, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("converts matching logs", func() { + converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(converter.PassedContractAddress).To(Equal(pit_file.PitFileConfig.ContractAddress)) + Expect(converter.PassedContractABI).To(Equal(pit_file.PitFileConfig.ContractAbi)) + Expect(converter.PassedLog).To(Equal(test_data.EthPitFileStabilityFeeLog)) + }) + + It("returns error if converter returns error", func() { + converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} + converter.SetConverterError(fakes.FakeError) + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1}}) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) + + It("persists pit file model", func() { + converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + fakeHeader := core.Header{BlockNumber: 1, Id: 2} + repository.SetMissingHeaders([]core.Header{fakeHeader}) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).NotTo(HaveOccurred()) + Expect(repository.PassedHeaderID).To(Equal(fakeHeader.Id)) + Expect(repository.PassedModel).To(Equal(test_data.PitFileStabilityFeeModel)) + }) + + It("returns error if repository returns error for create", func() { + converter := &stability_fee_mocks.MockPitFileStabilityFeeConverter{} + fetcher := &mocks.MockLogFetcher{} + fetcher.SetFetchedLogs([]types.Log{test_data.EthPitFileStabilityFeeLog}) + repository := &stability_fee_mocks.MockPitFileStabilityFeeRepository{} + repository.SetMissingHeaders([]core.Header{{BlockNumber: 1, Id: 2}}) + repository.SetCreateError(fakes.FakeError) + transformer := stability_fee.PitFileStabilityFeeTransformer{ + Fetcher: fetcher, + Converter: converter, + Repository: repository, + } + + err := transformer.Execute() + + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError(fakes.FakeError)) + }) +}) diff --git a/pkg/transformers/shared/constants.go b/pkg/transformers/shared/constants.go index c417e088..38bfb010 100644 --- a/pkg/transformers/shared/constants.go +++ b/pkg/transformers/shared/constants.go @@ -15,13 +15,13 @@ package shared var ( - biteMethod = "Bite(bytes32,bytes32,uint256,uint256,uint256,uint256,uint256)" - flipKickMethod = "Kick(uint256,uint256,uint256,address,uint48,bytes32,uint256)" - frobMethod = "Frob(bytes32,bytes32,uint256,uint256,int256,int256,uint256)" - pitFileMethodOne = "file(bytes32,bytes32,uint256)" - pitFileMethodThree = "file(bytes32,address)" - pitFileMethodTwo = "file(bytes32,uint256)" - tendMethod = "tend(uint256,uint256,uint256)" + biteMethod = "Bite(bytes32,bytes32,uint256,uint256,uint256,uint256,uint256)" + flipKickMethod = "Kick(uint256,uint256,uint256,address,uint48,bytes32,uint256)" + frobMethod = "Frob(bytes32,bytes32,uint256,uint256,int256,int256,uint256)" + pitFileIlkMethod = "file(bytes32,bytes32,uint256)" + pitFileStabilityFeeMethod = "file(bytes32,address)" + pitFileDebtCeilingMethod = "file(bytes32,uint256)" + tendMethod = "tend(uint256,uint256,uint256)" CatABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"vat\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"vow\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"flips\",\"outputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"lad\",\"type\":\"bytes32\"},{\"name\":\"ink\",\"type\":\"uint256\"},{\"name\":\"tab\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"nflip\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"live\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"wards\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"name\":\"ilks\",\"outputs\":[{\"name\":\"flip\",\"type\":\"address\"},{\"name\":\"chop\",\"type\":\"uint256\"},{\"name\":\"lump\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"pit\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"vat_\",\"type\":\"address\"},{\"name\":\"pit_\",\"type\":\"address\"},{\"name\":\"vow_\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"ilk\",\"type\":\"bytes32\"},{\"indexed\":true,\"name\":\"lad\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"ink\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"art\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"tab\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"flip\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"iArt\",\"type\":\"uint256\"}],\"name\":\"Bite\",\"type\":\"event\"},{\"anonymous\":true,\"inputs\":[{\"indexed\":true,\"name\":\"sig\",\"type\":\"bytes4\"},{\"indexed\":true,\"name\":\"guy\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"foo\",\"type\":\"bytes32\"},{\"indexed\":true,\"name\":\"bar\",\"type\":\"bytes32\"},{\"indexed\":false,\"name\":\"wad\",\"type\":\"uint256\"},{\"indexed\":false,\"name\":\"fax\",\"type\":\"bytes\"}],\"name\":\"LogNote\",\"type\":\"event\"},{\"constant\":false,\"inputs\":[{\"name\":\"guy\",\"type\":\"address\"}],\"name\":\"rely\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"guy\",\"type\":\"address\"}],\"name\":\"deny\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"what\",\"type\":\"bytes32\"},{\"name\":\"data\",\"type\":\"uint256\"}],\"name\":\"file\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"what\",\"type\":\"bytes32\"},{\"name\":\"flip\",\"type\":\"address\"}],\"name\":\"file\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"ilk\",\"type\":\"bytes32\"},{\"name\":\"lad\",\"type\":\"bytes32\"}],\"name\":\"bite\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"n\",\"type\":\"uint256\"},{\"name\":\"wad\",\"type\":\"uint256\"}],\"name\":\"flip\",\"outputs\":[{\"name\":\"id\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" FlipperABI = `[{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"bids","outputs":[{"name":"bid","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"guy","type":"address"},{"name":"tic","type":"uint48"},{"name":"end","type":"uint48"},{"name":"urn","type":"bytes32"},{"name":"gal","type":"address"},{"name":"tab","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x4423c5f1"},{"constant":true,"inputs":[],"name":"ttl","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x4e8b1dd5"},{"constant":true,"inputs":[],"name":"gem","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x7bd2bea7"},{"constant":true,"inputs":[],"name":"beg","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x7d780d82"},{"constant":true,"inputs":[],"name":"tau","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xcfc4af55"},{"constant":true,"inputs":[],"name":"kicks","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xcfdd3302"},{"constant":true,"inputs":[],"name":"dai","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function","signature":"0xf4b9fa75"},{"inputs":[{"name":"dai_","type":"address"},{"name":"gem_","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor","signature":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"id","type":"uint256"},{"indexed":false,"name":"lot","type":"uint256"},{"indexed":false,"name":"bid","type":"uint256"},{"indexed":false,"name":"gal","type":"address"},{"indexed":false,"name":"end","type":"uint48"},{"indexed":true,"name":"urn","type":"bytes32"},{"indexed":false,"name":"tab","type":"uint256"}],"name":"Kick","type":"event","signature":"0xbac86238bdba81d21995024470425ecb370078fa62b7271b90cf28cbd1e3e87e"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event","signature":"0x644843f351d3fba4abcd60109eaff9f54bac8fb8ccf0bab941009c21df21cf31"},{"constant":true,"inputs":[],"name":"era","outputs":[{"name":"","type":"uint48"}],"payable":false,"stateMutability":"view","type":"function","signature":"0x143e55e0"},{"constant":false,"inputs":[{"name":"urn","type":"bytes32"},{"name":"gal","type":"address"},{"name":"tab","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"bid","type":"uint256"}],"name":"kick","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xeae19d9e"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"tick","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xfc7b6aee"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"bid","type":"uint256"}],"name":"tend","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x4b43ed12"},{"constant":false,"inputs":[{"name":"id","type":"uint256"},{"name":"lot","type":"uint256"},{"name":"bid","type":"uint256"}],"name":"dent","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0x5ff3a382"},{"constant":false,"inputs":[{"name":"id","type":"uint256"}],"name":"deal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function","signature":"0xc959c42b"}]` @@ -30,12 +30,12 @@ var ( FlipperContractAddress = "0x6b59c42097e2fff7cad96cb08ceefd601081ad9c" //this is a temporary address deployed locally - BiteSignature = GetEventSignature(biteMethod) - FlipKickSignature = GetEventSignature(flipKickMethod) - FrobSignature = GetEventSignature(frobMethod) - LogValueSignature = GetEventSignature("") - PitFileSignatureOne = GetLogNoteSignature(pitFileMethodOne) - PitFileSignatureThree = GetLogNoteSignature(pitFileMethodThree) - PitFileSignatureTwo = GetLogNoteSignature(pitFileMethodTwo) - TendFunctionSignature = GetLogNoteSignature(tendMethod) + BiteSignature = GetEventSignature(biteMethod) + FlipKickSignature = GetEventSignature(flipKickMethod) + FrobSignature = GetEventSignature(frobMethod) + LogValueSignature = GetEventSignature("") + PitFileIlkSignature = GetLogNoteSignature(pitFileIlkMethod) + PitFileStabilityFeeSignature = GetLogNoteSignature(pitFileStabilityFeeMethod) + PitFileDebtCeilingSignature = GetLogNoteSignature(pitFileDebtCeilingMethod) + TendFunctionSignature = GetLogNoteSignature(tendMethod) ) diff --git a/pkg/transformers/test_data/mocks/pit_file/converter.go b/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go similarity index 51% rename from pkg/transformers/test_data/mocks/pit_file/converter.go rename to pkg/transformers/test_data/mocks/pit_file/ilk/converter.go index 45693ea4..89fe53e7 100644 --- a/pkg/transformers/test_data/mocks/pit_file/converter.go +++ b/pkg/transformers/test_data/mocks/pit_file/ilk/converter.go @@ -1,26 +1,26 @@ -package pit_file +package ilk import ( "github.com/ethereum/go-ethereum/core/types" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" ) -type MockPitFileConverter struct { +type MockPitFileIlkConverter struct { PassedContractAddress string PassedContractABI string PassedLog types.Log converterError error } -func (converter *MockPitFileConverter) SetConverterError(err error) { +func (converter *MockPitFileIlkConverter) SetConverterError(err error) { converter.converterError = err } -func (converter *MockPitFileConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (pit_file.PitFileModel, error) { +func (converter *MockPitFileIlkConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (ilk.PitFileIlkModel, error) { converter.PassedContractAddress = contractAddress converter.PassedContractABI = contractAbi converter.PassedLog = ethLog - return test_data.PitFileModel, converter.converterError + return test_data.PitFileIlkModel, converter.converterError } diff --git a/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go b/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go new file mode 100644 index 00000000..b1571a67 --- /dev/null +++ b/pkg/transformers/test_data/mocks/pit_file/ilk/repository.go @@ -0,0 +1,40 @@ +package ilk + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" +) + +type MockPitFileIlkRepository struct { + createError error + PassedEndingBlockNumber int64 + PassedModel ilk.PitFileIlkModel + PassedHeaderID int64 + PassedStartingBlockNumber int64 + missingHeaders []core.Header + missingHeadersErr error +} + +func (repository *MockPitFileIlkRepository) SetCreateError(err error) { + repository.createError = err +} + +func (repository *MockPitFileIlkRepository) SetMissingHeadersErr(err error) { + repository.missingHeadersErr = err +} + +func (repository *MockPitFileIlkRepository) SetMissingHeaders(headers []core.Header) { + repository.missingHeaders = headers +} + +func (repository *MockPitFileIlkRepository) Create(headerID int64, model ilk.PitFileIlkModel) error { + repository.PassedHeaderID = headerID + repository.PassedModel = model + return repository.createError +} + +func (repository *MockPitFileIlkRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { + repository.PassedStartingBlockNumber = startingBlockNumber + repository.PassedEndingBlockNumber = endingBlockNumber + return repository.missingHeaders, repository.missingHeadersErr +} diff --git a/pkg/transformers/test_data/mocks/pit_file/repository.go b/pkg/transformers/test_data/mocks/pit_file/repository.go deleted file mode 100644 index 9475adaa..00000000 --- a/pkg/transformers/test_data/mocks/pit_file/repository.go +++ /dev/null @@ -1,42 +0,0 @@ -package pit_file - -import ( - "github.com/vulcanize/vulcanizedb/pkg/core" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" -) - -type MockPitFileRepository struct { - createError error - PassedEndingBlockNumber int64 - PassedModel pit_file.PitFileModel - PassedHeaderID int64 - PassedStartingBlockNumber int64 - PassedTransactionIndex uint - missingHeaders []core.Header - missingHeadersErr error -} - -func (repository *MockPitFileRepository) SetCreateError(err error) { - repository.createError = err -} - -func (repository *MockPitFileRepository) SetMissingHeadersErr(err error) { - repository.missingHeadersErr = err -} - -func (repository *MockPitFileRepository) SetMissingHeaders(headers []core.Header) { - repository.missingHeaders = headers -} - -func (repository *MockPitFileRepository) Create(headerID int64, transactionIndex uint, model pit_file.PitFileModel) error { - repository.PassedHeaderID = headerID - repository.PassedTransactionIndex = transactionIndex - repository.PassedModel = model - return repository.createError -} - -func (repository *MockPitFileRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { - repository.PassedStartingBlockNumber = startingBlockNumber - repository.PassedEndingBlockNumber = endingBlockNumber - return repository.missingHeaders, repository.missingHeadersErr -} diff --git a/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go b/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go new file mode 100644 index 00000000..9e3d9583 --- /dev/null +++ b/pkg/transformers/test_data/mocks/pit_file/stability_fee/converter.go @@ -0,0 +1,25 @@ +package stability_fee + +import ( + "github.com/ethereum/go-ethereum/core/types" + + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" + "github.com/vulcanize/vulcanizedb/pkg/transformers/test_data" +) + +type MockPitFileStabilityFeeConverter struct { + converterErr error + PassedContractAddress string + PassedContractABI string + PassedLog types.Log +} + +func (converter *MockPitFileStabilityFeeConverter) ToModel(contractAddress string, contractAbi string, ethLog types.Log) (stability_fee.PitFileStabilityFeeModel, error) { + converter.PassedContractAddress = contractAddress + converter.PassedContractABI = contractAbi + converter.PassedLog = ethLog + return test_data.PitFileStabilityFeeModel, converter.converterErr +} +func (converter *MockPitFileStabilityFeeConverter) SetConverterError(e error) { + converter.converterErr = e +} diff --git a/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go b/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go new file mode 100644 index 00000000..d97274ba --- /dev/null +++ b/pkg/transformers/test_data/mocks/pit_file/stability_fee/repository.go @@ -0,0 +1,38 @@ +package stability_fee + +import ( + "github.com/vulcanize/vulcanizedb/pkg/core" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" +) + +type MockPitFileStabilityFeeRepository struct { + createErr error + missingHeaders []core.Header + missingHeadersErr error + PassedStartingBlockNumber int64 + PassedEndingBlockNumber int64 + PassedHeaderID int64 + PassedModel stability_fee.PitFileStabilityFeeModel +} + +func (repository *MockPitFileStabilityFeeRepository) Create(headerID int64, model stability_fee.PitFileStabilityFeeModel) error { + repository.PassedModel = model + repository.PassedHeaderID = headerID + return repository.createErr +} + +func (repository *MockPitFileStabilityFeeRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) { + repository.PassedStartingBlockNumber = startingBlockNumber + repository.PassedEndingBlockNumber = endingBlockNumber + return repository.missingHeaders, repository.missingHeadersErr +} + +func (repository *MockPitFileStabilityFeeRepository) SetMissingHeadersErr(e error) { + repository.missingHeadersErr = e +} +func (repository *MockPitFileStabilityFeeRepository) SetMissingHeaders(headers []core.Header) { + repository.missingHeaders = headers +} +func (repository *MockPitFileStabilityFeeRepository) SetCreateError(e error) { + repository.createErr = e +} diff --git a/pkg/transformers/test_data/pit_file.go b/pkg/transformers/test_data/pit_file.go index 49232706..664e3452 100644 --- a/pkg/transformers/test_data/pit_file.go +++ b/pkg/transformers/test_data/pit_file.go @@ -5,7 +5,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" - "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + ilk2 "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" "math/big" ) @@ -13,7 +14,7 @@ var ( PitAddress = "0xff3f2400f1600f3f493a9a92704a29b96795af1a" ) -var EthPitFileLog = types.Log{ +var EthPitFileIlkLog = types.Log{ Address: common.HexToAddress(PitAddress), Topics: []common.Hash{ common.HexToHash("0x1a0b287e00000000000000000000000000000000000000000000000000000000"), @@ -30,11 +31,36 @@ var EthPitFileLog = types.Log{ Removed: false, } -var rawPitFileLog, _ = json.Marshal(EthPitFileLog) -var PitFileModel = pit_file.PitFileModel{ +var rawPitFileIlkLog, _ = json.Marshal(EthPitFileIlkLog) +var PitFileIlkModel = ilk2.PitFileIlkModel{ Ilk: "fake ilk", What: "spot", - Risk: big.NewInt(123).String(), - TransactionIndex: EthPitFileLog.TxIndex, - Raw: rawPitFileLog, + Data: big.NewInt(123).String(), + TransactionIndex: EthPitFileIlkLog.TxIndex, + Raw: rawPitFileIlkLog, +} + +var EthPitFileStabilityFeeLog = types.Log{ + Address: common.HexToAddress("0x6b59c42097e2Fff7cad96cb08cEeFd601081aD9c"), + Topics: []common.Hash{ + common.HexToHash("0xd4e8be8300000000000000000000000000000000000000000000000000000000"), + common.HexToHash("0x00000000000000000000000064d922894153be9eef7b7218dc565d1d0ce2a092"), + common.HexToHash("0x6472697000000000000000000000000000000000000000000000000000000000"), + common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000"), + }, + Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000044d4e8be8364726970000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), + BlockNumber: 12, + TxHash: common.HexToHash("0x78cdc62316ccf8e31515d09745cc724f557569f01a557d0d09b1066bf7079fd2"), + TxIndex: 0, + BlockHash: common.HexToHash("0xe3d8e458421533170871b4033f978a3793ef10b7e33a9328a13c09e2fd90208d"), + Index: 0, + Removed: false, +} + +var rawPitFileStabilityFeeLog, _ = json.Marshal(EthPitFileStabilityFeeLog) +var PitFileStabilityFeeModel = stability_fee.PitFileStabilityFeeModel{ + What: "drip", + Data: "0x64d922894153BE9EEf7b7218dc565d1D0Ce2a092", + TransactionIndex: 0, + Raw: rawPitFileStabilityFeeLog, } diff --git a/pkg/transformers/transformers.go b/pkg/transformers/transformers.go index a8af1a44..a83a7e15 100644 --- a/pkg/transformers/transformers.go +++ b/pkg/transformers/transformers.go @@ -19,6 +19,8 @@ import ( "github.com/vulcanize/vulcanizedb/pkg/transformers/flip_kick" "github.com/vulcanize/vulcanizedb/pkg/transformers/frob" "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/ilk" + "github.com/vulcanize/vulcanizedb/pkg/transformers/pit_file/stability_fee" "github.com/vulcanize/vulcanizedb/pkg/transformers/price_feeds" "github.com/vulcanize/vulcanizedb/pkg/transformers/shared" "github.com/vulcanize/vulcanizedb/pkg/transformers/tend" @@ -30,7 +32,8 @@ func TransformerInitializers() []shared.TransformerInitializer { frobConfig := frob.FrobConfig frobTransformerInitializer := frob.FrobTransformerInitializer{Config: frobConfig} pitFileConfig := pit_file.PitFileConfig - pitFileTransformerInitializer := pit_file.PitFileTransformerInitializer{Config: pitFileConfig} + pitFileIlkTransformerInitializer := ilk.PitFileIlkTransformerInitializer{Config: pitFileConfig} + pitFileStabilityFeeTransformerInitializer := stability_fee.PitFileStabilityFeeTransformerInitializer{Config: pitFileConfig} priceFeedConfig := price_feeds.PriceFeedConfig priceFeedTransformerInitializer := price_feeds.PriceFeedTransformerInitializer{Config: priceFeedConfig} tendConfig := tend.TendConfig @@ -41,7 +44,8 @@ func TransformerInitializers() []shared.TransformerInitializer { biteTransformerInitializer.NewBiteTransformer, flipKickTransformerInitializer.NewFlipKickTransformer, frobTransformerInitializer.NewFrobTransformer, - pitFileTransformerInitializer.NewPitFileTransformer, + pitFileIlkTransformerInitializer.NewPitFileIlkTransformer, + pitFileStabilityFeeTransformerInitializer.NewPitFileStabilityFeeTransformer, priceFeedTransformerInitializer.NewPriceFeedTransformer, tendTransformerInitializer.NewTendTransformer, } diff --git a/test_config/test_config.go b/test_config/test_config.go index 29bf6ca1..5e14d6e5 100644 --- a/test_config/test_config.go +++ b/test_config/test_config.go @@ -77,7 +77,8 @@ func CleanTestDB(db *postgres.DB) { db.MustExec("DELETE FROM headers") db.MustExec("DELETE FROM log_filters") db.MustExec("DELETE FROM logs") - db.MustExec("DELETE FROM maker.pit_file") + db.MustExec("DELETE FROM maker.pit_file_ilk") + db.MustExec("DELETE FROM maker.pit_file_stability_fee") db.MustExec("DELETE FROM receipts") db.MustExec("DELETE FROM transactions") db.MustExec("DELETE FROM watched_contracts")