event_triggered transformer, transformer tests, and integrations tests for Transfer and Approval events

This commit is contained in:
Ian Norden 2018-08-28 16:03:55 -05:00
parent aa2068bd08
commit 4aa403d90d
7 changed files with 313 additions and 70 deletions

View File

@ -59,14 +59,14 @@ var ApprovalEventSignature = helpers.GenerateSignature("Approval(address,address
// Filters // Filters
var DaiFilters = []filters.LogFilter{ var DaiFilters = []filters.LogFilter{
{ {
Name: "Transfers", Name: "Transfer",
FromBlock: 0, FromBlock: 0,
ToBlock: -1, ToBlock: -1,
Address: DaiContractAddress, Address: DaiContractAddress,
Topics: core.Topics{TransferEventSignature}, Topics: core.Topics{TransferEventSignature},
}, },
{ {
Name: "Approvals", Name: "Approval",
FromBlock: 0, FromBlock: 0,
ToBlock: -1, ToBlock: -1,
Address: DaiContractAddress, Address: DaiContractAddress,

View File

@ -21,8 +21,8 @@ import (
"github.com/vulcanize/vulcanizedb/examples/constants" "github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered" "github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered"
"github.com/vulcanize/vulcanizedb/examples/generic" "github.com/vulcanize/vulcanizedb/examples/generic"
"github.com/vulcanize/vulcanizedb/examples/generic/helpers"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
"math/big"
) )
var expectedTransferModel = event_triggered.TransferModel{ var expectedTransferModel = event_triggered.TransferModel{
@ -35,14 +35,12 @@ var expectedTransferModel = event_triggered.TransferModel{
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
} }
var transferWad, approvalWad big.Int
var expectedTransferEntity = event_triggered.TransferEntity{ var expectedTransferEntity = event_triggered.TransferEntity{
TokenName: "Dai", TokenName: "Dai",
TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"), TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"),
Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"), Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"),
Dst: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"), Dst: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"),
Wad: &transferWad, Wad: helpers.BigFromString("1097077688018008265106216665536940668749033598146"),
Block: 5488076, Block: 5488076,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
} }
@ -62,14 +60,14 @@ var expectedApprovalEntity = event_triggered.ApprovalEntity{
TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"), TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"),
Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"), Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"),
Guy: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"), Guy: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"),
Wad: &approvalWad, Wad: helpers.BigFromString("1097077688018008265106216665536940668749033598146"),
Block: 5488076, Block: 5488076,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
} }
var transferEvent = core.WatchedEvent{ var transferEvent = core.WatchedEvent{
LogID: 1, LogID: 1,
Name: "Dai", Name: "Transfer",
BlockNumber: 5488076, BlockNumber: 5488076,
Address: constants.DaiContractAddress, Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
@ -83,7 +81,7 @@ var transferEvent = core.WatchedEvent{
var approvalEvent = core.WatchedEvent{ var approvalEvent = core.WatchedEvent{
LogID: 1, LogID: 1,
Name: "Dai", Name: "Approval",
BlockNumber: 5488076, BlockNumber: 5488076,
Address: constants.DaiContractAddress, Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
@ -166,8 +164,3 @@ var _ = Describe("Approval Converter", func() {
}) })
}) })
func init() {
transferWad.SetString("1097077688018008265106216665536940668749033598146", 10)
approvalWad.SetString("1097077688018008265106216665536940668749033598146", 10)
}

View File

@ -14,44 +14,54 @@
package event_triggered_test package event_triggered_test
/*
import ( import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/examples/constants" "github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered" "github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories" "github.com/vulcanize/vulcanizedb/examples/generic"
"github.com/vulcanize/vulcanizedb/examples/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/fakes" "github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
"math/rand"
"time" "time"
) )
var idOne = "0x000000000000000000000000000000000000000000000000000000000000af21" var transferLog = core.Log{
var logKill = core.Log{
BlockNumber: 5488076, BlockNumber: 5488076,
Address: constants.DaiContractAddress, Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
Index: 0, Index: 110,
Topics: [4]string{ Topics: [4]string{
constants.TransferEventSignature, constants.TransferEventSignature,
idOne, "0x000000000000000000000000000000000000000000000000000000000000af21",
"0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391", "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
"0x0000000000000000000000003dc389e0a69d6364a66ab64ebd51234da9569284", "",
}, },
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe", Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
} }
var expectedLogKill = event_triggered.TransferModel{ var approvalLog = core.Log{
Block: 5488076, BlockNumber: 5488076,
Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
Index: 110,
Topics: [4]string{
constants.ApprovalEventSignature,
"0x000000000000000000000000000000000000000000000000000000000000af21",
"0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
"",
},
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
} }
//converted logID to assert against //converted transfer to assert against
var logs = []core.Log{ var logs = []core.Log{
logKill, transferLog,
approvalLog,
{ {
BlockNumber: 0, BlockNumber: 0,
TxHash: "", TxHash: "",
@ -64,6 +74,7 @@ var logs = []core.Log{
var _ = Describe("Integration test with vulcanizedb", func() { var _ = Describe("Integration test with vulcanizedb", func() {
var db *postgres.DB var db *postgres.DB
rand.Seed(time.Now().UnixNano())
BeforeEach(func() { BeforeEach(func() {
var err error var err error
@ -73,18 +84,31 @@ var _ = Describe("Integration test with vulcanizedb", func() {
Port: 5432, Port: 5432,
}, core.Node{}) }, core.Node{})
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
lr := repositories.LogRepository{DB: db}
err = lr.CreateLogs(logs) receiptRepository := repositories.ReceiptRepository{DB: db}
blockRepository := *repositories.NewBlockRepository(db)
blockNumber := rand.Int63()
blockId := test_helpers.CreateBlock(blockNumber, blockRepository)
receipt := core.Receipt{
Logs: logs,
}
receipts := []core.Receipt{receipt}
err = receiptRepository.CreateReceiptsAndLogs(blockId, receipts)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
var vulcanizeLogIds []int64 var vulcanizeLogIds []int64
err = db.Select(&vulcanizeLogIds, `SELECT id FROM public.logs`) err = db.Select(&vulcanizeLogIds, `SELECT id FROM logs`)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
AfterEach(func() { AfterEach(func() {
_, err := db.Exec(`DELETE FROM oasis.kill`) _, err := db.Exec(`DELETE FROM token_transfers`)
Expect(err).ToNot(HaveOccurred())
_, err = db.Exec(`DELETE FROM token_approvals`)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
_, err = db.Exec(`DELETE FROM log_filters`) _, err = db.Exec(`DELETE FROM log_filters`)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
@ -92,36 +116,58 @@ var _ = Describe("Integration test with vulcanizedb", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
It("creates oasis.kill for each LogKill event received", func() { It("creates transfer entry for each Transfer event received", func() {
blockchain := &fakes.MockBlockChain{} transformer := event_triggered.NewTransformer(db, generic.DaiConfig)
transformer := event_triggered.NewTransformer(db, blockchain)
transformer.Execute() transformer.Execute()
var count int var count int
err := db.QueryRow(`SELECT COUNT(*) FROM oasis.kill`).Scan(&count) err := db.QueryRow(`SELECT COUNT(*) FROM token_transfers`).Scan(&count)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(1)) Expect(count).To(Equal(1))
type dbRow struct { type dbRow struct {
DBID uint64 `db:"db_id"` DBID uint64 `db:"id"`
VulcanizeLogID int64 `db:"vulcanize_log_id"` VulcanizeLogID int64 `db:"vulcanize_log_id"`
event_triggered.TransferModel event_triggered.TransferModel
} }
var logKill dbRow var transfer dbRow
err = db.Get(&logKill, `SELECT * FROM oasis.kill WHERE block=$1`, logs[0].BlockNumber) err = db.Get(&transfer, `SELECT * FROM token_transfers WHERE block=$1`, logs[0].BlockNumber)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
Expect(logKill.ID).To(Equal(expectedLogKill.ID)) Expect(transfer.TokenName).To(Equal(expectedTransferModel.TokenName))
Expect(logKill.Pair).To(Equal(expectedLogKill.Pair)) Expect(transfer.TokenAddress).To(Equal(expectedTransferModel.TokenAddress))
Expect(logKill.Guy).To(Equal(expectedLogKill.Guy)) Expect(transfer.To).To(Equal(expectedTransferModel.To))
Expect(logKill.Gem).To(Equal(expectedLogKill.Gem)) Expect(transfer.From).To(Equal(expectedTransferModel.From))
Expect(logKill.Lot).To(Equal(expectedLogKill.Lot)) Expect(transfer.Tokens).To(Equal(expectedTransferModel.Tokens))
Expect(logKill.Pie).To(Equal(expectedLogKill.Pie)) Expect(transfer.Block).To(Equal(expectedTransferModel.Block))
Expect(logKill.Bid).To(Equal(expectedLogKill.Bid)) Expect(transfer.TxHash).To(Equal(expectedTransferModel.TxHash))
Expect(logKill.Block).To(Equal(expectedLogKill.Block)) })
Expect(logKill.Tx).To(Equal(expectedLogKill.Tx))
Expect(logKill.Timestamp.Equal(expectedLogKill.Timestamp)).To(BeTrue()) It("creates approval entry for each Approval event received", func() {
transformer := event_triggered.NewTransformer(db, generic.DaiConfig)
transformer.Execute()
var count int
err := db.QueryRow(`SELECT COUNT(*) FROM token_approvals`).Scan(&count)
Expect(err).ToNot(HaveOccurred())
Expect(count).To(Equal(1))
type dbRow struct {
DBID uint64 `db:"id"`
VulcanizeLogID int64 `db:"vulcanize_log_id"`
event_triggered.ApprovalModel
}
var transfer dbRow
err = db.Get(&transfer, `SELECT * FROM token_approvals WHERE block=$1`, logs[0].BlockNumber)
Expect(err).ToNot(HaveOccurred())
Expect(transfer.TokenName).To(Equal(expectedApprovalModel.TokenName))
Expect(transfer.TokenAddress).To(Equal(expectedApprovalModel.TokenAddress))
Expect(transfer.Owner).To(Equal(expectedApprovalModel.Owner))
Expect(transfer.Spender).To(Equal(expectedApprovalModel.Spender))
Expect(transfer.Tokens).To(Equal(expectedApprovalModel.Tokens))
Expect(transfer.Block).To(Equal(expectedApprovalModel.Block))
Expect(transfer.TxHash).To(Equal(expectedApprovalModel.TxHash))
}) })
}) })
*/

View File

@ -19,6 +19,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered" "github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered"
"github.com/vulcanize/vulcanizedb/examples/generic/helpers"
"github.com/vulcanize/vulcanizedb/examples/test_helpers" "github.com/vulcanize/vulcanizedb/examples/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/config" "github.com/vulcanize/vulcanizedb/pkg/config"
"github.com/vulcanize/vulcanizedb/pkg/core" "github.com/vulcanize/vulcanizedb/pkg/core"
@ -33,7 +34,7 @@ var transferEntity = event_triggered.TransferEntity{
TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"), TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"),
Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"), Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"),
Dst: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"), Dst: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"),
Wad: &transferWad, Wad: helpers.BigFromString("1097077688018008265106216665536940668749033598146"),
Block: 5488076, Block: 5488076,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
} }
@ -43,7 +44,7 @@ var approvalEntity = event_triggered.ApprovalEntity{
TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"), TokenAddress: common.HexToAddress("0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359"),
Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"), Src: common.HexToAddress("0x000000000000000000000000000000000000Af21"),
Guy: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"), Guy: common.HexToAddress("0x09BbBBE21a5975cAc061D82f7b843bCE061BA391"),
Wad: &approvalWad, Wad: helpers.BigFromString("1097077688018008265106216665536940668749033598146"),
Block: 5488076, Block: 5488076,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
} }

View File

@ -13,3 +13,69 @@
// limitations under the License. // limitations under the License.
package event_triggered package event_triggered
import (
"log"
"fmt"
"github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/generic"
"github.com/vulcanize/vulcanizedb/libraries/shared"
"github.com/vulcanize/vulcanizedb/pkg/datastore"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres/repositories"
)
type DaiTransformer struct {
Converter ERC20ConverterInterface
WatchedEventRepository datastore.WatchedEventRepository
FilterRepository datastore.FilterRepository
Repository Datastore
}
func NewTransformer(db *postgres.DB, config generic.ContractConfig) shared.Transformer {
var transformer shared.Transformer
cnvtr := NewERC20Converter(config)
wer := repositories.WatchedEventRepository{DB: db}
fr := repositories.FilterRepository{DB: db}
lkr := Repository{DB: db}
transformer = &DaiTransformer{
Converter: cnvtr,
WatchedEventRepository: wer,
FilterRepository: fr,
Repository: lkr,
}
for _, filter := range constants.DaiFilters {
fr.CreateFilter(filter)
}
return transformer
}
func (tr DaiTransformer) Execute() error {
for _, filter := range constants.DaiFilters {
watchedEvents, err := tr.WatchedEventRepository.GetWatchedEvents(filter.Name)
if err != nil {
log.Println(fmt.Sprintf("Error fetching events for %s:", filter.Name), err)
return err
}
for _, we := range watchedEvents {
if filter.Name == "Transfer" {
entity, err := tr.Converter.ToTransferEntity(*we)
model := tr.Converter.ToTransferModel(*entity)
if err != nil {
log.Printf("Error persisting data for Dai Transfers (watchedEvent.LogID %d):\n %s", we.LogID, err)
}
tr.Repository.CreateTransfer(model, we.LogID)
}
if filter.Name == "Approval" {
entity, err := tr.Converter.ToApprovalEntity(*we)
model := tr.Converter.ToApprovalModel(*entity)
if err != nil {
log.Printf("Error persisting data for Dai Approvals (watchedEvent.LogID %d):\n %s", we.LogID, err)
}
tr.Repository.CreateApproval(model, we.LogID)
}
}
}
return nil
}

View File

@ -13,3 +13,128 @@
// limitations under the License. // limitations under the License.
package event_triggered_test package event_triggered_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/vulcanize/vulcanizedb/examples/constants"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered"
"github.com/vulcanize/vulcanizedb/examples/mocks"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
type MockERC20Converter struct {
watchedEvents []*core.WatchedEvent
transfersToConvert []event_triggered.TransferEntity
approvalsToConvert []event_triggered.ApprovalEntity
block int64
}
func (mlkc *MockERC20Converter) ToTransferModel(entity event_triggered.TransferEntity) event_triggered.TransferModel {
mlkc.transfersToConvert = append(mlkc.transfersToConvert, entity)
return event_triggered.TransferModel{}
}
func (mlkc *MockERC20Converter) ToTransferEntity(watchedEvent core.WatchedEvent) (*event_triggered.TransferEntity, error) {
mlkc.watchedEvents = append(mlkc.watchedEvents, &watchedEvent)
e := &event_triggered.TransferEntity{Block: watchedEvent.BlockNumber}
mlkc.block++
return e, nil
}
func (mlkc *MockERC20Converter) ToApprovalModel(entity event_triggered.ApprovalEntity) event_triggered.ApprovalModel {
mlkc.approvalsToConvert = append(mlkc.approvalsToConvert, entity)
return event_triggered.ApprovalModel{}
}
func (mlkc *MockERC20Converter) ToApprovalEntity(watchedEvent core.WatchedEvent) (*event_triggered.ApprovalEntity, error) {
mlkc.watchedEvents = append(mlkc.watchedEvents, &watchedEvent)
e := &event_triggered.ApprovalEntity{Block: watchedEvent.BlockNumber}
mlkc.block++
return e, nil
}
var blockID1 = int64(5428074)
var logID1 = int64(113)
var blockID2 = int64(5428405)
var logID2 = int64(100)
var fakeWatchedEvents = []*core.WatchedEvent{
{
LogID: logID1,
Name: "Transfer",
BlockNumber: blockID1,
Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
Index: 110,
Topic0: constants.TransferEventSignature,
Topic1: "0x000000000000000000000000000000000000000000000000000000000000af21",
Topic2: "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
Topic3: "",
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
},
{
LogID: logID2,
Name: "Approval",
BlockNumber: blockID2,
Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
Index: 110,
Topic0: constants.ApprovalEventSignature,
Topic1: "0x000000000000000000000000000000000000000000000000000000000000af21",
Topic2: "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
Topic3: "",
Data: "0x000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359000000000000000000000000000000000000000000000000392d2e2bda9c00000000000000000000000000000000000000000000000000927f41fa0a4a418000000000000000000000000000000000000000000000000000000000005adcfebe",
},
}
var _ = Describe("Mock ERC20 transformer", func() {
var mockERC20Converter MockERC20Converter
var watchedEventsRepo mocks.MockWatchedEventsRepository
var mockEventRepo mocks.MockEventRepo
var filterRepo mocks.MockFilterRepository
var transformer event_triggered.DaiTransformer
BeforeEach(func() {
mockERC20Converter = MockERC20Converter{}
watchedEventsRepo = mocks.MockWatchedEventsRepository{}
watchedEventsRepo.SetWatchedEvents(fakeWatchedEvents)
mockEventRepo = mocks.MockEventRepo{}
filterRepo = mocks.MockFilterRepository{}
transformer = event_triggered.DaiTransformer{
Converter: &mockERC20Converter,
WatchedEventRepository: &watchedEventsRepo,
FilterRepository: filterRepo,
Repository: &mockEventRepo,
}
})
It("calls the watched events repo with correct filter", func() {
transformer.Execute()
Expect(len(watchedEventsRepo.Names)).To(Equal(2))
Expect(watchedEventsRepo.Names).To(ConsistOf([]string{"Transfer", "Approval"}))
})
It("calls the mock ERC20 converter with the watched events", func() {
transformer.Execute()
Expect(len(mockERC20Converter.watchedEvents)).To(Equal(2))
Expect(mockERC20Converter.watchedEvents).To(ConsistOf(fakeWatchedEvents))
})
It("converts a Transfer entity to a model", func() {
transformer.Execute()
Expect(len(mockERC20Converter.transfersToConvert)).To(Equal(1))
Expect(mockERC20Converter.transfersToConvert[0].Block).To(Equal(blockID1))
Expect(len(mockERC20Converter.approvalsToConvert)).To(Equal(1))
Expect(mockERC20Converter.approvalsToConvert[0].Block).To(Equal(blockID2))
})
It("persists Transfer and Approval data for each watched Transfer or Approval event", func() {
transformer.Execute()
Expect(len(mockEventRepo.TransferLogs)).To(Equal(1))
Expect(len(mockEventRepo.ApprovalLogs)).To(Equal(1))
Expect(mockEventRepo.VulcanizeLogIDs).To(ConsistOf(logID1, logID2))
})
})

View File

@ -21,40 +21,52 @@ import (
) )
type MockWatchedEventsRepository struct { type MockWatchedEventsRepository struct {
watchedEvents []*core.WatchedEvent watchedTransferEvents []*core.WatchedEvent
watchedApprovalEvents []*core.WatchedEvent
Names []string Names []string
} }
func (mwer *MockWatchedEventsRepository) SetWatchedEvents(watchedEvents []*core.WatchedEvent) { func (mwer *MockWatchedEventsRepository) SetWatchedEvents(watchedEvents []*core.WatchedEvent) {
mwer.watchedEvents = watchedEvents for _, event := range watchedEvents {
if event.Name == "Transfer" {
mwer.watchedTransferEvents = append(mwer.watchedTransferEvents, event)
}
if event.Name == "Approval" {
mwer.watchedApprovalEvents = append(mwer.watchedApprovalEvents, event)
}
}
} }
func (mwer *MockWatchedEventsRepository) GetWatchedEvents(name string) ([]*core.WatchedEvent, error) { func (mwer *MockWatchedEventsRepository) GetWatchedEvents(name string) ([]*core.WatchedEvent, error) {
mwer.Names = append(mwer.Names, name) mwer.Names = append(mwer.Names, name)
result := mwer.watchedEvents var result []*core.WatchedEvent
if name == "Transfer" {
result = mwer.watchedTransferEvents
// clear watched events once returned so same events are returned for every filter while testing // clear watched events once returned so same events are returned for every filter while testing
mwer.watchedEvents = []*core.WatchedEvent{} mwer.watchedTransferEvents = []*core.WatchedEvent{}
}
if name == "Approval" {
result = mwer.watchedApprovalEvents
// clear watched events once returned so same events are returned for every filter while testing
mwer.watchedApprovalEvents = []*core.WatchedEvent{}
}
return result, nil return result, nil
} }
type MockTransferRepo struct { type MockEventRepo struct {
LogMakes []event_triggered.TransferModel TransferLogs []event_triggered.TransferModel
ApprovalLogs []event_triggered.ApprovalModel
VulcanizeLogIDs []int64 VulcanizeLogIDs []int64
} }
func (molr *MockTransferRepo) Create(offerModel event_triggered.TransferModel, vulcanizeLogId int64) error { func (molr *MockEventRepo) CreateTransfer(transferModel event_triggered.TransferModel, vulcanizeLogId int64) error {
molr.LogMakes = append(molr.LogMakes, offerModel) molr.TransferLogs = append(molr.TransferLogs, transferModel)
molr.VulcanizeLogIDs = append(molr.VulcanizeLogIDs, vulcanizeLogId) molr.VulcanizeLogIDs = append(molr.VulcanizeLogIDs, vulcanizeLogId)
return nil return nil
} }
type MockApprovalRepo struct { func (molk *MockEventRepo) CreateApproval(approvalModel event_triggered.ApprovalModel, vulcanizeLogID int64) error {
LogKills []event_triggered.ApprovalModel molk.ApprovalLogs = append(molk.ApprovalLogs, approvalModel)
VulcanizeLogIDs []int64
}
func (molk *MockApprovalRepo) Create(model event_triggered.ApprovalModel, vulcanizeLogID int64) error {
molk.LogKills = append(molk.LogKills, model)
molk.VulcanizeLogIDs = append(molk.VulcanizeLogIDs, vulcanizeLogID) molk.VulcanizeLogIDs = append(molk.VulcanizeLogIDs, vulcanizeLogID)
return nil return nil
} }