Store contract ABI on watched_contracts
This commit is contained in:
parent
52e3266495
commit
fa2766b64d
@ -0,0 +1,2 @@
|
||||
ALTER TABLE watched_contracts
|
||||
DROP COLUMN contract_abi;
|
@ -0,0 +1,2 @@
|
||||
ALTER TABLE watched_contracts
|
||||
ADD COLUMN contract_abi json;
|
@ -2,8 +2,8 @@
|
||||
-- PostgreSQL database dump
|
||||
--
|
||||
|
||||
-- Dumped from database version 10.1
|
||||
-- Dumped by pg_dump version 10.1
|
||||
-- Dumped from database version 10.0
|
||||
-- Dumped by pg_dump version 10.0
|
||||
|
||||
SET statement_timeout = 0;
|
||||
SET lock_timeout = 0;
|
||||
@ -124,7 +124,8 @@ ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
|
||||
|
||||
CREATE TABLE watched_contracts (
|
||||
contract_id integer NOT NULL,
|
||||
contract_hash character varying(66)
|
||||
contract_hash character varying(66),
|
||||
contract_abi json
|
||||
);
|
||||
|
||||
|
||||
|
@ -20,18 +20,18 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
||||
}
|
||||
|
||||
func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
|
||||
var transactions []core.Transaction
|
||||
if _, ok := repository.watchedContracts[contractHash]; !ok {
|
||||
watchedContract, ok := repository.watchedContracts[contractHash]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
for _, block := range repository.blocks {
|
||||
for _, transaction := range block.Transactions {
|
||||
if transaction.To == contractHash {
|
||||
transactions = append(transactions, transaction)
|
||||
watchedContract.Transactions = append(watchedContract.Transactions, transaction)
|
||||
}
|
||||
}
|
||||
}
|
||||
return &WatchedContract{Hash: contractHash, Transactions: transactions}
|
||||
return watchedContract
|
||||
}
|
||||
|
||||
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
|
||||
|
@ -32,8 +32,13 @@ func NewPostgres(databaseConfig config.Database) (Postgres, error) {
|
||||
}
|
||||
|
||||
func (repository Postgres) CreateWatchedContract(contract WatchedContract) error {
|
||||
abi := contract.Abi
|
||||
var abiToInsert *string
|
||||
if abi != "" {
|
||||
abiToInsert = &abi
|
||||
}
|
||||
_, err := repository.Db.Exec(
|
||||
`INSERT INTO watched_contracts (contract_hash) VALUES ($1)`, contract.Hash)
|
||||
`INSERT INTO watched_contracts (contract_hash, contract_abi) VALUES ($1, $2)`, contract.Hash, abiToInsert)
|
||||
if err != nil {
|
||||
return ErrDBInsertFailed
|
||||
}
|
||||
@ -50,7 +55,7 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
||||
func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract {
|
||||
var savedContracts []WatchedContract
|
||||
contractRows, _ := repository.Db.Query(
|
||||
`SELECT contract_hash FROM watched_contracts WHERE contract_hash=$1`, contractHash)
|
||||
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
|
||||
savedContracts = repository.loadContract(contractRows)
|
||||
if len(savedContracts) > 0 {
|
||||
return &savedContracts[0]
|
||||
@ -196,10 +201,11 @@ func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContrac
|
||||
var savedContracts []WatchedContract
|
||||
for contractRows.Next() {
|
||||
var savedContractHash string
|
||||
contractRows.Scan(&savedContractHash)
|
||||
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}
|
||||
savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
|
||||
savedContracts = append(savedContracts, savedContract)
|
||||
}
|
||||
return savedContracts
|
||||
|
@ -230,7 +230,6 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Transactions).To(BeEmpty())
|
||||
|
||||
})
|
||||
|
||||
It("returns transactions 'To' a watched contract", func() {
|
||||
@ -253,6 +252,16 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
||||
{Hash: "TRANSACTION3", To: "x123"},
|
||||
}))
|
||||
})
|
||||
|
||||
It("stores the ABI of the contract", func() {
|
||||
repository.CreateWatchedContract(repositories.WatchedContract{
|
||||
Abi: "{\"some\": \"json\"}",
|
||||
Hash: "x123",
|
||||
})
|
||||
watchedContract := repository.FindWatchedContract("x123")
|
||||
Expect(watchedContract).ToNot(BeNil())
|
||||
Expect(watchedContract.Abi).To(Equal("{\"some\": \"json\"}"))
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package repositories
|
||||
import "github.com/8thlight/vulcanizedb/pkg/core"
|
||||
|
||||
type WatchedContract struct {
|
||||
Abi string
|
||||
Hash string
|
||||
Transactions []core.Transaction
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user