Watch contact updates (#127)

* Downcase all arguments for contact watching

* ABI retrieval from test networks
This commit is contained in:
Matt K 2018-01-08 15:59:47 -06:00 committed by GitHub
parent 14e1fc4213
commit 70cfa20c68
11 changed files with 66 additions and 21 deletions

View File

@ -60,14 +60,16 @@ func tasks(p *do.Project) {
environment := parseEnvironment(context)
contractHash := context.Args.MayString("", "contract-hash", "c")
abiFilepath := context.Args.MayString("", "abi-filepath", "a")
network := context.Args.MayString("", "network", "n")
if contractHash == "" {
log.Fatalln("--contract-hash required")
}
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}} --abi-filepath={{.abiFilepath}}`,
context.Start(`go run main.go --environment={{.environment}} --contract-hash={{.contractHash}} --abi-filepath={{.abiFilepath}} --network={{.network}}`,
do.M{
"environment": environment,
"contractHash": contractHash,
"abiFilepath": abiFilepath,
"network": network,
"$in": "cmd/watch_contract",
})
})

View File

@ -86,8 +86,8 @@ vulcanizedb/
<contract-address>.json
private/
```
The name of the JSON file should correspond the contract's address.
2. Start watching the contract `godo watchContract -- --environment=<some-environment> --contract-hash=<contract-address>`
The name of the JSON file should correspond the contract's address.
2. Start watching the contract `godo watchContract -- --environment=<some-environment> --contract-hash=<contract-address>`
3. Request summary data `godo showContractSummary -- --environment=<some-environment> --contract-hash=<contract-address>`

View File

@ -9,6 +9,8 @@ import (
"time"
"strings"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth"
@ -29,11 +31,12 @@ const (
func main() {
environment := flag.String("environment", "", "Environment name")
contractHash := flag.String("contract-hash", "", "Contract hash to show summary")
flag.Parse()
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
flag.Parse()
contractHashLowered := strings.ToLower(*contractHash)
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
@ -43,7 +46,7 @@ func main() {
go func() {
for i := int64(0); i < lastBlockNumber; i = min(i+stepSize, lastBlockNumber) {
logs, err := blockchain.GetLogs(core.Contract{Hash: *contractHash}, big.NewInt(i), big.NewInt(i+stepSize))
logs, err := blockchain.GetLogs(core.Contract{Hash: contractHashLowered}, big.NewInt(i), big.NewInt(i+stepSize))
log.Println("Backfilling Logs:", i)
if err != nil {
log.Println(err)
@ -61,7 +64,7 @@ func main() {
z := &big.Int{}
z.Sub(blockchain.LastBlock(), big.NewInt(25))
log.Printf("Logs Window: %d - %d", z.Int64(), blockchain.LastBlock().Int64())
logs, _ := blockchain.GetLogs(core.Contract{Hash: *contractHash}, z, blockchain.LastBlock())
logs, _ := blockchain.GetLogs(core.Contract{Hash: contractHashLowered}, z, blockchain.LastBlock())
repository.CreateLogs(logs)
done <- struct{}{}
}()

View File

@ -17,11 +17,12 @@ const (
)
func main() {
environment := flag.String("environment", "", "Environment name")
flag.Parse()
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
environment := flag.String("environment", "", "Environment name")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())

View File

@ -7,6 +7,8 @@ import (
"fmt"
"strings"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/contract_summary"
"github.com/8thlight/vulcanizedb/pkg/geth"
@ -17,12 +19,14 @@ func main() {
contractHash := flag.String("contract-hash", "", "Contract hash to show summary")
_blockNumber := flag.Int64("block-number", -1, "Block number of summary")
flag.Parse()
contractHashLowered := strings.ToLower(*contractHash)
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
blockNumber := cmd.RequestedBlockNumber(_blockNumber)
contractSummary, err := contract_summary.NewSummary(blockchain, repository, *contractHash, blockNumber)
contractSummary, err := contract_summary.NewSummary(blockchain, repository, contractHashLowered, blockNumber)
if err != nil {
log.Fatalln(err)
}

View File

@ -5,8 +5,6 @@ import (
"path/filepath"
"fmt"
"math/big"
"github.com/8thlight/vulcanizedb/pkg/config"
@ -42,13 +40,14 @@ func ReadAbiFile(abiFilepath string) string {
return abi
}
func GetAbi(abiFilepath string, contractHash string) string {
func GetAbi(abiFilepath string, contractHash string, network string) string {
var contractAbiString string
if abiFilepath != "" {
contractAbiString = ReadAbiFile(abiFilepath)
} else {
etherscan := geth.NewEtherScanClient("https://api.etherscan.io")
fmt.Println("No ABI supplied. Retrieving ABI from Etherscan")
url := geth.GenUrl(network)
etherscan := geth.NewEtherScanClient(url)
log.Printf("No ABI supplied. Retrieving ABI from Etherscan: %s", url)
contractAbiString, _ = etherscan.GetAbi(contractHash)
}
_, err := geth.ParseAbi(contractAbiString)

View File

@ -25,11 +25,12 @@ func backFillAllBlocks(blockchain core.Blockchain, repository repositories.Postg
}
func main() {
environment := flag.String("environment", "", "Environment name")
flag.Parse()
ticker := time.NewTicker(pollingInterval)
defer ticker.Stop()
environment := flag.String("environment", "", "Environment name")
flag.Parse()
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())

View File

@ -3,6 +3,8 @@ package main
import (
"flag"
"strings"
"github.com/8thlight/vulcanizedb/cmd"
"github.com/8thlight/vulcanizedb/pkg/core"
"github.com/8thlight/vulcanizedb/pkg/geth"
@ -12,15 +14,18 @@ func main() {
environment := flag.String("environment", "", "Environment name")
contractHash := flag.String("contract-hash", "", "contract-hash=x1234")
abiFilepath := flag.String("abi-filepath", "", "path/to/abifile.json")
flag.Parse()
network := flag.String("network", "", "ropsten")
contractAbiString := cmd.GetAbi(*abiFilepath, *contractHash)
flag.Parse()
contractHashLowered := strings.ToLower(*contractHash)
contractAbiString := cmd.GetAbi(*abiFilepath, contractHashLowered, *network)
config := cmd.LoadConfig(*environment)
blockchain := geth.NewBlockchain(config.Client.IPCPath)
repository := cmd.LoadPostgres(config.Database, blockchain.Node())
watchedContract := core.Contract{
Abi: contractAbiString,
Hash: *contractHash,
Hash: contractHashLowered,
}
repository.CreateContract(watchedContract)
}

View File

@ -38,6 +38,19 @@ func NewEtherScanClient(url string) *EtherScanApi {
}
func GenUrl(network string) string {
switch network {
case "ropsten":
return "https://ropsten.etherscan.io"
case "kovan":
return "https://kovan.etherscan.io"
case "rinkeby":
return "https://rinkeby.etherscan.io"
default:
return "https://api.etherscan.io"
}
}
//https://api.etherscan.io/api?module=contract&action=getabi&address=%s
func (e *EtherScanApi) GetAbi(contractHash string) (string, error) {
target := new(Response)

View File

@ -101,5 +101,22 @@ var _ = Describe("ABI files", func() {
})
})
})
Describe("Generating etherscan endpoints based on network", func() {
It("should return the main endpoint as the default", func() {
url := geth.GenUrl("")
Expect(url).To(Equal("https://api.etherscan.io"))
})
It("generates various test network endpoint if test network is supplied", func() {
ropstenUrl := geth.GenUrl("ropsten")
rinkebyUrl := geth.GenUrl("rinkeby")
kovanUrl := geth.GenUrl("kovan")
Expect(ropstenUrl).To(Equal("https://ropsten.etherscan.io"))
Expect(kovanUrl).To(Equal("https://kovan.etherscan.io"))
Expect(rinkebyUrl).To(Equal("https://rinkeby.etherscan.io"))
})
})
})
})

View File

@ -25,7 +25,7 @@ func ToCoreBlock(gethBlock *types.Block, client GethClient) core.Block {
GasLimit: gethBlock.GasLimit().Int64(),
GasUsed: gethBlock.GasUsed().Int64(),
Hash: gethBlock.Hash().Hex(),
Miner: gethBlock.Coinbase().Hex(),
Miner: strings.ToLower(gethBlock.Coinbase().Hex()),
Nonce: hexutil.Encode(gethBlock.Header().Nonce[:]),
Number: gethBlock.Number().Int64(),
ParentHash: gethBlock.ParentHash().Hex(),