From a0cd7f773a56277ea3ffb4d1d935130165dccbae Mon Sep 17 00:00:00 2001 From: Eric Meyer Date: Mon, 4 Dec 2017 15:12:27 -0600 Subject: [PATCH] Require ABI filepath for watching a contract --- Gododir/main.go | 13 +++++++++++-- cmd/subscribe_contract/main.go | 7 ++++++- cmd/utils.go | 14 ++++++++++++++ pkg/geth/abi.go | 11 +++++++++-- pkg/geth/abi_test.go | 9 +++++++++ pkg/geth/testing/valid_abi.json | 2 +- 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/Gododir/main.go b/Gododir/main.go index 24c8d0a7..7d3ab6f8 100644 --- a/Gododir/main.go +++ b/Gododir/main.go @@ -39,11 +39,20 @@ func tasks(p *do.Project) { p.Task("watchContract", nil, func(context *do.Context) { environment := parseEnvironment(context) contractHash := context.Args.MayString("", "contract-hash", "c") + abiFilepath := context.Args.MayString("", "abi-filepath", "a") 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"}) + if abiFilepath == "" { + 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) { diff --git a/cmd/subscribe_contract/main.go b/cmd/subscribe_contract/main.go index 7d6dcf10..0028b9e9 100644 --- a/cmd/subscribe_contract/main.go +++ b/cmd/subscribe_contract/main.go @@ -10,8 +10,13 @@ import ( 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() config := cmd.LoadConfig(*environment) repository := cmd.LoadPostgres(config.Database) - repository.CreateWatchedContract(repositories.WatchedContract{Hash: *contractHash}) + watchedContract := repositories.WatchedContract{ + Abi: cmd.ReadAbiFile(*abiFilepath), + Hash: *contractHash, + } + repository.CreateWatchedContract(watchedContract) } diff --git a/cmd/utils.go b/cmd/utils.go index 2ca2d1a2..3dff2da8 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -3,7 +3,10 @@ package cmd import ( "log" + "path/filepath" + "github.com/8thlight/vulcanizedb/pkg/config" + "github.com/8thlight/vulcanizedb/pkg/geth" "github.com/8thlight/vulcanizedb/pkg/repositories" ) @@ -22,3 +25,14 @@ func LoadPostgres(database config.Database) repositories.Postgres { } 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 +} diff --git a/pkg/geth/abi.go b/pkg/geth/abi.go index eba7c25f..1da6a569 100644 --- a/pkg/geth/abi.go +++ b/pkg/geth/abi.go @@ -14,14 +14,21 @@ var ( ) func ParseAbiFile(abiFilePath string) (abi.ABI, error) { - filesBytes, err := ioutil.ReadFile(abiFilePath) + abiString, err := ReadAbiFile(abiFilePath) if err != nil { return abi.ABI{}, ErrMissingAbiFile } - abiString := string(filesBytes) parsedAbi, err := abi.JSON(strings.NewReader(abiString)) if err != nil { return abi.ABI{}, ErrInvalidAbiFile } return parsedAbi, nil } + +func ReadAbiFile(abiFilePath string) (string, error) { + filesBytes, err := ioutil.ReadFile(abiFilePath) + if err != nil { + return "", ErrMissingAbiFile + } + return string(filesBytes), nil +} diff --git a/pkg/geth/abi_test.go b/pkg/geth/abi_test.go index d12ea9fc..d638a212 100644 --- a/pkg/geth/abi_test.go +++ b/pkg/geth/abi_test.go @@ -21,6 +21,15 @@ var _ = Describe("Reading ABI files", func() { 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() { path := filepath.Join(cfg.ProjectRoot(), "pkg", "geth", "testing", "missing_abi.json") diff --git a/pkg/geth/testing/valid_abi.json b/pkg/geth/testing/valid_abi.json index 3d80565a..5a4cf7e4 100644 --- a/pkg/geth/testing/valid_abi.json +++ b/pkg/geth/testing/valid_abi.json @@ -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"}] \ No newline at end of file