Skip to content
This repository has been archived by the owner on Aug 31, 2021. It is now read-only.

Commit

Permalink
Add Vat toll transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
rmulhol committed Oct 9, 2018
1 parent 300da63 commit 8ee106e
Show file tree
Hide file tree
Showing 19 changed files with 866 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/continuousLogSync.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func buildTransformerInitializerMap() map[string]shared2.TransformerInitializer
transformerInitializerMap["priceFeed"] = transformers.PriceFeedTransformerInitializer
transformerInitializerMap["tend"] = transformers.TendTransformerInitializer
transformerInitializerMap["vatInit"] = transformers.VatInitTransformerInitializer
transformerInitializerMap["vatToll"] = transformers.VatTollTransformerInitializer

return transformerInitializerMap
}
Expand Down
3 changes: 3 additions & 0 deletions db/migrations/1538679495_create_vat_toll.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DROP TABLE maker.vat_toll;
ALTER TABLE public.checked_headers
DROP COLUMN vat_toll_checked;
13 changes: 13 additions & 0 deletions db/migrations/1538679495_create_vat_toll.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE maker.vat_toll (
id SERIAL PRIMARY KEY,
header_id INTEGER NOT NULL REFERENCES headers (id) ON DELETE CASCADE,
ilk TEXT,
urn TEXT,
take NUMERIC,
tx_idx INTEGER NOT NULL,
raw_log JSONB,
UNIQUE (header_id, tx_idx)
);

ALTER TABLE public.checked_headers
ADD COLUMN vat_toll_checked BOOLEAN NOT NULL DEFAULT FALSE;
69 changes: 68 additions & 1 deletion db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,41 @@ CREATE SEQUENCE maker.vat_init_id_seq
ALTER SEQUENCE maker.vat_init_id_seq OWNED BY maker.vat_init.id;


--
-- Name: vat_toll; Type: TABLE; Schema: maker; Owner: -
--

CREATE TABLE maker.vat_toll (
id integer NOT NULL,
header_id integer NOT NULL,
ilk text,
urn text,
take numeric,
tx_idx integer NOT NULL,
raw_log jsonb
);


--
-- Name: vat_toll_id_seq; Type: SEQUENCE; Schema: maker; Owner: -
--

CREATE SEQUENCE maker.vat_toll_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: vat_toll_id_seq; Type: SEQUENCE OWNED BY; Schema: maker; Owner: -
--

ALTER SEQUENCE maker.vat_toll_id_seq OWNED BY maker.vat_toll.id;


--
-- Name: logs; Type: TABLE; Schema: public; Owner: -
--
Expand Down Expand Up @@ -830,7 +865,8 @@ CREATE TABLE public.checked_headers (
pit_file_debt_ceiling_checked boolean DEFAULT false NOT NULL,
pit_file_ilk_checked boolean DEFAULT false NOT NULL,
pit_file_stability_fee_checked boolean DEFAULT false NOT NULL,
vat_init_checked boolean DEFAULT false NOT NULL
vat_init_checked boolean DEFAULT false NOT NULL,
vat_toll_checked boolean DEFAULT false NOT NULL
);


Expand Down Expand Up @@ -1284,6 +1320,13 @@ ALTER TABLE ONLY maker.tend ALTER COLUMN id SET DEFAULT nextval('maker.tend_id_s
ALTER TABLE ONLY maker.vat_init ALTER COLUMN id SET DEFAULT nextval('maker.vat_init_id_seq'::regclass);


--
-- Name: vat_toll id; Type: DEFAULT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.vat_toll ALTER COLUMN id SET DEFAULT nextval('maker.vat_toll_id_seq'::regclass);


--
-- Name: blocks id; Type: DEFAULT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -1674,6 +1717,22 @@ ALTER TABLE ONLY maker.vat_init
ADD CONSTRAINT vat_init_pkey PRIMARY KEY (id);


--
-- Name: vat_toll vat_toll_header_id_tx_idx_key; Type: CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.vat_toll
ADD CONSTRAINT vat_toll_header_id_tx_idx_key UNIQUE (header_id, tx_idx);


--
-- Name: vat_toll vat_toll_pkey; Type: CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.vat_toll
ADD CONSTRAINT vat_toll_pkey PRIMARY KEY (id);


--
-- Name: blocks blocks_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
Expand Down Expand Up @@ -1980,6 +2039,14 @@ ALTER TABLE ONLY maker.vat_init
ADD CONSTRAINT vat_init_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE;


--
-- Name: vat_toll vat_toll_header_id_fkey; Type: FK CONSTRAINT; Schema: maker; Owner: -
--

ALTER TABLE ONLY maker.vat_toll
ADD CONSTRAINT vat_toll_header_id_fkey FOREIGN KEY (header_id) REFERENCES public.headers(id) ON DELETE CASCADE;


--
-- Name: transactions blocks_fk; Type: FK CONSTRAINT; Schema: public; Owner: -
--
Expand Down
2 changes: 2 additions & 0 deletions pkg/transformers/shared/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
pitFileStabilityFeeMethod = GetSolidityMethodSignature(PitABI, "file")
tendMethod = GetSolidityMethodSignature(FlipperABI, "tend")
vatInitMethod = GetSolidityMethodSignature(VatABI, "init")
vatTollMethod = GetSolidityMethodSignature(VatABI, "toll")

BiteSignature = GetEventSignature(biteMethod)
DealSignature = GetLogNoteSignature(dealMethod)
Expand All @@ -76,4 +77,5 @@ var (
PitFileStabilityFeeSignature = GetLogNoteSignature(pitFileStabilityFeeMethod)
TendFunctionSignature = GetLogNoteSignature(tendMethod)
VatInitSignature = GetLogNoteSignature(vatInitMethod)
VatTollSignature = GetLogNoteSignature(vatTollMethod)
)
21 changes: 21 additions & 0 deletions pkg/transformers/test_data/mocks/vat_toll/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package vat_toll

import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
)

type MockVatTollConverter struct {
err error
PassedLogs []types.Log
}

func (converter *MockVatTollConverter) ToModels(ethLogs []types.Log) ([]vat_toll.VatTollModel, error) {
converter.PassedLogs = ethLogs
return []vat_toll.VatTollModel{test_data.VatTollModel}, converter.err
}

func (converter *MockVatTollConverter) SetConverterError(e error) {
converter.err = e
}
57 changes: 57 additions & 0 deletions pkg/transformers/test_data/mocks/vat_toll/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package vat_toll

import (
. "github.com/onsi/gomega"

"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
)

type MockVatTollRepository struct {
createErr error
markHeaderCheckedErr error
markHeaderCheckedPassedHeaderID int64
missingHeadersErr error
missingHeaders []core.Header
PassedStartingBlockNumber int64
PassedEndingBlockNumber int64
PassedHeaderID int64
PassedModels []vat_toll.VatTollModel
}

func (repository *MockVatTollRepository) Create(headerID int64, models []vat_toll.VatTollModel) error {
repository.PassedHeaderID = headerID
repository.PassedModels = models
return repository.createErr
}

func (repository *MockVatTollRepository) MarkHeaderChecked(headerID int64) error {
repository.markHeaderCheckedPassedHeaderID = headerID
return repository.markHeaderCheckedErr
}

func (repository *MockVatTollRepository) MissingHeaders(startingBlockNumber, endingBlockNumber int64) ([]core.Header, error) {
repository.PassedStartingBlockNumber = startingBlockNumber
repository.PassedEndingBlockNumber = endingBlockNumber
return repository.missingHeaders, repository.missingHeadersErr
}

func (repository *MockVatTollRepository) SetCreateError(e error) {
repository.createErr = e
}

func (repository *MockVatTollRepository) SetMarkHeaderCheckedErr(e error) {
repository.markHeaderCheckedErr = e
}

func (repository *MockVatTollRepository) SetMissingHeadersErr(e error) {
repository.missingHeadersErr = e
}

func (repository *MockVatTollRepository) SetMissingHeaders(headers []core.Header) {
repository.missingHeaders = headers
}

func (repository *MockVatTollRepository) AssertMarkHeaderCheckedCalledWith(i int64) {
Expect(repository.markHeaderCheckedPassedHeaderID).To(Equal(i))
}
36 changes: 36 additions & 0 deletions pkg/transformers/test_data/vat_toll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package test_data

import (
"encoding/json"
"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/vat_toll"
"math/big"
)

var EthVatTollLog = types.Log{
Address: common.HexToAddress("0x239E6f0AB02713f1F8AA90ebeDeD9FC66Dc96CD6"),
Topics: []common.Hash{
common.HexToHash("0x09b7a0b500000000000000000000000000000000000000000000000000000000"),
common.HexToHash("0x66616b6520696c6b000000000000000000000000000000000000000000000000"),
common.HexToHash("0xa3e37186e017747dba34042e83e3f76ad3cce9b0000000000000000000000000"),
common.HexToHash("0x00000000000000000000000000000000000000000000000000000000075bcd15"),
},
Data: hexutil.MustDecode("0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006409b7a0b566616b6520696c6b000000000000000000000000000000000000000000000000a3e37186e017747dba34042e83e3f76ad3cce9b000000000000000000000000000000000000000000000000000000000000000000000000000000000075bcd15"),
BlockNumber: 21,
TxHash: common.HexToHash("0x0d59cb158b033ffdfb9a021d1e80bfbbcd99594c62c501897ccee446bcd33828"),
TxIndex: 2,
BlockHash: common.HexToHash("0xdbfda0ecb4ac6267c50be21db97874dc1203c22033e19733a6f67ca00b51dfc5"),
Index: 4,
Removed: false,
}

var rawVatTollLog, _ = json.Marshal(EthVatTollLog)
var VatTollModel = vat_toll.VatTollModel{
Ilk: "fake ilk",
Urn: "0xA3E37186E017747DbA34042e83e3F76Ad3CcE9b0",
Take: big.NewInt(123456789).String(),
TransactionIndex: EthVatTollLog.TxIndex,
Raw: rawVatTollLog,
}
3 changes: 3 additions & 0 deletions pkg/transformers/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/vulcanize/vulcanizedb/pkg/transformers/shared"
"github.com/vulcanize/vulcanizedb/pkg/transformers/tend"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_init"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
)

var (
Expand All @@ -63,6 +64,7 @@ var (
PriceFeedTransformerInitializer = price_feeds.PriceFeedTransformerInitializer{Config: price_feeds.PriceFeedConfig}.NewPriceFeedTransformer
TendTransformerInitializer = tend.TendTransformerInitializer{Config: tend.TendConfig}.NewTendTransformer
VatInitTransformerInitializer = vat_init.VatInitTransformerInitializer{Config: vat_init.VatInitConfig}.NewVatInitTransformer
VatTollTransformerInitializer = vat_toll.VatTollTransformerInitializer{Config: vat_toll.VatTollConfig}.NewVatTollTransformer
)

func TransformerInitializers() []shared.TransformerInitializer {
Expand All @@ -86,5 +88,6 @@ func TransformerInitializers() []shared.TransformerInitializer {
PriceFeedTransformerInitializer,
TendTransformerInitializer,
VatInitTransformerInitializer,
VatTollTransformerInitializer,
}
}
11 changes: 11 additions & 0 deletions pkg/transformers/vat_toll/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package vat_toll

import "github.com/vulcanize/vulcanizedb/pkg/transformers/shared"

var VatTollConfig = shared.TransformerConfig{
ContractAddresses: []string{shared.VatContractAddress},
ContractAbi: shared.VatABI,
Topics: []string{shared.VatTollSignature},
StartingBlockNumber: 0,
EndingBlockNumber: 10000000,
}
51 changes: 51 additions & 0 deletions pkg/transformers/vat_toll/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package vat_toll

import (
"bytes"
"encoding/json"
"errors"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

type Converter interface {
ToModels(ethLogs []types.Log) ([]VatTollModel, error)
}

type VatTollConverter struct{}

func (VatTollConverter) ToModels(ethLogs []types.Log) ([]VatTollModel, error) {
var models []VatTollModel
for _, ethLog := range ethLogs {
err := verifyLog(ethLog)
if err != nil {
return nil, err
}
ilk := string(bytes.Trim(ethLog.Topics[1].Bytes(), "\x00"))
urn := common.BytesToAddress(ethLog.Topics[2].Bytes()[:common.AddressLength])
take := ethLog.Topics[3].Big()

raw, err := json.Marshal(ethLog)
if err != nil {
return nil, err
}
model := VatTollModel{
Ilk: ilk,
Urn: urn.String(),
Take: take.String(),
TransactionIndex: ethLog.TxIndex,
Raw: raw,
}
models = append(models, model)
}
return models, nil
}

func verifyLog(log types.Log) error {
numTopicInValidLog := 4
if len(log.Topics) < numTopicInValidLog {
return errors.New("log missing topics")
}
return nil
}
33 changes: 33 additions & 0 deletions pkg/transformers/vat_toll/converter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package vat_toll_test

import (
"github.com/ethereum/go-ethereum/core/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/vulcanize/vulcanizedb/pkg/transformers/test_data"
"github.com/vulcanize/vulcanizedb/pkg/transformers/vat_toll"
)

var _ = Describe("Vat toll converter", func() {
It("returns err if log is missing topics", func() {
converter := vat_toll.VatTollConverter{}
badLog := types.Log{
Data: []byte{1, 1, 1, 1, 1},
}

_, err := converter.ToModels([]types.Log{badLog})

Expect(err).To(HaveOccurred())
})

It("converts a log to an model", func() {
converter := vat_toll.VatTollConverter{}

models, err := converter.ToModels([]types.Log{test_data.EthVatTollLog})

Expect(err).NotTo(HaveOccurred())
Expect(len(models)).To(Equal(1))
Expect(models[0]).To(Equal(test_data.VatTollModel))
})
})
9 changes: 9 additions & 0 deletions pkg/transformers/vat_toll/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package vat_toll

type VatTollModel struct {
Ilk string
Urn string
Take string
TransactionIndex uint `db:"tx_idx"`
Raw []byte `db:"raw_log"`
}
Loading

0 comments on commit 8ee106e

Please sign in to comment.