2018-11-07 21:50:43 +00:00
|
|
|
// VulcanizeDB
|
2019-03-12 15:46:42 +00:00
|
|
|
// Copyright © 2019 Vulcanize
|
2018-11-07 21:50:43 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-02-13 16:31:57 +00:00
|
|
|
package repositories
|
2018-02-02 21:53:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-02-02 21:53:16 +00:00
|
|
|
)
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
type ContractRepository struct {
|
2018-02-13 16:31:57 +00:00
|
|
|
*postgres.DB
|
2018-02-12 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (contractRepository ContractRepository) CreateContract(contract core.Contract) error {
|
2018-02-02 21:53:16 +00:00
|
|
|
abi := contract.Abi
|
|
|
|
var abiToInsert *string
|
|
|
|
if abi != "" {
|
|
|
|
abiToInsert = &abi
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
_, err := contractRepository.DB.Exec(
|
2018-02-02 21:53:16 +00:00
|
|
|
`INSERT INTO watched_contracts (contract_hash, contract_abi)
|
|
|
|
VALUES ($1, $2)
|
|
|
|
ON CONFLICT (contract_hash)
|
|
|
|
DO UPDATE
|
|
|
|
SET contract_hash = $1, contract_abi = $2
|
|
|
|
`, contract.Hash, abiToInsert)
|
|
|
|
if err != nil {
|
2019-03-19 19:11:26 +00:00
|
|
|
return postgres.ErrDBInsertFailed(err)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:03:57 +00:00
|
|
|
func (contractRepository ContractRepository) ContractExists(contractHash string) (bool, error) {
|
2018-02-02 21:53:16 +00:00
|
|
|
var exists bool
|
2019-02-14 15:03:57 +00:00
|
|
|
err := contractRepository.DB.QueryRow(
|
2018-02-02 21:53:16 +00:00
|
|
|
`SELECT exists(
|
|
|
|
SELECT 1
|
|
|
|
FROM watched_contracts
|
|
|
|
WHERE contract_hash = $1)`, contractHash).Scan(&exists)
|
2019-02-14 15:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return exists, nil
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (contractRepository ContractRepository) GetContract(contractHash string) (core.Contract, error) {
|
2018-02-02 21:53:16 +00:00
|
|
|
var hash string
|
|
|
|
var abi string
|
2018-02-12 16:54:05 +00:00
|
|
|
contract := contractRepository.DB.QueryRow(
|
2018-02-02 21:53:16 +00:00
|
|
|
`SELECT contract_hash, contract_abi FROM watched_contracts WHERE contract_hash=$1`, contractHash)
|
|
|
|
err := contract.Scan(&hash, &abi)
|
|
|
|
if err == sql.ErrNoRows {
|
2018-02-13 16:31:57 +00:00
|
|
|
return core.Contract{}, datastore.ErrContractDoesNotExist(contractHash)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2019-02-14 15:03:57 +00:00
|
|
|
savedContract, err := contractRepository.addTransactions(core.Contract{Hash: hash, Abi: abi})
|
|
|
|
if err != nil {
|
|
|
|
return core.Contract{}, err
|
|
|
|
}
|
2018-02-02 21:53:16 +00:00
|
|
|
return savedContract, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:03:57 +00:00
|
|
|
func (contractRepository ContractRepository) addTransactions(contract core.Contract) (core.Contract, error) {
|
|
|
|
transactionRows, err := contractRepository.DB.Queryx(`
|
2018-02-02 22:12:14 +00:00
|
|
|
SELECT hash,
|
|
|
|
nonce,
|
2018-02-02 21:53:16 +00:00
|
|
|
tx_to,
|
|
|
|
tx_from,
|
2018-02-02 22:12:14 +00:00
|
|
|
gaslimit,
|
|
|
|
gasprice,
|
|
|
|
value,
|
|
|
|
input_data
|
2019-03-19 19:11:26 +00:00
|
|
|
FROM full_sync_transactions
|
2018-02-02 21:53:16 +00:00
|
|
|
WHERE tx_to = $1
|
|
|
|
ORDER BY block_id DESC`, contract.Hash)
|
2019-02-14 15:03:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return core.Contract{}, err
|
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
blockRepository := &BlockRepository{contractRepository.DB}
|
|
|
|
transactions := blockRepository.LoadTransactions(transactionRows)
|
2018-02-02 21:53:16 +00:00
|
|
|
savedContract := core.Contract{Hash: contract.Hash, Transactions: transactions, Abi: contract.Abi}
|
2019-02-14 15:03:57 +00:00
|
|
|
return savedContract, nil
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|