diff --git a/pkg/transformers/test_data/vat_fold.go b/pkg/transformers/test_data/vat_fold.go new file mode 100644 index 00000000..98334d8b --- /dev/null +++ b/pkg/transformers/test_data/vat_fold.go @@ -0,0 +1,53 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package test_data + +import ( + "bytes" + "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/shared" + "github.com/vulcanize/vulcanizedb/pkg/transformers/vat_fold" +) + +var EthVatFoldLog = types.Log{ + Address: common.HexToAddress(shared.VatContractAddress), + Topics: []common.Hash{ + common.HexToHash("0xe6a6a64d00000000000000000000000000000000000000000000000000000000"), + common.HexToHash("0x00000000000000000000000007Fa9eF6609cA7921112231F8f195138ebbA2977"), + common.HexToHash("0x00000000000000000000000064d922894153be9eef7b7218dc565d1d0ce2a092"), + common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002"), + }, + Data: hexutil.MustDecode("0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000243b66319566616b6520696c6b000000000000000000000000000000000000000000000000"), + BlockNumber: 72, + TxHash: common.HexToHash("0xe8f39fbb7fea3621f543868f19b1114e305aff6a063a30d32835ff1012526f91"), + TxIndex: 8, + BlockHash: common.HexToHash("0xe3dd2e05bd8b92833e20ed83e2171bbc06a9ec823232eca1730a807bd8f5edc0"), + Index: 5, + Removed: false, +} + +var rawVatFoldLog, _ = json.Marshal(EthVatFoldLog) +var VatFoldModel = vat_fold.VatFoldModel{ + Ilk: string(bytes.Trim(EthVatFoldLog.Topics[1].Bytes(), "\x00")), + Urn: string(bytes.Trim(EthVatFoldLog.Topics[2].Bytes(), "\x00")), + Rate: string(bytes.Trim(EthVatFoldLog.Topics[3].Bytes(), "\x00")), + TransactionIndex: EthVatFoldLog.TxIndex, + Raw: rawVatFoldLog, +} diff --git a/pkg/transformers/vat_fold/converter.go b/pkg/transformers/vat_fold/converter.go new file mode 100644 index 00000000..74fb3b63 --- /dev/null +++ b/pkg/transformers/vat_fold/converter.go @@ -0,0 +1,57 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vat_fold + +import ( + "bytes" + "encoding/json" + "errors" + + "github.com/ethereum/go-ethereum/core/types" +) + +type Converter interface { + ToModel(ethLog types.Log) (VatFoldModel, error) +} + +type VatFoldConverter struct{} + +func (VatFoldConverter) ToModel(ethLog types.Log) (VatFoldModel, error) { + err := verifyLog(ethLog) + if err != nil { + return VatFoldModel{}, err + } + + ilk := string(bytes.Trim(ethLog.Topics[1].Bytes(), "\x00")) + urn := string(bytes.Trim(ethLog.Topics[2].Bytes(), "\x00")) + rate := string(bytes.Trim(ethLog.Topics[3].Bytes(), "\x00")) + raw, err := json.Marshal(ethLog) + + return VatFoldModel{ + Ilk: ilk, + Urn: urn, + Rate: rate, + TransactionIndex: ethLog.TxIndex, + Raw: raw, + }, err +} + +func verifyLog(log types.Log) error { + if len(log.Topics) < 4 { + return errors.New("log missing topics") + } + + return nil +} diff --git a/pkg/transformers/vat_fold/converter_test.go b/pkg/transformers/vat_fold/converter_test.go new file mode 100644 index 00000000..021096db --- /dev/null +++ b/pkg/transformers/vat_fold/converter_test.go @@ -0,0 +1,44 @@ +// Copyright 2018 Vulcanize +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package vat_fold_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_fold" +) + +var _ = Describe("Vat fold converter", func() { + It("returns err if log missing topics", func() { + converter := vat_fold.VatFoldConverter{} + badLog := types.Log{} + + _, err := converter.ToModel(badLog) + + Expect(err).To(HaveOccurred()) + }) + + It("converts a log to an model", func() { + converter := vat_fold.VatFoldConverter{} + + model, err := converter.ToModel(test_data.EthVatFoldLog) + + Expect(err).NotTo(HaveOccurred()) + Expect(model).To(Equal(test_data.VatFoldModel)) + }) +}) diff --git a/pkg/transformers/vat_fold/model.go b/pkg/transformers/vat_fold/model.go new file mode 100644 index 00000000..9af973b3 --- /dev/null +++ b/pkg/transformers/vat_fold/model.go @@ -0,0 +1,9 @@ +package vat_fold + +type VatFoldModel struct { + Ilk string + Urn string + Rate string + TransactionIndex uint `db:"tx_idx"` + Raw []byte `db:"raw_log"` +} diff --git a/pkg/transformers/vat_fold/vat_fold_suite_test.go b/pkg/transformers/vat_fold/vat_fold_suite_test.go new file mode 100644 index 00000000..97a2aac7 --- /dev/null +++ b/pkg/transformers/vat_fold/vat_fold_suite_test.go @@ -0,0 +1,19 @@ +package vat_fold_test + +import ( + "io/ioutil" + "log" + "testing" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestVatFold(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "VatFold Suite") +} + +var _ = BeforeSuite(func() { + log.SetOutput(ioutil.Discard) +})