From d773808d1c080a37c9ed156d5279d0b22b041642 Mon Sep 17 00:00:00 2001 From: Wyatt Mufson Date: Mon, 28 Jan 2019 00:22:34 -0800 Subject: [PATCH 1/3] Migrate from parameter array argument to json string --- .gitignore | 1 + neoutils/ont.go | 25 ++++++++++++------------- neoutils/ont_test.go | 21 +++++++++++++-------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index ea1472e..8e00c45 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ output/ +.DS_Store diff --git a/neoutils/ont.go b/neoutils/ont.go index bd9a610..22c6a87 100644 --- a/neoutils/ont.go +++ b/neoutils/ont.go @@ -35,8 +35,8 @@ func ClaimONG(endpoint string, gasPrice int, gasLimit int, wif string) (string, return txid, nil } -func BuildOntologyInvocationTransaction(contractHex string, operation string, args []ontmobile.Parameter, gasPrice uint, gasLimit uint, wif string) (string, error) { - raw, err := ontmobile.BuildInvocationTransaction(contractHex, operation, args, gasPrice, gasLimit, wif) +func BuildOntologyInvocationTransaction(contractHex string, operation string, argString string, gasPrice uint, gasLimit uint, wif string) (string, error) { + raw, err := ontmobile.BuildInvocationTransaction(contractHex, operation, argString, gasPrice, gasLimit, wif) if err != nil { return "", err } @@ -44,8 +44,9 @@ func BuildOntologyInvocationTransaction(contractHex string, operation string, ar return raw, nil } -func OntologyInvoke(endpoint string, contractHex string, operation string, args []Parameter, gasPrice uint, gasLimit uint, wif string) (string, error) { - raw, err := ontmobile.BuildInvocationTransaction(contractHex, operation, args, gasPrice, gasLimit, wif) +// OntologyInvoke : Invoke a neovm contract in Ontology +func OntologyInvoke(endpoint string, contractHex string, operation string, argString string, gasPrice uint, gasLimit uint, wif string) (string, error) { + raw, err := ontmobile.BuildInvocationTransaction(contractHex, operation, argString, gasPrice, gasLimit, wif) if err != nil { return "", err } @@ -58,13 +59,11 @@ func OntologyInvoke(endpoint string, contractHex string, operation string, args return txid, nil } -type Parameter = ontmobile.Parameter -type ParameterType = ontmobile.ParameterType +type ParameterJSONArrayForm struct { + A []ParameterJSONForm `json:"array"` +} -const ( - Address = ontmobile.Address - String = ontmobile.String - Integer = ontmobile.Integer - Fixed8 = ontmobile.Fixed8 - Array = ontmobile.Array -) +type ParameterJSONForm struct { + T string `json:"type"` + V interface{} `json:"value"` +} diff --git a/neoutils/ont_test.go b/neoutils/ont_test.go index 47abdbd..e289bd6 100644 --- a/neoutils/ont_test.go +++ b/neoutils/ont_test.go @@ -4,6 +4,7 @@ import ( "log" "math" "testing" + "encoding/json" "github.com/o3labs/neo-utils/neoutils" ) @@ -59,15 +60,17 @@ func TestBuildOntologyInvocation(t *testing.T) { account, _ := neoutils.GenerateFromWIF(wif) address := account.Address - addr := neoutils.Parameter{neoutils.Address, address} - val := neoutils.Parameter{neoutils.String, "Hi there"} + addr := neoutils.ParameterJSONForm{T: "Address", V: address} + val := neoutils.ParameterJSONForm{T: "String", V: "Hi there"} - args := []neoutils.Parameter{addr, val} + jsondat := &neoutils.ParameterJSONArrayForm{A: []neoutils.ParameterJSONForm{addr, val}} + argData, _ := json.Marshal(jsondat) + argString := string(argData) gasPrice := uint(500) gasLimit := uint(20000) - txData, err := neoutils.BuildOntologyInvocationTransaction("c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", args, gasPrice, gasLimit, wif) + txData, err := neoutils.BuildOntologyInvocationTransaction("c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) if err != nil { log.Printf("Error creating invocation transaction: %s", err) t.Fail() @@ -86,17 +89,19 @@ func TestOntologyInvoke(t *testing.T) { account, _ := neoutils.GenerateFromWIF(wif) address := account.Address - addr := neoutils.Parameter{neoutils.Address, address} - val := neoutils.Parameter{neoutils.String, "Hi there"} + addr := neoutils.ParameterJSONForm{T: "Address", V: address} + val := neoutils.ParameterJSONForm{T: "String", V: "Hi there"} - args := []neoutils.Parameter{addr, val} + jsondat := &neoutils.ParameterJSONArrayForm{A: []neoutils.ParameterJSONForm{addr, val}} + argData, _ := json.Marshal(jsondat) + argString := string(argData) gasPrice := uint(500) gasLimit := uint(20000) endpoint := "http://polaris2.ont.io:20336" - txid, err := neoutils.OntologyInvoke(endpoint, "c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", args, gasPrice, gasLimit, wif) + txid, err := neoutils.OntologyInvoke(endpoint, "c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) if err != nil { log.Printf("Error creating invocation transaction: %s", err) t.Fail() From 1afcc060a465f6018eec444a857bd371a5cab2ab Mon Sep 17 00:00:00 2001 From: Wyatt Mufson Date: Mon, 28 Jan 2019 00:54:19 -0800 Subject: [PATCH 2/3] Argument fixes and moved some of the parsing logic into the tests --- neoutils/ont.go | 17 ++++------------- neoutils/ont_test.go | 29 +++++++++++++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/neoutils/ont.go b/neoutils/ont.go index 22c6a87..190bfb6 100644 --- a/neoutils/ont.go +++ b/neoutils/ont.go @@ -35,8 +35,8 @@ func ClaimONG(endpoint string, gasPrice int, gasLimit int, wif string) (string, return txid, nil } -func BuildOntologyInvocationTransaction(contractHex string, operation string, argString string, gasPrice uint, gasLimit uint, wif string) (string, error) { - raw, err := ontmobile.BuildInvocationTransaction(contractHex, operation, argString, gasPrice, gasLimit, wif) +func BuildOntologyInvocationTransaction(contract string, method string, args string, gasPrice int, gasLimit int, wif string) (string, error) { + raw, err := ontmobile.BuildInvocationTransaction(contract, method, args, uint(gasPrice), uint(gasLimit), wif) if err != nil { return "", err } @@ -45,8 +45,8 @@ func BuildOntologyInvocationTransaction(contractHex string, operation string, ar } // OntologyInvoke : Invoke a neovm contract in Ontology -func OntologyInvoke(endpoint string, contractHex string, operation string, argString string, gasPrice uint, gasLimit uint, wif string) (string, error) { - raw, err := ontmobile.BuildInvocationTransaction(contractHex, operation, argString, gasPrice, gasLimit, wif) +func OntologyInvoke(endpoint string, contract string, method string, args string, gasPrice int, gasLimit int, wif string) (string, error) { + raw, err := ontmobile.BuildInvocationTransaction(contract, method, args, uint(gasPrice), uint(gasLimit), wif) if err != nil { return "", err } @@ -58,12 +58,3 @@ func OntologyInvoke(endpoint string, contractHex string, operation string, argSt return txid, nil } - -type ParameterJSONArrayForm struct { - A []ParameterJSONForm `json:"array"` -} - -type ParameterJSONForm struct { - T string `json:"type"` - V interface{} `json:"value"` -} diff --git a/neoutils/ont_test.go b/neoutils/ont_test.go index e289bd6..1320acb 100644 --- a/neoutils/ont_test.go +++ b/neoutils/ont_test.go @@ -9,6 +9,15 @@ import ( "github.com/o3labs/neo-utils/neoutils" ) +type parameterJSONArrayForm struct { + A []parameterJSONForm `json:"array"` +} + +type parameterJSONForm struct { + T string `json:"type"` + V interface{} `json:"value"` +} + func TestONTTransfer(t *testing.T) { for i := 1; i <= 100; i++ { @@ -60,15 +69,15 @@ func TestBuildOntologyInvocation(t *testing.T) { account, _ := neoutils.GenerateFromWIF(wif) address := account.Address - addr := neoutils.ParameterJSONForm{T: "Address", V: address} - val := neoutils.ParameterJSONForm{T: "String", V: "Hi there"} + addr := parameterJSONForm{T: "Address", V: address} + val := parameterJSONForm{T: "String", V: "Hi there"} - jsondat := &neoutils.ParameterJSONArrayForm{A: []neoutils.ParameterJSONForm{addr, val}} + jsondat := ¶meterJSONArrayForm{A: []parameterJSONForm{addr, val}} argData, _ := json.Marshal(jsondat) argString := string(argData) - gasPrice := uint(500) - gasLimit := uint(20000) + gasPrice := int(500) + gasLimit := int(20000) txData, err := neoutils.BuildOntologyInvocationTransaction("c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) if err != nil { @@ -89,15 +98,15 @@ func TestOntologyInvoke(t *testing.T) { account, _ := neoutils.GenerateFromWIF(wif) address := account.Address - addr := neoutils.ParameterJSONForm{T: "Address", V: address} - val := neoutils.ParameterJSONForm{T: "String", V: "Hi there"} + addr := parameterJSONForm{T: "Address", V: address} + val := parameterJSONForm{T: "String", V: "Hi there"} - jsondat := &neoutils.ParameterJSONArrayForm{A: []neoutils.ParameterJSONForm{addr, val}} + jsondat := ¶meterJSONArrayForm{A: []parameterJSONForm{addr, val}} argData, _ := json.Marshal(jsondat) argString := string(argData) - gasPrice := uint(500) - gasLimit := uint(20000) + gasPrice := int(500) + gasLimit := int(20000) endpoint := "http://polaris2.ont.io:20336" From cab3b8cb964b0262c7bab32c9ae8382467a43ad0 Mon Sep 17 00:00:00 2001 From: Wyatt Mufson Date: Mon, 28 Jan 2019 23:14:03 -0800 Subject: [PATCH 3/3] Formatting fixes --- neoutils/ont_test.go | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/neoutils/ont_test.go b/neoutils/ont_test.go index 1320acb..5a82068 100644 --- a/neoutils/ont_test.go +++ b/neoutils/ont_test.go @@ -1,10 +1,10 @@ package neoutils_test import ( + "encoding/json" "log" "math" "testing" - "encoding/json" "github.com/o3labs/neo-utils/neoutils" ) @@ -14,7 +14,7 @@ type parameterJSONArrayForm struct { } type parameterJSONForm struct { - T string `json:"type"` + T string `json:"type"` V interface{} `json:"value"` } @@ -66,26 +66,26 @@ func TestBuildOntologyInvocation(t *testing.T) { return } - account, _ := neoutils.GenerateFromWIF(wif) - address := account.Address + account, _ := neoutils.GenerateFromWIF(wif) + address := account.Address addr := parameterJSONForm{T: "Address", V: address} - val := parameterJSONForm{T: "String", V: "Hi there"} + val := parameterJSONForm{T: "String", V: "Hi there"} - jsondat := ¶meterJSONArrayForm{A: []parameterJSONForm{addr, val}} - argData, _ := json.Marshal(jsondat) + jsondat := ¶meterJSONArrayForm{A: []parameterJSONForm{addr, val}} + argData, _ := json.Marshal(jsondat) argString := string(argData) - gasPrice := int(500) - gasLimit := int(20000) + gasPrice := int(500) + gasLimit := int(20000) - txData, err := neoutils.BuildOntologyInvocationTransaction("c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) - if err != nil { - log.Printf("Error creating invocation transaction: %s", err) - t.Fail() - } else { - log.Printf("Raw transaction: %s", txData) - } + txData, err := neoutils.BuildOntologyInvocationTransaction("c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) + if err != nil { + log.Printf("Error creating invocation transaction: %s", err) + t.Fail() + } else { + log.Printf("Raw transaction: %s", txData) + } } func TestOntologyInvoke(t *testing.T) { @@ -95,26 +95,26 @@ func TestOntologyInvoke(t *testing.T) { return } - account, _ := neoutils.GenerateFromWIF(wif) - address := account.Address + account, _ := neoutils.GenerateFromWIF(wif) + address := account.Address addr := parameterJSONForm{T: "Address", V: address} - val := parameterJSONForm{T: "String", V: "Hi there"} + val := parameterJSONForm{T: "String", V: "Hi there"} - jsondat := ¶meterJSONArrayForm{A: []parameterJSONForm{addr, val}} - argData, _ := json.Marshal(jsondat) + jsondat := ¶meterJSONArrayForm{A: []parameterJSONForm{addr, val}} + argData, _ := json.Marshal(jsondat) argString := string(argData) - gasPrice := int(500) - gasLimit := int(20000) + gasPrice := int(500) + gasLimit := int(20000) endpoint := "http://polaris2.ont.io:20336" - txid, err := neoutils.OntologyInvoke(endpoint, "c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) - if err != nil { - log.Printf("Error creating invocation transaction: %s", err) - t.Fail() - } else { + txid, err := neoutils.OntologyInvoke(endpoint, "c168e0fb1a2bddcd385ad013c2c98358eca5d4dc", "put", argString, gasPrice, gasLimit, wif) + if err != nil { + log.Printf("Error creating invocation transaction: %s", err) + t.Fail() + } else { log.Printf("tx id = %s", txid) - } + } }