diff --git a/db/migrations/1512417153_add_abi_to_watched_contracts.down.sql b/db/migrations/1512417153_add_abi_to_watched_contracts.down.sql new file mode 100644 index 00000000..e22ac41f --- /dev/null +++ b/db/migrations/1512417153_add_abi_to_watched_contracts.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE watched_contracts + DROP COLUMN contract_abi; \ No newline at end of file diff --git a/db/migrations/1512417153_add_abi_to_watched_contracts.up.sql b/db/migrations/1512417153_add_abi_to_watched_contracts.up.sql new file mode 100644 index 00000000..4212ee9f --- /dev/null +++ b/db/migrations/1512417153_add_abi_to_watched_contracts.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE watched_contracts + ADD COLUMN contract_abi json; \ No newline at end of file diff --git a/db/schema.sql b/db/schema.sql index 0c804371..c788d169 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -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 ); diff --git a/pkg/repositories/in_memory.go b/pkg/repositories/in_memory.go index 93d77fb2..dfd975d7 100644 --- a/pkg/repositories/in_memory.go +++ b/pkg/repositories/in_memory.go @@ -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 { diff --git a/pkg/repositories/postgres.go b/pkg/repositories/postgres.go index 5c5b67b2..e36b7fa1 100644 --- a/pkg/repositories/postgres.go +++ b/pkg/repositories/postgres.go @@ -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 diff --git a/pkg/repositories/testing/helpers.go b/pkg/repositories/testing/helpers.go index 4b66877b..766a37be 100644 --- a/pkg/repositories/testing/helpers.go +++ b/pkg/repositories/testing/helpers.go @@ -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\"}")) + }) }) } diff --git a/pkg/repositories/watched_contract.go b/pkg/repositories/watched_contract.go index 9d56544b..14daaa62 100644 --- a/pkg/repositories/watched_contract.go +++ b/pkg/repositories/watched_contract.go @@ -3,6 +3,7 @@ package repositories import "github.com/8thlight/vulcanizedb/pkg/core" type WatchedContract struct { + Abi string Hash string Transactions []core.Transaction }