Merge pull request #87 from 8thlight/store-abi-in-column
Start storing contract ABI in DB
This commit is contained in:
commit
e4a05858f8
@ -39,11 +39,20 @@ func tasks(p *do.Project) {
|
|||||||
p.Task("watchContract", nil, func(context *do.Context) {
|
p.Task("watchContract", nil, func(context *do.Context) {
|
||||||
environment := parseEnvironment(context)
|
environment := parseEnvironment(context)
|
||||||
contractHash := context.Args.MayString("", "contract-hash", "c")
|
contractHash := context.Args.MayString("", "contract-hash", "c")
|
||||||
|
abiFilepath := context.Args.MayString("", "abi-filepath", "a")
|
||||||
if contractHash == "" {
|
if contractHash == "" {
|
||||||
log.Fatalln("--contract-hash required")
|
log.Fatalln("--contract-hash required")
|
||||||
}
|
}
|
||||||
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}}`,
|
if abiFilepath == "" {
|
||||||
do.M{"environment": environment, "contractHash": contractHash, "$in": "cmd/subscribe_contract"})
|
log.Fatalln("--abi-filepath required")
|
||||||
|
}
|
||||||
|
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}} --abi-filepath={{.abiFilepath}}`,
|
||||||
|
do.M{
|
||||||
|
"environment": environment,
|
||||||
|
"contractHash": contractHash,
|
||||||
|
"abiFilepath": abiFilepath,
|
||||||
|
"$in": "cmd/subscribe_contract",
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
p.Task("migrate", nil, func(context *do.Context) {
|
p.Task("migrate", nil, func(context *do.Context) {
|
||||||
|
@ -4,14 +4,19 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"github.com/8thlight/vulcanizedb/cmd"
|
"github.com/8thlight/vulcanizedb/cmd"
|
||||||
"github.com/8thlight/vulcanizedb/pkg/core"
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
environment := flag.String("environment", "", "Environment name")
|
environment := flag.String("environment", "", "Environment name")
|
||||||
contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
|
contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
|
||||||
|
abiFilepath := flag.String("abi-filepath", "", "path/to/abifile.json")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
config := cmd.LoadConfig(*environment)
|
config := cmd.LoadConfig(*environment)
|
||||||
repository := cmd.LoadPostgres(config.Database)
|
repository := cmd.LoadPostgres(config.Database)
|
||||||
repository.CreateWatchedContract(core.WatchedContract{Hash: *contractHash})
|
watchedContract := repositories.WatchedContract{
|
||||||
|
Abi: cmd.ReadAbiFile(*abiFilepath),
|
||||||
|
Hash: *contractHash,
|
||||||
|
}
|
||||||
|
repository.CreateWatchedContract(watchedContract)
|
||||||
}
|
}
|
||||||
|
14
cmd/utils.go
14
cmd/utils.go
@ -3,7 +3,10 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/8thlight/vulcanizedb/pkg/config"
|
"github.com/8thlight/vulcanizedb/pkg/config"
|
||||||
|
"github.com/8thlight/vulcanizedb/pkg/geth"
|
||||||
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
"github.com/8thlight/vulcanizedb/pkg/repositories"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,3 +25,14 @@ func LoadPostgres(database config.Database) repositories.Postgres {
|
|||||||
}
|
}
|
||||||
return repository
|
return repository
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadAbiFile(abiFilepath string) string {
|
||||||
|
if !filepath.IsAbs(abiFilepath) {
|
||||||
|
abiFilepath = filepath.Join(config.ProjectRoot(), abiFilepath)
|
||||||
|
}
|
||||||
|
abi, err := geth.ReadAbiFile(abiFilepath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error reading ABI file at \"%s\"\n %v", abiFilepath, err)
|
||||||
|
}
|
||||||
|
return abi
|
||||||
|
}
|
||||||
|
@ -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
|
-- PostgreSQL database dump
|
||||||
--
|
--
|
||||||
|
|
||||||
-- Dumped from database version 10.1
|
-- Dumped from database version 10.0
|
||||||
-- Dumped by pg_dump version 10.1
|
-- Dumped by pg_dump version 10.0
|
||||||
|
|
||||||
SET statement_timeout = 0;
|
SET statement_timeout = 0;
|
||||||
SET lock_timeout = 0;
|
SET lock_timeout = 0;
|
||||||
@ -124,7 +124,8 @@ ALTER SEQUENCE transactions_id_seq OWNED BY transactions.id;
|
|||||||
|
|
||||||
CREATE TABLE watched_contracts (
|
CREATE TABLE watched_contracts (
|
||||||
contract_id integer NOT NULL,
|
contract_id integer NOT NULL,
|
||||||
contract_hash character varying(66)
|
contract_hash character varying(66),
|
||||||
|
contract_abi json
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
type WatchedContract struct {
|
|
||||||
Hash string
|
|
||||||
Transactions []Transaction
|
|
||||||
}
|
|
||||||
|
|
||||||
type Contract struct {
|
type Contract struct {
|
||||||
Attributes ContractAttributes
|
Attributes ContractAttributes
|
||||||
Hash string
|
Hash string
|
||||||
@ -20,6 +15,7 @@ type ContractAttributes []ContractAttribute
|
|||||||
func (attributes ContractAttributes) Len() int {
|
func (attributes ContractAttributes) Len() int {
|
||||||
return len(attributes)
|
return len(attributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (attributes ContractAttributes) Swap(i, j int) {
|
func (attributes ContractAttributes) Swap(i, j int) {
|
||||||
attributes[i], attributes[j] = attributes[j], attributes[i]
|
attributes[i], attributes[j] = attributes[j], attributes[i]
|
||||||
}
|
}
|
@ -14,14 +14,21 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
|
func ParseAbiFile(abiFilePath string) (abi.ABI, error) {
|
||||||
filesBytes, err := ioutil.ReadFile(abiFilePath)
|
abiString, err := ReadAbiFile(abiFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return abi.ABI{}, ErrMissingAbiFile
|
return abi.ABI{}, ErrMissingAbiFile
|
||||||
}
|
}
|
||||||
abiString := string(filesBytes)
|
|
||||||
parsedAbi, err := abi.JSON(strings.NewReader(abiString))
|
parsedAbi, err := abi.JSON(strings.NewReader(abiString))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return abi.ABI{}, ErrInvalidAbiFile
|
return abi.ABI{}, ErrInvalidAbiFile
|
||||||
}
|
}
|
||||||
return parsedAbi, nil
|
return parsedAbi, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadAbiFile(abiFilePath string) (string, error) {
|
||||||
|
filesBytes, err := ioutil.ReadFile(abiFilePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", ErrMissingAbiFile
|
||||||
|
}
|
||||||
|
return string(filesBytes), nil
|
||||||
|
}
|
||||||
|
@ -21,6 +21,15 @@ var _ = Describe("Reading ABI files", func() {
|
|||||||
Expect(err).To(BeNil())
|
Expect(err).To(BeNil())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("reads the contents of a valid ABI file", func() {
|
||||||
|
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "valid_abi.json")
|
||||||
|
|
||||||
|
contractAbi, err := geth.ReadAbiFile(path)
|
||||||
|
|
||||||
|
Expect(contractAbi).To(Equal("[{\"foo\": \"bar\"}]"))
|
||||||
|
Expect(err).To(BeNil())
|
||||||
|
})
|
||||||
|
|
||||||
It("returns an error when the file does not exist", func() {
|
It("returns an error when the file does not exist", func() {
|
||||||
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json")
|
path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json")
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attribute
|
|||||||
}
|
}
|
||||||
input, err := parsed.Pack(attributeName)
|
input, err := parsed.Pack(attributeName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, ErrInvalidStateAttribute
|
||||||
}
|
}
|
||||||
output, err := callContract(contract, input, err, blockchain, blockNumber)
|
output, err := callContract(contract, input, err, blockchain, blockNumber)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -59,7 +59,7 @@ func (blockchain *GethBlockchain) GetAttribute(contract core.Contract, attribute
|
|||||||
}
|
}
|
||||||
err = parsed.Unpack(&result, attributeName, output)
|
err = parsed.Unpack(&result, attributeName, output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ErrInvalidStateAttribute
|
return nil, err
|
||||||
}
|
}
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
[{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]
|
[{"foo": "bar"}]
|
@ -6,10 +6,10 @@ import (
|
|||||||
|
|
||||||
type InMemory struct {
|
type InMemory struct {
|
||||||
blocks map[int64]*core.Block
|
blocks map[int64]*core.Block
|
||||||
watchedContracts map[string]*core.WatchedContract
|
watchedContracts map[string]*WatchedContract
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository *InMemory) CreateWatchedContract(watchedContract core.WatchedContract) error {
|
func (repository *InMemory) CreateWatchedContract(watchedContract WatchedContract) error {
|
||||||
repository.watchedContracts[watchedContract.Hash] = &watchedContract
|
repository.watchedContracts[watchedContract.Hash] = &watchedContract
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -19,19 +19,19 @@ func (repository *InMemory) IsWatchedContract(contractHash string) bool {
|
|||||||
return present
|
return present
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository *InMemory) FindWatchedContract(contractHash string) *core.WatchedContract {
|
func (repository *InMemory) FindWatchedContract(contractHash string) *WatchedContract {
|
||||||
var transactions []core.Transaction
|
watchedContract, ok := repository.watchedContracts[contractHash]
|
||||||
if _, ok := repository.watchedContracts[contractHash]; !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
for _, block := range repository.blocks {
|
for _, block := range repository.blocks {
|
||||||
for _, transaction := range block.Transactions {
|
for _, transaction := range block.Transactions {
|
||||||
if transaction.To == contractHash {
|
if transaction.To == contractHash {
|
||||||
transactions = append(transactions, transaction)
|
watchedContract.Transactions = append(watchedContract.Transactions, transaction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &core.WatchedContract{Hash: contractHash, Transactions: transactions}
|
return watchedContract
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
|
func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64 {
|
||||||
@ -47,7 +47,7 @@ func (repository *InMemory) MissingBlockNumbers(startingBlockNumber int64, endin
|
|||||||
func NewInMemory() *InMemory {
|
func NewInMemory() *InMemory {
|
||||||
return &InMemory{
|
return &InMemory{
|
||||||
blocks: make(map[int64]*core.Block),
|
blocks: make(map[int64]*core.Block),
|
||||||
watchedContracts: make(map[string]*core.WatchedContract),
|
watchedContracts: make(map[string]*WatchedContract),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,9 +31,14 @@ func NewPostgres(databaseConfig config.Database) (Postgres, error) {
|
|||||||
return Postgres{Db: db}, nil
|
return Postgres{Db: db}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository Postgres) CreateWatchedContract(contract core.WatchedContract) error {
|
func (repository Postgres) CreateWatchedContract(contract WatchedContract) error {
|
||||||
|
abi := contract.Abi
|
||||||
|
var abiToInsert *string
|
||||||
|
if abi != "" {
|
||||||
|
abiToInsert = &abi
|
||||||
|
}
|
||||||
_, err := repository.Db.Exec(
|
_, 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 {
|
if err != nil {
|
||||||
return ErrDBInsertFailed
|
return ErrDBInsertFailed
|
||||||
}
|
}
|
||||||
@ -47,10 +52,10 @@ func (repository Postgres) IsWatchedContract(contractHash string) bool {
|
|||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository Postgres) FindWatchedContract(contractHash string) *core.WatchedContract {
|
func (repository Postgres) FindWatchedContract(contractHash string) *WatchedContract {
|
||||||
var savedContracts []core.WatchedContract
|
var savedContracts []WatchedContract
|
||||||
contractRows, _ := repository.Db.Query(
|
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)
|
savedContracts = repository.loadContract(contractRows)
|
||||||
if len(savedContracts) > 0 {
|
if len(savedContracts) > 0 {
|
||||||
return &savedContracts[0]
|
return &savedContracts[0]
|
||||||
@ -192,14 +197,15 @@ func (repository Postgres) loadTransactions(transactionRows *sql.Rows) []core.Tr
|
|||||||
return transactions
|
return transactions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repository Postgres) loadContract(contractRows *sql.Rows) []core.WatchedContract {
|
func (repository Postgres) loadContract(contractRows *sql.Rows) []WatchedContract {
|
||||||
var savedContracts []core.WatchedContract
|
var savedContracts []WatchedContract
|
||||||
for contractRows.Next() {
|
for contractRows.Next() {
|
||||||
var savedContractHash string
|
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)
|
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)
|
transactions := repository.loadTransactions(transactionRows)
|
||||||
savedContract := core.WatchedContract{Hash: savedContractHash, Transactions: transactions}
|
savedContract := WatchedContract{Hash: savedContractHash, Transactions: transactions, Abi: savedContractAbi}
|
||||||
savedContracts = append(savedContracts, savedContract)
|
savedContracts = append(savedContracts, savedContract)
|
||||||
}
|
}
|
||||||
return savedContracts
|
return savedContracts
|
||||||
|
@ -8,7 +8,7 @@ type Repository interface {
|
|||||||
FindBlockByNumber(blockNumber int64) *core.Block
|
FindBlockByNumber(blockNumber int64) *core.Block
|
||||||
MaxBlockNumber() int64
|
MaxBlockNumber() int64
|
||||||
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
MissingBlockNumbers(startingBlockNumber int64, endingBlockNumber int64) []int64
|
||||||
CreateWatchedContract(contract core.WatchedContract) error
|
CreateWatchedContract(contract WatchedContract) error
|
||||||
IsWatchedContract(contractHash string) bool
|
IsWatchedContract(contractHash string) bool
|
||||||
FindWatchedContract(contractHash string) *core.WatchedContract
|
FindWatchedContract(contractHash string) *WatchedContract
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
|||||||
|
|
||||||
Describe("Creating watched contracts", func() {
|
Describe("Creating watched contracts", func() {
|
||||||
It("returns the watched contract when it exists", func() {
|
It("returns the watched contract when it exists", func() {
|
||||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
|
||||||
|
|
||||||
watchedContract := repository.FindWatchedContract("x123")
|
watchedContract := repository.FindWatchedContract("x123")
|
||||||
Expect(watchedContract).NotTo(BeNil())
|
Expect(watchedContract).NotTo(BeNil())
|
||||||
@ -226,11 +226,10 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
It("returns empty array when no transactions 'To' a watched contract", func() {
|
It("returns empty array when no transactions 'To' a watched contract", func() {
|
||||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
|
||||||
watchedContract := repository.FindWatchedContract("x123")
|
watchedContract := repository.FindWatchedContract("x123")
|
||||||
Expect(watchedContract).ToNot(BeNil())
|
Expect(watchedContract).ToNot(BeNil())
|
||||||
Expect(watchedContract.Transactions).To(BeEmpty())
|
Expect(watchedContract.Transactions).To(BeEmpty())
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
It("returns transactions 'To' a watched contract", func() {
|
It("returns transactions 'To' a watched contract", func() {
|
||||||
@ -244,7 +243,7 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
|||||||
}
|
}
|
||||||
repository.CreateBlock(block)
|
repository.CreateBlock(block)
|
||||||
|
|
||||||
repository.CreateWatchedContract(core.WatchedContract{Hash: "x123"})
|
repository.CreateWatchedContract(repositories.WatchedContract{Hash: "x123"})
|
||||||
watchedContract := repository.FindWatchedContract("x123")
|
watchedContract := repository.FindWatchedContract("x123")
|
||||||
Expect(watchedContract).ToNot(BeNil())
|
Expect(watchedContract).ToNot(BeNil())
|
||||||
Expect(watchedContract.Transactions).To(
|
Expect(watchedContract.Transactions).To(
|
||||||
@ -253,6 +252,16 @@ func AssertRepositoryBehavior(buildRepository func() repositories.Repository) {
|
|||||||
{Hash: "TRANSACTION3", To: "x123"},
|
{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\"}"))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
9
pkg/repositories/watched_contract.go
Normal file
9
pkg/repositories/watched_contract.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package repositories
|
||||||
|
|
||||||
|
import "github.com/8thlight/vulcanizedb/pkg/core"
|
||||||
|
|
||||||
|
type WatchedContract struct {
|
||||||
|
Abi string
|
||||||
|
Hash string
|
||||||
|
Transactions []core.Transaction
|
||||||
|
}
|
@ -39,7 +39,7 @@ func (contractSummary ContractSummary) GetStateAttribute(attributeName string) i
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func newContractSummary(blockchain core.Blockchain, watchedContract core.WatchedContract, blockNumber *big.Int) ContractSummary {
|
func newContractSummary(blockchain core.Blockchain, watchedContract repositories.WatchedContract, blockNumber *big.Int) ContractSummary {
|
||||||
contract, _ := blockchain.GetContract(watchedContract.Hash)
|
contract, _ := blockchain.GetContract(watchedContract.Hash)
|
||||||
return ContractSummary{
|
return ContractSummary{
|
||||||
blockChain: blockchain,
|
blockChain: blockchain,
|
||||||
@ -52,9 +52,9 @@ func newContractSummary(blockchain core.Blockchain, watchedContract core.Watched
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func lastTransaction(contract core.WatchedContract) *core.Transaction {
|
func lastTransaction(watchedContract repositories.WatchedContract) *core.Transaction {
|
||||||
if len(contract.Transactions) > 0 {
|
if len(watchedContract.Transactions) > 0 {
|
||||||
return &contract.Transactions[0]
|
return &watchedContract.Transactions[0]
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
Context("when the given contract is being watched", func() {
|
Context("when the given contract is being watched", func() {
|
||||||
It("returns the summary", func() {
|
It("returns the summary", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
blockchain := fakes.NewBlockchain()
|
blockchain := fakes.NewBlockchain()
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
It("includes the contract hash in the summary", func() {
|
It("includes the contract hash in the summary", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
blockchain := fakes.NewBlockchain()
|
blockchain := fakes.NewBlockchain()
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
It("sets the number of transactions", func() {
|
It("sets the number of transactions", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
block := core.Block{
|
block := core.Block{
|
||||||
Transactions: []core.Transaction{
|
Transactions: []core.Transaction{
|
||||||
@ -73,7 +73,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
It("sets the last transaction", func() {
|
It("sets the last transaction", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
block := core.Block{
|
block := core.Block{
|
||||||
Transactions: []core.Transaction{
|
Transactions: []core.Transaction{
|
||||||
@ -91,7 +91,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
It("gets contract state attribute for the contract from the blockchain", func() {
|
It("gets contract state attribute for the contract from the blockchain", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
blockchain := fakes.NewBlockchain()
|
blockchain := fakes.NewBlockchain()
|
||||||
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
||||||
@ -104,7 +104,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
It("gets contract state attribute for the contract from the blockchain at specific block height", func() {
|
It("gets contract state attribute for the contract from the blockchain at specific block height", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
blockchain := fakes.NewBlockchain()
|
blockchain := fakes.NewBlockchain()
|
||||||
blockNumber := big.NewInt(1000)
|
blockNumber := big.NewInt(1000)
|
||||||
@ -119,7 +119,7 @@ var _ = Describe("The watched contract summary", func() {
|
|||||||
|
|
||||||
It("gets attributes for the contract from the blockchain", func() {
|
It("gets attributes for the contract from the blockchain", func() {
|
||||||
repository := repositories.NewInMemory()
|
repository := repositories.NewInMemory()
|
||||||
watchedContract := core.WatchedContract{Hash: "0x123"}
|
watchedContract := repositories.WatchedContract{Hash: "0x123"}
|
||||||
repository.CreateWatchedContract(watchedContract)
|
repository.CreateWatchedContract(watchedContract)
|
||||||
blockchain := fakes.NewBlockchain()
|
blockchain := fakes.NewBlockchain()
|
||||||
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
blockchain.SetContractStateAttribute("0x123", nil, "foo", "bar")
|
||||||
|
Loading…
Reference in New Issue
Block a user