forked from cerc-io/ipld-eth-server
Move WatchedContract back to core
This commit is contained in:
@@ -6,10 +6,10 @@ import (
|
||||
|
||||
type InMemory struct {
|
||||
blocks map[int64]*core.Block
|
||||
watchedContracts map[string]*WatchedContract
|
||||
watchedContracts map[string]*core.WatchedContract
|
||||
}
|
||||
|
||||
func (repository *InMemory) CreateWatchedContract(watchedContract WatchedContract) error {
|
||||
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error {
|
||||
repository.watchedContracts[watchedContract.Hash] = &watchedContract
|
||||
return nil
|
||||
}
|
||||
@@ -19,7 +19,7 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
||||
return present
|
||||
}
|
||||
|
||||
func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
|
||||
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract {
|
||||
watchedContract, ok := repository.watchedContracts[contractHash]
|
||||
if !ok {
|
||||
return nil
|
||||
@@ -47,7 +47,7 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
|
||||
func NewInMemory() *InMemory {
|
||||
return &InMemory{
|
||||
blocks: make(map[int64]*core.Block),
|
||||
watchedContracts: make(map[string]*WatchedContract),
|
||||
watchedContracts: make(map[string]*core.WatchedContract),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ func NewPostgres(databaseConfig config.Database) (Postgres, error) {
|
||||
return Postgres{Db: db}, nil
|
||||
}
|
||||
|
||||
func (repository Postgres) CreateWatchedContract(contract WatchedContract) error {
|
||||
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
|
||||
abi := contract.Abi
|
||||
var abiToInsert *string
|
||||
if abi != "" {
|
||||
@@ -52,8 +52,8 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
||||
return exists
|
||||
}
|
||||
|
||||
func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract {
|
||||
var savedContracts []WatchedContract
|
||||
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract {
|
||||
var savedContracts []core.WatchedContract
|
||||
contractRows, _ := repository.Db.Query(
|
||||
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
|
||||
savedContracts = repository.loadContract(contractRows)
|
||||
@@ -197,15 +197,15 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
|
||||
return transactions
|
||||
}
|
||||
|
||||
func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContract {
|
||||
var savedContracts []WatchedContract
|
||||
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedContract {
|
||||
var savedContracts []core.WatchedContract
|
||||
for contractRows.Next() {
|
||||
var savedContractHash string
|
||||
var savedContractAbi string
|
||||
contractRows.Scan(&savedContractHash, &savedContractAbi)
|
||||
transactionRows, _ := repository.Db.Query(`SELECT tx_hash, tx_nonce, tx_to, tx_from, tx_gaslimit, tx_gasprice, tx_value FROM transactions WHERE tx_to = $1 ORDER BY block_id desc`, savedContractHash)
|
||||
transactions := repository.loadTransactions(transactionRows)
|
||||
savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
|
||||
savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
|
||||
savedContracts = append(savedContracts, savedContract)
|
||||
}
|
||||
return savedContracts
|
||||
|
||||
@@ -8,7 +8,7 @@ type Repository interface {
|
||||
FindBlockByNumber(blockNumber int64) *core.Block
|
||||
MaxBlockNumber() int64
|
||||
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
||||
CreateWatchedContract(contract WatchedContract) error
|
||||
CreateWatchedContract(contract core.WatchedContract) error
|
||||
IsWatchedContract(contractHash string) bool
|
||||
FindWatchedContract(contractHash string) *WatchedContract
|
||||
FindWatchedContract(contractHash string) *core.WatchedContract
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
|
||||
Describe("Creating watched contracts", func() {
|
||||
It("returns the watched contract when it exists", func() {
|
||||
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).NotTo(BeNil())
|
||||
@@ -226,7 +226,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
})
|
||||
|
||||
It("returns empty array when no transactions 'To' a watched contract", func() {
|
||||
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Transactions).To(BeEmpty())
|
||||
@@ -243,7 +243,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
}
|
||||
repository.CreateBlock(block)
|
||||
|
||||
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
|
||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Transactions).To(
|
||||
@@ -254,7 +254,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
})
|
||||
|
||||
It("stores the ABI of the contract", func() {
|
||||
repository.CreateWatchedContract(repositories.WatchedContract{
|
||||
repository.CreateWatchedContract(core.WatchedContract{
|
||||
Abi: "{\"some\": \"json\"}",
|
||||
Hash: "x123",
|
||||
})
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package repositories
|
||||
|
||||
import "github.com/8thlight/vulcanizedb/pkg/core"
|
||||
|
||||
type WatchedContract struct {
|
||||
Abi string
|
||||
Hash string
|
||||
Transactions []core.Transaction
|
||||
}
|
||||
Reference in New Issue
Block a user