2018-02-13 16:31:57 +00:00
|
|
|
package repositories
|
2018-02-02 21:53:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"database/sql"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
2018-02-13 16:31:57 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
2018-02-02 21:53:16 +00:00
|
|
|
)
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
type LogRepository struct {
|
2018-02-13 16:31:57 +00:00
|
|
|
*postgres.DB
|
2018-02-12 16:54:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 16:17:02 +00:00
|
|
|
func (logRepository LogRepository) CreateLogs(lgs []core.Log, receiptId int64) error {
|
2018-02-12 16:54:05 +00:00
|
|
|
tx, _ := logRepository.DB.BeginTx(context.Background(), nil)
|
|
|
|
for _, tlog := range lgs {
|
2018-02-02 21:53:16 +00:00
|
|
|
_, err := tx.Exec(
|
2018-05-02 16:17:02 +00:00
|
|
|
`INSERT INTO logs (block_number, address, tx_hash, index, topic0, topic1, topic2, topic3, data, receipt_id)
|
|
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
2018-02-02 21:53:16 +00:00
|
|
|
`,
|
2018-05-02 16:17:02 +00:00
|
|
|
tlog.BlockNumber, tlog.Address, tlog.TxHash, tlog.Index, tlog.Topics[0], tlog.Topics[1], tlog.Topics[2], tlog.Topics[3], tlog.Data, receiptId,
|
2018-02-02 21:53:16 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2018-02-13 16:31:57 +00:00
|
|
|
return postgres.ErrDBInsertFailed
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (logRepository LogRepository) GetLogs(address string, blockNumber int64) []core.Log {
|
|
|
|
logRows, _ := logRepository.DB.Query(
|
2018-02-02 21:53:16 +00:00
|
|
|
`SELECT block_number,
|
|
|
|
address,
|
|
|
|
tx_hash,
|
|
|
|
index,
|
|
|
|
topic0,
|
|
|
|
topic1,
|
|
|
|
topic2,
|
|
|
|
topic3,
|
|
|
|
data
|
|
|
|
FROM logs
|
|
|
|
WHERE address = $1 AND block_number = $2
|
|
|
|
ORDER BY block_number DESC`, address, blockNumber)
|
2018-02-12 16:54:05 +00:00
|
|
|
return logRepository.loadLogs(logRows)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 16:54:05 +00:00
|
|
|
func (logRepository LogRepository) loadLogs(logsRows *sql.Rows) []core.Log {
|
|
|
|
var lgs []core.Log
|
2018-02-02 21:53:16 +00:00
|
|
|
for logsRows.Next() {
|
|
|
|
var blockNumber int64
|
|
|
|
var address string
|
|
|
|
var txHash string
|
|
|
|
var index int64
|
|
|
|
var data string
|
|
|
|
var topics core.Topics
|
|
|
|
logsRows.Scan(&blockNumber, &address, &txHash, &index, &topics[0], &topics[1], &topics[2], &topics[3], &data)
|
2018-02-12 16:54:05 +00:00
|
|
|
lg := core.Log{
|
2018-02-02 21:53:16 +00:00
|
|
|
BlockNumber: blockNumber,
|
|
|
|
TxHash: txHash,
|
|
|
|
Address: address,
|
|
|
|
Index: index,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
for i, topic := range topics {
|
2018-02-12 16:54:05 +00:00
|
|
|
lg.Topics[i] = topic
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
lgs = append(lgs, lg)
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|
2018-02-12 16:54:05 +00:00
|
|
|
return lgs
|
2018-02-02 21:53:16 +00:00
|
|
|
}
|