Add contracts table / start building out watch contracts
This commit is contained in:
parent
84205a21ea
commit
30fadffb14
@ -36,6 +36,16 @@ func tasks(p *do.Project) {
|
||||
do.M{"environment": environment, "startingNumber": startingNumber, "$in": "cmd/populate_blocks"})
|
||||
})
|
||||
|
||||
p.Task("subscribeToContract", nil, func(context *do.Context) {
|
||||
environment := parseEnvironment(context)
|
||||
contractHash := context.Args.MayString("", "contract-hash", "c")
|
||||
if contractHash == "" {
|
||||
log.Fatalln("--contract-hash required")
|
||||
}
|
||||
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}}`,
|
||||
do.M{"environment": environment, "contractHash": contractHash, "$in": "cmd/subscribe_contract"})
|
||||
})
|
||||
|
||||
p.Task("migrate", nil, func(context *do.Context) {
|
||||
environment := parseEnvironment(context)
|
||||
cfg := cmd.LoadConfig(environment)
|
||||
|
18
cmd/subscribe_contract/main.go
Normal file
18
cmd/subscribe_contract/main.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/8thlight/vulcanizedb/cmd"
|
||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||
)
|
||||
|
||||
func main() {
|
||||
environment := flag.String("environment", "", "Environment name")
|
||||
contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
|
||||
flag.Parse()
|
||||
config := cmd.LoadConfig(*environment)
|
||||
repository := repositories.NewPostgres(config.Database)
|
||||
repository.CreateContract(core.Contract{Hash: *contractHash})
|
||||
}
|
1
db/migrations/1510257608_add_contracts_table.down.sql
Normal file
1
db/migrations/1510257608_add_contracts_table.down.sql
Normal file
@ -0,0 +1 @@
|
||||
DROP TABLE contracts
|
5
db/migrations/1510257608_add_contracts_table.up.sql
Normal file
5
db/migrations/1510257608_add_contracts_table.up.sql
Normal file
@ -0,0 +1,5 @@
|
||||
CREATE TABLE contracts
|
||||
(
|
||||
contract_id SERIAL PRIMARY KEY,
|
||||
contract_hash VARCHAR(66)
|
||||
)
|
@ -72,6 +72,35 @@ CREATE SEQUENCE blocks_id_seq
|
||||
ALTER SEQUENCE blocks_id_seq OWNED BY blocks.id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: contracts; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE TABLE contracts (
|
||||
contract_id integer NOT NULL,
|
||||
contract_hash character varying(66)
|
||||
);
|
||||
|
||||
|
||||
--
|
||||
-- Name: contracts_contract_id_seq; Type: SEQUENCE; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE SEQUENCE contracts_contract_id_seq
|
||||
START WITH 1
|
||||
INCREMENT BY 1
|
||||
NO MINVALUE
|
||||
NO MAXVALUE
|
||||
CACHE 1;
|
||||
|
||||
|
||||
--
|
||||
-- Name: contracts_contract_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER SEQUENCE contracts_contract_id_seq OWNED BY contracts.contract_id;
|
||||
|
||||
|
||||
--
|
||||
-- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
|
||||
--
|
||||
@ -124,6 +153,13 @@ ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
|
||||
ALTER TABLE ONLY blocks ALTER COLUMN id SET DEFAULT nextval('blocks_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: contracts contract_id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY contracts ALTER COLUMN contract_id SET DEFAULT nextval('contracts_contract_id_seq'::regclass);
|
||||
|
||||
|
||||
--
|
||||
-- Name: transactions id; Type: DEFAULT; Schema: public; Owner: -
|
||||
--
|
||||
@ -139,6 +175,14 @@ ALTER TABLE ONLY blocks
|
||||
ADD CONSTRAINT blocks_pkey PRIMARY KEY (id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: contracts contracts_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY contracts
|
||||
ADD CONSTRAINT contracts_pkey PRIMARY KEY (contract_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
|
||||
--
|
||||
|
5
pkg/core/contract.go
Normal file
5
pkg/core/contract.go
Normal file
@ -0,0 +1,5 @@
|
||||
package core
|
||||
|
||||
type Contract struct {
|
||||
Hash string
|
||||
}
|
@ -5,7 +5,18 @@ import (
|
||||
)
|
||||
|
||||
type InMemory struct {
|
||||
blocks map[int64]*core.Block
|
||||
blocks map[int64]*core.Block
|
||||
contracts map[string]*core.Contract
|
||||
}
|
||||
|
||||
func (repository *InMemory) CreateContract(contract core.Contract) error {
|
||||
repository.contracts[contract.Hash] = &contract
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
||||
_, present := repository.contracts[contractHash]
|
||||
return present
|
||||
}
|
||||
|
||||
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
|
||||
@ -20,7 +31,8 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
|
||||
|
||||
func NewInMemory() *InMemory {
|
||||
return &InMemory{
|
||||
blocks: make(map[int64]*core.Block),
|
||||
blocks: make(map[int64]*core.Block),
|
||||
contracts: make(map[string]*core.Contract),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,10 @@ type Postgres struct {
|
||||
Db *sqlx.DB
|
||||
}
|
||||
|
||||
var (
|
||||
ErrDBInsertFailed = errors.New("postgres: insert failed")
|
||||
)
|
||||
|
||||
func NewPostgres(databaseConfig config.Database) Postgres {
|
||||
connectString := config.DbConnectionString(databaseConfig)
|
||||
db, err := sqlx.Connect("postgres", connectString)
|
||||
@ -27,9 +31,24 @@ func NewPostgres(databaseConfig config.Database) Postgres {
|
||||
return Postgres{Db: db}
|
||||
}
|
||||
|
||||
var (
|
||||
ErrDBInsertFailed = errors.New("postgres: insert failed")
|
||||
)
|
||||
func (repository Postgres) CreateContract(contract core.Contract) error {
|
||||
_, err := repository.Db.Exec(
|
||||
`INSERT INTO contracts (contract_hash) VALUES ($1)`, contract.Hash)
|
||||
if err != nil {
|
||||
return ErrDBInsertFailed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
||||
var exists bool
|
||||
err := repository.Db.QueryRow(
|
||||
`SELECT exists(select 1 from contracts where contract_hash=$1) FROM contracts`, contractHash).Scan(&exists)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Fatalf("error checking if row exists %v", err)
|
||||
}
|
||||
return exists
|
||||
}
|
||||
|
||||
func (repository Postgres) MaxBlockNumber() int64 {
|
||||
var highestBlockNumber int64
|
||||
|
@ -8,4 +8,6 @@ type Repository interface {
|
||||
FindBlockByNumber(blockNumber int64) *core.Block
|
||||
MaxBlockNumber() int64
|
||||
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
||||
CreateContract(contract core.Contract) error
|
||||
IsWatchedContract(contractHash string) bool
|
||||
}
|
||||
|
@ -192,6 +192,13 @@ var _ = Describe("Repositories", func() {
|
||||
Expect(repository.MissingBlockNumbers(1, 5)).To(Equal([]int64{1, 2, 4, 5}))
|
||||
})
|
||||
|
||||
It("Adds a contract to the contracts table", func() {
|
||||
repository.CreateContract(core.Contract{Hash: "x123"})
|
||||
|
||||
Expect(repository.IsWatchedContract("x123")).To(BeTrue())
|
||||
Expect(repository.IsWatchedContract("x456")).To(BeFalse())
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Describe("The max block numbers", func() {
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
if [ $# -eq 1 ]
|
||||
then
|
||||
migrate create -dir ./migrations -ext sql $1
|
||||
migrate create -dir ./db/migrations -ext sql $1
|
||||
else
|
||||
echo "**An Error Occurred**"
|
||||
echo "Usage: ./scripts/create_migration <migration-name>"
|
||||
|
Loading…
Reference in New Issue
Block a user