diff --git a/Gododir/main.go b/Gododir/main.go index 6acd911b..09b49130 100644 --- a/Gododir/main.go +++ b/Gododir/main.go @@ -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", }) }) diff --git a/README.md b/README.md index 5af21eaa..9ff7ea88 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,8 @@ vulcanizedb/ .json private/ ``` -The name of the JSON file should correspond the contract's address. -2. Start watching the contract `godo watchContract -- --environment= --contract-hash=` +The name of the JSON file should correspond the contract's address. +2. Start watching the contract `godo watchContract -- --environment= --contract-hash=` 3. Request summary data `godo showContractSummary -- --environment= --contract-hash=` diff --git a/cmd/get_logs/main.go b/cmd/get_logs/main.go index 465349f9..3e013882 100644 --- a/cmd/get_logs/main.go +++ b/cmd/get_logs/main.go @@ -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{}{} }() diff --git a/cmd/run/main.go b/cmd/run/main.go index 2fd84573..a8fe9152 100644 --- a/cmd/run/main.go +++ b/cmd/run/main.go @@ -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()) diff --git a/cmd/show_contract_summary/main.go b/cmd/show_contract_summary/main.go index 2b2a5e52..23ff73d1 100644 --- a/cmd/show_contract_summary/main.go +++ b/cmd/show_contract_summary/main.go @@ -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) } diff --git a/cmd/utils.go b/cmd/utils.go index f4963b04..c7e5f8db 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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) diff --git a/cmd/vulcanize_db/main.go b/cmd/vulcanize_db/main.go index 1b5ee7be..b74be6b5 100644 --- a/cmd/vulcanize_db/main.go +++ b/cmd/vulcanize_db/main.go @@ -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()) diff --git a/cmd/watch_contract/main.go b/cmd/watch_contract/main.go index 5bd35cb5..0a29f552 100644 --- a/cmd/watch_contract/main.go +++ b/cmd/watch_contract/main.go @@ -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) } diff --git a/pkg/geth/abi.go b/pkg/geth/abi.go index e84523fb..51b72103 100644 --- a/pkg/geth/abi.go +++ b/pkg/geth/abi.go @@ -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) diff --git a/pkg/geth/abi_test.go b/pkg/geth/abi_test.go index 6a89faa9..da3391b1 100644 --- a/pkg/geth/abi_test.go +++ b/pkg/geth/abi_test.go @@ -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")) + }) + }) }) }) diff --git a/pkg/geth/block_to_core_block.go b/pkg/geth/block_to_core_block.go index 34df1660..8cc806e8 100644 --- a/pkg/geth/block_to_core_block.go +++ b/pkg/geth/block_to_core_block.go @@ -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(),